Skip to content

Commit

Permalink
Improve handling of window focus (#830)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McNab <[email protected]>
  • Loading branch information
PoignardAzur and DJMcNab authored Jan 16, 2025
1 parent f0e553a commit eb63bf6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions masonry/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ impl_context_method!(
self.widget_state.has_focus
}

/// Whether the window is focused.
pub fn is_window_focused(&self) -> bool {
self.global_state.window_focused
}

/// Whether this widget gets pointer events and hovered status.
pub fn accepts_pointer_interaction(&self) -> bool {
self.widget_state.accepts_pointer_interaction
Expand Down
7 changes: 3 additions & 4 deletions masonry/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ pub enum TextEvent {
/// Modifier keys (e.g. Shift, Ctrl, Alt) were pressed or released.
ModifierChange(ModifiersState),
/// The window took or lost focus.
// TODO - Document difference with Update focus change
FocusChange(bool),
WindowFocusChange(bool),
}

// TODO - Go into more detail.
Expand Down Expand Up @@ -470,7 +469,7 @@ impl TextEvent {
Self::Ime(Ime::Preedit(s, _)) if s.is_empty() => "Ime::Preedit(\"\")",
Self::Ime(Ime::Preedit(_, _)) => "Ime::Preedit(\"...\")",
Self::ModifierChange(_) => "ModifierChange",
Self::FocusChange(_) => "FocusChange",
Self::WindowFocusChange(_) => "WindowFocusChange",
}
}

Expand All @@ -484,7 +483,7 @@ impl TextEvent {
Self::Ime(_) => false,
// Basically every mouse click/scroll event seems to produce a modifier change event.
Self::ModifierChange(_) => true,
Self::FocusChange(_) => false,
Self::WindowFocusChange(_) => false,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion masonry/src/event_loop_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ impl MasonryState<'_> {
}
WinitWindowEvent::Focused(new_focus) => {
self.render_root
.handle_text_event(TextEvent::FocusChange(new_focus));
.handle_text_event(TextEvent::WindowFocusChange(new_focus));
}
WinitWindowEvent::CursorEntered { .. } => {
self.render_root
Expand Down
6 changes: 5 additions & 1 deletion masonry/src/passes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub(crate) fn run_on_pointer_event_pass(root: &mut RenderRoot, event: &PointerEv
// --- MARK: TEXT EVENT ---
/// See the [passes documentation](../doc/05_pass_system.md#event-passes).
pub(crate) fn run_on_text_event_pass(root: &mut RenderRoot, event: &TextEvent) -> Handled {
if matches!(event, TextEvent::FocusChange(false)) {
if matches!(event, TextEvent::WindowFocusChange(false)) {
run_on_pointer_event_pass(root, &PointerEvent::new_pointer_leave());
}

Expand All @@ -203,6 +203,10 @@ pub(crate) fn run_on_text_event_pass(root: &mut RenderRoot, event: &TextEvent) -
debug!("Running ON_TEXT_EVENT pass with {}", event.short_name());
}

if let TextEvent::WindowFocusChange(focused) = event {
root.global_state.window_focused = *focused;
}

let target = root.global_state.focused_widget;

let mut handled = run_event_pass(
Expand Down
4 changes: 4 additions & 0 deletions masonry/src/render_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub(crate) struct RenderRootState {
/// This is used to pick the focused widget on Tab events.
pub(crate) most_recently_clicked_widget: Option<WidgetId>,

/// Whether the window is focused.
pub(crate) window_focused: bool,

/// Widgets that have requested to be scrolled into view.
pub(crate) scroll_request_targets: Vec<(WidgetId, Rect)>,

Expand Down Expand Up @@ -260,6 +263,7 @@ impl RenderRoot {
focused_path: Vec::new(),
next_focused_widget: None,
most_recently_clicked_widget: None,
window_focused: true,
scroll_request_targets: Vec::new(),
hovered_path: Vec::new(),
pointer_capture_target: None,
Expand Down
2 changes: 1 addition & 1 deletion masonry/src/widget/text_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ impl<const EDITABLE: bool> Widget for TextArea<EDITABLE> {
}
}
// TODO: Set our highlighting colour to a lighter blue as window unfocused
TextEvent::FocusChange(_) => {}
TextEvent::WindowFocusChange(_) => {}
TextEvent::Ime(e) => {
// TODO: Handle the cursor movement things from https://github.com/rust-windowing/winit/pull/3824
let (fctx, lctx) = ctx.text_contexts();
Expand Down

0 comments on commit eb63bf6

Please sign in to comment.