diff --git a/.changes/api-set-focus.md b/.changes/api-set-focus.md new file mode 100644 index 00000000000..1e214ce8e09 --- /dev/null +++ b/.changes/api-set-focus.md @@ -0,0 +1,5 @@ +--- +"api": patch +--- + +Adds `setFocus` to the window API. diff --git a/.changes/set-focus.md b/.changes/set-focus.md new file mode 100644 index 00000000000..838b032085d --- /dev/null +++ b/.changes/set-focus.md @@ -0,0 +1,7 @@ +--- +"tauri": patch +"tauri-runtime": patch +"tauri-runtime-wry": patch +--- + +Adds `set_focus` API on Window. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index f42079db754..ebdd803e586 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -423,6 +423,7 @@ enum WindowMessage { SetMaxSize(Option), SetPosition(Position), SetFullscreen(bool), + SetFocus, SetIcon(WindowIcon), DragWindow, } @@ -763,6 +764,14 @@ impl Dispatch for WryDispatcher { .map_err(|_| Error::FailedToSendMessage) } + fn set_focus(&self) -> Result<()> { + self + .context + .proxy + .send_event(Message::Window(self.window_id, WindowMessage::SetFocus)) + .map_err(|_| Error::FailedToSendMessage) + } + fn set_icon(&self, icon: Icon) -> Result<()> { self .context @@ -1193,6 +1202,9 @@ fn handle_event_loop( window.set_fullscreen(None) } } + WindowMessage::SetFocus => { + window.set_focus(); + } WindowMessage::SetIcon(icon) => { window.set_window_icon(Some(icon)); } diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 1a4f1a42276..dce0bd2abd4 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -289,6 +289,9 @@ pub trait Dispatch: Clone + Send + Sized + 'static { /// Updates the window fullscreen state. fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()>; + /// Bring the window to front and focus. + fn set_focus(&self) -> crate::Result<()>; + /// Updates the window icon. fn set_icon(&self, icon: Icon) -> crate::Result<()>; diff --git a/core/tauri/src/endpoints/window.rs b/core/tauri/src/endpoints/window.rs index e17996656ae..405e72e474b 100644 --- a/core/tauri/src/endpoints/window.rs +++ b/core/tauri/src/endpoints/window.rs @@ -69,6 +69,7 @@ pub enum Cmd { SetMaxSize(Option), SetPosition(Position), SetFullscreen(bool), + SetFocus, SetIcon { icon: IconDto, }, @@ -153,6 +154,7 @@ impl Cmd { Self::SetMaxSize(size) => window.set_max_size(size)?, Self::SetPosition(position) => window.set_position(position)?, Self::SetFullscreen(fullscreen) => window.set_fullscreen(fullscreen)?, + Self::SetFocus => window.set_focus()?, Self::SetIcon { icon } => window.set_icon(icon.into())?, Self::StartDragging => window.start_dragging()?, Self::Print => window.print()?, diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index c0613f9b3b4..c5d89060c7c 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -528,6 +528,11 @@ impl Window

{ .map_err(Into::into) } + /// Bring the window to front and focus. + pub fn set_focus(&self) -> crate::Result<()> { + self.window.dispatcher.set_focus().map_err(Into::into) + } + /// Sets this window' icon. pub fn set_icon(&self, icon: Icon) -> crate::Result<()> { self.window.dispatcher.set_icon(icon).map_err(Into::into) diff --git a/tooling/api/src/window.ts b/tooling/api/src/window.ts index 368c8f9226d..1ebac11a388 100644 --- a/tooling/api/src/window.ts +++ b/tooling/api/src/window.ts @@ -593,12 +593,12 @@ class WindowManager { cmd: 'setMinSize', data: size ? { - type: size.type, - data: { - width: size.width, - height: size.height - } + type: size.type, + data: { + width: size.width, + height: size.height } + } : null } }) @@ -629,12 +629,12 @@ class WindowManager { cmd: 'setMaxSize', data: size ? { - type: size.type, - data: { - width: size.width, - height: size.height - } + type: size.type, + data: { + width: size.width, + height: size.height } + } : null } }) @@ -693,6 +693,20 @@ class WindowManager { }) } + /** + * Bring the window to front and focus. + * + * @returns A promise indicating the success or failure of the operation. + */ + async setFocus(): Promise { + return invokeTauriCommand({ + __tauriModule: 'Window', + message: { + cmd: 'setFocus' + } + }) + } + /** * Sets the window icon. *