Merge pull request #149 from nworbnhoj/index

modal selected index support
This commit is contained in:
bunnie 2022-04-24 21:27:08 +08:00 committed by GitHub
commit 62d56fb70b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 3 deletions

View file

@ -20,6 +20,7 @@ gam = {path="../gam"}
trng = {path="../trng"}
tts-frontend = {path="../tts"}
locales = {path = "../../locales"}
bit_field = "0.9.0"
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -83,6 +83,8 @@ pub(crate) enum Opcode {
/// add an item to the radio box or check box. Note that all added items
/// are cleared after the relevant "action" call happens (PromptWith[Fixed,Multi]Response)
AddModalItem,
/// get the index of the selected radio button / checkboxes
GetModalIndex,
/// raise a progress bar
StartProgress,
/// update the progress bar

View file

@ -8,6 +8,7 @@ use xous::{CID, send_message, Message};
use num_traits::*;
use xous_ipc::Buffer;
use core::cell::Cell;
use bit_field::BitField;
pub type TextValidationFn = fn(TextEntryPayload) -> Option<ValidatorErr>;
@ -200,6 +201,14 @@ impl Modals {
Ok(())
}
pub fn add_list(&self, items: Vec<&str>) -> Result<(), xous::Error> {
for (_, text) in items.iter().enumerate() {
self.add_list_item(text)
.or(Err(xous::Error::InternalError))?;
}
Ok(())
}
pub fn add_list_item(&self, item: &str) -> Result<(), xous::Error> {
self.lock();
let itemname = ManagedListItem {
@ -224,6 +233,24 @@ impl Modals {
Ok(String::from(itemname.as_str()))
}
pub fn get_radio_index(&self) -> Result<usize, xous::Error> {
let msg = Message::new_blocking_scalar(Opcode::GetModalIndex.to_usize().unwrap(), 0, 0, 0, 0);
match send_message(self.conn, msg) {
Ok(xous::Result::Scalar1(bitfield)) => {
let mut i = 0;
while (i < u32::bit_length()) & !bitfield.get_bit(i) {
i = i + 1;
}
if i < u32::bit_length() {
Ok(i)
} else {
Err(xous::Error::InternalError)
}
}
_ => Err(xous::Error::InternalError),
}
}
pub fn get_checkbox(&self, prompt: &str) -> Result<Vec::<String>, xous::Error> {
self.lock();
let spec = ManagedPromptWithFixedResponse {
@ -243,6 +270,24 @@ impl Modals {
Ok(ret)
}
pub fn get_check_index(&self) -> Result<Vec<usize>, xous::Error> {
let mut ret = Vec::<usize>::new();
let msg = Message::new_blocking_scalar(Opcode::GetModalIndex.to_usize().unwrap(), 0, 0, 0, 0);
match send_message(self.conn, msg) {
Ok(xous::Result::Scalar1(bitfield)) => {
let mut i = 0;
while i < u32::bit_length() {
if bitfield.get_bit(i) {
ret.push(i);
}
i = i + 1;
}
Ok(ret)
}
_ => Err(xous::Error::InternalError),
}
}
pub fn dynamic_notification(&self, title: Option<&str>, text: Option<&str>) -> Result<(), xous::Error> {
self.lock();
let spec = DynamicNotification {

View file

@ -42,6 +42,8 @@ use locales::t;
const TICK_INTERVAL: u64 = 2500;
use num_traits::*;
use bit_field::BitField;
use std::collections::HashMap;
#[derive(Debug)]
enum RendererState {
@ -108,6 +110,9 @@ fn xmain() -> ! {
Opcode::ModalDrop.to_u32().unwrap(),
);
let mut list_hash = HashMap::<String, usize>::new();
let mut list_selected = 0u32;
if cfg!(feature = "ux_tests") {
tt.sleep_ms(1000).unwrap();
tests::spawn_test();
@ -240,6 +245,9 @@ fn xmain() -> ! {
}
fixed_items.push(manageditem.item);
}
Some(Opcode::GetModalIndex) => {
xous::return_scalar(msg.sender, list_selected as usize).expect("couldn't return list selected");
}
Some(Opcode::DynamicNotification) => {
let spec = {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -364,8 +372,11 @@ fn xmain() -> ! {
renderer_cid,
Opcode::RadioReturn.to_u32().unwrap()
);
list_hash.clear();
list_selected = 0u32;
for item in fixed_items.iter() {
radiobuttons.add_item(*item);
list_hash.insert(item.as_str().to_string(), list_hash.len());
}
fixed_items.clear();
#[cfg(feature="tts")]
@ -385,8 +396,11 @@ fn xmain() -> ! {
renderer_cid,
Opcode::CheckBoxReturn.to_u32().unwrap()
);
list_hash.clear();
list_selected = 0u32;
for item in fixed_items.iter() {
checkbox.add_item(*item);
list_hash.insert(item.as_str().to_string(), list_hash.len());
}
fixed_items.clear();
#[cfg(feature="tts")]
@ -533,6 +547,15 @@ fn xmain() -> ! {
let mut response = unsafe { Buffer::from_memory_message_mut(origin.body.memory_message_mut().unwrap()) };
response.replace(item).unwrap();
op = RendererState::None;
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 {
log::error!("Ux routine returned but no origin was recorded");
panic!("Ux routine returned but no origin was recorded");
@ -555,6 +578,22 @@ fn xmain() -> ! {
let mut response = unsafe { Buffer::from_memory_message_mut(origin.body.memory_message_mut().unwrap()) };
response.replace(item).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 => {},
}
}
} else {
log::error!("Ux routine returned but no origin was recorded");
panic!("Ux routine returned but no origin was recorded");

View file

@ -55,9 +55,8 @@ pub(crate) fn spawn_test() {
modals.finish_progress().expect("couldn't dismiss progress bar");
// 2. test check box
for item in CHECKBOX_TEST {
modals.add_list_item(item).expect("couldn't build checkbox list");
}
let items: Vec<&str> = CHECKBOX_TEST.iter().map(|s| s.to_owned()).collect();
modals.add_list(items).expect("couldn't build checkbox list");
match modals.get_checkbox("You can have it all:") {
Ok(things) => {
log::info!("The user picked {} things:", things.len());
@ -67,6 +66,7 @@ pub(crate) fn spawn_test() {
},
_ => log::error!("get_checkbox failed"),
}
log::info!("Checkbox indices selected = {:?}", modals.get_check_index());
// 3. test notificatons
log::info!("testing notification");
@ -88,6 +88,7 @@ pub(crate) fn spawn_test() {
Ok(animal) => log::info!("{} was picked", animal),
_ => log::error!("get_radiobutton failed"),
}
log::info!("Radio index selected = {:?}", modals.get_radio_index().unwrap());
// 2. test the modal dialog box function
log::info!("test text input");