Skip to main content

gdk/
display.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::Display;
4use glib::object::IsA;
5use glib::ObjectExt;
6
7#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
8pub enum Backend {
9    Wayland,
10    X11,
11    Win32,
12    MacOS,
13    Broadway,
14}
15
16impl Backend {
17    // rustdoc-stripper-ignore-next
18    /// Equivalent to the C macro `GDK_IS_WAYLAND_DISPLAY`
19    #[doc(alias = "GDK_IS_WAYLAND_DISPLAY")]
20    pub fn is_wayland(&self) -> bool {
21        matches!(self, Self::Wayland)
22    }
23
24    // rustdoc-stripper-ignore-next
25    /// Equivalent to the C macro `GDK_IS_X11_DISPLAY`
26    #[doc(alias = "GDK_IS_X11_DISPLAY")]
27    pub fn is_x11(&self) -> bool {
28        matches!(self, Self::X11)
29    }
30
31    // rustdoc-stripper-ignore-next
32    /// Equivalent to the C macro `GDK_IS_WIN32_DISPLAY`
33    #[doc(alias = "GDK_IS_WIN32_DISPLAY")]
34    pub fn is_win32(&self) -> bool {
35        matches!(self, Self::Win32)
36    }
37
38    // rustdoc-stripper-ignore-next
39    /// Equivalent to the C macro `GDK_IS_QUARTZ_DISPLAY`
40    #[doc(alias = "GDK_IS_QUARTZ_DISPLAY")]
41    pub fn is_macos(&self) -> bool {
42        matches!(self, Self::MacOS)
43    }
44
45    // rustdoc-stripper-ignore-next
46    /// Equivalent to the C macro `GDK_IS_BROADWAY_DISPLAY`
47    #[doc(alias = "GDK_IS_BROADWAY_DISPLAY")]
48    pub fn is_broadway(&self) -> bool {
49        matches!(self, Self::Broadway)
50    }
51}
52
53mod sealed {
54    pub trait Sealed {}
55    impl<T: glib::IsA<crate::Display>> Sealed for T {}
56}
57
58pub trait DisplayExtManual: IsA<Display> + sealed::Sealed + 'static {
59    // rustdoc-stripper-ignore-next
60    /// Get the currently used display backend
61    fn backend(&self) -> Backend {
62        match self.as_ref().type_().name() {
63            "GdkWaylandDisplay" => Backend::Wayland,
64            "GdkX11Display" => Backend::X11,
65            "GdkQuartzDisplay" => Backend::MacOS,
66            "GdkWin32Display" => Backend::Win32,
67            "GdkBroadwayDisplay" => Backend::Broadway,
68            e => panic!("Unsupported display backend {e}"),
69        }
70    }
71}
72
73impl<O: IsA<Display>> DisplayExtManual for O {}