minor fixes to multi-modals input

- track field heights using the drawing height directly
- use an &mut reference in the text processing section so changes stick
- other minor cosmetic improvements
This commit is contained in:
bunnie 2022-04-22 17:44:25 +08:00
parent 34c2ffe1a3
commit 987f40f5de

View file

@ -6,6 +6,7 @@ use xous_ipc::{String, Buffer};
use num_traits::*; use num_traits::*;
use core::fmt::Write; use core::fmt::Write;
use core::cell::Cell;
// TODO: figure out this, do we really have to limit ourselves to 10? // TODO: figure out this, do we really have to limit ourselves to 10?
const MAX_FIELDS: i16 = 10; const MAX_FIELDS: i16 = 10;
@ -53,6 +54,7 @@ pub struct TextEntry {
max_field_amount: u32, max_field_amount: u32,
selected_field: i16, selected_field: i16,
field_height: Cell::<i16>,
} }
impl Default for TextEntry { impl Default for TextEntry {
@ -66,6 +68,7 @@ impl Default for TextEntry {
selected_field: Default::default(), selected_field: Default::default(),
action_payloads: Default::default(), action_payloads: Default::default(),
max_field_amount: 0, max_field_amount: 0,
field_height: Cell::new(0),
} }
} }
} }
@ -127,17 +130,23 @@ impl ActionApi for TextEntry {
auto-closes on enter auto-closes on enter
*/ */
// the glyph_height is an opaque value because the final value depends upon a lookup table
// stored in the graphics_server crate. To obtain this would require a handle to that server
// which is private to the GAM. Thus we receive a copy of this from our caller and stash it
// here for future reference. `glyph_height` can change depending upon the locale; in particular,
// CJK languages have taller glyphs.
self.field_height.set(glyph_height + 2*margin); // stash a copy for later
let glyph_height = match self.action_payloads.len() { // compute the overall_height of the entry fields
0 | 1 => 1, let mut overall_height =
_ => glyph_height * (self.action_payloads.len() as i16 + 2), self.field_height.get() * self.action_payloads.len() as i16;
} as i16;
// if we're a password, we add an extra glyph_height to the bottom for the text visibility items
if self.is_password { if self.is_password {
glyph_height + 2*margin + glyph_height overall_height += glyph_height;
} else {
glyph_height + 2*margin
} }
overall_height
} }
fn redraw(&self, at_height: i16, modal: &Modal) { fn redraw(&self, at_height: i16, modal: &Modal) {
const MAX_CHARS: usize = 33; const MAX_CHARS: usize = 33;
@ -150,12 +159,13 @@ impl ActionApi for TextEntry {
let mut current_height = at_height; let mut current_height = at_height;
let payloads = self.action_payloads.clone(); let payloads = self.action_payloads.clone();
for (index, payload) in payloads.iter().enumerate() { let bullet_margin = if payloads.len() > 1 {
current_height = match index { 17 // this is the margin for drawing the selection bullet
0 => at_height, } else {
_ => current_height + at_height, 0 // no selection bullet
}; };
for (index, payload) in payloads.iter().enumerate() {
if index as i16 == self.selected_field && payloads.len() > 1 { if index as i16 == self.selected_field && payloads.len() > 1 {
// draw the dot // draw the dot
let mut tv = TextView::new( let mut tv = TextView::new(
@ -168,21 +178,19 @@ impl ActionApi for TextEntry {
tv.text.clear(); tv.text.clear();
tv.bounds_computed = None; tv.bounds_computed = None;
tv.draw_border = false; tv.draw_border = false;
write!(tv, "•").unwrap(); write!(tv, "•").unwrap(); // emoji glyph will be summoned in this case
modal.gam.post_textview(&mut tv).expect("couldn't post tv"); modal.gam.post_textview(&mut tv).expect("couldn't post tv");
} }
let left_text_margin = match payloads.len() {
1 => modal.margin, let left_text_margin = modal.margin + bullet_margin; // space for the bullet point on the left, if it's there
_ => modal.margin + 15,
};
// draw the currently entered text // draw the currently entered text
let mut tv = TextView::new( let mut tv = TextView::new(
modal.canvas, modal.canvas,
TextBounds::BoundingBox(Rectangle::new( TextBounds::BoundingBox(Rectangle::new(
Point::new(left_text_margin, current_height), Point::new(left_text_margin, current_height),
Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height)) Point::new(modal.canvas_width - (modal.margin + bullet_margin), current_height + modal.line_height))
)); ));
tv.ellipsis = true; tv.ellipsis = true;
tv.invert = self.is_password; tv.invert = self.is_password;
@ -190,6 +198,7 @@ impl ActionApi for TextEntry {
GlyphStyle::Monospace GlyphStyle::Monospace
} else { } else {
if payload.placeholder.is_some() && payload.content.len().is_zero() { if payload.placeholder.is_some() && payload.content.len().is_zero() {
// note: this is just a "recommendation" - if there is an emoji or chinese character in this string, the height revers to modal.style's height
GlyphStyle::Small GlyphStyle::Small
} else { } else {
modal.style modal.style
@ -339,15 +348,17 @@ impl ActionApi for TextEntry {
// draw a line for where text gets entered (don't use a box, fitting could be awkward) // 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( modal.gam.draw_line(modal.canvas, Line::new_with_style(
Point::new(left_text_margin, current_height + modal.line_height + 4), Point::new(left_text_margin, current_height + modal.line_height + 3),
Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height + 4), Point::new(modal.canvas_width - (modal.margin + bullet_margin), current_height + modal.line_height + 3),
DrawStyle::new(color, color, 1)) DrawStyle::new(color, color, 1))
).expect("couldn't draw entry line"); ).expect("couldn't draw entry line");
current_height += self.field_height.get();
} }
} }
fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
let mut payload = self.action_payloads[self.selected_field as usize]; // needs to be a reference, otherwise we're operating on a copy of the payload!
let payload = &mut self.action_payloads[self.selected_field as usize];
let can_move_downwards = !(self.selected_field+1 == self.max_field_amount as i16); let can_move_downwards = !(self.selected_field+1 == self.max_field_amount as i16);
let can_move_upwards = !(self.selected_field-1 < 0); let can_move_upwards = !(self.selected_field-1 < 0);
@ -382,7 +393,7 @@ impl ActionApi for TextEntry {
}, },
'∴' | '\u{d}' => { '∴' | '\u{d}' => {
if let Some(validator) = self.validator { if let Some(validator) = self.validator {
if let Some(err_msg) = validator(payload, self.action_opcode) { if let Some(err_msg) = validator(*payload, self.action_opcode) {
payload.content.clear(); // reset the input field payload.content.clear(); // reset the input field
return (Some(err_msg), false); return (Some(err_msg), false);
} }
@ -444,7 +455,6 @@ impl ActionApi for TextEntry {
payload.content.push(k).expect("ran out of space storing password"); payload.content.push(k).expect("ran out of space storing password");
log::trace!("****update payload: {}", payload.content); log::trace!("****update payload: {}", payload.content);
payload.dirty = true; payload.dirty = true;
self.action_payloads[self.selected_field as usize] = payload;
} }
} }