mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
refactor checkboxes and radiobuttons to use dynamically allocated lists
This commit is contained in:
parent
60fc03351a
commit
44453bcbde
6 changed files with 104 additions and 140 deletions
|
|
@ -31,7 +31,6 @@ use core::fmt::Write;
|
|||
pub const MAX_ITEMS: usize = 8;
|
||||
|
||||
#[enum_dispatch(ActionApi)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum ActionType {
|
||||
TextEntry,
|
||||
RadioButtons,
|
||||
|
|
@ -95,6 +94,9 @@ impl RadioButtonPayload {
|
|||
pub fn as_str(&self) -> &str {
|
||||
self.0.as_str()
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.0.0.clear();
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
pub struct CheckBoxPayload([Option<ItemName>; MAX_ITEMS]); // returns a list of potential items that could be selected
|
||||
|
|
@ -167,7 +169,7 @@ pub struct Modal<'a> {
|
|||
bot_memoized_height: Option<i16>,
|
||||
}
|
||||
|
||||
fn recompute_canvas(modal: &mut Modal, action: ActionType, top_text: Option<&str>, bot_text: Option<&str>, style: GlyphStyle) {
|
||||
fn recompute_canvas(modal: &mut Modal, top_text: Option<&str>, bot_text: Option<&str>, style: GlyphStyle) {
|
||||
// we need to set a "max" size to our modal box, so that the text computations don't fail later on
|
||||
let current_bounds = modal.gam.get_canvas_bounds(modal.canvas).expect("couldn't get current bounds");
|
||||
|
||||
|
|
@ -214,7 +216,7 @@ fn recompute_canvas(modal: &mut Modal, action: ActionType, top_text: Option<&str
|
|||
|
||||
// compute height of action item
|
||||
log::trace!("step 1 total_height: {}", total_height);
|
||||
total_height += action.height(modal.line_height, modal.margin);
|
||||
total_height += modal.action.height(modal.line_height, modal.margin);
|
||||
total_height += modal.margin;
|
||||
|
||||
// compute height of bot_text, if any
|
||||
|
|
@ -323,7 +325,7 @@ impl<'a> Modal<'a> {
|
|||
top_memoized_height: None,
|
||||
bot_memoized_height: None,
|
||||
};
|
||||
recompute_canvas(&mut modal, action, top_text, bot_text, style);
|
||||
recompute_canvas(&mut modal, top_text, bot_text, style);
|
||||
modal
|
||||
}
|
||||
pub fn activate(&self) {
|
||||
|
|
@ -447,11 +449,8 @@ impl<'a> Modal<'a> {
|
|||
update_top_text: Option<&str>, remove_top: bool,
|
||||
update_bot_text: Option<&str>, remove_bot: bool,
|
||||
update_style: Option<GlyphStyle>) {
|
||||
let action = if let Some(action) = update_action {
|
||||
if let Some(action) = update_action {
|
||||
self.action = action;
|
||||
action
|
||||
} else {
|
||||
self.action
|
||||
};
|
||||
|
||||
if remove_top {
|
||||
|
|
@ -504,7 +503,7 @@ impl<'a> Modal<'a> {
|
|||
} else {
|
||||
self.style
|
||||
};
|
||||
recompute_canvas(self, action, top_text, bot_text, style);
|
||||
recompute_canvas(self, top_text, bot_text, style);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ use xous_ipc::Buffer;
|
|||
use core::fmt::Write;
|
||||
use locales::t;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct CheckBoxes {
|
||||
pub items: [Option<ItemName>; MAX_ITEMS],
|
||||
pub items: Vec::<ItemName>,
|
||||
pub action_conn: xous::CID,
|
||||
pub action_opcode: u32,
|
||||
pub action_payload: CheckBoxPayload,
|
||||
|
|
@ -19,7 +19,7 @@ pub struct CheckBoxes {
|
|||
impl CheckBoxes {
|
||||
pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self {
|
||||
CheckBoxes {
|
||||
items: [None; MAX_ITEMS],
|
||||
items: Vec::new(),
|
||||
action_conn,
|
||||
action_opcode,
|
||||
action_payload: CheckBoxPayload::new(),
|
||||
|
|
@ -27,26 +27,18 @@ impl CheckBoxes {
|
|||
select_index: 0,
|
||||
}
|
||||
}
|
||||
pub fn add_item(&mut self, new_item: ItemName) -> Option<ItemName> {
|
||||
for item in self.items.iter_mut() {
|
||||
if item.is_none() {
|
||||
self.max_items += 1;
|
||||
*item = Some(new_item);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
return Some(new_item);
|
||||
pub fn add_item(&mut self, new_item: ItemName) {
|
||||
self.items.push(new_item);
|
||||
}
|
||||
pub fn clear_items(&mut self) {
|
||||
self.items.clear();
|
||||
}
|
||||
}
|
||||
impl ActionApi for CheckBoxes {
|
||||
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
|
||||
fn height(&self, glyph_height: i16, margin: i16) -> i16 {
|
||||
let mut total_items = 0;
|
||||
// total items, then +1 for the "Okay" message
|
||||
for item in self.items.iter() {
|
||||
if item.is_some(){ total_items += 1}
|
||||
}
|
||||
(total_items + 1) * glyph_height + margin * 2 + 5 // some slop needed because of the prompt character
|
||||
(self.items.len() as i16 + 1) * glyph_height + margin * 2 + 5 // some slop needed because of the prompt character
|
||||
}
|
||||
fn redraw(&self, at_height: i16, modal: &Modal) {
|
||||
// prime a textview with the correct general style parameters
|
||||
|
|
@ -69,41 +61,39 @@ impl ActionApi for CheckBoxes {
|
|||
|
||||
let mut cur_line = 0;
|
||||
let mut do_okay = true;
|
||||
for maybe_item in self.items.iter() {
|
||||
if let Some(item) = maybe_item {
|
||||
let cur_y = at_height + cur_line * modal.line_height;
|
||||
if cur_line == self.select_index {
|
||||
// draw the cursor
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36)
|
||||
));
|
||||
write!(tv, "»").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
do_okay = false;
|
||||
}
|
||||
if self.action_payload.contains(item.as_str()) {
|
||||
// draw the check mark
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(select_x, cur_y - emoji_slop), Point::new(select_x + 36, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "\u{d7}").unwrap(); // multiplication sign
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
}
|
||||
// draw the text
|
||||
for item in self.items.iter() {
|
||||
let cur_y = at_height + cur_line * modal.line_height;
|
||||
if cur_line == self.select_index {
|
||||
// draw the cursor
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height)
|
||||
Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36)
|
||||
));
|
||||
write!(tv, "{}", item.as_str()).unwrap();
|
||||
write!(tv, "»").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
|
||||
cur_line += 1;
|
||||
do_okay = false;
|
||||
}
|
||||
if self.action_payload.contains(item.as_str()) {
|
||||
// draw the check mark
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(select_x, cur_y - emoji_slop), Point::new(select_x + 36, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "\u{d7}").unwrap(); // multiplication sign
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
}
|
||||
// draw the text
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "{}", item.as_str()).unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
|
||||
cur_line += 1;
|
||||
}
|
||||
cur_line += 1;
|
||||
let cur_y = at_height + cur_line * modal.line_height;
|
||||
|
|
@ -149,22 +139,12 @@ impl ActionApi for CheckBoxes {
|
|||
}
|
||||
}
|
||||
'∴' | '\u{d}' => {
|
||||
if self.select_index < self.max_items {
|
||||
// iterate through to find the index -- because if we support a remove() API later,
|
||||
// the list can have "holes", such that the index != index in the array
|
||||
let mut cur_index = 0;
|
||||
for maybe_item in self.items.iter() {
|
||||
if let Some(item) = maybe_item {
|
||||
if cur_index == self.select_index {
|
||||
if self.action_payload.contains(item.as_str()) {
|
||||
self.action_payload.remove(item.as_str());
|
||||
} else {
|
||||
self.action_payload.add(item.as_str());
|
||||
}
|
||||
break;
|
||||
}
|
||||
cur_index += 1;
|
||||
}
|
||||
if (self.select_index as usize) < self.items.len() {
|
||||
let item_name = self.items[self.select_index as usize].as_str();
|
||||
if self.action_payload.contains(item_name) {
|
||||
self.action_payload.remove(item_name);
|
||||
} else {
|
||||
self.action_payload.add(item_name);
|
||||
}
|
||||
} else { // the OK button select
|
||||
let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload");
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ use xous_ipc::Buffer;
|
|||
use core::fmt::Write;
|
||||
use locales::t;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct RadioButtons {
|
||||
pub items: [Option<ItemName>; MAX_ITEMS],
|
||||
pub items: Vec::<ItemName>,
|
||||
pub action_conn: xous::CID,
|
||||
pub action_opcode: u32,
|
||||
pub action_payload: RadioButtonPayload, // the current "radio button" selection
|
||||
|
|
@ -20,7 +20,7 @@ pub struct RadioButtons {
|
|||
impl RadioButtons {
|
||||
pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self {
|
||||
RadioButtons {
|
||||
items: [None; MAX_ITEMS],
|
||||
items: Vec::new(),
|
||||
action_conn,
|
||||
action_opcode,
|
||||
action_payload: RadioButtonPayload::new(""),
|
||||
|
|
@ -29,30 +29,23 @@ impl RadioButtons {
|
|||
is_password: false,
|
||||
}
|
||||
}
|
||||
pub fn add_item(&mut self, new_item: ItemName) -> Option<ItemName> {
|
||||
pub fn add_item(&mut self, new_item: ItemName) {
|
||||
if self.action_payload.as_str().len() == 0 {
|
||||
// default to the first item added
|
||||
self.action_payload = RadioButtonPayload::new(new_item.as_str());
|
||||
}
|
||||
for item in self.items.iter_mut() {
|
||||
if item.is_none() {
|
||||
self.max_items += 1;
|
||||
*item = Some(new_item);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
return Some(new_item);
|
||||
self.items.push(new_item);
|
||||
}
|
||||
pub fn clear_items(&mut self) {
|
||||
self.items.clear();
|
||||
self.action_payload.clear();
|
||||
}
|
||||
}
|
||||
impl ActionApi for RadioButtons {
|
||||
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
|
||||
fn height(&self, glyph_height: i16, margin: i16) -> i16 {
|
||||
let mut total_items = 0;
|
||||
// total items, then +1 for the "Okay" message
|
||||
for item in self.items.iter() {
|
||||
if item.is_some(){ total_items += 1}
|
||||
}
|
||||
(total_items + 1) * glyph_height + margin * 2 + margin * 2 + 5 // +4 for some bottom margin slop
|
||||
(self.items.len() as i16 + 1) * glyph_height + margin * 2 + margin * 2 + 5 // +4 for some bottom margin slop
|
||||
}
|
||||
fn redraw(&self, at_height: i16, modal: &Modal) {
|
||||
let color = if self.is_password {
|
||||
|
|
@ -83,41 +76,39 @@ impl ActionApi for RadioButtons {
|
|||
|
||||
let mut cur_line = 0;
|
||||
let mut do_okay = true;
|
||||
for maybe_item in self.items.iter() {
|
||||
if let Some(item) = maybe_item {
|
||||
let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2;
|
||||
if cur_line == self.select_index {
|
||||
// draw the cursor
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36)
|
||||
));
|
||||
write!(tv, "»").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
do_okay = false;
|
||||
}
|
||||
if item.as_str() == self.action_payload.as_str() {
|
||||
// draw the radio dot
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(select_x, cur_y), Point::new(select_x + 36, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "•").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
}
|
||||
// draw the text
|
||||
for item in self.items.iter() {
|
||||
let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2;
|
||||
if cur_line == self.select_index {
|
||||
// draw the cursor
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height)
|
||||
Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36)
|
||||
));
|
||||
write!(tv, "{}", item.as_str()).unwrap();
|
||||
write!(tv, "»").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
|
||||
cur_line += 1;
|
||||
do_okay = false;
|
||||
}
|
||||
if item.as_str() == self.action_payload.as_str() {
|
||||
// draw the radio dot
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(select_x, cur_y), Point::new(select_x + 36, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "•").unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
}
|
||||
// draw the text
|
||||
tv.text.clear();
|
||||
tv.bounds_computed = None;
|
||||
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height)
|
||||
));
|
||||
write!(tv, "{}", item.as_str()).unwrap();
|
||||
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
|
||||
|
||||
cur_line += 1;
|
||||
}
|
||||
cur_line += 1;
|
||||
let cur_y = at_height + cur_line * modal.line_height + modal.margin * 2;
|
||||
|
|
@ -164,18 +155,7 @@ impl ActionApi for RadioButtons {
|
|||
}
|
||||
'∴' | '\u{d}' => {
|
||||
if self.select_index < self.max_items {
|
||||
// iterate through to find the index -- because if we support a remove() API later,
|
||||
// the list can have "holes", such that the index != index in the array
|
||||
let mut cur_index = 0;
|
||||
for maybe_item in self.items.iter() {
|
||||
if let Some(item) = maybe_item {
|
||||
if cur_index == self.select_index {
|
||||
self.action_payload = RadioButtonPayload::new(item.as_str());
|
||||
break;
|
||||
}
|
||||
cur_index += 1;
|
||||
}
|
||||
}
|
||||
self.action_payload = RadioButtonPayload::new(self.items[self.select_index as usize].as_str());
|
||||
} else { // the OK button select
|
||||
let buf = Buffer::into_buf(self.action_payload).expect("couldn't convert message to payload");
|
||||
buf.send(self.action_conn, self.action_opcode).map(|_| ()).expect("couldn't send action message");
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ pub(crate) enum ValidationOp {
|
|||
pub struct ManagedPromptWithFixedResponse {
|
||||
pub prompt: xous_ipc::String::<1024>,
|
||||
pub items: [Option<ItemName>; MAX_ITEMS],
|
||||
pub single_item: bool,
|
||||
}
|
||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||
pub struct ManagedPromptWithTextResponse {
|
||||
|
|
|
|||
|
|
@ -147,6 +147,10 @@ fn xmain() -> ! {
|
|||
);
|
||||
renderer_modal.activate();
|
||||
},
|
||||
RendererState::RunFixed(config) => {
|
||||
// going back to modify the GAM to use a smarter heap allocated struct for radio list
|
||||
//radiobox.add_item();
|
||||
},
|
||||
RendererState::None => {
|
||||
log::error!("Operation initiated with no argument specified. Ignoring request.");
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -602,17 +602,6 @@ fn xmain() -> ! {
|
|||
action_payload: TextEntryPayload::new(),
|
||||
validator: Some(rtc_ux_validator),
|
||||
};
|
||||
let mut rtc_radiobox = gam::modal::RadioButtons::new(
|
||||
CB_TO_MAIN_CONN.load(Ordering::Relaxed).to_u32().unwrap(),
|
||||
Opcode::UxDayOfWeek.to_u32().unwrap()
|
||||
);
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.monday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.tuesday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.wednesday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.thursday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.friday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.saturday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.sunday", xous::LANG)));
|
||||
log::trace!("building rtc ux modal");
|
||||
let mut modal = gam::Modal::new(
|
||||
crate::api::RTC_MODAL_NAME,
|
||||
|
|
@ -784,6 +773,17 @@ fn xmain() -> ! {
|
|||
years = input as u8;
|
||||
|
||||
rtc_textentry.action_opcode = Opcode::UxDayOfWeek.to_u32().unwrap();
|
||||
let mut rtc_radiobox = gam::modal::RadioButtons::new(
|
||||
CB_TO_MAIN_CONN.load(Ordering::Relaxed).to_u32().unwrap(),
|
||||
Opcode::UxDayOfWeek.to_u32().unwrap()
|
||||
);
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.monday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.tuesday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.wednesday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.thursday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.friday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.saturday", xous::LANG)));
|
||||
rtc_radiobox.add_item(ItemName::new(t!("rtc.sunday", xous::LANG)));
|
||||
modal.modify(Some(RadioButtons(rtc_radiobox)),
|
||||
Some(t!("rtc.day_of_week", xous::LANG)), false, None, true, None);
|
||||
modal.activate();
|
||||
|
|
|
|||
Loading…
Reference in a new issue