add notification api

This commit is contained in:
bunnie 2021-12-16 02:28:37 +08:00
parent dcad15284d
commit 4a0f062558
3 changed files with 53 additions and 6 deletions

View file

@ -76,6 +76,16 @@ impl Modals {
_ => Err(xous::Error::InternalError)
}
}
/// this blocks until the notification has been acknowledged.
pub fn show_notification(&self, notification: &str) -> Result<(), xous::Error> {
let spec = ManagedNotification {
message: xous_ipc::String::from_str(notification),
};
let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
Ok(())
}
}
use core::sync::atomic::{AtomicU32, Ordering};

View file

@ -80,7 +80,7 @@ fn xmain() -> ! {
renderer_cid,
RendererOp::RadioReturn.to_u32().unwrap()
);
let mut notification = gam::modal::Notification::new(
let notification = gam::modal::Notification::new(
renderer_cid,
RendererOp::NotificationReturn.to_u32().unwrap()
);
@ -116,11 +116,19 @@ fn xmain() -> ! {
renderer_modal.modify(
Some(ActionType::TextEntry(text_action)),
Some(config.prompt.as_str().unwrap()), false,
None, false, None
None, true, None
);
renderer_modal.activate();
log::info!("should be active!");
},
RendererState::RunNotification(config) => {
renderer_modal.modify(
Some(ActionType::Notification(notification)),
Some(config.message.as_str().unwrap()), false,
None, true, None
);
renderer_modal.activate();
},
RendererState::None => {
log::error!("Operation initiated with no argument specified. Ignoring request.");
continue;
@ -170,6 +178,16 @@ fn xmain() -> ! {
}
}
}
Some(RendererOp::NotificationReturn) => {
let mut mutex_op = op.lock().unwrap();
match *mutex_op {
RendererState::RunNotification(_) => *mutex_op = RendererState::None,
_ => {
log::error!("UX return opcode does not match our current operation in flight. This is a serious internal error.");
panic!("UX return opcode does not match our current operation in flight. This is a serious internal error.");
}
}
},
Some(RendererOp::ModalRedraw) => {
renderer_modal.redraw();
},
@ -219,9 +237,7 @@ fn xmain() -> ! {
Some(Opcode::PromptWithTextResponse) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let spec = buffer.to_original::<ManagedPromptWithTextResponse, _>().unwrap();
{
*op.lock().unwrap() = RendererState::RunText(spec);
}
*op.lock().unwrap() = RendererState::RunText(spec);
send_message(
renderer_cid,
Message::new_scalar(RendererOp::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0)
@ -242,7 +258,24 @@ fn xmain() -> ! {
}
},
Some(Opcode::Notification) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let spec = buffer.to_original::<ManagedNotification, _>().unwrap();
*op.lock().unwrap() = RendererState::RunNotification(spec);
send_message(
renderer_cid,
Message::new_scalar(RendererOp::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't initiate UX op");
loop {
match *op.lock().unwrap() {
RendererState::RunNotification(_) => (),
RendererState::None => break,
_ => {
log::error!("Illegal state transition in renderer");
panic!("Illegal state transition in renderer");
}
}
tt.sleep_ms(100).unwrap(); // don't put the idle in the match/lock(), it'll prevent the other thread from running!
}
},
Some(Opcode::StartProgress) => {

View file

@ -21,6 +21,10 @@ pub(crate) fn spawn_test() {
}
}
log::info!("text input test done");
log::info!("testing notification");
modals.show_notification("This is a test!\n这是一个测验!").expect("notification failed");
log::info!("notification test done");
}
});
}