Răsfoiți Sursa

feat(core): expose `gtk_window`, closes #2083 (#2141)

Lucas Fernandes Nogueira 4 ani în urmă
părinte
comite
e0a8e09cab

+ 7 - 0
.changes/gtk-window.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch
+"tauri-runtime": patch
+"tauri-runtime-wry": patch
+---
+
+Expose `gtk_window` getter.

+ 2 - 1
core/tauri-runtime-wry/Cargo.toml

@@ -22,8 +22,9 @@ infer = "0.4"
 ico = "0.1"
 winapi = "0.3"
 
-[target."cfg(target_os = \"linux\")".dependencies]
+[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
 png = "0.16"
+gtk = { version = "0.9", features = [ "v3_16" ] }
 
 [features]
 dox = [ "wry/dox" ]

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

@@ -599,6 +599,23 @@ struct Hwnd(HWND);
 #[cfg(windows)]
 unsafe impl Send for Hwnd {}
 
+#[cfg(any(
+  target_os = "linux",
+  target_os = "dragonfly",
+  target_os = "freebsd",
+  target_os = "netbsd",
+  target_os = "openbsd"
+))]
+struct GtkWindow(gtk::ApplicationWindow);
+#[cfg(any(
+  target_os = "linux",
+  target_os = "dragonfly",
+  target_os = "freebsd",
+  target_os = "netbsd",
+  target_os = "openbsd"
+))]
+unsafe impl Send for GtkWindow {}
+
 #[derive(Debug, Clone)]
 enum WindowMessage {
   // Getters
@@ -619,6 +636,14 @@ enum WindowMessage {
   AvailableMonitors(Sender<Vec<MonitorHandle>>),
   #[cfg(windows)]
   Hwnd(Sender<Hwnd>),
+  #[cfg(any(
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "netbsd",
+    target_os = "openbsd"
+  ))]
+  GtkWindow(Sender<GtkWindow>),
   // Setters
   Center(Sender<Result<()>>),
   RequestUserAttention(Option<UserAttentionTypeWrapper>),
@@ -830,6 +855,18 @@ impl Dispatch for WryDispatcher {
     Ok(dispatcher_getter!(self, WindowMessage::Hwnd).0)
   }
 
+  /// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
+  #[cfg(any(
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "netbsd",
+    target_os = "openbsd"
+  ))]
+  fn gtk_window(&self) -> Result<gtk::ApplicationWindow> {
+    Ok(dispatcher_getter!(self, WindowMessage::GtkWindow).0)
+  }
+
   // Setters
 
   fn center(&self) -> Result<()> {
@@ -1592,6 +1629,17 @@ fn handle_event_loop(
               use wry::application::platform::windows::WindowExtWindows;
               tx.send(Hwnd(window.hwnd() as HWND)).unwrap()
             }
+            #[cfg(any(
+              target_os = "linux",
+              target_os = "dragonfly",
+              target_os = "freebsd",
+              target_os = "netbsd",
+              target_os = "openbsd"
+            ))]
+            WindowMessage::GtkWindow(tx) => {
+              use wry::application::platform::unix::WindowExtUnix;
+              tx.send(GtkWindow(window.gtk_window().clone())).unwrap()
+            }
             // Setters
             WindowMessage::Center(tx) => {
               tx.send(center_window(window)).unwrap();

+ 3 - 0
core/tauri-runtime/Cargo.toml

@@ -31,6 +31,9 @@ uuid = { version = "0.8.2", features = [ "v4" ] }
 [target."cfg(windows)".dependencies]
 winapi = "0.3"
 
+[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
+gtk = { version = "0.9", features = [ "v3_16" ] }
+
 [features]
 menu = [ ]
 system-tray = [ ]

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

@@ -417,6 +417,16 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
   #[cfg(windows)]
   fn hwnd(&self) -> crate::Result<HWND>;
 
+  /// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
+  #[cfg(any(
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "netbsd",
+    target_os = "openbsd"
+  ))]
+  fn gtk_window(&self) -> crate::Result<gtk::ApplicationWindow>;
+
   // SETTERS
 
   /// Centers the window.

+ 3 - 0
core/tauri/Cargo.toml

@@ -69,6 +69,9 @@ rfd = "0.4"
 raw-window-handle = { version = "0.3.3", optional = true }
 minisign-verify = { version = "0.1", optional = true }
 
+[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
+gtk = { version = "0.9", features = [ "v3_16" ] }
+
 [build-dependencies]
 cfg_aliases = "0.1.1"
 

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

@@ -511,6 +511,20 @@ impl<P: Params> Window<P> {
       .map_err(Into::into)
   }
 
+  /// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
+  ///
+  /// Note that this can only be used on the main thread.
+  #[cfg(any(
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "netbsd",
+    target_os = "openbsd"
+  ))]
+  pub fn gtk_window(&self) -> crate::Result<gtk::ApplicationWindow> {
+    self.window.dispatcher.gtk_window().map_err(Into::into)
+  }
+
   // Setters
 
   /// Centers the window.