add progress bar primitives

This commit is contained in:
bunnie 2021-12-16 21:39:49 +08:00
parent 4a0f062558
commit 60fc03351a
3 changed files with 109 additions and 9 deletions

View file

@ -59,7 +59,7 @@ impl Modals {
None
};
let spec = ManagedPromptWithTextResponse {
prompt: xous_ipc::String::<1024>::from_str(prompt),
prompt: xous_ipc::String::from_str(prompt),
validator,
validator_op: maybe_validator_op.unwrap_or(0)
};
@ -86,6 +86,33 @@ impl Modals {
buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
Ok(())
}
pub fn start_progress(&self, title: &str, start: u32, end: u32, current: u32) -> Result<(), xous::Error> {
let spec = ManagedProgress {
title: xous_ipc::String::from_str(title),
start_work: start,
end_work: end,
current_work: current,
};
let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::StartProgress.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
Ok(())
}
pub fn update_progress(&self, current: u32) -> Result<(), xous::Error> {
send_message(self.conn,
Message::new_scalar(Opcode::UpdateProgress.to_usize().unwrap(), current as usize, 0, 0, 0)
).expect("couldn't update progress");
Ok(())
}
/// close the progress bar, regardless of the current state
pub fn finish_progress(&self) -> Result<(), xous::Error> {
send_message(self.conn,
Message::new_scalar(Opcode::StopProgress.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't stop progress");
Ok(())
}
}
use core::sync::atomic::{AtomicU32, Ordering};

View file

@ -36,6 +36,8 @@ enum RendererState {
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum RendererOp {
InitiateOp,
UpdateProgress,
FinishProgress,
TextEntryReturn,
RadioReturn,
@ -85,8 +87,11 @@ fn xmain() -> ! {
RendererOp::NotificationReturn.to_u32().unwrap()
);
let mut progress_action = Slider::new(renderer_cid, RendererOp::NotificationReturn.to_u32().unwrap(),
0, 100, 10, Some("%"), 0, true, true
0, 100, 1, Some("%"), 0, true, true
);
let mut last_percentage = 0;
let mut start_work: u32 = 0;
let mut end_work: u32 = 100;
let mut renderer_modal =
Modal::new(
crate::api::SHARED_MODAL_NAME,
@ -96,7 +101,6 @@ fn xmain() -> ! {
GlyphStyle::Small,
8
);
let mut progressbar = ProgressBar::new(&mut renderer_modal, &mut progress_action);
renderer_modal.spawn_helper(renderer_sid, renderer_modal.sid,
RendererOp::ModalRedraw.to_u32().unwrap(),
RendererOp::ModalKeypress.to_u32().unwrap(),
@ -129,6 +133,20 @@ fn xmain() -> ! {
);
renderer_modal.activate();
},
RendererState::RunProgress(config) => {
start_work = config.start_work;
end_work = config.end_work;
last_percentage = compute_checked_percentage(
config.current_work, start_work, end_work);
progress_action.set_state(last_percentage);
renderer_modal.modify(
Some(ActionType::Slider(progress_action)),
Some(config.title.as_str().unwrap()), false,
None, true, None
);
renderer_modal.activate();
},
RendererState::None => {
log::error!("Operation initiated with no argument specified. Ignoring request.");
continue;
@ -139,6 +157,25 @@ fn xmain() -> ! {
}
}
},
Some(RendererOp::UpdateProgress) => msg_scalar_unpack!(msg, current, _, _, _, {
let new_percentage = compute_checked_percentage(
current as u32, start_work, end_work);
if new_percentage != last_percentage {
last_percentage = new_percentage;
progress_action.set_state(last_percentage);
renderer_modal.modify(
Some(ActionType::Slider(progress_action)),
None, false,
None, false, None
);
renderer_modal.redraw();
xous::yield_slice(); // give time for the GAM to redraw
}
}),
Some(RendererOp::FinishProgress) => {
renderer_modal.gam.relinquish_focus().unwrap();
},
Some(RendererOp::TextEntryReturn) => {
let mut mutex_op = op.lock().unwrap();
match *mutex_op {
@ -278,13 +315,25 @@ fn xmain() -> ! {
}
},
Some(Opcode::StartProgress) => {
},
Some(Opcode::UpdateProgress) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let spec = buffer.to_original::<ManagedProgress, _>().unwrap();
*op.lock().unwrap() = RendererState::RunProgress(spec);
send_message(
renderer_cid,
Message::new_scalar(RendererOp::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't initiate UX op");
},
Some(Opcode::UpdateProgress) => msg_scalar_unpack!(msg, current, _, _, _, {
send_message(
renderer_cid,
Message::new_scalar(RendererOp::UpdateProgress.to_usize().unwrap(), current, 0, 0, 0)
).expect("couldn't update progress bar");
}),
Some(Opcode::StopProgress) => {
send_message(
renderer_cid,
Message::new_scalar(RendererOp::FinishProgress.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't update progress bar");
},
Some(Opcode::Quit) => {
log::warn!("Shared modal UX handler exiting.");
@ -306,3 +355,17 @@ fn xmain() -> ! {
log::trace!("quitting");
xous::terminate_process(0)
}
fn compute_checked_percentage(current: u32, start: u32, end: u32) -> u32 {
if end <= start {
100
} else {
if current < start {
0
} else if current >= end {
100
} else {
(current * 100) / (end - start)
}
}
}

View file

@ -8,9 +8,18 @@ pub(crate) fn spawn_test() {
thread::spawn({
move || {
let xns = XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).unwrap();
let tt = ticktimer_server::Ticktimer::new().unwrap();
// test progress bar
modals.start_progress("Progress Quest", 100, 500, 100).expect("couldn't raise progress bar");
for i in (100..500).step_by(8) {
modals.update_progress(i).expect("couldn't update progress bar");
tt.sleep_ms(100).unwrap();
}
modals.finish_progress().expect("couldn't dismiss progress bar");
// test the modal dialog box function
let modals = modals::Modals::new(&xns).unwrap();
log::info!("test text input");
match modals.get_text_input("Test input", Some(test_validator), None) {
Ok(text) => {
@ -22,6 +31,7 @@ pub(crate) fn spawn_test() {
}
log::info!("text input test done");
// test notificatons
log::info!("testing notification");
modals.show_notification("This is a test!\n这是一个测验!").expect("notification failed");
log::info!("notification test done");