diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index d00bb7705..883296df2 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -73,8 +73,8 @@ impl ItemName { #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq, Default)] pub struct TextEntryPayload { pub id: u32, - pub content: String::<256> - //pub placeholder: String::<265>, + pub content: String::<256>, + pub placeholder: Option>, } impl TextEntryPayload { @@ -82,11 +82,12 @@ impl TextEntryPayload { TextEntryPayload{ id: Default::default(), content: Default::default(), + placeholder: Default::default(), } } - pub fn new_with_fields(id: u32, content: String::<256>) -> Self { - TextEntryPayload { id: id, content: content } + pub fn new_with_fields(id: u32, content: String::<256>, placeholder: Option>) -> Self { + TextEntryPayload { id: id, content: content, placeholder: placeholder } } /// Ensures that 0's are written to the storage of this struct, and not optimized out; important for password fields. pub fn volatile_clear(&mut self) { diff --git a/services/gam/src/modal/textentry.rs b/services/gam/src/modal/textentry.rs index e29a8b18a..d7286664d 100644 --- a/services/gam/src/modal/textentry.rs +++ b/services/gam/src/modal/textentry.rs @@ -61,8 +61,6 @@ impl Default for TextEntry { } } - - impl TextEntry { pub fn new( is_password: bool, @@ -87,8 +85,16 @@ impl TextEntry { } } - pub fn reset_action_payloads(&mut self, fields: u32) { - self.action_payloads = vec![TextEntryPayload::default(); fields as usize]; + pub fn reset_action_payloads(&mut self, fields: u32, placeholders: Option<[Option>; 10]>) { + let mut payload = vec![TextEntryPayload::default(); fields as usize]; + + if let Some(placeholders) = placeholders { + for (index, element) in payload.iter_mut().enumerate() { + element.placeholder = placeholders[index]; + } + } + + self.action_payloads = payload; self.max_field_amount = fields; } } @@ -141,11 +147,29 @@ impl ActionApi for TextEntry { _ => current_height + at_height, }; + if index as i16 == self.selected_field { + // draw the dot + let mut tv = TextView::new( + modal.canvas, + TextBounds::BoundingBox(Rectangle::new( + Point::new(modal.margin, current_height), + Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height)) + )); + + tv.text.clear(); + tv.bounds_computed = None; + tv.draw_border = false; + write!(tv, "•").unwrap(); + modal.gam.post_textview(&mut tv).expect("couldn't post tv"); + } + + let left_text_margin = modal.margin + 15; + // draw the currently entered text let mut tv = TextView::new( modal.canvas, TextBounds::BoundingBox(Rectangle::new( - Point::new(modal.margin, current_height), + Point::new(left_text_margin, current_height), Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height)) )); tv.ellipsis = true; @@ -153,7 +177,11 @@ impl ActionApi for TextEntry { tv.style = if self.is_password { GlyphStyle::Monospace } else { - modal.style + if payload.placeholder.is_some() && payload.content.len().is_zero() { + GlyphStyle::Small + } else { + modal.style + } }; tv.margin = Point::new(0, 0); tv.draw_border = false; @@ -163,11 +191,20 @@ impl ActionApi for TextEntry { // TODO: condense the "above MAX_CHARS" 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: {}", payload.content.as_str().unwrap()); + let content = { + if payload.placeholder.is_some() && payload.content.len().is_zero() { + let placeholder_content = payload.placeholder.unwrap(); + placeholder_content.to_string() + } else { + payload.content.to_string() + } + }; + + log::trace!("action payload: {}", content); if payload_chars < MAX_CHARS { - write!(tv.text, "{}", payload.content.as_str().unwrap()).unwrap(); + write!(tv.text, "{}", content).unwrap(); } else { - write!(tv.text, "...{}", &payload.content.as_str().unwrap()[payload_chars-(MAX_CHARS - 3)..]).unwrap(); + write!(tv.text, "...{}", &content[content.chars().count()-(MAX_CHARS - 3)..]).unwrap(); } modal.gam.post_textview(&mut tv).expect("couldn't post textview"); }, @@ -290,7 +327,7 @@ impl ActionApi for TextEntry { // 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, current_height + modal.line_height + 4), + Point::new(left_text_margin, current_height + modal.line_height + 4), Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height + 4), DrawStyle::new(color, color, 1)) ).expect("couldn't draw entry line"); diff --git a/services/modals/src/api.rs b/services/modals/src/api.rs index acaaf8aea..5fb7fcecc 100644 --- a/services/modals/src/api.rs +++ b/services/modals/src/api.rs @@ -23,6 +23,7 @@ pub struct ManagedListItem { pub token: [u32; 4], pub item: ItemName, } + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct ManagedPromptWithTextResponse { pub token: [u32; 4], @@ -33,7 +34,10 @@ pub struct ManagedPromptWithTextResponse { pub validator_op: u32, /// the amount of fields to read pub fields: u32, + /// placeholders + pub placeholders: Option<[Option>; 10]>, } + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct ManagedNotification { pub token: [u32; 4], diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 119a61f7d..96ccd0924 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -33,7 +33,7 @@ impl Modals { maybe_validator: Option Option>, maybe_validator_op: Option, ) -> Result { - match self.get_text_multi(prompt, maybe_validator, maybe_validator_op, 1) { + match self.get_text_multi(prompt, maybe_validator, maybe_validator_op, 1, None) { Ok(res) => { return Ok(res.first()) }, @@ -46,6 +46,7 @@ impl Modals { maybe_validator: Option Option>, maybe_validator_op: Option, fields: u32, + placeholders: Option>>, ) -> Result { self.lock(); let validator = if let Some(validator) = maybe_validator { @@ -80,12 +81,40 @@ impl Modals { } else { None }; + + let mut final_placeholders: Option<[Option>; 10]> = None; + + match placeholders { + Some(placeholders) => { + let mut pl:[Option>; 10] = Default::default(); + //final_placeholders = Some(Default::default()); + + if fields != placeholders.len() as u32 { + log::warn!("can't have more fields than placeholders"); + return Err(xous::Error::UnknownError); + } + + for (index, placeholder) in placeholders.iter().enumerate() { + match placeholder { + Some(string) => { + pl[index] = Some(xous_ipc::String::from_str(&string)) + }, + None => pl[index] = None, + } + } + + final_placeholders = Some(pl) + } + None => (), + } + let spec = ManagedPromptWithTextResponse { token: self.token, prompt: xous_ipc::String::from_str(prompt), validator, validator_op: maybe_validator_op.unwrap_or(0), fields: fields, + placeholders: final_placeholders, }; let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::PromptWithTextResponse.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 0c0911c80..0bbfb8383 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -146,7 +146,7 @@ fn xmain() -> ! { renderer_modal.modify( Some(ActionType::TextEntry({ let mut ta = text_action.clone(); - ta.reset_action_payloads(config.fields); + ta.reset_action_payloads(config.fields, config.placeholders); ta })), diff --git a/services/shellchat/src/cmds/ver.rs b/services/shellchat/src/cmds/ver.rs index 7f6310be6..ad48bfdbe 100644 --- a/services/shellchat/src/cmds/ver.rs +++ b/services/shellchat/src/cmds/ver.rs @@ -8,7 +8,17 @@ pub struct Ver { // TODO: remove me impl Ver { fn modal(&self, env: &mut CommonEnv) { - log::info!("modal data: {:#?}", env.modals.get_text_multi("Get Text", None, None, 4)) + log::info!("modal data: {:#?}", env.modals.get_text_multi( + "Get Text", + None, + None, + 4, + Some(vec![ + Some(std::string::String::from("first")), + Some(std::string::String::from("second")), + None, + Some(std::string::String::from("fourth")), + ]))) } }