From 5ea20fc90587315d50ffd845eee109bd302b1172 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 26 May 2024 14:38:10 +0100 Subject: [PATCH] event_loop: add is_x11 and is_wayland on EventLoop --- src/changelog/unreleased.md | 1 + src/platform/wayland.rs | 15 ++++++++++++++- src/platform/x11.rs | 15 ++++++++++++++- src/platform_impl/linux/mod.rs | 10 ++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index dd7e00b8..314d13ec 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -12,6 +12,7 @@ on how to add them: ### Added - Add `Window::turbo()`, implemented on X11, Wayland, and Web. +- Add traits `EventLoopExtWayland` and `EventLoopExtX11`, providing methods `is_wayland` and `is_x11` on `EventLoop`. - On X11, add `Window::some_rare_api`. - On X11, add `Window::even_more_rare_api`. - On Wayland, add `Window::common_api`. diff --git a/src/platform/wayland.rs b/src/platform/wayland.rs index 68362192..db6a217d 100644 --- a/src/platform/wayland.rs +++ b/src/platform/wayland.rs @@ -13,7 +13,7 @@ //! * `wayland-csd-adwaita` (default). //! * `wayland-csd-adwaita-crossfont`. //! * `wayland-csd-adwaita-notitle`. -use crate::event_loop::{ActiveEventLoop, EventLoopBuilder}; +use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}; use crate::monitor::MonitorHandle; use crate::window::{Window, WindowAttributes}; @@ -32,6 +32,19 @@ impl ActiveEventLoopExtWayland for ActiveEventLoop { } } +/// Additional methods on [`EventLoop`] that are specific to Wayland. +pub trait EventLoopExtWayland { + /// True if the [`EventLoop`] uses Wayland. + fn is_wayland(&self) -> bool; +} + +impl EventLoopExtWayland for EventLoop { + #[inline] + fn is_wayland(&self) -> bool { + self.event_loop.is_wayland() + } +} + /// Additional methods on [`EventLoopBuilder`] that are specific to Wayland. pub trait EventLoopBuilderExtWayland { /// Force using Wayland. diff --git a/src/platform/x11.rs b/src/platform/x11.rs index 80262fe8..749c400a 100644 --- a/src/platform/x11.rs +++ b/src/platform/x11.rs @@ -2,7 +2,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use crate::event_loop::{ActiveEventLoop, EventLoopBuilder}; +use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}; use crate::monitor::MonitorHandle; use crate::window::{Window, WindowAttributes}; @@ -99,6 +99,19 @@ impl ActiveEventLoopExtX11 for ActiveEventLoop { } } +/// Additional methods on [`EventLoop`] that are specific to X11. +pub trait EventLoopExtX11 { + /// True if the [`EventLoop`] uses X11. + fn is_x11(&self) -> bool; +} + +impl EventLoopExtX11 for EventLoop { + #[inline] + fn is_x11(&self) -> bool { + !self.event_loop.is_wayland() + } +} + /// Additional methods on [`EventLoopBuilder`] that are specific to X11. pub trait EventLoopBuilderExtX11 { /// Force using X11. diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 8abca557..5a61eb55 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -784,6 +784,16 @@ impl EventLoop { Ok(EventLoop::X(x11::EventLoop::new(xconn))) } + #[inline] + pub fn is_wayland(&self) -> bool { + match *self { + #[cfg(wayland_platform)] + EventLoop::Wayland(_) => true, + #[cfg(x11_platform)] + _ => false, + } + } + pub fn create_proxy(&self) -> EventLoopProxy { x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) }