Skip to main content

winit_gtk4/
lib.rs

1//! Winit's GTK4 backend.
2
3use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
4use winit_core::window::{PlatformWindowAttributes, Window as CoreWindow};
5
6pub use self::event_loop::{ActiveEventLoop, EventLoop, PlatformSpecificEventLoopAttributes};
7pub use self::window::Window;
8
9macro_rules! os_error {
10    ($error:expr $(,)?) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }};
11}
12
13mod cursor;
14mod dnd;
15mod event_loop;
16mod icon;
17mod keysym;
18mod monitor;
19mod sink;
20mod wayland;
21mod window;
22mod x11;
23
24/// Additional methods on [`ActiveEventLoop`] that are specific to GTK4.
25pub trait ActiveEventLoopExtGtk4 {
26    /// True if the [`ActiveEventLoop`] uses GTK4.
27    fn is_gtk4(&self) -> bool;
28}
29
30impl ActiveEventLoopExtGtk4 for dyn CoreActiveEventLoop + '_ {
31    #[inline]
32    fn is_gtk4(&self) -> bool {
33        self.cast_ref::<ActiveEventLoop>().is_some()
34    }
35}
36
37/// Additional methods on [`EventLoop`] that are specific to GTK4.
38pub trait EventLoopExtGtk4 {
39    /// True if the [`EventLoop`] uses GTK4.
40    fn is_gtk4(&self) -> bool;
41}
42
43/// Additional methods when building event loop that are specific to GTK4.
44pub trait EventLoopBuilderExtGtk4 {
45    /// Force using GTK4.
46    fn with_gtk4(&mut self) -> &mut Self;
47
48    /// Whether to allow the event loop to be created off of the main thread.
49    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
50
51    /// Set the GTK application ID.
52    fn with_application_id(&mut self, application_id: String) -> &mut Self;
53}
54
55/// Additional methods on [`Window`] that are specific to GTK4.
56///
57/// [`Window`]: winit_core::window::Window
58pub trait WindowExtGtk4 {
59    /// Returns the underlying GTK application window.
60    ///
61    /// GTK objects must be used according to GTK's thread and main-context rules.
62    fn gtk_window(&self) -> Option<gtk4::ApplicationWindow>;
63}
64
65impl WindowExtGtk4 for dyn CoreWindow + '_ {
66    #[inline]
67    fn gtk_window(&self) -> Option<gtk4::ApplicationWindow> {
68        Some(self.cast_ref::<Window>()?.gtk_window())
69    }
70}
71
72/// Window attributes specific to GTK4.
73///
74/// GTK application identity is configured on
75/// [`EventLoopBuilderExtGtk4::with_application_id`] because it is a process/application property,
76/// not a per-window setting.
77#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
78pub struct WindowAttributesGtk4 {}
79
80impl PlatformWindowAttributes for WindowAttributesGtk4 {
81    fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
82        Box::new(self.clone())
83    }
84}