Skip to content

Commit

Permalink
feat: add is_focused APIs, closes #6472 (#6530)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed May 26, 2023
1 parent 8fd9cfa commit 000104b
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/is_focused-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'minor:feat'
---

Add `WebviewWindow.is_focused` and `WebviewWindow.getFocusedWindow` getters.
6 changes: 6 additions & 0 deletions .changes/is_focused-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-runtime': 'minor:feat'
'tauri-runtime-wry': 'minor:feat'
---

Add `Window::is_focused` getter.
5 changes: 5 additions & 0 deletions .changes/is_focused-tauri.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'minor:feat'
---

Add `Window::is_focused` and `Manager::get_focused_window` getters.
6 changes: 6 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ pub enum WindowMessage {
IsFullscreen(Sender<bool>),
IsMinimized(Sender<bool>),
IsMaximized(Sender<bool>),
IsFocused(Sender<bool>),
IsDecorated(Sender<bool>),
IsResizable(Sender<bool>),
IsMaximizable(Sender<bool>),
Expand Down Expand Up @@ -1308,6 +1309,10 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
window_getter!(self, WindowMessage::IsMaximized)
}

fn is_focused(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsFocused)
}

/// Gets the window’s current decoration state.
fn is_decorated(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsDecorated)
Expand Down Expand Up @@ -2467,6 +2472,7 @@ fn handle_user_message<T: UserEvent>(
WindowMessage::IsFullscreen(tx) => tx.send(window.fullscreen().is_some()).unwrap(),
WindowMessage::IsMinimized(tx) => tx.send(window.is_minimized()).unwrap(),
WindowMessage::IsMaximized(tx) => tx.send(window.is_maximized()).unwrap(),
WindowMessage::IsFocused(tx) => tx.send(window.is_focused()).unwrap(),
WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
WindowMessage::IsMaximizable(tx) => tx.send(window.is_maximizable()).unwrap(),
Expand Down
3 changes: 3 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
/// Gets the window's current maximized state.
fn is_maximized(&self) -> Result<bool>;

/// Gets the window's current focus state.
fn is_focused(&self) -> Result<bool>;

/// Gets the window’s current decoration state.
fn is_decorated(&self) -> Result<bool>;

Expand Down
8 changes: 4 additions & 4 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions core/tauri/src/endpoints/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub enum WindowManagerCmd {
IsFullscreen,
IsMinimized,
IsMaximized,
IsFocused,
IsDecorated,
IsResizable,
IsMaximizable,
Expand Down Expand Up @@ -263,6 +264,7 @@ impl Cmd {
WindowManagerCmd::IsFullscreen => return Ok(window.is_fullscreen()?.into()),
WindowManagerCmd::IsMinimized => return Ok(window.is_minimized()?.into()),
WindowManagerCmd::IsMaximized => return Ok(window.is_maximized()?.into()),
WindowManagerCmd::IsFocused => return Ok(window.is_focused()?.into()),
WindowManagerCmd::IsDecorated => return Ok(window.is_decorated()?.into()),
WindowManagerCmd::IsResizable => return Ok(window.is_resizable()?.into()),
WindowManagerCmd::IsMaximizable => return Ok(window.is_maximizable()?.into()),
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
fn get_window(&self, label: &str) -> Option<Window<R>> {
self.manager().get_window(label)
}
/// Fetch the focused window. Returns `None` if there is not any focused window.
fn get_focused_window(&self) -> Option<Window<R>> {
self.manager().get_focused_window()
}

/// Fetch all managed windows.
fn windows(&self) -> HashMap<String, Window<R>> {
Expand Down
8 changes: 8 additions & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,14 @@ impl<R: Runtime> WindowManager<R> {
self.windows_lock().get(label).cloned()
}

pub fn get_focused_window(&self) -> Option<Window<R>> {
self
.windows_lock()
.iter()
.find(|w| w.1.is_focused().unwrap_or(false))
.map(|w| w.1.clone())
}

pub fn windows(&self) -> HashMap<String, Window<R>> {
self.windows_lock().clone()
}
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
Ok(false)
}

fn is_focused(&self) -> Result<bool> {
Ok(false)
}

fn is_decorated(&self) -> Result<bool> {
Ok(false)
}
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,11 @@ impl<R: Runtime> Window<R> {
self.window.dispatcher.is_maximized().map_err(Into::into)
}

/// Gets the window's current focus state.
pub fn is_focused(&self) -> crate::Result<bool> {
self.window.dispatcher.is_focused().map_err(Into::into)
}

/// Gets the window’s current decoration state.
pub fn is_decorated(&self) -> crate::Result<bool> {
self.window.dispatcher.is_decorated().map_err(Into::into)
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,33 @@ class WindowManager extends WebviewWindowHandle {
})
}

/**
* Gets the window's current focus state.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/api/window';
* const focused = await appWindow.isFocused();
* ```
*
* @returns Whether the window is focused or not.
*
* @since 1.4
* */
async isFocused(): Promise<boolean> {
return invokeTauriCommand({
__tauriModule: 'Window',
message: {
cmd: 'manage',
data: {
label: this.label,
cmd: {
type: 'isFocused'
}
}
}
})
}

/**
* Gets the window's current decorated state.
* @example
Expand Down Expand Up @@ -2228,6 +2255,27 @@ class WebviewWindow extends WindowManager {
}
return null
}

/**
* Gets the focused window.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/window';
* const focusedWindow = WebviewWindow.getFocusedWindow();
* ```
*
* @returns The WebviewWindow instance to communicate with the webview or `undefined` if there is not any focused window.
*
* @since 1.4
*/
static async getFocusedWindow(): Promise<WebviewWindow | null> {
for (const w of getAll()) {
if (await w.isFocused()) {
return w
}
}
return null
}
}

/** The WebviewWindow for the current window. */
Expand Down

0 comments on commit 000104b

Please sign in to comment.