winit/platform/
unix.rs

1use glib::IsA;
2
3use crate::{
4    event_loop::EventLoopWindowTarget,
5    platform_impl::ApplicationName,
6    window::{Window, WindowBuilder},
7};
8
9pub use crate::platform_impl::hit_test;
10
11/// Additional methods on `Window` that are specific to Unix.
12pub trait WindowExtUnix {
13    /// Returns the `gtk::ApplicatonWindow` from gtk crate that is used by this window.
14    fn gtk_window(&self) -> &gtk::ApplicationWindow;
15
16    /// Returns the vertical `gtk::Box` that is added by default as the sole child of this window.
17    /// Returns `None` if the default vertical `gtk::Box` creation was disabled by [`WindowBuilderExtUnix::with_default_vbox`].
18    fn default_vbox(&self) -> Option<&gtk::Box>;
19
20    /// Whether to show the window icon in the taskbar or not.
21    fn set_skip_taskbar(&self, skip: bool);
22}
23
24impl WindowExtUnix for Window {
25    fn gtk_window(&self) -> &gtk::ApplicationWindow {
26        &self.window.window
27    }
28
29    fn default_vbox(&self) -> Option<&gtk::Box> {
30        self.window.default_vbox.as_ref()
31    }
32
33    fn set_skip_taskbar(&self, skip: bool) {
34        self.window.set_skip_taskbar(skip);
35    }
36}
37
38pub trait WindowBuilderExtUnix {
39    /// Build window with the given `general` and `instance` names.
40    ///
41    /// The `general` sets general class of `WM_CLASS(STRING)`, while `instance` set the
42    /// instance part of it. The resulted property looks like `WM_CLASS(STRING) = "general", "instance"`.
43    ///
44    /// For details about application ID conventions, see the
45    /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
46    fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
47
48    /// Whether to create the window icon with the taskbar icon or not.
49    fn with_skip_taskbar(self, skip: bool) -> WindowBuilder;
50
51    /// Set this window as a transient dialog for `parent`
52    /// <https://gtk-rs.org/gtk3-rs/stable/latest/docs/gdk/struct.Window.html#method.set_transient_for>
53    fn with_transient_for(self, parent: &impl IsA<gtk::Window>) -> WindowBuilder;
54
55    /// Whether to enable or disable the internal draw for transparent window.
56    ///
57    /// When tranparent attribute is enabled, we will call `connect_draw` and draw a transparent background.
58    /// For anyone who wants to draw the background themselves, set this to `false`.
59    /// Default is `true`.
60    fn with_transparent_draw(self, draw: bool) -> WindowBuilder;
61
62    /// Whether to enable or disable the double buffered rendering of the window.
63    ///
64    /// Default is `true`.
65    fn with_double_buffered(self, double_buffered: bool) -> WindowBuilder;
66
67    /// Whether to enable the rgba visual for the window.
68    ///
69    /// Default is `false` but is always `true` if [`WindowAttributes::transparent`](crate::window::WindowAttributes::transparent) is `true`
70    fn with_rgba_visual(self, rgba_visual: bool) -> WindowBuilder;
71
72    /// Wether to set this window as app paintable
73    ///
74    /// <https://docs.gtk.org/gtk3/method.Widget.set_app_paintable.html>
75    ///
76    /// Default is `false` but is always `true` if [`WindowAttributes::transparent`](crate::window::WindowAttributes::transparent) is `true`
77    fn with_app_paintable(self, app_paintable: bool) -> WindowBuilder;
78
79    /// Whether to create a vertical `gtk::Box` and add it as the sole child of this window.
80    /// Created by default.
81    fn with_default_vbox(self, add: bool) -> WindowBuilder;
82}
83
84impl WindowBuilderExtUnix for WindowBuilder {
85    fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
86        // TODO We haven't implemented it yet.
87        self.platform_specific.name = Some(ApplicationName::new(general.into(), instance.into()));
88        self
89    }
90
91    fn with_transient_for(mut self, parent: &impl IsA<gtk::Window>) -> WindowBuilder {
92        self.platform_specific.parent = Some(parent.clone().into());
93        self
94    }
95
96    fn with_skip_taskbar(mut self, skip: bool) -> WindowBuilder {
97        self.platform_specific.skip_taskbar = skip;
98        self
99    }
100
101    fn with_transparent_draw(mut self, draw: bool) -> WindowBuilder {
102        self.platform_specific.auto_transparent = draw;
103        self
104    }
105
106    fn with_double_buffered(mut self, double_buffered: bool) -> WindowBuilder {
107        self.platform_specific.double_buffered = double_buffered;
108        self
109    }
110
111    fn with_rgba_visual(mut self, rgba_visual: bool) -> WindowBuilder {
112        self.platform_specific.rgba_visual = rgba_visual;
113        self
114    }
115
116    fn with_app_paintable(mut self, app_paintable: bool) -> WindowBuilder {
117        self.platform_specific.app_paintable = app_paintable;
118        self
119    }
120
121    fn with_default_vbox(mut self, add: bool) -> WindowBuilder {
122        self.platform_specific.default_vbox = add;
123        self
124    }
125}
126
127/// Additional methods on `EventLoopWindowTarget` that are specific to Unix.
128pub trait EventLoopWindowTargetExtUnix {
129    /// True if the `EventLoopWindowTarget` uses Wayland.
130    fn is_wayland(&self) -> bool;
131}
132
133impl<T> EventLoopWindowTargetExtUnix for EventLoopWindowTarget<T> {
134    #[inline]
135    fn is_wayland(&self) -> bool {
136        self.p.is_wayland()
137    }
138}