From 60fc03351ac78eab1e752c8c8d672b5b8226e77f Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 16 Dec 2021 21:39:49 +0800 Subject: [PATCH] add progress bar primitives --- services/modals/src/lib.rs | 29 +++++++++++++- services/modals/src/main.rs | 77 ++++++++++++++++++++++++++++++++---- services/modals/src/tests.rs | 12 +++++- 3 files changed, 109 insertions(+), 9 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index a78aa9e65..6998184e7 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -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}; diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 2ffadbc3c..64903b23d 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -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::().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) + } + } +} \ No newline at end of file diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 671ab77c6..e3246e813 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -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");