fix index selection in menus

This commit is contained in:
bunnie 2025-11-12 16:26:35 +08:00
parent c650b0fb94
commit 0d1105fb25
2 changed files with 6 additions and 5 deletions

View file

@ -67,7 +67,6 @@ pub struct Menu<'a> {
pub gfx: Gfx, pub gfx: Gfx,
pub sid: xous::SID, pub sid: xous::SID,
pub items: Vec<MenuItem>, pub items: Vec<MenuItem>,
pub index: usize, // currently selected item
pub helper_data: Option<Buffer<'a>>, pub helper_data: Option<Buffer<'a>>,
pub name: String, pub name: String,
pub list: ScrollableList, // UI rendering element pub list: ScrollableList, // UI rendering element
@ -106,7 +105,6 @@ impl<'a> Menu<'a> {
gfx: Gfx::new(&xns).unwrap(), gfx: Gfx::new(&xns).unwrap(),
sid, sid,
items: Vec::new(), items: Vec::new(),
index: 0,
helper_data: None, helper_data: None,
name: String::from(name), name: String::from(name),
list, list,
@ -116,7 +114,7 @@ impl<'a> Menu<'a> {
} }
} }
pub fn set_index(&mut self, index: usize) { self.index = index; } pub fn set_index(&mut self, index: usize) { self.list.set_selected(0, index).unwrap() }
/// this function spawns a client-side thread to forward redraw and key event /// this function spawns a client-side thread to forward redraw and key event
/// messages on to a local server. The goal is to keep the local server's SID /// messages on to a local server. The goal is to keep the local server's SID
@ -219,7 +217,9 @@ impl<'a> Menu<'a> {
log::debug!("got key '{}'", k); log::debug!("got key '{}'", k);
match k { match k {
'∴' => { '∴' => {
let mi = &self.items[self.index]; let (_, selected) = self.list.get_selected_index();
log::info!("selected index {}", selected);
let mi = &self.items[selected];
if let Some(action) = mi.action_conn { if let Some(action) = mi.action_conn {
log::debug!("doing menu action for {}", mi.name); log::debug!("doing menu action for {}", mi.name);
match mi.action_payload { match mi.action_payload {
@ -241,7 +241,7 @@ impl<'a> Menu<'a> {
} }
} }
} }
self.index = 0; // reset the index to 0 self.list.set_selected(0, 0).ok(); // reset selection
if !mi.close_on_select { if !mi.close_on_select {
self.redraw(); self.redraw();
} else { } else {

View file

@ -36,6 +36,7 @@ pub enum TextAlignment {
pub struct ScrollableList { pub struct ScrollableList {
items: Vec<Vec<String>>, items: Vec<Vec<String>>,
/// corresponds to the index into `items` that should be rendered as selected /// corresponds to the index into `items` that should be rendered as selected
/// format is (col, row)
select_index: (usize, usize), select_index: (usize, usize),
/// offset in the list that represents the top left drawable item /// offset in the list that represents the top left drawable item
scroll_offset: (usize, usize), scroll_offset: (usize, usize),