Browse Source

feat(core): add `is_visible` API

Lucas Nogueira 4 years ago
parent
commit
36506c967d

+ 5 - 0
.changes/api-is-visible.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Adds `isVisible` getter on the window API.

+ 7 - 0
.changes/is-visible.md

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

+ 3 - 0
Cargo.toml

@@ -22,6 +22,9 @@ members = [
   "examples/updater/src-tauri",
 ]
 
+[patch.crates-io]
+tao = { git = "https://github.com/tauri-apps/tao", rev = "a3f533232df25dc30998809094ed5431b449489c" }
+
 # default to small, optimized workspace release binaries
 [profile.release]
 panic = "abort"

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

@@ -405,6 +405,7 @@ enum WindowMessage {
   IsMaximized(Sender<bool>),
   IsDecorated(Sender<bool>),
   IsResizable(Sender<bool>),
+  IsVisible(Sender<bool>),
   CurrentMonitor(Sender<Option<MonitorHandle>>),
   PrimaryMonitor(Sender<Option<MonitorHandle>>),
   AvailableMonitors(Sender<Vec<MonitorHandle>>),
@@ -548,6 +549,10 @@ impl Dispatch for WryDispatcher {
     Ok(dispatcher_getter!(self, WindowMessage::IsResizable))
   }
 
+  fn is_visible(&self) -> Result<bool> {
+    Ok(dispatcher_getter!(self, WindowMessage::IsVisible))
+  }
+
   fn current_monitor(&self) -> Result<Option<Monitor>> {
     Ok(
       dispatcher_getter!(self, WindowMessage::CurrentMonitor)
@@ -1160,6 +1165,7 @@ fn handle_event_loop(
             WindowMessage::IsMaximized(tx) => tx.send(window.is_maximized()).unwrap(),
             WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
             WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
+            WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
             WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
             WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
             WindowMessage::AvailableMonitors(tx) => {

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

@@ -213,6 +213,9 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
   /// Gets the window’s current resizable state.
   fn is_resizable(&self) -> crate::Result<bool>;
 
+  /// Gets the window's current vibility state.
+  fn is_visible(&self) -> crate::Result<bool>;
+
   /// Returns the monitor on which the window currently resides.
   ///
   /// Returns None if current monitor can't be detected.

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

@@ -48,6 +48,7 @@ pub enum Cmd {
   IsMaximized,
   IsDecorated,
   IsResizable,
+  IsVisible,
   CurrentMonitor,
   PrimaryMonitor,
   AvailableMonitors,
@@ -134,6 +135,7 @@ impl Cmd {
         Self::IsMaximized => return Ok(window.is_maximized()?.into()),
         Self::IsDecorated => return Ok(window.is_decorated()?.into()),
         Self::IsResizable => return Ok(window.is_resizable()?.into()),
+        Self::IsVisible => return Ok(window.is_visible()?.into()),
         Self::CurrentMonitor => return Ok(window.current_monitor()?.into()),
         Self::PrimaryMonitor => return Ok(window.primary_monitor()?.into()),
         Self::AvailableMonitors => return Ok(window.available_monitors()?.into()),

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

@@ -361,6 +361,11 @@ impl<P: Params> Window<P> {
     self.window.dispatcher.is_resizable().map_err(Into::into)
   }
 
+  /// Gets the window's current vibility state.
+  pub fn is_visible(&self) -> crate::Result<bool> {
+    self.window.dispatcher.is_visible().map_err(Into::into)
+  }
+
   /// Returns the monitor on which the window currently resides.
   ///
   /// Returns None if current monitor can't be detected.

+ 5 - 2
examples/api/src-tauri/src/main.rs

@@ -45,8 +45,11 @@ fn main() {
       match event.menu_item_id().as_str() {
         "toggle" => {
           let window = app.get_window("main").unwrap();
-          // TODO: window.is_visible API
-          window.hide().unwrap();
+          if window.is_visible().unwrap() {
+            window.hide().unwrap();
+          } else {
+            window.show().unwrap();
+          }
         }
         "new" => app
           .create_window(

+ 20 - 0
tooling/api/src/window.ts

@@ -372,6 +372,26 @@ class WindowManager {
     })
   }
 
+  /** Gets the window's current resizable state. */
+  async isResizable(): Promise<boolean> {
+    return invokeTauriCommand({
+      __tauriModule: 'Window',
+      message: {
+        cmd: 'isResizable'
+      }
+    })
+  }
+
+  /** Gets the window's current visible state. */
+  async isVisible(): Promise<boolean> {
+    return invokeTauriCommand({
+      __tauriModule: 'Window',
+      message: {
+        cmd: 'isVisible'
+      }
+    })
+  }
+
   // Setters
 
   /**