From e55630fd5760b2da730508e66794d958563e2ef8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 28 Apr 2025 19:01:59 +0800 Subject: [PATCH] allow for centering of list items and make OSK entries centered this makes the list a little nicer to navigate --- libs/ux-api/src/widgets/scroll.rs | 28 +++++++++++++++++++++++++--- libs/ux-api/src/widgets/textentry.rs | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/libs/ux-api/src/widgets/scroll.rs b/libs/ux-api/src/widgets/scroll.rs index ffc230ac5..d9c4b9f8a 100644 --- a/libs/ux-api/src/widgets/scroll.rs +++ b/libs/ux-api/src/widgets/scroll.rs @@ -22,6 +22,12 @@ pub enum Direction { Right, } +#[derive(Clone, Copy, Debug)] +pub enum TextAlignment { + Left, + Center, +} + /// This object takes an array of strings and attempts to render them /// as a scrollabel list. The number of columns rendered is equal /// to the dimensionality of the items passed; i.e., if two lists of @@ -47,6 +53,7 @@ pub struct ScrollableList { height_hint: usize, /// Minimum column width for the list min_col_width: usize, + text_alignment: TextAlignment, /// This is public so we can "reach around" the abstraction and fix up some /// abstraction barrier bodges. Ideally this would be private, but there is need /// for some global shared state to lock the UI for a given model which I haven't @@ -61,7 +68,8 @@ impl Clone for ScrollableList { .set_min_col_width(self.min_col_width) .style(self.style) .set_with_scrollbars(self.with_scrollbars) - .set_margin(self.margin); + .set_margin(self.margin) + .set_alignment(self.text_alignment); let items = self.get_all(); for (c, cols) in items.enumerate() { for row in cols { @@ -92,11 +100,19 @@ impl ScrollableList { margin: Point::new(0, 0), with_scrollbars: true, max_rows: 0, + text_alignment: TextAlignment::Left, pane: RefCell::new(pane), gfx, } } + pub fn get_alignment(&self) -> TextAlignment { self.text_alignment } + + pub fn set_alignment(mut self, alignment: TextAlignment) -> Self { + self.text_alignment = alignment; + self + } + pub fn pane_size(mut self, pane: Rectangle) -> Self { self.pane = RefCell::new(pane); self @@ -347,7 +363,10 @@ impl ScrollableList { let textbox = Rectangle::new(Point::new(0, 0), Point::new(col_width as isize, self.height_hint as isize)); - let mut tv = TextView::new(Gid::dummy(), TextBounds::BoundingBox(textbox)); + let mut tv = match self.text_alignment { + TextAlignment::Left => TextView::new(Gid::dummy(), TextBounds::BoundingBox(textbox)), + TextAlignment::Center => TextView::new(Gid::dummy(), TextBounds::CenteredTop(textbox)), + }; tv.margin = self.margin; tv.invert = true; tv.draw_border = false; @@ -387,7 +406,10 @@ impl ScrollableList { if textbox.tl().y > self.pane.borrow().br().y { break; } - tv.bounds_hint = TextBounds::BoundingBox(textbox); + tv.bounds_hint = match self.text_alignment { + TextAlignment::Left => TextBounds::BoundingBox(textbox), + TextAlignment::Center => TextBounds::CenteredTop(textbox), + }; self.gfx.draw_textview(&mut tv).unwrap(); rows_shown += 1; diff --git a/libs/ux-api/src/widgets/textentry.rs b/libs/ux-api/src/widgets/textentry.rs index ab0fcfe0b..049915741 100644 --- a/libs/ux-api/src/widgets/textentry.rs +++ b/libs/ux-api/src/widgets/textentry.rs @@ -57,7 +57,7 @@ impl Default for TextEntry { let row_height = sl.row_height(); sl = sl.pane_size(Rectangle::new(Point::new(0, row_height as isize + 2), br)); - let mut osk = ScrollableList::default(); + let mut osk = ScrollableList::default().set_alignment(TextAlignment::Center); match locales::LANG { "en" => { let osk_matrix = en::OSK_MATRIX;