Skip to content

Commit

Permalink
feat(core): add set_focus window API, fixes #1737
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 30, 2021
1 parent 1e8af28 commit bb6992f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/api-set-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Adds `setFocus` to the window API.
7 changes: 7 additions & 0 deletions .changes/set-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Adds `set_focus` API on Window.
12 changes: 12 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ enum WindowMessage {
SetMaxSize(Option<Size>),
SetPosition(Position),
SetFullscreen(bool),
SetFocus,
SetIcon(WindowIcon),
DragWindow,
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
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 @@ -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<()>;

Expand Down
2 changes: 2 additions & 0 deletions core/tauri/src/endpoints/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum Cmd {
SetMaxSize(Option<Size>),
SetPosition(Position),
SetFullscreen(bool),
SetFocus,
SetIcon {
icon: IconDto,
},
Expand Down Expand Up @@ -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()?,
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 @@ -528,6 +528,11 @@ impl<P: Params> Window<P> {
.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)
Expand Down
34 changes: 24 additions & 10 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
Expand Down Expand Up @@ -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
}
})
Expand Down Expand Up @@ -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<void> {
return invokeTauriCommand({
__tauriModule: 'Window',
message: {
cmd: 'setFocus'
}
})
}

/**
* Sets the window icon.
*
Expand Down

0 comments on commit bb6992f

Please sign in to comment.