mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add placeholders, current selection highlight
This commit is contained in:
parent
0b62a2657a
commit
ba347f6c79
6 changed files with 98 additions and 17 deletions
|
|
@ -73,8 +73,8 @@ impl ItemName {
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq, Default)]
|
||||||
pub struct TextEntryPayload {
|
pub struct TextEntryPayload {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
pub content: String::<256>
|
pub content: String::<256>,
|
||||||
//pub placeholder: String::<265>,
|
pub placeholder: Option<String::<256>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextEntryPayload {
|
impl TextEntryPayload {
|
||||||
|
|
@ -82,11 +82,12 @@ impl TextEntryPayload {
|
||||||
TextEntryPayload{
|
TextEntryPayload{
|
||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
content: Default::default(),
|
content: Default::default(),
|
||||||
|
placeholder: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_fields(id: u32, content: String::<256>) -> Self {
|
pub fn new_with_fields(id: u32, content: String::<256>, placeholder: Option<String::<256>>) -> Self {
|
||||||
TextEntryPayload { id: id, content: content }
|
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.
|
/// 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) {
|
pub fn volatile_clear(&mut self) {
|
||||||
|
|
|
||||||
|
|
@ -61,8 +61,6 @@ impl Default for TextEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl TextEntry {
|
impl TextEntry {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
is_password: bool,
|
is_password: bool,
|
||||||
|
|
@ -87,8 +85,16 @@ impl TextEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_action_payloads(&mut self, fields: u32) {
|
pub fn reset_action_payloads(&mut self, fields: u32, placeholders: Option<[Option<xous_ipc::String<256>>; 10]>) {
|
||||||
self.action_payloads = vec![TextEntryPayload::default(); fields as usize];
|
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;
|
self.max_field_amount = fields;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -141,11 +147,29 @@ impl ActionApi for TextEntry {
|
||||||
_ => current_height + at_height,
|
_ => 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
|
// 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(modal.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, current_height + modal.line_height))
|
||||||
));
|
));
|
||||||
tv.ellipsis = true;
|
tv.ellipsis = true;
|
||||||
|
|
@ -153,7 +177,11 @@ impl ActionApi for TextEntry {
|
||||||
tv.style = if self.is_password {
|
tv.style = if self.is_password {
|
||||||
GlyphStyle::Monospace
|
GlyphStyle::Monospace
|
||||||
} else {
|
} 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.margin = Point::new(0, 0);
|
||||||
tv.draw_border = false;
|
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
|
// 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 {
|
match self.visibility {
|
||||||
TextEntryVisibility::Visible => {
|
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 {
|
if payload_chars < MAX_CHARS {
|
||||||
write!(tv.text, "{}", payload.content.as_str().unwrap()).unwrap();
|
write!(tv.text, "{}", content).unwrap();
|
||||||
} else {
|
} 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");
|
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)
|
// 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(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),
|
Point::new(modal.canvas_width - modal.margin, current_height + modal.line_height + 4),
|
||||||
DrawStyle::new(color, color, 1))
|
DrawStyle::new(color, color, 1))
|
||||||
).expect("couldn't draw entry line");
|
).expect("couldn't draw entry line");
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ pub struct ManagedListItem {
|
||||||
pub token: [u32; 4],
|
pub token: [u32; 4],
|
||||||
pub item: ItemName,
|
pub item: ItemName,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
pub struct ManagedPromptWithTextResponse {
|
pub struct ManagedPromptWithTextResponse {
|
||||||
pub token: [u32; 4],
|
pub token: [u32; 4],
|
||||||
|
|
@ -33,7 +34,10 @@ pub struct ManagedPromptWithTextResponse {
|
||||||
pub validator_op: u32,
|
pub validator_op: u32,
|
||||||
/// the amount of fields to read
|
/// the amount of fields to read
|
||||||
pub fields: u32,
|
pub fields: u32,
|
||||||
|
/// placeholders
|
||||||
|
pub placeholders: Option<[Option<xous_ipc::String::<256>>; 10]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
pub struct ManagedNotification {
|
pub struct ManagedNotification {
|
||||||
pub token: [u32; 4],
|
pub token: [u32; 4],
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ impl Modals {
|
||||||
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
|
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
|
||||||
maybe_validator_op: Option<u32>,
|
maybe_validator_op: Option<u32>,
|
||||||
) -> Result<TextEntryPayload, xous::Error> {
|
) -> 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) => {
|
Ok(res) => {
|
||||||
return Ok(res.first())
|
return Ok(res.first())
|
||||||
},
|
},
|
||||||
|
|
@ -46,6 +46,7 @@ impl Modals {
|
||||||
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
|
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr>>,
|
||||||
maybe_validator_op: Option<u32>,
|
maybe_validator_op: Option<u32>,
|
||||||
fields: u32,
|
fields: u32,
|
||||||
|
placeholders: Option<Vec<Option<String>>>,
|
||||||
) -> Result<TextEntryPayloads, xous::Error> {
|
) -> Result<TextEntryPayloads, xous::Error> {
|
||||||
self.lock();
|
self.lock();
|
||||||
let validator = if let Some(validator) = maybe_validator {
|
let validator = if let Some(validator) = maybe_validator {
|
||||||
|
|
@ -80,12 +81,40 @@ impl Modals {
|
||||||
} else {
|
} else {
|
||||||
None
|
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 {
|
let spec = ManagedPromptWithTextResponse {
|
||||||
token: self.token,
|
token: self.token,
|
||||||
prompt: xous_ipc::String::from_str(prompt),
|
prompt: xous_ipc::String::from_str(prompt),
|
||||||
validator,
|
validator,
|
||||||
validator_op: maybe_validator_op.unwrap_or(0),
|
validator_op: maybe_validator_op.unwrap_or(0),
|
||||||
fields: fields,
|
fields: fields,
|
||||||
|
placeholders: final_placeholders,
|
||||||
};
|
};
|
||||||
let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
|
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))?;
|
buf.lend_mut(self.conn, Opcode::PromptWithTextResponse.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,7 @@ fn xmain() -> ! {
|
||||||
renderer_modal.modify(
|
renderer_modal.modify(
|
||||||
Some(ActionType::TextEntry({
|
Some(ActionType::TextEntry({
|
||||||
let mut ta = text_action.clone();
|
let mut ta = text_action.clone();
|
||||||
ta.reset_action_payloads(config.fields);
|
ta.reset_action_payloads(config.fields, config.placeholders);
|
||||||
|
|
||||||
ta
|
ta
|
||||||
})),
|
})),
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,17 @@ pub struct Ver {
|
||||||
// TODO: remove me
|
// TODO: remove me
|
||||||
impl Ver {
|
impl Ver {
|
||||||
fn modal(&self, env: &mut CommonEnv) {
|
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")),
|
||||||
|
])))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue