Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement focusable attribute for Window #4124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lunixbochs
Copy link
Contributor

@lunixbochs lunixbochs commented Feb 11, 2025

  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality

if focusable is false, a window won't take focus, even if you interact with it
this is very useful for always-on-top floating utility windows

@kchibisov
Copy link
Member

How keyboard input generally works with such windows or it doesn't and you have mostly pointer input?

@lunixbochs
Copy link
Contributor Author

Yeah, they don't take keyboard focus, which is a feature - it allows you to interact with the overlay window without taking focus away from the current app. Imagine an on-screen keyboard, for example, we need to be able to click the keyboard buttons but have keyboard input still go to the app in the background.

@madsmtm madsmtm added S - enhancement Wouldn't this be the coolest? DS - macos DS - windows DS - x11 S - api Design and usability labels Feb 24, 2025
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS impl looks fine apart from the duplicated state. Requested changes because I'd like to see more docs, but am otherwise fine with the API.

///
/// The default is `true`.
///
/// - **Windows:** Supported.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add section ## Platform-specific.

self.window.maybe_queue_on_main(move |w| w.set_focusable(focusable))
}

/// Gets whether the window can be focused or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Link to set_focusable.

///
/// - **Windows:** Supported.
#[inline]
pub fn set_focusable(&self, focusable: bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the semantics of the feature aren't clear just from reading the description here. Perhaps you could expand on how it works? Or state the example you gave in the PR (useful for ...).

In the absence of clear cross-platform semantics, I'd like if this could link to the underlying implementations (e.g. link to shouldDelayWindowOrderingForEvent: for macOS, and others for the other platforms).

@@ -801,6 +809,7 @@ impl WinitView {
forward_key_to_app: Default::default(),
marked_text: Default::default(),
accepts_first_mouse,
focusable: focusable.into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I prefer Cell::new explicitly.

Suggested change
focusable: focusable.into(),
focusable: Cell::new(focusable),

if !self.ivars().focusable.get() {
let mtm = MainThreadMarker::from(self);
unsafe {
NSApplication::sharedApplication(mtm).preventWindowOrdering();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It'd be nice with a comment here detailing why preventWindowOrdering, an application-level method, is fine to call inside NSView.

@@ -116,6 +116,7 @@ pub(crate) struct State {
decorations: Cell<bool>,
resizable: Cell<bool>,
maximized: Cell<bool>,
focusable: Cell<bool>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid this state, query it from view.ivars().focusable instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DS - macos DS - windows DS - x11 S - api Design and usability S - enhancement Wouldn't this be the coolest?
Development

Successfully merging this pull request may close these issues.

3 participants