Skip to content

Commit

Permalink
feat: Window::is_focused (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
henry40408 committed Aug 17, 2022
1 parent 5bbd4f8 commit 7d2eeee
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 1 deletion.
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
}
}

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

0 comments on commit 7d2eeee

Please sign in to comment.