clean up menu to use std primitives

This commit is contained in:
bunnie 2022-01-16 02:04:58 +08:00
parent dd4042f315
commit ff2d68b392
2 changed files with 67 additions and 97 deletions

View file

@ -11,8 +11,6 @@ use num_traits::*;
use graphics_server::api::{PixelColor, TextBounds, DrawStyle};
const MAX_ITEMS: usize = 16;
#[allow(dead_code)] // here until Memory types are implemented
#[derive(Debug, Copy, Clone)]
pub enum MenuPayload {
@ -36,7 +34,7 @@ pub struct Menu<'a> {
pub sid: xous::SID,
pub gam: Gam,
pub xns: xous_names::XousNames,
pub items: [Option<MenuItem>; MAX_ITEMS],
pub items: Vec<MenuItem>,
pub index: usize, // currently selected item
pub canvas: Gid,
pub authtoken: [u32; 4],
@ -45,7 +43,7 @@ pub struct Menu<'a> {
pub line_height: i16,
pub canvas_width: Option<i16>,
pub helper_data: Option<Buffer<'a>>,
pub name: String::<128>,
pub name: std::string::String,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
@ -80,7 +78,7 @@ impl<'a> Menu<'a> {
sid,
gam,
xns,
items: [None; MAX_ITEMS],
items: Vec::new(),
index: 0,
canvas,
authtoken: authtoken.unwrap(),
@ -89,11 +87,11 @@ impl<'a> Menu<'a> {
line_height,
canvas_width: None,
helper_data: None,
name: String::<128>::from_str(name),
name: std::string::String::from(name),
}
}
pub fn activate(&self) {
self.gam.raise_menu(self.name.to_str()).expect("couldn't activate menu");
self.gam.raise_menu(self.name.as_str()).expect("couldn't raise menu");
}
/// this function spawns a client-side thread to forward redraw and key event
@ -115,24 +113,16 @@ impl<'a> Menu<'a> {
}
// if successful, returns None, otherwise, the menu item
pub fn add_item(&mut self, new_item: MenuItem) -> Option<MenuItem> {
pub fn add_item(&mut self, new_item: MenuItem) {
if new_item.name.as_str().unwrap() == "🔇" { // suppress the addition of menu items that are not applicable for a given locale
return None;
return;
}
// first, do the insertion.
// add the menu item to the first free slot
// any modifications to the menu structure should guarantee that the list is compacted
// and has no holes, in order for the "selected index" logic to work
let mut success = false;
for item in self.items.iter_mut() {
if item.is_none() {
*item = Some(new_item);
success = true;
break;
}
}
self.items.push(new_item);
if success {
// now, recompute the height
let mut total_items = self.num_items();
if total_items == 0 {
@ -147,17 +137,12 @@ impl<'a> Menu<'a> {
};
log::debug!("add_item requesting bounds of {:?}", new_bounds);
self.gam.set_canvas_bounds_request(&mut new_bounds).expect("couldn't call set bounds");
None
} else {
Some(new_item)
}
}
pub fn draw_item(&self, index: i16, with_marker: bool) {
use core::fmt::Write;
let canvas_size = self.gam.get_canvas_bounds(self.canvas).unwrap();
if let Some(item) = self.items[index as usize] {
let item = self.items[index as usize];
let mut item_tv = TextView::new(
self.canvas,
TextBounds::BoundingBox(Rectangle::new(
@ -178,7 +163,6 @@ impl<'a> Menu<'a> {
self.gam.post_textview(&mut item_tv).expect("couldn't render menu list item");
}
}
// draw a dividing line above the indexed item
pub fn draw_divider(&self, index: i16) {
if let Some(canvas_width) = self.canvas_width {
@ -243,42 +227,27 @@ impl<'a> Menu<'a> {
// draw the line items
// we require that the items list be in index-order, with no holes: we abort at the first None item
let mut cur_index: i16 = 0;
for maybe_item in self.items.iter() {
if let Some(_item) = maybe_item {
for cur_index in 0..self.items.len() {
if self.index == cur_index as usize {
self.draw_item(cur_index as i16, true);
} else {
self.draw_item(cur_index as i16, false);
}
if cur_index != 0 {
self.draw_divider(cur_index);
}
cur_index += 1;
} else {
break;
self.draw_divider(cur_index as _);
}
}
self.gam.redraw().unwrap();
}
fn num_items(&self) -> usize {
let mut items = 0;
for maybe_item in self.items.iter() {
if maybe_item.is_some() {
items += 1;
} else {
break;
}
}
items
self.items.len()
}
pub fn key_event(&mut self, keys: [char; 4]) {
for &k in keys.iter() {
log::debug!("got key '{}'", k);
match k {
'∴' => {
if let Some(mi) = self.items[self.index] {
let mi = self.items[self.index];
// give up focus before issuing the command, as some commands conflict with loss of focus...
if mi.close_on_select {
self.gam.relinquish_focus().unwrap();
@ -296,7 +265,6 @@ impl<'a> Menu<'a> {
}
}
self.index = 0; // reset the index to 0
}
self.gam.redraw().unwrap();
},
'←' => {

View file

@ -10,7 +10,7 @@
*/
// if you add more UxContexts, and you want them authorized by the GAM, add their names here.
const TOKEN_SLOTS: usize = 8;
const TOKEN_SLOTS: usize = 9;
const EXPECTED_BOOT_CONTEXTS: [&'static str; TOKEN_SLOTS] = [
"shellchat",
"main menu",
@ -20,6 +20,8 @@ const EXPECTED_BOOT_CONTEXTS: [&'static str; TOKEN_SLOTS] = [
"rootkeys menu",
"pddb modal",
"shared modal",
"pddb menu",
//"user app menu",
];
#[derive(Clone, Debug)]