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

Window::is_focused #533

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/is-focused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Add `Window::is_focused`.
6 changes: 6 additions & 0 deletions examples/window_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ fn main() {
window_id,
..
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
Event::WindowEvent {
event: WindowEvent::Focused(focused),
..
} => {
dbg!(focused, window.is_focused());
}
_ => (),
}
});
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ impl Window {
warn!("set_focus not yet implemented on Android");
}

pub fn is_focused(&self) -> bool {
log::warn!("`Window::is_focused` is ignored on Android");
false
}

pub fn set_resizable(&self, _resizeable: bool) {}

pub fn set_minimized(&self, _minimized: bool) {}
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ impl Inner {
warn!("set_focus not yet implemented on iOS");
}

pub fn is_focused(&self) -> bool {
warn!("`Window::is_focused` is ignored on iOS");
false
}

pub fn request_redraw(&self) {
unsafe {
if self.gl_or_metal_backed {
Expand Down
4 changes: 4 additions & 0 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ impl Window {
}
}

pub fn is_focused(&self) -> bool {
self.window.is_active()
}

pub fn set_resizable(&self, resizable: bool) {
if let Err(e) = self
.window_requests_tx
Expand Down
8 changes: 8 additions & 0 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ impl UnownedWindow {
}
}

#[inline]
pub fn is_focused(&self) -> bool {
unsafe {
let is_key_window: BOOL = msg_send![*self.ns_window, isKeyWindow];
is_key_window == YES
}
}
amrbashir marked this conversation as resolved.
Show resolved Hide resolved

pub fn request_redraw(&self) {
AppState::queue_redraw(RootWindowId(self.id()));
}
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ impl Window {
}
}

#[inline]
pub fn is_focused(&self) -> bool {
let window_state = self.window_state.lock();
window_state.has_active_focus()
}

#[inline]
pub fn request_redraw(&self) {
unsafe {
Expand Down
12 changes: 11 additions & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,16 @@ impl Window {
self.window.set_focus()
}

/// Is window active and focused?
///
/// ## Platform-specific
///
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn is_focused(&self) -> bool {
self.window.is_focused()
}

/// Sets whether the window is resizable or not.
///
/// Note that making the window unresizable doesn't exempt you from handling `Resized`, as that event can still be
Expand Down Expand Up @@ -697,7 +707,7 @@ impl Window {
self.window.is_minimized()
}

/// Gets the window's current vibility state.
/// Gets the window's current visibility state.
///
/// ## Platform-specific
///
Expand Down