Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Introduce text view for centered labels
Erik Kundt committed 1 year ago
commit 723fa63c0e986725e69f5fb062a4c5cc3b50534a
parent 1505db3e08117364efcb92e15c4fc8f92b408455
2 files changed +47 -0
modified src/ui/im.rs
@@ -569,6 +569,15 @@ where
        widget::TextView::new(text, scroll, borders).ui(self, frame)
    }

+
    pub fn centered_text_view<'a>(
+
        &mut self,
+
        frame: &mut Frame,
+
        text: impl Into<Text<'a>>,
+
        borders: Option<Borders>,
+
    ) -> Response {
+
        widget::CenteredTextView::new(text, borders).ui(self, frame)
+
    }
+

    pub fn text_edit_singleline(
        &mut self,
        frame: &mut Frame,
modified src/ui/im/widget.rs
@@ -849,6 +849,44 @@ impl<'a> Widget for TextView<'a> {
    }
}

+
pub struct CenteredTextView<'a> {
+
    content: Text<'a>,
+
    borders: Option<Borders>,
+
}
+

+
impl<'a> CenteredTextView<'a> {
+
    pub fn new(content: impl Into<Text<'a>>, borders: Option<Borders>) -> Self {
+
        Self {
+
            content: content.into(),
+
            borders,
+
        }
+
    }
+
}
+

+
impl<'a> Widget for CenteredTextView<'a> {
+
    fn ui<M>(self, ui: &mut Ui<M>, frame: &mut Frame) -> Response {
+
        let (area, area_focus) = ui.next_area().unwrap_or_default();
+

+
        let border_style = if area_focus && ui.has_focus() {
+
            ui.theme.focus_border_style
+
        } else {
+
            ui.theme.border_style
+
        };
+

+
        let area = render_block(frame, area, self.borders, border_style);
+
        let area = Rect {
+
            x: area.x.saturating_add(1),
+
            width: area.width.saturating_sub(1),
+
            ..area
+
        };
+
        let center = layout::centered_rect(area, 50, 10);
+

+
        frame.render_widget(self.content.centered(), center);
+

+
        Response::default()
+
    }
+
}
+

#[derive(Clone, Debug)]
pub struct TextEditState {
    pub text: String,