From ae856ecc823456f852af64fefe7f08a8015057ee Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 13 Feb 2025 17:28:56 +0800 Subject: [PATCH] start to add in skeletons of trait implementations for ux elements --- libs/ux-api/src/widgets/action.rs | 14 +++- libs/ux-api/src/widgets/bip39entry.rs | 3 + libs/ux-api/src/widgets/checkboxes.rs | 3 + libs/ux-api/src/widgets/modal.rs | 98 +++++++++++++++++++++++++ libs/ux-api/src/widgets/notification.rs | 4 +- libs/ux-api/src/widgets/radiobuttons.rs | 3 + libs/ux-api/src/widgets/slider.rs | 3 + libs/ux-api/src/widgets/textentry.rs | 3 + 8 files changed, 129 insertions(+), 2 deletions(-) diff --git a/libs/ux-api/src/widgets/action.rs b/libs/ux-api/src/widgets/action.rs index f7a02e501..854ed07af 100644 --- a/libs/ux-api/src/widgets/action.rs +++ b/libs/ux-api/src/widgets/action.rs @@ -1,7 +1,8 @@ -use core::ops::Not; +use enum_dispatch::enum_dispatch; use super::*; +#[enum_dispatch(ActionApi)] pub enum ActionType { #[cfg(feature = "textentry")] TextEntry(TextEntry), @@ -20,3 +21,14 @@ pub enum ActionType { #[cfg(feature = "consoleinput")] ConsoleInput, } + +#[enum_dispatch] +pub trait ActionApi { + fn height(&self, glyph_height: i16, margin: i16, _modal: &Modal) -> i16 { glyph_height + margin * 2 } + fn redraw(&self, _at_height: i16, _modal: &Modal) { unimplemented!() } + fn close(&mut self) {} + fn is_password(&self) -> bool { false } + /// navigation is one of '∴' | '←' | '→' | '↑' | '↓' + fn key_action(&mut self, _key: char) -> Option { None } + fn set_action_opcode(&mut self, _op: u32) {} +} diff --git a/libs/ux-api/src/widgets/bip39entry.rs b/libs/ux-api/src/widgets/bip39entry.rs index 20e05f368..80fcd5af5 100644 --- a/libs/ux-api/src/widgets/bip39entry.rs +++ b/libs/ux-api/src/widgets/bip39entry.rs @@ -48,6 +48,9 @@ impl Bip39Entry { } } +use crate::widgets::ActionApi; +impl ActionApi for Bip39Entry {} + // Eventually more language support can be added here: // // In order to integrate this well, we need to re-do the language build diff --git a/libs/ux-api/src/widgets/checkboxes.rs b/libs/ux-api/src/widgets/checkboxes.rs index cefc2d060..2173f410d 100644 --- a/libs/ux-api/src/widgets/checkboxes.rs +++ b/libs/ux-api/src/widgets/checkboxes.rs @@ -23,3 +23,6 @@ impl CheckBoxes { pub fn clear_items(&mut self) { self.items.clear(); } } + +use crate::widgets::ActionApi; +impl ActionApi for CheckBoxes {} diff --git a/libs/ux-api/src/widgets/modal.rs b/libs/ux-api/src/widgets/modal.rs index 4e2b7cad0..852f4cffb 100644 --- a/libs/ux-api/src/widgets/modal.rs +++ b/libs/ux-api/src/widgets/modal.rs @@ -5,6 +5,7 @@ use xous_ipc::Buffer; use super::*; use crate::blitstr2::glyph_height_hint; use crate::minigfx::*; +use crate::platform::{LINES, WIDTH}; pub struct Modal<'a> { pub top_text: Option, @@ -79,6 +80,8 @@ impl<'a> Modal<'a> { bot_memoized_height: None, growable: false, }; + layout(&mut modal, top_text, bot_text, style); + modal } @@ -87,6 +90,8 @@ impl<'a> Modal<'a> { } pub fn redraw(&mut self) { + let action_resolver: Box<&dyn ActionApi> = Box::new(&self.action); + action_resolver.redraw(0, &self); todo!(); } @@ -169,3 +174,96 @@ impl<'a> Modal<'a> { }; } } + +pub fn screen_bounds() -> Point { Point::new(WIDTH, LINES) } + +// comment this out while I figure out how to shim in the HAL layer for the graphics emulation +fn layout(modal: &mut Modal, top_text: Option<&str>, bot_text: Option<&str>, style: GlyphStyle) {} +/* +fn layout(modal: &mut Modal, top_text: Option<&str>, bot_text: Option<&str>, style: GlyphStyle) { + // we need to set a "max" size to our modal box, so that the text computations don't fail later on + let current_bounds = screen_bounds(); + + // method: + // - we assume the GAM gives us an initial modal with a "maximum" height setting + // - items are populated within this maximal canvas setting, and then the actual height needed is + // computed + // - the canvas is resized to this actual height + // problems: + // - there is no sanity check on the size of the text boxes. So if you give the UX element a top_text + // box that's huge, it will just overflow the canvas size and nothing else will get drawn. + + let mut total_height = modal.margin; + log::trace!("step 0 total_height: {}", total_height); + // compute height of top_text, if any + if let Some(top_str) = top_text { + let mut top_tv = TextView::new(TextBounds::GrowableFromTl( + Point::new(modal.margin, modal.margin), + (modal.canvas_width - modal.margin * 2) as u16, + )); + top_tv.draw_border = false; + top_tv.style = style; + top_tv.margin = Point::new(0, 0); // all margin already accounted for in the raw bounds of the text drawing + top_tv.ellipsis = false; + top_tv.invert = modal.inverted; + // specify a clip rect that's the biggest possible allowed. If we don't do this, the current canvas + // bounds are used, and the operation will fail if the text has to get bigger. + top_tv.clip_rect = Some(Rectangle::new( + Point::new(0, 0), + Point::new(current_bounds.x, LINES - 2 * modal.line_height), + )); + write!(top_tv.text, "{}", top_str).unwrap(); + + log::trace!("posting top tv: {:?}", top_tv); + modal.gam.bounds_compute_textview(&mut top_tv).expect("couldn't simulate top text size"); + if let Some(bounds) = top_tv.bounds_computed { + log::trace!("top_tv bounds computed {}", bounds.br.y - bounds.tl.y); + total_height += bounds.br.y - bounds.tl.y; + } else { + log::warn!("couldn't compute height for modal top_text: {:?}", top_tv); + // probably should find a better way to deal with this. + total_height += LINES - (modal.line_height * 2); + } + modal.top_text = Some(top_tv); + } + total_height += modal.margin; + + // compute height of action item + log::trace!("step 1 total_height: {}", total_height); + total_height += modal.action.height(modal.line_height, modal.margin, &modal); + total_height += modal.margin; + + // compute height of bot_text, if any + log::trace!("step 2 total_height: {}", total_height); + if let Some(bot_str) = bot_text { + let mut bot_tv = TextView::new(TextBounds::GrowableFromTl( + Point::new(modal.margin, total_height), + (modal.canvas_width - modal.margin * 2) as u16, + )); + bot_tv.draw_border = false; + bot_tv.style = style; + bot_tv.margin = Point::new(0, 0); // all margin already accounted for in the raw bounds of the text drawing + bot_tv.ellipsis = false; + bot_tv.invert = modal.inverted; + // specify a clip rect that's the biggest possible allowed. If we don't do this, the current canvas + // bounds are used, and the operation will fail if the text has to get bigger. + bot_tv.clip_rect = Some(Rectangle::new( + Point::new(0, 0), + Point::new(current_bounds.x, LINES - 2 * modal.line_height), + )); + write!(bot_tv.text, "{}", bot_str).unwrap(); + + log::trace!("posting bot tv: {:?}", bot_tv); + modal.gam.bounds_compute_textview(&mut bot_tv).expect("couldn't simulate bot text size"); + if let Some(bounds) = bot_tv.bounds_computed { + total_height += bounds.br.y - bounds.tl.y; + } else { + log::error!("couldn't compute height for modal bot_text: {:?}", bot_tv); + panic!("couldn't compute height for modal bot_text"); + } + modal.bot_text = Some(bot_tv); + total_height += modal.margin; + } + log::trace!("step 3 total_height: {}", total_height); +} +*/ diff --git a/libs/ux-api/src/widgets/notification.rs b/libs/ux-api/src/widgets/notification.rs index c2043ba97..2598ba613 100644 --- a/libs/ux-api/src/widgets/notification.rs +++ b/libs/ux-api/src/widgets/notification.rs @@ -1,5 +1,4 @@ use core::fmt::Write; -use std::convert::TryInto; use locales::t; use qrcode::{Color, QrCode}; @@ -71,3 +70,6 @@ impl Notification { todo!(); } } + +use crate::widgets::ActionApi; +impl ActionApi for Notification {} diff --git a/libs/ux-api/src/widgets/radiobuttons.rs b/libs/ux-api/src/widgets/radiobuttons.rs index dbac0251d..f25c594e1 100644 --- a/libs/ux-api/src/widgets/radiobuttons.rs +++ b/libs/ux-api/src/widgets/radiobuttons.rs @@ -34,3 +34,6 @@ impl RadioButtons { self.action_payload.clear(); } } + +use crate::widgets::ActionApi; +impl ActionApi for RadioButtons {} diff --git a/libs/ux-api/src/widgets/slider.rs b/libs/ux-api/src/widgets/slider.rs index 53f21c247..045eed5b0 100644 --- a/libs/ux-api/src/widgets/slider.rs +++ b/libs/ux-api/src/widgets/slider.rs @@ -78,3 +78,6 @@ impl Slider { } } } + +use crate::widgets::ActionApi; +impl ActionApi for Slider {} diff --git a/libs/ux-api/src/widgets/textentry.rs b/libs/ux-api/src/widgets/textentry.rs index 6e37fc838..a115d3d6b 100644 --- a/libs/ux-api/src/widgets/textentry.rs +++ b/libs/ux-api/src/widgets/textentry.rs @@ -125,3 +125,6 @@ impl TextEntry { } } } + +use crate::widgets::ActionApi; +impl ActionApi for TextEntry {}