add placeholders, current selection highlight

This commit is contained in:
Gianguido Sorà 2022-04-05 16:18:43 +02:00
parent 0b62a2657a
commit ba347f6c79
6 changed files with 98 additions and 17 deletions

View file

@ -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<String::<256>>,
}
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<String::<256>>) -> 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) {

View file

@ -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<xous_ipc::String<256>>; 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");

View file

@ -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<xous_ipc::String::<256>>; 10]>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ManagedNotification {
pub token: [u32; 4],

View file

@ -33,7 +33,7 @@ impl Modals {
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
maybe_validator_op: Option<u32>,
) -> Result<TextEntryPayload, xous::Error> {
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<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
maybe_validator_op: Option<u32>,
fields: u32,
placeholders: Option<Vec<Option<String>>>,
) -> Result<TextEntryPayloads, xous::Error> {
self.lock();
let validator = if let Some(validator) = maybe_validator {
@ -80,12 +81,40 @@ impl Modals {
} else {
None
};
let mut final_placeholders: Option<[Option<xous_ipc::String<256>>; 10]> = None;
match placeholders {
Some(placeholders) => {
let mut pl:[Option<xous_ipc::String<256>>; 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))?;

View file

@ -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
})),

View file

@ -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")),
])))
}
}