Repository: gtk-rs/gtk3-rs (language: rust)

Issue:
Add helpers for checking which display is being used
Like what is done in https://github.com/gtk-rs/gtk4-rs/pull/534.


Discussion / hints:

Fix patch:
diff --git a/gdk/Gir.toml b/gdk/Gir.toml
index 9bcdaf97b0eb..1794c75be0c5 100644
--- a/gdk/Gir.toml
+++ b/gdk/Gir.toml
@@ -264,6 +264,7 @@ version = "3.22"
 [[object]]
 name = "Gdk.Display"
 status = "generate"
+manual_traits = ["DisplayExtManual"]
     [[object.function]]
     name = "get_default_group"
         [object.function.return]
diff --git a/gdk/src/display.rs b/gdk/src/display.rs
new file mode 100644
index 000000000000..1da127fd9b44
--- /dev/null
+++ b/gdk/src/display.rs
@@ -0,0 +1,70 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+use crate::Display;
+use glib::object::IsA;
+use glib::ObjectExt;
+
+#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
+pub enum Backend {
+    Wayland,
+    X11,
+    Win32,
+    MacOS,
+    Broadway,
+}
+
+impl Backend {
+    // rustdoc-stripper-ignore-next
+    /// Equivalent to the C macro `GDK_IS_WAYLAND_DISPLAY`
+    #[doc(alias = "GDK_IS_WAYLAND_DISPLAY")]
+    pub fn is_wayland(&self) -> bool {
+        matches!(self, Self::Wayland)
+    }
+
+    // rustdoc-stripper-ignore-next
+    /// Equivalent to the C macro `GDK_IS_X11_DISPLAY`
+    #[doc(alias = "GDK_IS_X11_DISPLAY")]
+    pub fn is_x11(&self) -> bool {
+        matches!(self, Self::X11)
+    }
+
+    // rustdoc-stripper-ignore-next
+    /// Equivalent to the C macro `GDK_IS_WIN32_DISPLAY`
+    #[doc(alias = "GDK_IS_WIN32_DISPLAY")]
+    pub fn is_win32(&self) -> bool {
+        matches!(self, Self::Win32)
+    }
+
+    // rustdoc-stripper-ignore-next
+    /// Equivalent to the C macro `GDK_IS_QUARTZ_DISPLAY`
+    #[doc(alias = "GDK_IS_QUARTZ_DISPLAY")]
+    pub fn is_macos(&self) -> bool {
+        matches!(self, Self::MacOS)
+    }
+
+    // rustdoc-stripper-ignore-next
+    /// Equivalent to the C macro `GDK_IS_BROADWAY_DISPLAY`
+    #[doc(alias = "GDK_IS_BROADWAY_DISPLAY")]
+    pub fn is_broadway(&self) -> bool {
+        matches!(self, Self::Broadway)
+    }
+}
+
+pub trait DisplayExtManual: 'static {
+    // rustdoc-stripper-ignore-next
+    /// Get the currently used display backend
+    fn backend(&self) -> Backend;
+}
+
+impl<O: IsA<Display>> DisplayExtManual for O {
+    fn backend(&self) -> Backend {
+        match self.as_ref().type_().name() {
+            "GdkWaylandDisplay" => Backend::Wayland,
+            "GdkX11Display" => Backend::X11,
+            "GdkQuartzDisplay" => Backend::MacOS,
+            "GdkWin32Display" => Backend::Win32,
+            "GdkBroadwayDisplay" => Backend::Broadway,
+            e => panic!("Unsupported display backend {}", e),
+        }
+    }
+}
diff --git a/gdk/src/lib.rs b/gdk/src/lib.rs
index 846cdd687c2b..f25eac2bad82 100644
--- a/gdk/src/lib.rs
+++ b/gdk/src/lib.rs
@@ -32,6 +32,7 @@ mod cairo_interaction;
 mod change_data;
 mod device;
 mod device_manager;
+mod display;
 mod drag_context;
 mod event_button;
 mod event_configure;
@@ -96,6 +97,7 @@ pub use crate::atom::TARGET_DRAWABLE;
 pub use crate::atom::TARGET_PIXMAP;
 pub use crate::atom::TARGET_STRING;
 pub use crate::change_data::ChangeData;
+pub use crate::display::Backend;
 pub use crate::event::{Event, FromEvent};
 pub use crate::event_button::EventButton;
 pub use crate::event_configure::EventConfigure;
diff --git a/gdk/src/prelude.rs b/gdk/src/prelude.rs
index 9b6245545652..d8635a3740a3 100644
--- a/gdk/src/prelude.rs
+++ b/gdk/src/prelude.rs
@@ -4,7 +4,9 @@
 
 pub use crate::auto::traits::*;
 pub use crate::cairo_interaction::{GdkContextExt, GdkPixbufExt, GdkSurfaceExt};
+pub use crate::display::DisplayExtManual;
 pub use crate::window::WindowExtManual;
+
 #[doc(hidden)]
 pub use gdk_pixbuf::prelude::*;
 #[doc(hidden)]


Test patch: