Skip to content

Commit

Permalink
make sure focus is handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Sep 2, 2023
1 parent 1742d17 commit 95684c3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 46 deletions.
41 changes: 37 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,42 @@ impl AppState {
}
}
}

pub(crate) fn get_event_listener(
&self,
id: Id,
listener: &EventListener,
) -> Option<&impl Fn(&Event) -> bool> {
self.view_states
.get(&id)
.and_then(|s| s.event_listeners.get(listener))
}

pub(crate) fn focus_changed(&mut self, old: Option<Id>, new: Option<Id>) {
if let Some(old_id) = old {
// To remove the styles applied by the Focus selector
if self.has_style_for_sel(old_id, StyleSelector::Focus)
|| self.has_style_for_sel(old_id, StyleSelector::FocusVisible)
{
self.request_layout(old_id);
}
if let Some(action) = self.get_event_listener(old_id, &EventListener::FocusLost) {
(*action)(&Event::FocusLost);
}
}

if let Some(id) = new {
// To apply the styles of the Focus selector
if self.has_style_for_sel(id, StyleSelector::Focus)
|| self.has_style_for_sel(id, StyleSelector::FocusVisible)
{
self.request_layout(id);
}
if let Some(action) = self.get_event_listener(id, &EventListener::FocusGained) {
(*action)(&Event::FocusGained);
}
}
}
}

/// A bundle of helper methods to be used by `View::event` handlers
Expand Down Expand Up @@ -663,10 +699,7 @@ impl<'a> EventCx<'a> {
id: Id,
listener: &EventListener,
) -> Option<&impl Fn(&Event) -> bool> {
self.app_state
.view_states
.get(&id)
.and_then(|s| s.event_listeners.get(listener))
self.app_state.get_event_listener(id, listener)
}

/// translate a window-positioned event to the local coordinate system of a view
Expand Down
16 changes: 16 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum Event {
WindowClosed,
WindowResized(Size),
WindowMoved(Point),
FocusGained,
FocusLost,
}

impl Event {
Expand All @@ -67,6 +69,8 @@ impl Event {
| Event::PointerUp(_)
| Event::PointerMove(_)
| Event::PointerWheel(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand All @@ -88,6 +92,8 @@ impl Event {
| Event::PointerWheel(_) => true,
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand Down Expand Up @@ -118,6 +124,8 @@ impl Event {
Event::PointerDown(_)
| Event::PointerUp(_)
| Event::PointerWheel(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand All @@ -142,6 +150,8 @@ impl Event {
Event::PointerWheel(pointer_event) => Some(pointer_event.pos),
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand Down Expand Up @@ -170,6 +180,8 @@ impl Event {
}
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand All @@ -196,6 +208,8 @@ impl Event {
}
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::FocusGained
| Event::FocusLost
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
Expand Down Expand Up @@ -226,6 +240,8 @@ impl Event {
Event::WindowMoved(_) => Some(EventListener::WindowMoved),
Event::WindowGotFocus => Some(EventListener::WindowGotFocus),
Event::WindowLostFocus => Some(EventListener::WindowLostFocus),
Event::FocusLost => Some(EventListener::FocusLost),
Event::FocusGained => Some(EventListener::FocusGained),
}
}
}
3 changes: 3 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ pub trait View {
let _ = app_state.taffy.remove(node);
id.remove_id_path();
app_state.view_states.remove(&id);
if app_state.focus == Some(id) {
app_state.focus = None;
}
}

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
Expand Down
56 changes: 14 additions & 42 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ impl WindowHandle {
processed |= self
.view
.event_main(&mut cx, Some(&id_path.0), event.clone());
} else {
cx.app_state.focus = None;
}
} else if let Some(listener) = event.listener() {
if let Some(action) = cx.get_event_listener(self.view.id(), &listener) {
processed |= (*action)(&event);
}

if !processed {
if let Some(listener) = event.listener() {
if let Some(action) = cx.get_event_listener(self.view.id(), &listener) {
processed |= (*action)(&event);
}
}
}

Expand Down Expand Up @@ -220,33 +226,7 @@ impl WindowHandle {
}
}
if was_focused != cx.app_state.focus {
if let Some(old_id) = was_focused {
// To remove the styles applied by the Focus selector
if cx.app_state.has_style_for_sel(old_id, StyleSelector::Focus)
|| cx
.app_state
.has_style_for_sel(old_id, StyleSelector::FocusVisible)
{
cx.app_state.request_layout(old_id);
}
if let Some(action) = cx.get_event_listener(old_id, &EventListener::FocusLost) {
(*action)(&event);
}
}

if let Some(id) = cx.app_state.focus {
// To apply the styles of the Focus selector
if cx.app_state.has_style_for_sel(id, StyleSelector::Focus)
|| cx
.app_state
.has_style_for_sel(id, StyleSelector::FocusVisible)
{
cx.app_state.request_layout(id);
}
if let Some(action) = cx.get_event_listener(id, &EventListener::FocusGained) {
(*action)(&event);
}
}
cx.app_state.focus_changed(was_focused, cx.app_state.focus);
}

self.process_update();
Expand Down Expand Up @@ -464,18 +444,10 @@ impl WindowHandle {
cx.app_state.request_layout(id);
}
UpdateMessage::Focus(id) => {
let old = cx.app_state.focus;
cx.app_state.focus = Some(id);

if let Some(old_id) = old {
// To remove the styles applied by the Focus selector
if cx.app_state.has_style_for_sel(old_id, StyleSelector::Focus) {
cx.app_state.request_layout(old_id);
}
}

if cx.app_state.has_style_for_sel(id, StyleSelector::Focus) {
cx.app_state.request_layout(id);
if cx.app_state.focus != Some(id) {
let old = cx.app_state.focus;
cx.app_state.focus = Some(id);
cx.app_state.focus_changed(old, cx.app_state.focus);
}
}
UpdateMessage::Active(id) => {
Expand Down

0 comments on commit 95684c3

Please sign in to comment.