add stateful list creation

this allows us to have "pre-checked" checklists
This commit is contained in:
bunnie 2025-11-30 21:31:39 +08:00
parent 7d7fa75e7d
commit 98e9d99de8
3 changed files with 37 additions and 22 deletions

View file

@ -28,6 +28,7 @@ pub struct ManagedPromptWithFixedResponse {
pub struct ManagedListItem {
pub token: [u32; 4],
pub item: ItemName,
pub state: bool,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)]

View file

@ -595,6 +595,13 @@ impl Modals {
Ok(())
}
pub fn add_stateful_list(&self, items: Vec<(bool, &str)>) -> Result<(), xous::Error> {
for (_, &(state, text)) in items.iter().enumerate() {
self.add_stateful_list_item(state, text).or(Err(xous::Error::InternalError))?;
}
Ok(())
}
/// Add individual items to a list to be used by get_radiobutton or get_checkbox.
/// - Does not display on its own, the above mentioned methods prompt display of the list.
///
@ -610,7 +617,15 @@ impl Modals {
/// ```
pub fn add_list_item(&self, item: &str) -> Result<(), xous::Error> {
self.lock();
let itemname = ManagedListItem { token: self.token, item: ItemName::new(item) };
let itemname = ManagedListItem { token: self.token, item: ItemName::new(item), state: false };
let buf = Buffer::into_buf(itemname).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::AddModalItem.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
Ok(())
}
pub fn add_stateful_list_item(&self, state: bool, item: &str) -> Result<(), xous::Error> {
self.lock();
let itemname = ManagedListItem { token: self.token, item: ItemName::new(item), state };
let buf = Buffer::into_buf(itemname).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::AddModalItem.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
Ok(())
@ -739,10 +754,8 @@ impl Modals {
.or(Err(xous::Error::InternalError))?;
let selected_items = buf.to_original::<CheckBoxPayload, _>().unwrap();
let mut ret = Vec::<String>::new();
for maybe_item in selected_items.payload() {
if let Some(item) = maybe_item {
ret.push(String::from(item.as_str()));
}
for item in selected_items.payload() {
ret.push(String::from(item.as_str()));
}
self.unlock();
Ok(ret)

View file

@ -117,7 +117,7 @@ fn wrapped_main() -> ! {
text_action.action_conn = renderer_cid;
text_action.action_opcode = Opcode::TextEntryReturn.to_u32().unwrap();
let mut fixed_items = Vec::<ItemName>::new();
let mut fixed_items = Vec::<(bool, ItemName)>::new();
let mut progress_action = Slider::new(
renderer_cid,
Opcode::SliderReturn.to_u32().unwrap(),
@ -386,7 +386,7 @@ fn wrapped_main() -> ! {
);
continue;
}
fixed_items.push(manageditem.item);
fixed_items.push((manageditem.state, manageditem.item));
}
Some(Opcode::GetModalIndex) => {
xous::return_scalar(msg.sender, list_selected as usize)
@ -635,7 +635,7 @@ fn wrapped_main() -> ! {
RadioButtons::new(renderer_cid, Opcode::RadioReturn.to_u32().unwrap());
list_hash.clear();
list_selected = 0u32;
for item in fixed_items.iter() {
for (_, item) in fixed_items.iter() {
radiobuttons.add_item(item.clone());
list_hash.insert(item.as_str().to_string(), list_hash.len());
}
@ -660,8 +660,12 @@ fn wrapped_main() -> ! {
CheckBoxes::new(renderer_cid, Opcode::CheckBoxReturn.to_u32().unwrap());
list_hash.clear();
list_selected = 0u32;
for item in fixed_items.iter() {
checkbox.add_item(item.clone());
for (checked, item) in fixed_items.iter() {
if *checked {
checkbox.add_checked_item(item.clone());
} else {
checkbox.add_item(item.clone());
}
list_hash.insert(item.as_str().to_string(), list_hash.len());
}
fixed_items.clear();
@ -981,18 +985,15 @@ fn wrapped_main() -> ! {
};
response.replace(item.clone()).unwrap();
op = RendererState::None;
for (_, check_item) in item.payload().iter().enumerate() {
match check_item {
Some(item) => match list_hash.get(item.as_str()) {
Some(index) => {
match index {
0..=31 => drop(list_selected.set_bit(*index, true)),
_ => log::warn!("invalid bitfield index"),
};
}
None => log::warn!("failed to set list_selected index"),
},
None => {}
for (_, item) in item.payload().iter().enumerate() {
match list_hash.get(item.as_str()) {
Some(index) => {
match index {
0..=31 => drop(list_selected.set_bit(*index, true)),
_ => log::warn!("invalid bitfield index"),
};
}
None => log::warn!("failed to set list_selected index"),
}
}
} else {