Skip to content

Commit bb6992f

Browse files
committed
feat(core): add set_focus window API, fixes #1737
1 parent 1e8af28 commit bb6992f

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed

.changes/api-set-focus.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Adds `setFocus` to the window API.

.changes/set-focus.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": patch
4+
"tauri-runtime-wry": patch
5+
---
6+
7+
Adds `set_focus` API on Window.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ enum WindowMessage {
423423
SetMaxSize(Option<Size>),
424424
SetPosition(Position),
425425
SetFullscreen(bool),
426+
SetFocus,
426427
SetIcon(WindowIcon),
427428
DragWindow,
428429
}
@@ -763,6 +764,14 @@ impl Dispatch for WryDispatcher {
763764
.map_err(|_| Error::FailedToSendMessage)
764765
}
765766

767+
fn set_focus(&self) -> Result<()> {
768+
self
769+
.context
770+
.proxy
771+
.send_event(Message::Window(self.window_id, WindowMessage::SetFocus))
772+
.map_err(|_| Error::FailedToSendMessage)
773+
}
774+
766775
fn set_icon(&self, icon: Icon) -> Result<()> {
767776
self
768777
.context
@@ -1193,6 +1202,9 @@ fn handle_event_loop(
11931202
window.set_fullscreen(None)
11941203
}
11951204
}
1205+
WindowMessage::SetFocus => {
1206+
window.set_focus();
1207+
}
11961208
WindowMessage::SetIcon(icon) => {
11971209
window.set_window_icon(Some(icon));
11981210
}

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
289289
/// Updates the window fullscreen state.
290290
fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()>;
291291

292+
/// Bring the window to front and focus.
293+
fn set_focus(&self) -> crate::Result<()>;
294+
292295
/// Updates the window icon.
293296
fn set_icon(&self, icon: Icon) -> crate::Result<()>;
294297

core/tauri/src/endpoints/window.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub enum Cmd {
6969
SetMaxSize(Option<Size>),
7070
SetPosition(Position),
7171
SetFullscreen(bool),
72+
SetFocus,
7273
SetIcon {
7374
icon: IconDto,
7475
},
@@ -153,6 +154,7 @@ impl Cmd {
153154
Self::SetMaxSize(size) => window.set_max_size(size)?,
154155
Self::SetPosition(position) => window.set_position(position)?,
155156
Self::SetFullscreen(fullscreen) => window.set_fullscreen(fullscreen)?,
157+
Self::SetFocus => window.set_focus()?,
156158
Self::SetIcon { icon } => window.set_icon(icon.into())?,
157159
Self::StartDragging => window.start_dragging()?,
158160
Self::Print => window.print()?,

core/tauri/src/window.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,11 @@ impl<P: Params> Window<P> {
528528
.map_err(Into::into)
529529
}
530530

531+
/// Bring the window to front and focus.
532+
pub fn set_focus(&self) -> crate::Result<()> {
533+
self.window.dispatcher.set_focus().map_err(Into::into)
534+
}
535+
531536
/// Sets this window' icon.
532537
pub fn set_icon(&self, icon: Icon) -> crate::Result<()> {
533538
self.window.dispatcher.set_icon(icon).map_err(Into::into)

tooling/api/src/window.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ class WindowManager {
593593
cmd: 'setMinSize',
594594
data: size
595595
? {
596-
type: size.type,
597-
data: {
598-
width: size.width,
599-
height: size.height
600-
}
596+
type: size.type,
597+
data: {
598+
width: size.width,
599+
height: size.height
601600
}
601+
}
602602
: null
603603
}
604604
})
@@ -629,12 +629,12 @@ class WindowManager {
629629
cmd: 'setMaxSize',
630630
data: size
631631
? {
632-
type: size.type,
633-
data: {
634-
width: size.width,
635-
height: size.height
636-
}
632+
type: size.type,
633+
data: {
634+
width: size.width,
635+
height: size.height
637636
}
637+
}
638638
: null
639639
}
640640
})
@@ -693,6 +693,20 @@ class WindowManager {
693693
})
694694
}
695695

696+
/**
697+
* Bring the window to front and focus.
698+
*
699+
* @returns A promise indicating the success or failure of the operation.
700+
*/
701+
async setFocus(): Promise<void> {
702+
return invokeTauriCommand({
703+
__tauriModule: 'Window',
704+
message: {
705+
cmd: 'setFocus'
706+
}
707+
})
708+
}
709+
696710
/**
697711
* Sets the window icon.
698712
*

0 commit comments

Comments
 (0)