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

This commit is contained in:
Lucas Fernandes Nogueira 2021-07-02 13:08:51 -03:00 committed by GitHub
parent 6569c2bf5c
commit e0a8e09cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 1 deletions

7
.changes/gtk-window.md Normal file
View File

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

View File

@ -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" ]

View File

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

View File

@ -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 = [ ]

View File

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

View File

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

View File

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