initial slider implementation

This commit is contained in:
bunnie 2025-04-28 19:01:31 +08:00
parent 74b427e1c4
commit 8be197a2b5

View file

@ -1,4 +1,9 @@
use std::fmt::Write;
use super::*;
use crate::minigfx::*;
use crate::service::api::Gid;
use crate::service::gfx::Gfx;
// This structure needs to be "shallow copy capable" so we can use it with
// the enum_actions API to update the progress state in an efficient manner.
@ -13,7 +18,6 @@ pub struct Slider {
pub action_opcode: u32,
pub action_payload: u32,
pub is_progressbar: bool,
pub is_password: bool,
pub show_legend: bool,
pub units: [u8; 8],
pub units_len: usize,
@ -47,7 +51,6 @@ impl Slider {
Slider {
action_conn,
action_opcode,
is_password: false,
is_progressbar,
min,
max,
@ -61,13 +64,6 @@ impl Slider {
pub fn set_is_progressbar(&mut self, setting: bool) { self.is_progressbar = setting; }
pub fn set_is_password(&mut self, setting: bool) {
// this will cause text to be inverted. Untrusted entities can try to set this,
// but the GAM should defeat this for dialog boxes outside of the trusted boot
// set because they can't achieve a high enough trust level.
self.is_password = setting;
}
pub fn set_state(&mut self, state: u32) {
if state < self.min {
self.action_payload = self.min;
@ -79,5 +75,147 @@ impl Slider {
}
}
use crate::widgets::ActionApi;
impl ActionApi for Slider {}
use crate::{minigfx::PixelColor, widgets::ActionApi};
impl ActionApi for Slider {
fn height(&self, glyph_height: isize, margin: isize, _modal: &Modal) -> isize {
/*
margin
min max <- glyph height
-----O---------- <- glyph height
legend
margin
*/
if self.show_legend { glyph_height * 3 + margin * 2 } else { glyph_height * 2 + margin * 2 }
}
fn set_action_opcode(&mut self, op: u32) { self.action_opcode = op }
fn redraw(&self, at_height: isize, modal: &Modal) {
let color = PixelColor::Light;
let fill_color = PixelColor::Dark;
// prime a textview with the correct general style parameters
let mut tv = TextView::new(Gid::dummy(), TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1)));
tv.ellipsis = true;
tv.style = modal.style;
tv.invert = true;
tv.draw_border = false;
tv.margin = Point::new(0, 0);
tv.insertion = None;
let maxwidth = (modal.canvas_width - modal.margin * 2) as u16;
if self.show_legend {
log::info!(
"{}{}",
self.action_payload,
String::from_utf8(self.units[..self.units_len].to_vec()).unwrap_or("UTF8-Err".to_string())
);
// estimate width of current setting
tv.bounds_computed = None;
tv.bounds_hint = TextBounds::GrowableFromTl(Point::new(0, 0), maxwidth);
write!(
tv,
"{}{}",
self.action_payload,
String::from_utf8(self.units[..self.units_len].to_vec()).unwrap_or("UTF8-Err".to_string())
)
.unwrap();
modal.gfx.bounds_compute_textview(&mut tv).expect("couldn't simulate text size");
let textwidth = if let Some(bounds) = tv.bounds_computed {
bounds.br.x - bounds.tl.x
} else {
maxwidth as isize
};
let offset = (modal.canvas_width - textwidth) / 2;
// render current setting
tv.bounds_computed = None;
tv.bounds_hint = TextBounds::GrowableFromTl(
Point::new(offset, at_height + modal.margin + modal.line_height * 2 + modal.margin),
maxwidth,
);
modal.gfx.draw_textview(&mut tv).expect("couldn't draw legend");
}
// the actual slider
let mut draw_list = ObjectList::new();
let outer_rect = Rectangle::new_with_style(
Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height),
Point::new(
modal.canvas_width - modal.margin * 2,
modal.margin + modal.line_height * 2 + at_height,
),
DrawStyle::new(fill_color, color, 2),
);
draw_list.push(ClipObjectType::Rect(outer_rect)).unwrap();
let total_width = modal.canvas_width - modal.margin * 4;
let slider_point =
(total_width * (self.action_payload - self.min) as isize) / (self.max - self.min) as isize;
let inner_rect = Rectangle::new_with_style(
Point::new(modal.margin * 2, modal.margin + modal.line_height + at_height),
Point::new(modal.margin * 2 + slider_point, modal.margin + modal.line_height * 2 + at_height),
DrawStyle::new(color, color, 1),
);
draw_list.push(ClipObjectType::Rect(inner_rect)).unwrap();
modal.gfx.draw_object_list(draw_list).expect("couldn't execute draw list");
}
fn key_action(&mut self, k: char) -> Option<ValidatorErr> {
log::trace!("key_action: {}", k);
if !self.is_progressbar {
match k {
'←' => {
if self.action_payload >= self.min + self.step {
self.action_payload -= self.step;
} else if self.action_payload >= self.min && self.action_payload < self.min + self.step {
self.action_payload = self.min
}
}
'→' => {
if self.action_payload <= self.max - self.step {
self.action_payload += self.step;
} else if self.action_payload < self.max && self.action_payload > self.max - self.step {
self.action_payload = self.max
}
}
'\u{0}' => {
// ignore null messages
}
'∴' | '\u{d}' => {
// relinquish focus before returning the result
let xns = xous_names::XousNames::new().unwrap();
let gfx = Gfx::new(&xns).unwrap();
gfx.release_modal().ok();
xous::yield_slice();
let ret_payload = SliderPayload(self.action_payload);
let buf =
xous_ipc::Buffer::into_buf(ret_payload).expect("couldn't convert message to payload");
buf.send(self.action_conn, self.action_opcode)
.map(|_| ())
.expect("couldn't send action message");
return None;
}
_ => {
// ignore all other messages
}
}
None
} else {
if k == '🛑' {
// use the "stop" emoji as a signal that we should close the progress bar
// relinquish focus on stop
let xns = xous_names::XousNames::new().unwrap();
let gfx = Gfx::new(&xns).unwrap();
log::info!("bef release modal");
gfx.release_modal().ok();
xous::yield_slice();
log::info!("release modal");
None
} else {
None
}
}
}
}