mirror of
https://seed.radicle.dev/rad:z39mP9rQAaGmERfUMPULfPUi473tY.git
synced 2026-11-21 18:40:46 +08:00
lib: Introduce text view for centered labels
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user