From 10044579aa84693dc0e26190668c8a952271549d Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 28 Apr 2025 02:27:41 +0800 Subject: [PATCH] initial take at radiobuttons --- libs/ux-api/src/widgets/radiobuttons.rs | 50 +++++++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/libs/ux-api/src/widgets/radiobuttons.rs b/libs/ux-api/src/widgets/radiobuttons.rs index f25c594e1..9e69dd944 100644 --- a/libs/ux-api/src/widgets/radiobuttons.rs +++ b/libs/ux-api/src/widgets/radiobuttons.rs @@ -2,22 +2,18 @@ use crate::widgets::*; #[derive(Debug)] pub struct RadioButtons { - pub items: Vec, + pub items: ScrollableList, pub action_conn: xous::CID, pub action_opcode: u32, pub action_payload: RadioButtonPayload, // the current "radio button" selection - pub select_index: i16, // the current candidate to be selected - pub is_password: bool, } impl RadioButtons { pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self { RadioButtons { - items: Vec::new(), + items: ScrollableList::default(), action_conn, action_opcode, action_payload: RadioButtonPayload::new(""), - select_index: 0, - is_password: false, } } @@ -26,7 +22,7 @@ impl RadioButtons { // default to the first item added self.action_payload = RadioButtonPayload::new(new_item.as_str()); } - self.items.push(new_item); + self.items.add_item(0, new_item.as_str()); } pub fn clear_items(&mut self) { @@ -36,4 +32,42 @@ impl RadioButtons { } use crate::widgets::ActionApi; -impl ActionApi for RadioButtons {} +impl ActionApi for RadioButtons { + fn height(&self, glyph_height: isize, margin: isize, _modal: &Modal) -> isize { + // total items, then +1 for the "Okay" message + (self.items.len() as isize + 1) * glyph_height + margin * 2 + margin * 2 + 5 // +4 for some bottom margin slop + } + + fn redraw(&self, _at_height: isize, _modal: &Modal) { self.items.draw(); } + + fn set_action_opcode(&mut self, op: u32) { self.action_opcode = op } + + fn key_action(&mut self, k: char) -> Option { + log::trace!("key_action: {}", k); + match k { + '←' | '→' => { + // ignore these navigation keys, we have only one column + } + '↑' | '↓' => { + self.items.key_action(k); + } + '∴' | '\u{d}' => { + self.action_payload = RadioButtonPayload::new(self.items.get_selected()); + xous::yield_slice(); + let buf = xous_ipc::Buffer::into_buf(self.action_payload.clone()) + .expect("couldn't convert message to payload"); + buf.send(self.action_conn, self.action_opcode) + .map(|_| ()) + .expect("couldn't send action message"); + return None; + } + '\u{0}' => { + // ignore null messages + } + _ => { + // ignore text entry + } + } + None + } +}