From ca8bead91962ae288d7bed92da2b382b725b5f2c Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Aug 2021 00:29:42 +0800 Subject: [PATCH] improve organization of the modal crate --- services/gam/src/modal.rs | 1058 +----------------------- services/gam/src/modal/checkboxes.rs | 184 +++++ services/gam/src/modal/consoleinput.rs | 45 + services/gam/src/modal/notification.rs | 106 +++ services/gam/src/modal/progressbar.rs | 95 +++ services/gam/src/modal/radiobuttons.rs | 194 +++++ services/gam/src/modal/slider.rs | 203 +++++ services/gam/src/modal/textentry.rs | 251 ++++++ 8 files changed, 1094 insertions(+), 1042 deletions(-) create mode 100644 services/gam/src/modal/checkboxes.rs create mode 100644 services/gam/src/modal/consoleinput.rs create mode 100644 services/gam/src/modal/notification.rs create mode 100644 services/gam/src/modal/progressbar.rs create mode 100644 services/gam/src/modal/radiobuttons.rs create mode 100644 services/gam/src/modal/slider.rs create mode 100644 services/gam/src/modal/textentry.rs diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index 46e078552..9d82505dc 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -47,8 +47,23 @@ General form for modals: - check boxes - has an explicit "okay" button to close the modal; up/down arrows + select/enter checks boxes - slider - left/right moves the slider, enter/select closes the modal */ + +mod textentry; +pub use textentry::*; +mod radiobuttons; +pub use radiobuttons::*; +mod checkboxes; +pub use checkboxes::*; +mod notification; +pub use notification::*; +mod slider; +pub use slider::*; +mod progressbar; +pub use progressbar::*; +mod consoleinput; +pub use consoleinput::*; + use enum_dispatch::enum_dispatch; -use xous::send_message; use crate::api::*; use crate::Gam; @@ -62,7 +77,6 @@ use num_traits::*; use crate::MsgForwarder; use core::fmt::Write; -use locales::t; pub const MAX_ITEMS: usize = 8; @@ -147,908 +161,6 @@ impl CheckBoxPayload { } } -#[derive(Debug, Copy, Clone, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub enum TextEntryVisibility { - /// text is fully visible - Visible = 0, - /// only last chars are shown of text entry, the rest obscured with * - LastChars = 1, - /// all chars hidden as * - Hidden = 2, -} -#[derive(Copy, Clone)] -pub struct TextEntry { - pub is_password: bool, - pub visibility: TextEntryVisibility, - pub action_conn: xous::CID, - pub action_opcode: u32, - pub action_payload: TextEntryPayload, - // validator borrows the text entry payload, and returns an error message if something didn't go well. - // validator takes as ragument the current action_payload, and the current action_opcode - pub validator: Option Option> >, -} -impl ActionApi for TextEntry { - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - fn is_password(&self) -> bool { - self.is_password - } - /// The total canvas height is computed with this API call - /// The canvas height is not dynamically adjustable for modals. - fn height(&self, glyph_height: i16, margin: i16) -> i16 { - /* - ------------------- - | **** | <-- glyph_height + 2*margin - ------------------- - โ† ๐Ÿ‘๏ธ ๐Ÿ•ถ๏ธ * โ†’ <-- glyph_height - - + 2 * margin top/bottom - - auto-closes on enter - */ - if self.is_password { - glyph_height + 2*margin + glyph_height + 2*margin + 8 // 8 pixels extra margin because the emoji glyphs are oversized - } else { - glyph_height + 2*margin - } - } - fn redraw(&self, at_height: i16, modal: &Modal) { - let color = if self.is_password { - PixelColor::Light - } else { - PixelColor::Dark - }; - - // draw the currently entered text - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new( - Point::new(modal.margin, at_height), - Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height)) - )); - tv.ellipsis = true; // TODO: fix so we are drawing from the right-most entered text and old text is ellipsis *to the left* - tv.invert = self.is_password; - tv.style = modal.style; - tv.margin = Point::new(0, 0); - tv.draw_border = false; - tv.insertion = Some(self.action_payload.0.len() as i32); - tv.text.clear(); // make sure this is blank - let payload_chars = self.action_payload.0.as_str().unwrap().chars().count(); - // TODO: condense the "above 20" chars length path a bit -- written out "the dumb way" just to reason out the logic a bit - match self.visibility { - TextEntryVisibility::Visible => { - log::trace!("action payload: {}", self.action_payload.0.as_str().unwrap()); - if payload_chars < 20 { - write!(tv.text, "{}", self.action_payload.0.as_str().unwrap()).unwrap(); - } else { - write!(tv.text, "...{}", &self.action_payload.0.as_str().unwrap()[payload_chars-18..]).unwrap(); - } - modal.gam.post_textview(&mut tv).expect("couldn't post textview"); - }, - TextEntryVisibility::Hidden => { - if payload_chars < 20 { - for _char in self.action_payload.0.as_str().unwrap().chars() { - tv.text.push('*').expect("text field too long"); - } - } else { - // just render a pure dummy string - tv.text.push('.').unwrap(); - tv.text.push('.').unwrap(); - tv.text.push('.').unwrap(); - for _ in 0..18 { - tv.text.push('*').expect("text field too long"); - } - } - modal.gam.post_textview(&mut tv).expect("couldn't post textview"); - }, - TextEntryVisibility::LastChars => { - if payload_chars < 20 { - let hide_to = if self.action_payload.0.as_str().unwrap().chars().count() >= 2 { - self.action_payload.0.as_str().unwrap().chars().count() - 2 - } else { - 0 - }; - for (index, ch) in self.action_payload.0.as_str().unwrap().chars().enumerate() { - if index < hide_to { - tv.text.push('*').expect("text field too long"); - } else { - tv.text.push(ch).expect("text field too long"); - } - } - } else { - tv.text.push('.').unwrap(); - tv.text.push('.').unwrap(); - tv.text.push('.').unwrap(); - let hide_to = if self.action_payload.0.as_str().unwrap().chars().count() >= 2 { - self.action_payload.0.as_str().unwrap().chars().count() - 2 - } else { - 0 - }; - for (index, ch) in self.action_payload.0.as_str().unwrap()[payload_chars-18..].chars().enumerate() { - if index + payload_chars-18 < hide_to { - tv.text.push('*').expect("text field too long"); - } else { - tv.text.push(ch).expect("text field too long"); - } - } - } - modal.gam.post_textview(&mut tv).expect("couldn't post textview"); - } - } - if self.is_password { - // draw the visibility selection area - // "<๐Ÿ‘€๐Ÿคซโœด๏ธ>" coded explicitly. Pasting unicode into vscode yields extra cruft that we can't parse (e.g. skin tones and color mods). - let prompt = "\u{2b05} \u{1f440}\u{1f576}\u{26d4} \u{27a1}"; - let select_index = match self.visibility { - TextEntryVisibility::Visible => 2, - TextEntryVisibility::LastChars => 3, - TextEntryVisibility::Hidden => 4, - }; - let spacing = 38; // fixed width spacing for the array - let emoji_width = 36; - // center the prompt nicely, if possible - let left_edge = if modal.canvas_width > prompt.chars().count() as i16 * spacing { - (modal.canvas_width - prompt.chars().count() as i16 * spacing) / 2 - } else { - 0 - }; - for (i, ch) in prompt.chars().enumerate() { - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new( - Point::new(left_edge + i as i16 * spacing, at_height + modal.line_height + modal.margin * 4), - Point::new(left_edge + i as i16 * spacing + emoji_width, at_height + modal.line_height + 34 + modal.margin * 4)) - )); - tv.style = GlyphStyle::Regular; - tv.margin = Point::new(0, 0); - tv.draw_border = false; - if i == select_index { - tv.invert = !self.is_password; - } else { - tv.invert = self.is_password; - } - tv.text.clear(); - write!(tv.text, "{}", ch).unwrap(); - log::trace!("tv.text: {} : {}/{}", i, tv.text, ch); - modal.gam.post_textview(&mut tv).expect("couldn't post textview"); - } - } - - // draw a line for where text gets entered (don't use a box, fitting could be awkward) - modal.gam.draw_line(modal.canvas, Line::new_with_style( - Point::new(modal.margin, at_height + modal.line_height + 4), - Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height + 4), - DrawStyle::new(color, color, 1)) - ).expect("couldn't draw entry line"); - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - match k { - 'โ†' => { - if self.visibility as u32 > 0 { - match FromPrimitive::from_u32(self.visibility as u32 - 1) { - Some(new_visibility) => { - log::trace!("new visibility: {:?}", new_visibility); - self.visibility = new_visibility; - }, - _ => { - panic!("internal error: an TextEntryVisibility did not resolve correctly"); - } - } - } - }, - 'โ†’' => { - if (self.visibility as u32) < (TextEntryVisibility::Hidden as u32) { - match FromPrimitive::from_u32(self.visibility as u32 + 1) { - Some(new_visibility) => { - log::trace!("new visibility: {:?}", new_visibility); - self.visibility = new_visibility - }, - _ => { - panic!("internal error: an TextEntryVisibility did not resolve correctly"); - } - } - } - }, - 'โˆด' | '\u{d}' => { - if let Some(validator) = self.validator { - if let Some(err_msg) = validator(self.action_payload, self.action_opcode) { - self.action_payload.0.clear(); // reset the input field - return (Some(err_msg), false); - } - } - - let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); - buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - self.action_payload.volatile_clear(); // ensure the local copy of text is zero'd out - return (None, true) - } - 'โ†‘' | 'โ†“' => { - // ignore these navigation keys - } - '\u{0}' => { - // ignore null messages - } - '\u{8}' => { // backspace - // coded in a conservative manner to avoid temporary allocations that can leave the plaintext on the stack - if self.action_payload.0.len() > 0 { // don't backspace if we have no string. - let mut temp_str = String::<256>::from_str(self.action_payload.0.as_str().unwrap()); - let cur_len = temp_str.as_str().unwrap().chars().count(); - let mut c_iter = temp_str.as_str().unwrap().chars(); - self.action_payload.0.clear(); - for _ in 0..cur_len-1 { - self.action_payload.0.push(c_iter.next().unwrap()).unwrap(); - } - temp_str.volatile_clear(); - } - } - _ => { // text entry - self.action_payload.0.push(k).expect("ran out of space storing password"); - log::trace!("****update payload: {}", self.action_payload.0); - } - } - (None, false) - } -} -#[derive(Debug, Copy, Clone)] -pub struct RadioButtons { - pub items: [Option; MAX_ITEMS], - pub action_conn: xous::CID, - pub action_opcode: u32, - pub action_payload: RadioButtonPayload, // the current "radio button" selection - pub select_index: i16, // the current candidate to be selected - pub max_items: i16, - pub is_password: bool, -} -impl RadioButtons { - pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { - RadioButtons { - items: [None; MAX_ITEMS], - action_conn, - action_opcode, - action_payload: RadioButtonPayload::new(""), - select_index: 0, - max_items: 0, - is_password: false, - } - } - pub fn add_item(&mut self, new_item: ItemName) -> Option { - if self.action_payload.as_str().len() == 0 { - // default to the first item added - self.action_payload = RadioButtonPayload::new(new_item.as_str()); - } - for item in self.items.iter_mut() { - if item.is_none() { - self.max_items += 1; - *item = Some(new_item); - return None; - } - } - return Some(new_item); - } -} -impl ActionApi for RadioButtons { - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - fn height(&self, glyph_height: i16, margin: i16) -> i16 { - let mut total_items = 0; - // total items, then +1 for the "Okay" message - for item in self.items.iter() { - if item.is_some(){ total_items += 1} - } - (total_items + 1) * glyph_height + margin * 2 + margin * 2 + 5 // +4 for some bottom margin slop - } - fn redraw(&self, at_height: i16, modal: &Modal) { - let color = if self.is_password { - PixelColor::Light - } else { - PixelColor::Dark - }; - - // prime a textview with the correct general style parameters - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) - ); - tv.ellipsis = true; - tv.style = modal.style; - tv.invert = self.is_password; - tv.draw_border= false; - tv.margin = Point::new(0, 0,); - tv.insertion = None; - - let cursor_x = modal.margin; - let select_x = modal.margin + 20; - let text_x = modal.margin + 20 + 20; - - //let mut emoji_slop = (36 - modal.line_height) / 2; - //if emoji_slop < 0 { emoji_slop = 0; } - let emoji_slop = 2; // tweaked for a non-emoji glyph - - let mut cur_line = 0; - let mut do_okay = true; - for maybe_item in self.items.iter() { - if let Some(item) = maybe_item { - let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2; - if cur_line == self.select_index { - // draw the cursor - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) - )); - write!(tv, "ยป").unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - do_okay = false; - } - if item.as_str() == self.action_payload.as_str() { - // draw the radio dot - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(select_x, cur_y), Point::new(select_x + 36, cur_y + modal.line_height) - )); - write!(tv, "โ€ข").unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - } - // draw the text - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) - )); - write!(tv, "{}", item.as_str()).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - - cur_line += 1; - } - } - cur_line += 1; - let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2; - if do_okay { - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) - )); - write!(tv, "ยป").unwrap(); // right arrow emoji. use unicode numbers, because text editors do funny shit with emojis - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - } - // draw the "OK" line - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) - )); - write!(tv, "{}", t!("radio.select_and_close", xous::LANG)).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - - // divider lines - modal.gam.draw_line(modal.canvas, Line::new_with_style( - Point::new(modal.margin, at_height + modal.margin), - Point::new(modal.canvas_width - modal.margin, at_height + modal.margin), - DrawStyle::new(color, color, 1)) - ).expect("couldn't draw entry line"); - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - match k { - 'โ†' | 'โ†’' => { - // ignore these navigation keys - }, - 'โ†‘' => { - if self.select_index > 0 { - self.select_index -= 1; - } - } - 'โ†“' => { - if self.select_index < self.max_items + 1 { // +1 is the "OK" button - self.select_index += 1; - } - } - 'โˆด' | '\u{d}' => { - if self.select_index < self.max_items { - // iterate through to find the index -- because if we support a remove() API later, - // the list can have "holes", such that the index != index in the array - let mut cur_index = 0; - for maybe_item in self.items.iter() { - if let Some(item) = maybe_item { - if cur_index == self.select_index { - self.action_payload = RadioButtonPayload::new(item.as_str()); - break; - } - cur_index += 1; - } - } - } else { // the OK button select - let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); - buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) - } - } - '\u{0}' => { - // ignore null messages - } - _ => { - // ignore text entry - } - } - (None, false) - } -} -#[derive(Debug, Copy, Clone)] -pub struct CheckBoxes { - pub items: [Option; MAX_ITEMS], - pub action_conn: xous::CID, - pub action_opcode: u32, - pub action_payload: CheckBoxPayload, - pub max_items: i16, - pub select_index: i16, -} -impl CheckBoxes { - pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { - CheckBoxes { - items: [None; MAX_ITEMS], - action_conn, - action_opcode, - action_payload: CheckBoxPayload::new(), - max_items: 0, - select_index: 0, - } - } - pub fn add_item(&mut self, new_item: ItemName) -> Option { - for item in self.items.iter_mut() { - if item.is_none() { - self.max_items += 1; - *item = Some(new_item); - return None; - } - } - return Some(new_item); - } -} -impl ActionApi for CheckBoxes { - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - fn height(&self, glyph_height: i16, margin: i16) -> i16 { - let mut total_items = 0; - // total items, then +1 for the "Okay" message - for item in self.items.iter() { - if item.is_some(){ total_items += 1} - } - (total_items + 1) * glyph_height + margin * 2 + 5 // some slop needed because of the prompt character - } - fn redraw(&self, at_height: i16, modal: &Modal) { - // prime a textview with the correct general style parameters - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) - ); - tv.ellipsis = true; - tv.style = modal.style; - tv.invert = false; - tv.draw_border= false; - tv.margin = Point::new(0, 0,); - tv.insertion = None; - - let cursor_x = modal.margin; - let select_x = modal.margin + 20; - let text_x = modal.margin + 20 + 20; - - let emoji_slop = 2; // tweaked for a non-emoji glyph - - let mut cur_line = 0; - let mut do_okay = true; - for maybe_item in self.items.iter() { - if let Some(item) = maybe_item { - let cur_y = at_height + cur_line * modal.line_height; - if cur_line == self.select_index { - // draw the cursor - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) - )); - write!(tv, "ยป").unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - do_okay = false; - } - if self.action_payload.contains(item.as_str()) { - // draw the check mark - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(select_x, cur_y - emoji_slop), Point::new(select_x + 36, cur_y + modal.line_height) - )); - write!(tv, "\u{d7}").unwrap(); // multiplication sign - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - } - // draw the text - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) - )); - write!(tv, "{}", item.as_str()).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - - cur_line += 1; - } - } - cur_line += 1; - let cur_y = at_height + cur_line * modal.line_height; - if do_okay { - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) - )); - write!(tv, "ยป").unwrap(); // right arrow emoji. use unicode numbers, because text editors do funny shit with emojis - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - } - // draw the "OK" line - tv.text.clear(); - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) - )); - write!(tv, "{}", t!("radio.select_and_close", xous::LANG)).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - - // divider lines - modal.gam.draw_line(modal.canvas, Line::new_with_style( - Point::new(modal.margin, at_height), - Point::new(modal.canvas_width - modal.margin, at_height), - DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)) - ).expect("couldn't draw entry line"); - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - match k { - 'โ†' | 'โ†’' => { - // ignore these navigation keys - }, - 'โ†‘' => { - if self.select_index > 0 { - self.select_index -= 1; - } - } - 'โ†“' => { - if self.select_index < self.max_items + 1 { // +1 is the "OK" button - self.select_index += 1; - } - } - 'โˆด' | '\u{d}' => { - if self.select_index < self.max_items { - // iterate through to find the index -- because if we support a remove() API later, - // the list can have "holes", such that the index != index in the array - let mut cur_index = 0; - for maybe_item in self.items.iter() { - if let Some(item) = maybe_item { - if cur_index == self.select_index { - if self.action_payload.contains(item.as_str()) { - self.action_payload.remove(item.as_str()); - } else { - self.action_payload.add(item.as_str()); - } - break; - } - cur_index += 1; - } - } - } else { // the OK button select - let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); - buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) - } - } - '\u{0}' => { - // ignore null messages - } - _ => { - // ignore text entry - } - } - (None, false) - } -} - -#[derive(Debug, Copy, Clone)] -pub struct Notification { - pub action_conn: xous::CID, - pub action_opcode: u32, - pub is_password: bool, - pub manual_dismiss: bool, -} -impl Notification { - pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { - Notification { - action_conn, - action_opcode, - is_password: false, - manual_dismiss: true, - } - } - pub fn set_is_password(&mut self, setting: bool) { - // this will cause text to be inverted. Untrusted entities can try to set this, - // but the GAM should defeat this for dialog boxes outside of the trusted boot - // set because they can't achieve a high enough trust level. - self.is_password = setting; - } - pub fn set_manual_dismiss(&mut self, setting: bool) { - self.manual_dismiss = setting; - } -} -impl ActionApi for Notification { - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - fn height(&self, glyph_height: i16, margin: i16) -> i16 { - if self.manual_dismiss { - glyph_height + margin * 2 + 5 - } else { - margin + 5 - } - } - fn redraw(&self, at_height: i16, modal: &Modal) { - if self.manual_dismiss { - // prime a textview with the correct general style parameters - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) - ); - tv.ellipsis = true; - tv.style = modal.style; - tv.invert = self.is_password; - tv.draw_border= false; - tv.margin = Point::new(0, 0,); - tv.insertion = None; - - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::GrowableFromTl( - Point::new(modal.margin, at_height + modal.margin * 2), - (modal.canvas_width - modal.margin * 2) as u16 - ); - write!(tv, "{}", t!("notification.dismiss", xous::LANG)).unwrap(); - modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size"); - let textwidth = if let Some(bounds) = tv.bounds_computed { - bounds.br.x - bounds.tl.x - } else { - modal.canvas_width - modal.margin * 2 - }; - let offset = (modal.canvas_width - textwidth) / 2; - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(offset, at_height + modal.margin * 2), - Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height + modal.margin * 2) - )); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - - // divider lines - let color = if self.is_password { - PixelColor::Light - } else { - PixelColor::Dark - }; - - modal.gam.draw_line(modal.canvas, Line::new_with_style( - Point::new(modal.margin, at_height + modal.margin), - Point::new(modal.canvas_width - modal.margin, at_height + modal.margin), - DrawStyle::new(color, color, 1)) - ).expect("couldn't draw entry line"); - } - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - match k { - '\u{0}' => { - // ignore null messages - } - _ => { - if self.manual_dismiss { - send_message(self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, 0, 0, 0, 0)).expect("couldn't pass on dismissal"); - return(None, true) - } - } - } - (None, false) - } -} - -#[derive(Debug, Copy, Clone)] -pub struct Slider { - pub min: u32, - pub max: u32, - pub step: u32, - pub action_conn: xous::CID, - pub action_opcode: u32, - pub action_payload: u32, - pub is_progressbar: bool, - pub is_password: bool, - pub show_legend: bool, - pub units: xous_ipc::String::<8>, -} -impl Slider { - pub fn new(action_conn: xous::CID, action_opcode: u32, min: u32, max: u32, step: u32, units: Option<&str>, initial_setting: u32, is_progressbar: bool, show_legend: bool) -> Self { - let checked_units = if let Some(unit_str) = units { - if unit_str.len() < 8 { - String::<8>::from_str(unit_str) - } else { - log::error!("Unit string must be less than 8 *bytes* long (are you using unicode?), ignoring length {} string", unit_str.len()); - String::<8>::new() - } - } else { - String::<8>::new() // just populate with a blank string, easier than checking Some/None later on everywhere - }; - - Slider { - action_conn, - action_opcode, - is_password: false, - is_progressbar, - min, - max, - step, - action_payload: initial_setting, - units: checked_units, - show_legend, - } - } - pub fn set_is_password(&mut self, setting: bool) { - // this will cause text to be inverted. Untrusted entities can try to set this, - // but the GAM should defeat this for dialog boxes outside of the trusted boot - // set because they can't achieve a high enough trust level. - self.is_password = setting; - } - pub fn set_state(&mut self, state: u32) { - if state < self.min { - self.action_payload = self.min; - } else if state > self.max { - self.action_payload = self.max; - } else { - self.action_payload = state; - } - } -} -impl ActionApi for Slider { - fn height(&self, glyph_height: i16, margin: i16) -> i16 { - /* - margin - min max <- glyph height - -----O---------- <- glyph height - legend - margin - */ - if self.show_legend { - glyph_height * 3 + margin * 2 - } else { - glyph_height * 2 + margin * 2 - } - } - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - - fn redraw(&self, at_height: i16, modal: &Modal) { - let color = if self.is_password { - PixelColor::Light - } else { - PixelColor::Dark - }; - let fill_color = if self.is_password { - PixelColor::Dark - } else { - PixelColor::Light - }; - - // prime a textview with the correct general style parameters - let mut tv = TextView::new( - modal.canvas, - TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) - ); - tv.ellipsis = true; - tv.style = modal.style; - tv.invert = self.is_password; - tv.draw_border= false; - tv.margin = Point::new(0, 0,); - tv.insertion = None; - - let maxwidth = (modal.canvas_width - modal.margin * 2) as u16; - if self.show_legend { - /* // min/max doesn't look good, leave it out for now - // render min - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::GrowableFromTl( - Point::new(modal.margin, at_height + modal.margin), - maxwidth - ); - tv.text.clear(); - write!(tv, "{}{}", self.min, self.units.to_str()).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - // render max - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::GrowableFromBr( - Point::new(modal.canvas_width - modal.margin, at_height + modal.margin + modal.line_height), - maxwidth - ); - tv.text.clear(); - write!(tv, "{}{}", self.max, self.units.to_str()).unwrap(); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - */ - // estimate width of current setting - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::GrowableFromTl( - Point::new(0, 0), - maxwidth - ); - write!(tv, "{}{}", self.action_payload, self.units.to_str()).unwrap(); - modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size"); - let textwidth = if let Some(bounds) = tv.bounds_computed { - bounds.br.x - bounds.tl.x - } else { - maxwidth as i16 - }; - let offset = (modal.canvas_width - textwidth) / 2; - // render current setting - tv.bounds_computed = None; - tv.bounds_hint = TextBounds::GrowableFromTl( - Point::new(offset, at_height + modal.margin + modal.line_height*2 + modal.margin), - maxwidth - ); - modal.gam.post_textview(&mut tv).expect("couldn't post tv"); - } - - // the actual slider - let outer_rect = Rectangle::new_with_style( - Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height), - Point::new(modal.canvas_width - modal.margin * 2, modal.margin + modal.line_height * 2 + at_height), - DrawStyle::new(fill_color, color, 2) - ); - modal.gam.draw_rectangle(modal.canvas, outer_rect).expect("couldn't draw outer rectangle"); - let total_width = modal.canvas_width - modal.margin * 4; - let slider_point = (total_width * (self.action_payload - self.min) as i16) / (self.max - self.min) as i16; - let inner_rect = Rectangle::new_with_style( - Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height), - Point::new(modal.margin * 2 + slider_point, modal.margin + modal.line_height * 2 + at_height), - DrawStyle::new(color, color, 1) - ); - modal.gam.draw_rectangle(modal.canvas, inner_rect).expect("couldn't draw inner rectangle"); - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - if !self.is_progressbar { - match k { - 'โ†' => { - if self.action_payload >= self.min + self.step { - self.action_payload -= self.step; - } else if self.action_payload >= self.min && self.action_payload < self.min + self.step { - self.action_payload = self.min - } - }, - 'โ†’' => { - if self.action_payload <= self.max - self.step { - self.action_payload += self.step; - } else if self.action_payload < self.max && self.action_payload > self.max - self.step { - self.action_payload = self.max - } - }, - '\u{0}' => { - // ignore null messages - } - 'โˆด' | '\u{d}' => { - send_message(self.action_conn, - xous::Message::new_scalar(self.action_opcode as usize, self.action_payload as usize, 0, 0, 0)).expect("couldn't pass on action payload"); - return(None, true) - } - _ => { - // ignore all other messages - } - } - (None, false) - } else { - if k == '๐Ÿ›‘' { // use the "stop" emoji as a signal that we should close the progress bar - (None, true) - } else { - (None, false) - } - } - } -} - - #[enum_dispatch] pub trait ActionApi { fn height(&self, glyph_height: i16, margin: i16) -> i16 {glyph_height + margin * 2} @@ -1442,141 +554,3 @@ impl<'a> Modal<'a> { recompute_canvas(self, action, top_text, bot_text, style); } } - -/// This is an extention to the Slider struct that allows it to be used as a progress bar -pub struct ProgressBar<'a, 'b> { - // work is the measure of the actual work being done (e.g. sectors to erase start/end) - subtask_start_work: u32, - subtask_end_work: u32, - // this is the value of the work that's been done - current_work: u32, - // percent is the start/end percentage points of the overall 100% range this subtask maps to - subtask_start_percent: u32, - subtask_end_percent: u32, - // this is the absolute value of the current progress in percent - current_progress_percent: u32, - modal: &'a mut Modal<'b>, - slider: &'a mut Slider, -} -impl<'a, 'b> ProgressBar<'a, 'b> { - pub fn new(modal: &'a mut Modal<'b>, slider: &'a mut Slider) -> ProgressBar<'a, 'b> { - ProgressBar { - subtask_start_work: 0, - subtask_end_work: 255, - current_work: 0, - subtask_start_percent: 0, - subtask_end_percent: 100, - current_progress_percent: 0, - modal, - slider, - } - } - pub fn modify(&mut self, update_action: Option, - update_top_text: Option<&str>, remove_top: bool, - update_bot_text: Option<&str>, remove_bot: bool, - update_style: Option) { - self.modal.modify(update_action, update_top_text, remove_top, update_bot_text, remove_bot, update_style); - } - pub fn activate(&self) { - self.modal.activate(); - } - pub fn update_text(&mut self, text: &str) { - self.modal.modify(None, Some(text), false, None, false, None); - } - /// There is a significant caveat for performance/stability for this routine. - /// This works well for a modal that is both managed and owned by the same thread so long as - /// one is not resizing the top or bottom text within the modal while changing the progress bar - /// However, if one triggers a redraw/resize of the text, then the system needs to re-run the - /// defacement computations on the underlying canvases. If one or two calls happen, this might - /// not be a big deal, but because the redraw requests that are looped back from the GAM as a - /// result of the defacement operation cannot be processed until the thread managing the progress - /// bar has finished and returned control to the main loop, redraw requests will eventually fill - /// up the server queue and cause a deadlock situation. - fn update_ui(&mut self, new_percent: u32) { - if new_percent != self.current_progress_percent { - log::debug!("progress: {}", new_percent); - self.slider.set_state(new_percent); - self.modal.modify( - Some(crate::ActionType::Slider(*self.slider)), - None, false, None, false, None); - self.modal.redraw(); // stage the modal box pixels to the back buffer - xous::yield_slice(); // this gives time for the GAM to do the sending - self.current_progress_percent = new_percent; - } - } - pub fn increment_work(&mut self, increment: u32) { - self.current_work += increment; - if self.current_work > self.subtask_end_work { - self.current_work = self.subtask_end_work; - } - let new_progress_percent = self.subtask_start_percent + - ((self.subtask_end_percent - self.subtask_start_percent) * self.current_work) / (self.subtask_end_work - self.subtask_start_work); - self.update_ui(new_progress_percent); - } - pub fn set_percentage(&mut self, setting: u32) { - let checked_setting = if setting > 100 { - 100 - } else { - setting - }; - self.update_ui(checked_setting); - } - pub fn rebase_subtask_work(&mut self, subtask_start_work: u32, subtask_end_work: u32) { - assert!(subtask_end_work > subtask_start_work); - self.subtask_start_work = subtask_start_work; - self.subtask_end_work = subtask_end_work; - self.current_work = self.subtask_start_work; - self.increment_work(0); // this will recompute the Ux state and draw it - } - pub fn rebase_subtask_percentage(&mut self, subtask_start_percent: u32, subtask_end_percent: u32) { - assert!(subtask_start_percent <= subtask_end_percent); - self.subtask_start_percent = subtask_start_percent; - self.subtask_end_percent = subtask_end_percent; - self.update_ui(self.subtask_start_percent); // this will redraw the UI if the start percent is different from the current - } -} - - -/// This is a specialty structure that takes input from the serial console and records it to a string. -#[derive(Debug, Copy, Clone)] -pub struct ConsoleInput { - pub action_conn: xous::CID, - pub action_opcode: u32, - pub action_payload: TextEntryPayload, -} -impl ConsoleInput { - pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { - ConsoleInput { - action_conn, - action_opcode, - action_payload: TextEntryPayload::new(), - } - } -} -impl ActionApi for ConsoleInput { - fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} - fn height(&self, _glyph_height: i16, margin: i16) -> i16 { - margin - } - fn redraw(&self, _at_height: i16, _modal: &Modal) { - // has nothing - } - fn key_action(&mut self, k: char) -> (Option>, bool) { - log::trace!("key_action: {}", k); - match k { - '\u{0}' => { - // ignore null messages - } - 'โˆด' | '\u{d}' => { - let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); - buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); - return (None, true) - } - _ => { // text entry - self.action_payload.0.push(k).expect("ran out of space storing password"); - log::trace!("****update payload: {}", self.action_payload.0); - } - } - (None, false) - } -} \ No newline at end of file diff --git a/services/gam/src/modal/checkboxes.rs b/services/gam/src/modal/checkboxes.rs new file mode 100644 index 000000000..0ec452616 --- /dev/null +++ b/services/gam/src/modal/checkboxes.rs @@ -0,0 +1,184 @@ +use crate::*; + +use graphics_server::api::*; + +use xous_ipc::Buffer; + +use core::fmt::Write; +use locales::t; + +#[derive(Debug, Copy, Clone)] +pub struct CheckBoxes { + pub items: [Option; MAX_ITEMS], + pub action_conn: xous::CID, + pub action_opcode: u32, + pub action_payload: CheckBoxPayload, + pub max_items: i16, + pub select_index: i16, +} +impl CheckBoxes { + pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { + CheckBoxes { + items: [None; MAX_ITEMS], + action_conn, + action_opcode, + action_payload: CheckBoxPayload::new(), + max_items: 0, + select_index: 0, + } + } + pub fn add_item(&mut self, new_item: ItemName) -> Option { + for item in self.items.iter_mut() { + if item.is_none() { + self.max_items += 1; + *item = Some(new_item); + return None; + } + } + return Some(new_item); + } +} +impl ActionApi for CheckBoxes { + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + fn height(&self, glyph_height: i16, margin: i16) -> i16 { + let mut total_items = 0; + // total items, then +1 for the "Okay" message + for item in self.items.iter() { + if item.is_some(){ total_items += 1} + } + (total_items + 1) * glyph_height + margin * 2 + 5 // some slop needed because of the prompt character + } + fn redraw(&self, at_height: i16, modal: &Modal) { + // prime a textview with the correct general style parameters + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) + ); + tv.ellipsis = true; + tv.style = modal.style; + tv.invert = false; + tv.draw_border= false; + tv.margin = Point::new(0, 0,); + tv.insertion = None; + + let cursor_x = modal.margin; + let select_x = modal.margin + 20; + let text_x = modal.margin + 20 + 20; + + let emoji_slop = 2; // tweaked for a non-emoji glyph + + let mut cur_line = 0; + let mut do_okay = true; + for maybe_item in self.items.iter() { + if let Some(item) = maybe_item { + let cur_y = at_height + cur_line * modal.line_height; + if cur_line == self.select_index { + // draw the cursor + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) + )); + write!(tv, "ยป").unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + do_okay = false; + } + if self.action_payload.contains(item.as_str()) { + // draw the check mark + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(select_x, cur_y - emoji_slop), Point::new(select_x + 36, cur_y + modal.line_height) + )); + write!(tv, "\u{d7}").unwrap(); // multiplication sign + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + // draw the text + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) + )); + write!(tv, "{}", item.as_str()).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + + cur_line += 1; + } + } + cur_line += 1; + let cur_y = at_height + cur_line * modal.line_height; + if do_okay { + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) + )); + write!(tv, "ยป").unwrap(); // right arrow emoji. use unicode numbers, because text editors do funny shit with emojis + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + // draw the "OK" line + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) + )); + write!(tv, "{}", t!("radio.select_and_close", xous::LANG)).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + + // divider lines + modal.gam.draw_line(modal.canvas, Line::new_with_style( + Point::new(modal.margin, at_height), + Point::new(modal.canvas_width - modal.margin, at_height), + DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)) + ).expect("couldn't draw entry line"); + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + match k { + 'โ†' | 'โ†’' => { + // ignore these navigation keys + }, + 'โ†‘' => { + if self.select_index > 0 { + self.select_index -= 1; + } + } + 'โ†“' => { + if self.select_index < self.max_items + 1 { // +1 is the "OK" button + self.select_index += 1; + } + } + 'โˆด' | '\u{d}' => { + if self.select_index < self.max_items { + // iterate through to find the index -- because if we support a remove() API later, + // the list can have "holes", such that the index != index in the array + let mut cur_index = 0; + for maybe_item in self.items.iter() { + if let Some(item) = maybe_item { + if cur_index == self.select_index { + if self.action_payload.contains(item.as_str()) { + self.action_payload.remove(item.as_str()); + } else { + self.action_payload.add(item.as_str()); + } + break; + } + cur_index += 1; + } + } + } else { // the OK button select + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); + buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); + return (None, true) + } + } + '\u{0}' => { + // ignore null messages + } + _ => { + // ignore text entry + } + } + (None, false) + } +} diff --git a/services/gam/src/modal/consoleinput.rs b/services/gam/src/modal/consoleinput.rs new file mode 100644 index 000000000..a00085be5 --- /dev/null +++ b/services/gam/src/modal/consoleinput.rs @@ -0,0 +1,45 @@ +use crate::*; + +/// This is a specialty structure that takes input from the serial console and records it to a string. +#[derive(Debug, Copy, Clone)] +pub struct ConsoleInput { + pub action_conn: xous::CID, + pub action_opcode: u32, + pub action_payload: TextEntryPayload, +} +impl ConsoleInput { + pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { + ConsoleInput { + action_conn, + action_opcode, + action_payload: TextEntryPayload::new(), + } + } +} +impl ActionApi for ConsoleInput { + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + fn height(&self, _glyph_height: i16, margin: i16) -> i16 { + margin + } + fn redraw(&self, _at_height: i16, _modal: &Modal) { + // has nothing + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + match k { + '\u{0}' => { + // ignore null messages + } + 'โˆด' | '\u{d}' => { + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); + buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); + return (None, true) + } + _ => { // text entry + self.action_payload.0.push(k).expect("ran out of space storing password"); + log::trace!("****update payload: {}", self.action_payload.0); + } + } + (None, false) + } +} \ No newline at end of file diff --git a/services/gam/src/modal/notification.rs b/services/gam/src/modal/notification.rs new file mode 100644 index 000000000..06b50613a --- /dev/null +++ b/services/gam/src/modal/notification.rs @@ -0,0 +1,106 @@ +use crate::*; + +use graphics_server::api::*; + +use locales::t; +use core::fmt::Write; + +#[derive(Debug, Copy, Clone)] +pub struct Notification { + pub action_conn: xous::CID, + pub action_opcode: u32, + pub is_password: bool, + pub manual_dismiss: bool, +} +impl Notification { + pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { + Notification { + action_conn, + action_opcode, + is_password: false, + manual_dismiss: true, + } + } + pub fn set_is_password(&mut self, setting: bool) { + // this will cause text to be inverted. Untrusted entities can try to set this, + // but the GAM should defeat this for dialog boxes outside of the trusted boot + // set because they can't achieve a high enough trust level. + self.is_password = setting; + } + pub fn set_manual_dismiss(&mut self, setting: bool) { + self.manual_dismiss = setting; + } +} +impl ActionApi for Notification { + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + fn height(&self, glyph_height: i16, margin: i16) -> i16 { + if self.manual_dismiss { + glyph_height + margin * 2 + 5 + } else { + margin + 5 + } + } + fn redraw(&self, at_height: i16, modal: &Modal) { + if self.manual_dismiss { + // prime a textview with the correct general style parameters + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) + ); + tv.ellipsis = true; + tv.style = modal.style; + tv.invert = self.is_password; + tv.draw_border= false; + tv.margin = Point::new(0, 0,); + tv.insertion = None; + + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::GrowableFromTl( + Point::new(modal.margin, at_height + modal.margin * 2), + (modal.canvas_width - modal.margin * 2) as u16 + ); + write!(tv, "{}", t!("notification.dismiss", xous::LANG)).unwrap(); + modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size"); + let textwidth = if let Some(bounds) = tv.bounds_computed { + bounds.br.x - bounds.tl.x + } else { + modal.canvas_width - modal.margin * 2 + }; + let offset = (modal.canvas_width - textwidth) / 2; + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(offset, at_height + modal.margin * 2), + Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height + modal.margin * 2) + )); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + + // divider lines + let color = if self.is_password { + PixelColor::Light + } else { + PixelColor::Dark + }; + + modal.gam.draw_line(modal.canvas, Line::new_with_style( + Point::new(modal.margin, at_height + modal.margin), + Point::new(modal.canvas_width - modal.margin, at_height + modal.margin), + DrawStyle::new(color, color, 1)) + ).expect("couldn't draw entry line"); + } + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + match k { + '\u{0}' => { + // ignore null messages + } + _ => { + if self.manual_dismiss { + send_message(self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, 0, 0, 0, 0)).expect("couldn't pass on dismissal"); + return(None, true) + } + } + } + (None, false) + } +} \ No newline at end of file diff --git a/services/gam/src/modal/progressbar.rs b/services/gam/src/modal/progressbar.rs new file mode 100644 index 000000000..e700a4716 --- /dev/null +++ b/services/gam/src/modal/progressbar.rs @@ -0,0 +1,95 @@ + +use crate::*; + +/// This is an extention to the Slider struct that allows it to be used as a progress bar +pub struct ProgressBar<'a, 'b> { + // work is the measure of the actual work being done (e.g. sectors to erase start/end) + subtask_start_work: u32, + subtask_end_work: u32, + // this is the value of the work that's been done + current_work: u32, + // percent is the start/end percentage points of the overall 100% range this subtask maps to + subtask_start_percent: u32, + subtask_end_percent: u32, + // this is the absolute value of the current progress in percent + current_progress_percent: u32, + modal: &'a mut Modal<'b>, + slider: &'a mut Slider, +} +impl<'a, 'b> ProgressBar<'a, 'b> { + pub fn new(modal: &'a mut Modal<'b>, slider: &'a mut Slider) -> ProgressBar<'a, 'b> { + ProgressBar { + subtask_start_work: 0, + subtask_end_work: 255, + current_work: 0, + subtask_start_percent: 0, + subtask_end_percent: 100, + current_progress_percent: 0, + modal, + slider, + } + } + pub fn modify(&mut self, update_action: Option, + update_top_text: Option<&str>, remove_top: bool, + update_bot_text: Option<&str>, remove_bot: bool, + update_style: Option) { + self.modal.modify(update_action, update_top_text, remove_top, update_bot_text, remove_bot, update_style); + } + pub fn activate(&self) { + self.modal.activate(); + } + pub fn update_text(&mut self, text: &str) { + self.modal.modify(None, Some(text), false, None, false, None); + } + /// There is a significant caveat for performance/stability for this routine. + /// This works well for a modal that is both managed and owned by the same thread so long as + /// one is not resizing the top or bottom text within the modal while changing the progress bar + /// However, if one triggers a redraw/resize of the text, then the system needs to re-run the + /// defacement computations on the underlying canvases. If one or two calls happen, this might + /// not be a big deal, but because the redraw requests that are looped back from the GAM as a + /// result of the defacement operation cannot be processed until the thread managing the progress + /// bar has finished and returned control to the main loop, redraw requests will eventually fill + /// up the server queue and cause a deadlock situation. + fn update_ui(&mut self, new_percent: u32) { + if new_percent != self.current_progress_percent { + log::debug!("progress: {}", new_percent); + self.slider.set_state(new_percent); + self.modal.modify( + Some(crate::ActionType::Slider(*self.slider)), + None, false, None, false, None); + self.modal.redraw(); // stage the modal box pixels to the back buffer + xous::yield_slice(); // this gives time for the GAM to do the sending + self.current_progress_percent = new_percent; + } + } + pub fn increment_work(&mut self, increment: u32) { + self.current_work += increment; + if self.current_work > self.subtask_end_work { + self.current_work = self.subtask_end_work; + } + let new_progress_percent = self.subtask_start_percent + + ((self.subtask_end_percent - self.subtask_start_percent) * self.current_work) / (self.subtask_end_work - self.subtask_start_work); + self.update_ui(new_progress_percent); + } + pub fn set_percentage(&mut self, setting: u32) { + let checked_setting = if setting > 100 { + 100 + } else { + setting + }; + self.update_ui(checked_setting); + } + pub fn rebase_subtask_work(&mut self, subtask_start_work: u32, subtask_end_work: u32) { + assert!(subtask_end_work > subtask_start_work); + self.subtask_start_work = subtask_start_work; + self.subtask_end_work = subtask_end_work; + self.current_work = self.subtask_start_work; + self.increment_work(0); // this will recompute the Ux state and draw it + } + pub fn rebase_subtask_percentage(&mut self, subtask_start_percent: u32, subtask_end_percent: u32) { + assert!(subtask_start_percent <= subtask_end_percent); + self.subtask_start_percent = subtask_start_percent; + self.subtask_end_percent = subtask_end_percent; + self.update_ui(self.subtask_start_percent); // this will redraw the UI if the start percent is different from the current + } +} diff --git a/services/gam/src/modal/radiobuttons.rs b/services/gam/src/modal/radiobuttons.rs new file mode 100644 index 000000000..85d5c875f --- /dev/null +++ b/services/gam/src/modal/radiobuttons.rs @@ -0,0 +1,194 @@ +use crate::*; + +use graphics_server::api::*; + +use xous_ipc::Buffer; + +use core::fmt::Write; +use locales::t; + +#[derive(Debug, Copy, Clone)] +pub struct RadioButtons { + pub items: [Option; MAX_ITEMS], + pub action_conn: xous::CID, + pub action_opcode: u32, + pub action_payload: RadioButtonPayload, // the current "radio button" selection + pub select_index: i16, // the current candidate to be selected + pub max_items: i16, + pub is_password: bool, +} +impl RadioButtons { + pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { + RadioButtons { + items: [None; MAX_ITEMS], + action_conn, + action_opcode, + action_payload: RadioButtonPayload::new(""), + select_index: 0, + max_items: 0, + is_password: false, + } + } + pub fn add_item(&mut self, new_item: ItemName) -> Option { + if self.action_payload.as_str().len() == 0 { + // default to the first item added + self.action_payload = RadioButtonPayload::new(new_item.as_str()); + } + for item in self.items.iter_mut() { + if item.is_none() { + self.max_items += 1; + *item = Some(new_item); + return None; + } + } + return Some(new_item); + } +} +impl ActionApi for RadioButtons { + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + fn height(&self, glyph_height: i16, margin: i16) -> i16 { + let mut total_items = 0; + // total items, then +1 for the "Okay" message + for item in self.items.iter() { + if item.is_some(){ total_items += 1} + } + (total_items + 1) * glyph_height + margin * 2 + margin * 2 + 5 // +4 for some bottom margin slop + } + fn redraw(&self, at_height: i16, modal: &Modal) { + let color = if self.is_password { + PixelColor::Light + } else { + PixelColor::Dark + }; + + // prime a textview with the correct general style parameters + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) + ); + tv.ellipsis = true; + tv.style = modal.style; + tv.invert = self.is_password; + tv.draw_border= false; + tv.margin = Point::new(0, 0,); + tv.insertion = None; + + let cursor_x = modal.margin; + let select_x = modal.margin + 20; + let text_x = modal.margin + 20 + 20; + + //let mut emoji_slop = (36 - modal.line_height) / 2; + //if emoji_slop < 0 { emoji_slop = 0; } + let emoji_slop = 2; // tweaked for a non-emoji glyph + + let mut cur_line = 0; + let mut do_okay = true; + for maybe_item in self.items.iter() { + if let Some(item) = maybe_item { + let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2; + if cur_line == self.select_index { + // draw the cursor + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) + )); + write!(tv, "ยป").unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + do_okay = false; + } + if item.as_str() == self.action_payload.as_str() { + // draw the radio dot + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(select_x, cur_y), Point::new(select_x + 36, cur_y + modal.line_height) + )); + write!(tv, "โ€ข").unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + // draw the text + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) + )); + write!(tv, "{}", item.as_str()).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + + cur_line += 1; + } + } + cur_line += 1; + let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2; + if do_okay { + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) + )); + write!(tv, "ยป").unwrap(); // right arrow emoji. use unicode numbers, because text editors do funny shit with emojis + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + // draw the "OK" line + tv.text.clear(); + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( + Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) + )); + write!(tv, "{}", t!("radio.select_and_close", xous::LANG)).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + + // divider lines + modal.gam.draw_line(modal.canvas, Line::new_with_style( + Point::new(modal.margin, at_height + modal.margin), + Point::new(modal.canvas_width - modal.margin, at_height + modal.margin), + DrawStyle::new(color, color, 1)) + ).expect("couldn't draw entry line"); + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + match k { + 'โ†' | 'โ†’' => { + // ignore these navigation keys + }, + 'โ†‘' => { + if self.select_index > 0 { + self.select_index -= 1; + } + } + 'โ†“' => { + if self.select_index < self.max_items + 1 { // +1 is the "OK" button + self.select_index += 1; + } + } + 'โˆด' | '\u{d}' => { + if self.select_index < self.max_items { + // iterate through to find the index -- because if we support a remove() API later, + // the list can have "holes", such that the index != index in the array + let mut cur_index = 0; + for maybe_item in self.items.iter() { + if let Some(item) = maybe_item { + if cur_index == self.select_index { + self.action_payload = RadioButtonPayload::new(item.as_str()); + break; + } + cur_index += 1; + } + } + } else { // the OK button select + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); + buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); + return (None, true) + } + } + '\u{0}' => { + // ignore null messages + } + _ => { + // ignore text entry + } + } + (None, false) + } +} \ No newline at end of file diff --git a/services/gam/src/modal/slider.rs b/services/gam/src/modal/slider.rs new file mode 100644 index 000000000..b6318156f --- /dev/null +++ b/services/gam/src/modal/slider.rs @@ -0,0 +1,203 @@ +use crate::*; + +use graphics_server::api::*; + +use core::fmt::Write; + +#[derive(Debug, Copy, Clone)] +pub struct Slider { + pub min: u32, + pub max: u32, + pub step: u32, + pub action_conn: xous::CID, + pub action_opcode: u32, + pub action_payload: u32, + pub is_progressbar: bool, + pub is_password: bool, + pub show_legend: bool, + pub units: xous_ipc::String::<8>, +} +impl Slider { + pub fn new(action_conn: xous::CID, action_opcode: u32, min: u32, max: u32, step: u32, units: Option<&str>, initial_setting: u32, is_progressbar: bool, show_legend: bool) -> Self { + let checked_units = if let Some(unit_str) = units { + if unit_str.len() < 8 { + String::<8>::from_str(unit_str) + } else { + log::error!("Unit string must be less than 8 *bytes* long (are you using unicode?), ignoring length {} string", unit_str.len()); + String::<8>::new() + } + } else { + String::<8>::new() // just populate with a blank string, easier than checking Some/None later on everywhere + }; + + Slider { + action_conn, + action_opcode, + is_password: false, + is_progressbar, + min, + max, + step, + action_payload: initial_setting, + units: checked_units, + show_legend, + } + } + pub fn set_is_password(&mut self, setting: bool) { + // this will cause text to be inverted. Untrusted entities can try to set this, + // but the GAM should defeat this for dialog boxes outside of the trusted boot + // set because they can't achieve a high enough trust level. + self.is_password = setting; + } + pub fn set_state(&mut self, state: u32) { + if state < self.min { + self.action_payload = self.min; + } else if state > self.max { + self.action_payload = self.max; + } else { + self.action_payload = state; + } + } +} +impl ActionApi for Slider { + fn height(&self, glyph_height: i16, margin: i16) -> i16 { + /* + margin + min max <- glyph height + -----O---------- <- glyph height + legend + margin + */ + if self.show_legend { + glyph_height * 3 + margin * 2 + } else { + glyph_height * 2 + margin * 2 + } + } + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + + fn redraw(&self, at_height: i16, modal: &Modal) { + let color = if self.is_password { + PixelColor::Light + } else { + PixelColor::Dark + }; + let fill_color = if self.is_password { + PixelColor::Dark + } else { + PixelColor::Light + }; + + // prime a textview with the correct general style parameters + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)) + ); + tv.ellipsis = true; + tv.style = modal.style; + tv.invert = self.is_password; + tv.draw_border= false; + tv.margin = Point::new(0, 0,); + tv.insertion = None; + + let maxwidth = (modal.canvas_width - modal.margin * 2) as u16; + if self.show_legend { + /* // min/max doesn't look good, leave it out for now + // render min + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::GrowableFromTl( + Point::new(modal.margin, at_height + modal.margin), + maxwidth + ); + tv.text.clear(); + write!(tv, "{}{}", self.min, self.units.to_str()).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + // render max + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::GrowableFromBr( + Point::new(modal.canvas_width - modal.margin, at_height + modal.margin + modal.line_height), + maxwidth + ); + tv.text.clear(); + write!(tv, "{}{}", self.max, self.units.to_str()).unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + */ + // estimate width of current setting + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::GrowableFromTl( + Point::new(0, 0), + maxwidth + ); + write!(tv, "{}{}", self.action_payload, self.units.to_str()).unwrap(); + modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size"); + let textwidth = if let Some(bounds) = tv.bounds_computed { + bounds.br.x - bounds.tl.x + } else { + maxwidth as i16 + }; + let offset = (modal.canvas_width - textwidth) / 2; + // render current setting + tv.bounds_computed = None; + tv.bounds_hint = TextBounds::GrowableFromTl( + Point::new(offset, at_height + modal.margin + modal.line_height*2 + modal.margin), + maxwidth + ); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + + // the actual slider + let outer_rect = Rectangle::new_with_style( + Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height), + Point::new(modal.canvas_width - modal.margin * 2, modal.margin + modal.line_height * 2 + at_height), + DrawStyle::new(fill_color, color, 2) + ); + modal.gam.draw_rectangle(modal.canvas, outer_rect).expect("couldn't draw outer rectangle"); + let total_width = modal.canvas_width - modal.margin * 4; + let slider_point = (total_width * (self.action_payload - self.min) as i16) / (self.max - self.min) as i16; + let inner_rect = Rectangle::new_with_style( + Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height), + Point::new(modal.margin * 2 + slider_point, modal.margin + modal.line_height * 2 + at_height), + DrawStyle::new(color, color, 1) + ); + modal.gam.draw_rectangle(modal.canvas, inner_rect).expect("couldn't draw inner rectangle"); + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + if !self.is_progressbar { + match k { + 'โ†' => { + if self.action_payload >= self.min + self.step { + self.action_payload -= self.step; + } else if self.action_payload >= self.min && self.action_payload < self.min + self.step { + self.action_payload = self.min + } + }, + 'โ†’' => { + if self.action_payload <= self.max - self.step { + self.action_payload += self.step; + } else if self.action_payload < self.max && self.action_payload > self.max - self.step { + self.action_payload = self.max + } + }, + '\u{0}' => { + // ignore null messages + } + 'โˆด' | '\u{d}' => { + send_message(self.action_conn, + xous::Message::new_scalar(self.action_opcode as usize, self.action_payload as usize, 0, 0, 0)).expect("couldn't pass on action payload"); + return(None, true) + } + _ => { + // ignore all other messages + } + } + (None, false) + } else { + if k == '๐Ÿ›‘' { // use the "stop" emoji as a signal that we should close the progress bar + (None, true) + } else { + (None, false) + } + } + } +} diff --git a/services/gam/src/modal/textentry.rs b/services/gam/src/modal/textentry.rs new file mode 100644 index 000000000..a2fba34f5 --- /dev/null +++ b/services/gam/src/modal/textentry.rs @@ -0,0 +1,251 @@ +use crate::*; + +use graphics_server::api::*; + +use xous_ipc::{String, Buffer}; +use num_traits::*; + +use core::fmt::Write; + +#[derive(Debug, Copy, Clone, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum TextEntryVisibility { + /// text is fully visible + Visible = 0, + /// only last chars are shown of text entry, the rest obscured with * + LastChars = 1, + /// all chars hidden as * + Hidden = 2, +} +#[derive(Copy, Clone)] +pub struct TextEntry { + pub is_password: bool, + pub visibility: TextEntryVisibility, + pub action_conn: xous::CID, + pub action_opcode: u32, + pub action_payload: TextEntryPayload, + // validator borrows the text entry payload, and returns an error message if something didn't go well. + // validator takes as ragument the current action_payload, and the current action_opcode + pub validator: Option Option> >, +} +impl ActionApi for TextEntry { + fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} + fn is_password(&self) -> bool { + self.is_password + } + /// The total canvas height is computed with this API call + /// The canvas height is not dynamically adjustable for modals. + fn height(&self, glyph_height: i16, margin: i16) -> i16 { + /* + ------------------- + | **** | <-- glyph_height + 2*margin + ------------------- + โ† ๐Ÿ‘๏ธ ๐Ÿ•ถ๏ธ * โ†’ <-- glyph_height + + + 2 * margin top/bottom + + auto-closes on enter + */ + if self.is_password { + glyph_height + 2*margin + glyph_height + 2*margin + 8 // 8 pixels extra margin because the emoji glyphs are oversized + } else { + glyph_height + 2*margin + } + } + fn redraw(&self, at_height: i16, modal: &Modal) { + let color = if self.is_password { + PixelColor::Light + } else { + PixelColor::Dark + }; + + // draw the currently entered text + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new( + Point::new(modal.margin, at_height), + Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height)) + )); + tv.ellipsis = true; // TODO: fix so we are drawing from the right-most entered text and old text is ellipsis *to the left* + tv.invert = self.is_password; + tv.style = modal.style; + tv.margin = Point::new(0, 0); + tv.draw_border = false; + tv.insertion = Some(self.action_payload.0.len() as i32); + tv.text.clear(); // make sure this is blank + let payload_chars = self.action_payload.0.as_str().unwrap().chars().count(); + // TODO: condense the "above 20" chars length path a bit -- written out "the dumb way" just to reason out the logic a bit + match self.visibility { + TextEntryVisibility::Visible => { + log::trace!("action payload: {}", self.action_payload.0.as_str().unwrap()); + if payload_chars < 20 { + write!(tv.text, "{}", self.action_payload.0.as_str().unwrap()).unwrap(); + } else { + write!(tv.text, "...{}", &self.action_payload.0.as_str().unwrap()[payload_chars-18..]).unwrap(); + } + modal.gam.post_textview(&mut tv).expect("couldn't post textview"); + }, + TextEntryVisibility::Hidden => { + if payload_chars < 20 { + for _char in self.action_payload.0.as_str().unwrap().chars() { + tv.text.push('*').expect("text field too long"); + } + } else { + // just render a pure dummy string + tv.text.push('.').unwrap(); + tv.text.push('.').unwrap(); + tv.text.push('.').unwrap(); + for _ in 0..18 { + tv.text.push('*').expect("text field too long"); + } + } + modal.gam.post_textview(&mut tv).expect("couldn't post textview"); + }, + TextEntryVisibility::LastChars => { + if payload_chars < 20 { + let hide_to = if self.action_payload.0.as_str().unwrap().chars().count() >= 2 { + self.action_payload.0.as_str().unwrap().chars().count() - 2 + } else { + 0 + }; + for (index, ch) in self.action_payload.0.as_str().unwrap().chars().enumerate() { + if index < hide_to { + tv.text.push('*').expect("text field too long"); + } else { + tv.text.push(ch).expect("text field too long"); + } + } + } else { + tv.text.push('.').unwrap(); + tv.text.push('.').unwrap(); + tv.text.push('.').unwrap(); + let hide_to = if self.action_payload.0.as_str().unwrap().chars().count() >= 2 { + self.action_payload.0.as_str().unwrap().chars().count() - 2 + } else { + 0 + }; + for (index, ch) in self.action_payload.0.as_str().unwrap()[payload_chars-18..].chars().enumerate() { + if index + payload_chars-18 < hide_to { + tv.text.push('*').expect("text field too long"); + } else { + tv.text.push(ch).expect("text field too long"); + } + } + } + modal.gam.post_textview(&mut tv).expect("couldn't post textview"); + } + } + if self.is_password { + // draw the visibility selection area + // "<๐Ÿ‘€๐Ÿคซโœด๏ธ>" coded explicitly. Pasting unicode into vscode yields extra cruft that we can't parse (e.g. skin tones and color mods). + let prompt = "\u{2b05} \u{1f440}\u{1f576}\u{26d4} \u{27a1}"; + let select_index = match self.visibility { + TextEntryVisibility::Visible => 2, + TextEntryVisibility::LastChars => 3, + TextEntryVisibility::Hidden => 4, + }; + let spacing = 38; // fixed width spacing for the array + let emoji_width = 36; + // center the prompt nicely, if possible + let left_edge = if modal.canvas_width > prompt.chars().count() as i16 * spacing { + (modal.canvas_width - prompt.chars().count() as i16 * spacing) / 2 + } else { + 0 + }; + for (i, ch) in prompt.chars().enumerate() { + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new( + Point::new(left_edge + i as i16 * spacing, at_height + modal.line_height + modal.margin * 4), + Point::new(left_edge + i as i16 * spacing + emoji_width, at_height + modal.line_height + 34 + modal.margin * 4)) + )); + tv.style = GlyphStyle::Regular; + tv.margin = Point::new(0, 0); + tv.draw_border = false; + if i == select_index { + tv.invert = !self.is_password; + } else { + tv.invert = self.is_password; + } + tv.text.clear(); + write!(tv.text, "{}", ch).unwrap(); + log::trace!("tv.text: {} : {}/{}", i, tv.text, ch); + modal.gam.post_textview(&mut tv).expect("couldn't post textview"); + } + } + + // draw a line for where text gets entered (don't use a box, fitting could be awkward) + modal.gam.draw_line(modal.canvas, Line::new_with_style( + Point::new(modal.margin, at_height + modal.line_height + 4), + Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height + 4), + DrawStyle::new(color, color, 1)) + ).expect("couldn't draw entry line"); + } + fn key_action(&mut self, k: char) -> (Option>, bool) { + log::trace!("key_action: {}", k); + match k { + 'โ†' => { + if self.visibility as u32 > 0 { + match FromPrimitive::from_u32(self.visibility as u32 - 1) { + Some(new_visibility) => { + log::trace!("new visibility: {:?}", new_visibility); + self.visibility = new_visibility; + }, + _ => { + panic!("internal error: an TextEntryVisibility did not resolve correctly"); + } + } + } + }, + 'โ†’' => { + if (self.visibility as u32) < (TextEntryVisibility::Hidden as u32) { + match FromPrimitive::from_u32(self.visibility as u32 + 1) { + Some(new_visibility) => { + log::trace!("new visibility: {:?}", new_visibility); + self.visibility = new_visibility + }, + _ => { + panic!("internal error: an TextEntryVisibility did not resolve correctly"); + } + } + } + }, + 'โˆด' | '\u{d}' => { + if let Some(validator) = self.validator { + if let Some(err_msg) = validator(self.action_payload, self.action_opcode) { + self.action_payload.0.clear(); // reset the input field + return (Some(err_msg), false); + } + } + + let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload"); + buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message"); + self.action_payload.volatile_clear(); // ensure the local copy of text is zero'd out + return (None, true) + } + 'โ†‘' | 'โ†“' => { + // ignore these navigation keys + } + '\u{0}' => { + // ignore null messages + } + '\u{8}' => { // backspace + // coded in a conservative manner to avoid temporary allocations that can leave the plaintext on the stack + if self.action_payload.0.len() > 0 { // don't backspace if we have no string. + let mut temp_str = String::<256>::from_str(self.action_payload.0.as_str().unwrap()); + let cur_len = temp_str.as_str().unwrap().chars().count(); + let mut c_iter = temp_str.as_str().unwrap().chars(); + self.action_payload.0.clear(); + for _ in 0..cur_len-1 { + self.action_payload.0.push(c_iter.next().unwrap()).unwrap(); + } + temp_str.volatile_clear(); + } + } + _ => { // text entry + self.action_payload.0.push(k).expect("ran out of space storing password"); + log::trace!("****update payload: {}", self.action_payload.0); + } + } + (None, false) + } +} \ No newline at end of file