Limit scope of Foreground/Background messages to just app changes

Don't send FocusState::Foreground / FocusState::Background messages
to Layouts that are non-App type. This allows this to be used more
reliably as a signal of if the background is the running app or not,
rather than if it is merely the drawable canvas.
This commit is contained in:
bunnie 2022-04-01 22:54:33 +08:00
parent fa10cfc040
commit d026102d41
5 changed files with 33 additions and 37 deletions

View file

@ -97,6 +97,7 @@ pub struct UxRegistration {
/// optional opcode ID for raw keystrokes. They are passed on to the caller in real-time.
pub rawkeys_id: Option<u32>,
/// optional opcode ID code for focus change notifications. Most applications will want to provide this to stop hogging resources when backgrounded
/// If the LayoutType is not an App, this field is ignored and does nothing
pub focuschange_id: Option<u32>,
}

View file

@ -366,10 +366,6 @@ impl ContextManager {
// let all the previous operations go out of scope, so we can "check out" the old copy and modify it
if self.focused_context.is_some() {
// immutable borrow here can't be combined with mutable borrow below
if let Some(old_context) = self.get_context_by_token(self.focused_context.unwrap()) {
self.notify_focus_change_to(gam::FocusState::Background, old_context).unwrap();
log::trace!("lowered focus to: {:?}", old_context);
}
if let Some(old_context) = self.get_context_by_token_mut(self.focused_context.unwrap()) {
old_context.layout.set_visibility_state(leaving_visibility, canvases);
}
@ -430,7 +426,6 @@ impl ContextManager {
// revert the keyboard vibe state
self.kbd.set_vibe(context.vibe).expect("couldn't restore keyboard vibe");
self.notify_focus_change_to(gam::FocusState::Foreground, context).unwrap();
log::trace!("raised focus to: {:?}", context);
let last_token = context.app_token;
self.last_context = self.focused_context;
@ -455,12 +450,26 @@ impl ContextManager {
Err(xous::Error::UseBeforeInit)
}
}
fn notify_focus_change_to(&self, new_state: gam::FocusState, context: &UxContext) -> Result<(), xous::Error> {
if let Some(focuschange_id) = context.focuschange_id {
log::trace!("focus change {:?} msg to {}, id {}", new_state, context.listener, context.redraw_id);
return xous::send_message(context.listener,
xous::Message::new_scalar(focuschange_id as usize, new_state as usize, 0, 0, 0)
).map(|_| ())
pub(crate) fn notify_app_switch(&self, new_app_token: [u32; 4]) -> Result<(), xous::Error> {
if let Some(old_context) = self.get_context_by_token(self.focused_context.unwrap()) {
if let Some(focuschange_id) = old_context.focuschange_id {
log::trace!("Background focus change to {}, id {}", old_context.listener, old_context.redraw_id);
xous::send_message(old_context.listener,
xous::Message::new_scalar(focuschange_id as usize, gam::FocusState::Background as usize, 0, 0, 0)
).map(|_| ())?;
} else {
return Err(xous::Error::ServerNotFound);
}
}
if let Some(new_context) = self.get_context_by_token(new_app_token) {
if let Some(focuschange_id) = new_context.focuschange_id {
log::trace!("Foreground focus change to {}, id {}", new_context.listener, new_context.redraw_id);
xous::send_message(new_context.listener,
xous::Message::new_scalar(focuschange_id as usize, gam::FocusState::Foreground as usize, 0, 0, 0)
).map(|_| ())?;
} else {
return Err(xous::Error::ServerNotFound);
}
}
Ok(())
}

View file

@ -491,31 +491,17 @@ fn xmain() -> ! {
log::debug!("trying to switch to {:?} with token {:?}", switchapp.app_name.as_str().unwrap(), switchapp.token);
if let Some(new_app_token) = context_mgr.find_app_token_by_name(switchapp.app_name.as_str().unwrap()) {
if let Some(menu_token) = context_mgr.find_app_token_by_name(MAIN_MENU_NAME) {
if menu_token == switchapp.token {
match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) {
Ok(_) => (),
Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()),
let authorized_switchers = vec![MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME];
for switchers in authorized_switchers {
if let Some(auth_token) = context_mgr.find_app_token_by_name(switchers) {
if auth_token == switchapp.token {
context_mgr.notify_app_switch(new_app_token).expect("couldn't send focus swap notifications!");
match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) {
Ok(_) => (),
Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()),
}
continue;
}
continue;
}
}
if let Some(modal_token) = context_mgr.find_app_token_by_name(ROOTKEY_MODAL_NAME) {
if modal_token == switchapp.token {
match context_mgr.activate(&gfx, &mut canvases, new_app_token, false) {
Ok(_) => (),
Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()),
}
continue;
}
}
if let Some(token) = context_mgr.find_app_token_by_name(gam::STATUS_BAR_NAME) {
if token == switchapp.token {
match context_mgr.activate(&gfx, &mut canvases, new_app_token, true) {
Ok(_) => (),
Err(_) => log::warn!("failed to switch to {}, silent error!", switchapp.app_name.as_str().unwrap()),
}
continue;
}
}
// this message came from ourselves

View file

@ -53,7 +53,7 @@ impl<'a> Menu<'a> {
redraw_id: MenuOpcode::Redraw.to_u32().unwrap(),
gotinput_id: None,
audioframe_id: None,
focuschange_id: None,
focuschange_id: None, // should always be None because we're not an app
rawkeys_id: Some(MenuOpcode::Rawkeys.to_u32().unwrap()),
}
).expect("couldn't register my Ux element with GAM");

View file

@ -279,7 +279,7 @@ impl<'a> Modal<'a> {
redraw_id: ModalOpcode::Redraw.to_u32().unwrap(),
gotinput_id: None,
audioframe_id: None,
focuschange_id: None,
focuschange_id: None, // should always be none because we're not an app
rawkeys_id: Some(ModalOpcode::Rawkeys.to_u32().unwrap()),
}
).expect("couldn't register my Ux element with GAM");