initial take at radiobuttons

This commit is contained in:
bunnie 2025-04-28 02:27:41 +08:00
parent 39b0fb9e12
commit 10044579aa

View file

@ -2,22 +2,18 @@ use crate::widgets::*;
#[derive(Debug)]
pub struct RadioButtons {
pub items: Vec<ItemName>,
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<ValidatorErr> {
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
}
}