mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
clean up menu to use std primitives
This commit is contained in:
parent
dd4042f315
commit
ff2d68b392
2 changed files with 67 additions and 97 deletions
|
|
@ -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,69 +113,55 @@ 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 {
|
||||
total_items = 1; // just so we see a blank menu at least, and have a clue how to debug
|
||||
}
|
||||
let current_bounds = self.gam.get_canvas_bounds(self.canvas).expect("couldn't get current bounds");
|
||||
let mut new_bounds = SetCanvasBoundsRequest {
|
||||
requested: Point::new(current_bounds.x, total_items as i16 * self.line_height + self.margin * 2),
|
||||
granted: None,
|
||||
token_type: TokenType::App,
|
||||
token: self.authtoken,
|
||||
};
|
||||
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)
|
||||
// now, recompute the height
|
||||
let mut total_items = self.num_items();
|
||||
if total_items == 0 {
|
||||
total_items = 1; // just so we see a blank menu at least, and have a clue how to debug
|
||||
}
|
||||
let current_bounds = self.gam.get_canvas_bounds(self.canvas).expect("couldn't get current bounds");
|
||||
let mut new_bounds = SetCanvasBoundsRequest {
|
||||
requested: Point::new(current_bounds.x, total_items as i16 * self.line_height + self.margin * 2),
|
||||
granted: None,
|
||||
token_type: TokenType::App,
|
||||
token: self.authtoken,
|
||||
};
|
||||
log::debug!("add_item requesting bounds of {:?}", new_bounds);
|
||||
self.gam.set_canvas_bounds_request(&mut new_bounds).expect("couldn't call set bounds");
|
||||
}
|
||||
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 mut item_tv = TextView::new(
|
||||
self.canvas,
|
||||
TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(self.margin, index * self.line_height + self.margin),
|
||||
Point::new(canvas_size.x - self.margin, (index + 1) * self.line_height + self.margin),
|
||||
)));
|
||||
let item = self.items[index as usize];
|
||||
let mut item_tv = TextView::new(
|
||||
self.canvas,
|
||||
TextBounds::BoundingBox(Rectangle::new(
|
||||
Point::new(self.margin, index * self.line_height + self.margin),
|
||||
Point::new(canvas_size.x - self.margin, (index + 1) * self.line_height + self.margin),
|
||||
)));
|
||||
|
||||
if with_marker {
|
||||
write!(item_tv.text, " • ").unwrap();
|
||||
} else {
|
||||
write!(item_tv.text, " ").unwrap();
|
||||
}
|
||||
write!(item_tv.text, "{}", item.name.as_str().unwrap()).unwrap();
|
||||
item_tv.draw_border = false;
|
||||
item_tv.style = GlyphStyle::Small;
|
||||
item_tv.margin = Point::new(0, 0);
|
||||
item_tv.ellipsis = true;
|
||||
|
||||
self.gam.post_textview(&mut item_tv).expect("couldn't render menu list item");
|
||||
if with_marker {
|
||||
write!(item_tv.text, " • ").unwrap();
|
||||
} else {
|
||||
write!(item_tv.text, " ").unwrap();
|
||||
}
|
||||
write!(item_tv.text, "{}", item.name.as_str().unwrap()).unwrap();
|
||||
item_tv.draw_border = false;
|
||||
item_tv.style = GlyphStyle::Small;
|
||||
item_tv.margin = Point::new(0, 0);
|
||||
item_tv.ellipsis = true;
|
||||
|
||||
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) {
|
||||
|
|
@ -243,60 +227,44 @@ 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 {
|
||||
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;
|
||||
for cur_index in 0..self.items.len() {
|
||||
if self.index == cur_index as usize {
|
||||
self.draw_item(cur_index as i16, true);
|
||||
} else {
|
||||
break;
|
||||
self.draw_item(cur_index as i16, false);
|
||||
}
|
||||
if cur_index != 0 {
|
||||
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] {
|
||||
// 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();
|
||||
}
|
||||
log::debug!("doing menu action for {}", mi.name);
|
||||
match mi.action_payload {
|
||||
MenuPayload::Scalar(args) => {
|
||||
xous::send_message(mi.action_conn,
|
||||
xous::Message::new_scalar(mi.action_opcode as usize,
|
||||
args[0] as usize, args[1] as usize, args[2] as usize, args[3] as usize)
|
||||
).expect("couldn't send menu action");
|
||||
},
|
||||
MenuPayload::Memory((_buf, _len)) => {
|
||||
unimplemented!("menu buffer targets are a future feature");
|
||||
}
|
||||
}
|
||||
self.index = 0; // reset the index to 0
|
||||
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();
|
||||
}
|
||||
log::debug!("doing menu action for {}", mi.name);
|
||||
match mi.action_payload {
|
||||
MenuPayload::Scalar(args) => {
|
||||
xous::send_message(mi.action_conn,
|
||||
xous::Message::new_scalar(mi.action_opcode as usize,
|
||||
args[0] as usize, args[1] as usize, args[2] as usize, args[3] as usize)
|
||||
).expect("couldn't send menu action");
|
||||
},
|
||||
MenuPayload::Memory((_buf, _len)) => {
|
||||
unimplemented!("menu buffer targets are a future feature");
|
||||
}
|
||||
}
|
||||
self.index = 0; // reset the index to 0
|
||||
self.gam.redraw().unwrap();
|
||||
},
|
||||
'←' => {
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue