Răsfoiți Sursa

feat(core): add `set_focus` window API, fixes #1737

Lucas Nogueira 4 ani în urmă
părinte
comite
bb6992f888

+ 5 - 0
.changes/api-set-focus.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Adds `setFocus` to the window API.

+ 7 - 0
.changes/set-focus.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch
+"tauri-runtime": patch
+"tauri-runtime-wry": patch
+---
+
+Adds `set_focus` API on Window.

+ 12 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -423,6 +423,7 @@ enum WindowMessage {
   SetMaxSize(Option<Size>),
   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));
             }

+ 3 - 0
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<()>;
 

+ 2 - 0
core/tauri/src/endpoints/window.rs

@@ -69,6 +69,7 @@ pub enum Cmd {
   SetMaxSize(Option<Size>),
   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()?,

+ 5 - 0
core/tauri/src/window.rs

@@ -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)

+ 24 - 10
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<void> {
+    return invokeTauriCommand({
+      __tauriModule: 'Window',
+      message: {
+        cmd: 'setFocus'
+      }
+    })
+  }
+
   /**
    * Sets the window icon.
    *