Browse Source

feat(core): add focus API to the WindowBuilder and WindowOptions, #1737

Lucas Nogueira 4 years ago
parent
commit
5f351622c7

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

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Adds `focus?: boolean` to the WindowOptions interface.

+ 7 - 0
.changes/focus.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch
+"tauri-runtime": patch
+"tauri-runtime-wry": patch
+---
+
+Adds `focus` API to the WindowBuilder.

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

@@ -315,6 +315,10 @@ impl WindowBuilder for WindowBuilderWrapper {
     }
   }
 
+  fn focus(self) -> Self {
+    Self(self.0.with_focus())
+  }
+
   fn maximized(self, maximized: bool) -> Self {
     Self(self.0.with_maximized(maximized))
   }

+ 3 - 0
core/tauri-runtime/src/webview.rs

@@ -124,6 +124,9 @@ pub trait WindowBuilder: WindowBuilderBase {
   /// Whether to start the window in fullscreen or not.
   fn fullscreen(self, fullscreen: bool) -> Self;
 
+  /// Whether the window will be initially hidden or focused.
+  fn focus(self) -> Self;
+
   /// Whether the window should be maximized upon creation.
   fn maximized(self, maximized: bool) -> Self;
 

+ 7 - 0
core/tauri-utils/src/config.rs

@@ -69,6 +69,9 @@ pub struct WindowConfig {
   /// Whether the window starts as fullscreen or not.
   #[serde(default)]
   pub fullscreen: bool,
+  /// Whether the window will be initially hidden or focused.
+  #[serde(default)]
+  pub focus: bool,
   /// Whether the window is transparent or not.
   #[serde(default)]
   pub transparent: bool,
@@ -130,6 +133,7 @@ impl Default for WindowConfig {
       resizable: default_resizable(),
       title: default_title(),
       fullscreen: false,
+      focus: false,
       transparent: false,
       maximized: false,
       visible: default_visible(),
@@ -630,6 +634,7 @@ mod build {
       let resizable = self.resizable;
       let title = str_lit(&self.title);
       let fullscreen = self.fullscreen;
+      let focus = self.focus;
       let transparent = self.transparent;
       let maximized = self.maximized;
       let visible = self.visible;
@@ -652,6 +657,7 @@ mod build {
         resizable,
         title,
         fullscreen,
+        focus,
         transparent,
         maximized,
         visible,
@@ -899,6 +905,7 @@ mod test {
         resizable: true,
         title: String::from("Tauri App"),
         fullscreen: false,
+        focus: false,
         transparent: false,
         maximized: false,
         visible: true,

+ 13 - 11
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
       }
     })
@@ -695,7 +695,7 @@ class WindowManager {
 
   /**
    * Bring the window to front and focus.
-   * 
+   *
    * @returns A promise indicating the success or failure of the operation.
    */
   async setFocus(): Promise<void> {
@@ -771,6 +771,8 @@ interface WindowOptions {
   title?: string
   /** Whether the window is in fullscreen mode or not. */
   fullscreen?: boolean
+  /** Whether the window will be initially hidden or focused. */
+  focus?: boolean
   /** Whether the window is transparent or not. */
   transparent?: boolean
   /** Whether the window should be maximized upon creation or not. */