add persistent default text for issue #281

this adds a method field_placeholder_persist() to the
text entry box builder. When this is used (instead of
field()), the placeholder, if provided, is treated as
"persistent" data, that is, when you press a key, it is
not automatically cleared, but instead, the user starts
editing the existing data.
This commit is contained in:
bunnie 2022-11-30 14:58:23 +08:00
parent e8671650e3
commit 6a1271e890
4 changed files with 47 additions and 10 deletions

View file

@ -92,6 +92,7 @@ pub struct TextEntryPayload {
dirty: bool,
pub content: String::<256>,
pub placeholder: Option<String::<256>>,
pub placeholder_persist: bool,
}
impl TextEntryPayload {
@ -100,11 +101,12 @@ impl TextEntryPayload {
dirty: Default::default(),
content: Default::default(),
placeholder: Default::default(),
placeholder_persist: false,
}
}
pub fn new_with_fields(content: String::<256>, placeholder: Option<String::<256>>) -> Self {
TextEntryPayload { dirty: false, content: content, placeholder: placeholder }
TextEntryPayload { dirty: false, content: content, placeholder: placeholder, placeholder_persist: false }
}
/// 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

@ -99,12 +99,18 @@ impl TextEntry {
}
}
pub fn reset_action_payloads(&mut self, fields: u32, placeholders: Option<[Option<xous_ipc::String<256>>; 10]>) {
pub fn reset_action_payloads(&mut self, fields: u32, placeholders: Option<[Option<(xous_ipc::String<256>, bool)>; 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];
if let Some((p, persist)) = placeholders[index] {
element.placeholder = Some(p);
element.placeholder_persist = persist;
} else {
element.placeholder = None;
element.placeholder_persist = false;
}
}
}
@ -467,6 +473,10 @@ impl ActionApi for TextEntry {
let tts = tts_frontend::TtsFrontend::new(&xns).unwrap();
tts.tts_blocking(locales::t!("input.delete-tts", xous::LANG)).unwrap();
}
if payload.placeholder_persist && payload.placeholder.is_some() && payload.content.len() == 0 {
// copy the placeholder into the content string before processing the backspace
payload.content.append(payload.placeholder.unwrap().to_str()).ok();
}
// coded in a conservative manner to avoid temporary allocations that can leave the plaintext on the stack
if payload.content.len() > 0 { // don't backspace if we have no string.
let mut temp_str = String::<256>::from_str(payload.content.as_str().unwrap());
@ -481,6 +491,10 @@ impl ActionApi for TextEntry {
}
_ => { // text entry
self.keys_hit[self.selected_field as usize] = true;
if payload.placeholder_persist && payload.placeholder.is_some() && payload.content.len() == 0 {
// copy the placeholder into the content string before processing the backspace
payload.content.append(payload.placeholder.unwrap().to_str()).ok();
}
#[cfg(feature="tts")]
{
let xns = xous_names::XousNames::new().unwrap();

View file

@ -32,7 +32,7 @@ pub struct ManagedPromptWithTextResponse {
pub prompt: xous_ipc::String<1024>,
pub fields: u32,
/// placeholders
pub placeholders: Option<[Option<xous_ipc::String<256>>; 10]>,
pub placeholders: Option<[Option<(xous_ipc::String<256>, bool)>; 10]>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]

View file

@ -21,24 +21,45 @@ pub type TextValidationFn = fn(TextEntryPayload) -> Option<ValidatorErr>;
pub struct AlertModalBuilder<'a> {
prompt: String,
validators: Vec<Option<TextValidationFn>>,
placeholders: Vec<Option<String>>,
placeholders: Vec<Option<(String, bool)>>,
modals: &'a Modals,
}
impl<'a> AlertModalBuilder<'a> {
/// Placeholders, when provided, disappear on keypress or backspace; they persist with left/right arrows.
/// This is useful for suggesting an input, or explaining what a field does.
pub fn field(
&'a mut self,
placeholder: Option<String>,
validator: Option<TextValidationFn>,
) -> &'a mut Self {
self.validators.push(validator);
self.placeholders.push(placeholder);
if let Some(p) = placeholder {
self.placeholders.push(Some((p, false)));
} else {
self.placeholders.push(None)
}
self
}
/// Placeholders provided in this method persist when any keys are pressed, instead of disappearing.
/// This is useful for "edit" functions.
pub fn field_placeholder_persist(
&'a mut self,
placeholder: Option<String>,
validator: Option<TextValidationFn>,
) -> &'a mut Self {
self.validators.push(validator);
if let Some(p) = placeholder {
self.placeholders.push(Some((p, true)));
} else {
self.placeholders.push(None)
}
self
}
pub fn build(&self) -> Result<TextEntryPayloads, xous::Error> {
self.modals.lock();
let mut final_placeholders: Option<[Option<xous_ipc::String<256>>; 10]> = None;
let mut final_placeholders: Option<[Option<(xous_ipc::String<256>, bool)>; 10]> = None;
let fields_amt = self.validators.len();
if fields_amt == 0 {
@ -49,7 +70,7 @@ impl<'a> AlertModalBuilder<'a> {
match self.placeholders.len() {
1.. => {
let mut pl: [Option<xous_ipc::String<256>>; 10] = Default::default();
let mut pl: [Option<(xous_ipc::String<256>, bool)>; 10] = Default::default();
if fields_amt != self.placeholders.len() {
log::warn!("can't have more fields than placeholders");
@ -58,8 +79,8 @@ impl<'a> AlertModalBuilder<'a> {
}
for (index, placeholder) in self.placeholders.iter().enumerate() {
if let Some(string) = placeholder {
pl[index] = Some(xous_ipc::String::from_str(&string))
if let Some((string, persist)) = placeholder {
pl[index] = Some((xous_ipc::String::from_str(&string), *persist))
} else {
pl[index] = None
}