winit/platform/
wayland.rs1use std::marker::PhantomData;
17use std::os::raw::c_void;
18use std::ptr::NonNull;
19
20use rwh_06::HandleError;
21use sctk::reexports::csd_frame::WindowState;
22
23use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
24use crate::monitor::MonitorHandle;
25pub use crate::window::Theme;
26use crate::window::{Window as CoreWindow, WindowAttributes};
27
28pub trait ActiveEventLoopExtWayland {
30 fn is_wayland(&self) -> bool;
32}
33
34impl ActiveEventLoopExtWayland for dyn ActiveEventLoop + '_ {
35 #[inline]
36 fn is_wayland(&self) -> bool {
37 self.as_any().downcast_ref::<crate::platform_impl::wayland::ActiveEventLoop>().is_some()
38 }
39}
40
41pub trait EventLoopExtWayland {
43 fn is_wayland(&self) -> bool;
45}
46
47impl EventLoopExtWayland for EventLoop {
48 #[inline]
49 fn is_wayland(&self) -> bool {
50 self.event_loop.is_wayland()
51 }
52}
53
54pub trait EventLoopBuilderExtWayland {
56 fn with_wayland(&mut self) -> &mut Self;
58
59 fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
64}
65
66impl EventLoopBuilderExtWayland for EventLoopBuilder {
67 #[inline]
68 fn with_wayland(&mut self) -> &mut Self {
69 self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::Wayland);
70 self
71 }
72
73 #[inline]
74 fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
75 self.platform_specific.any_thread = any_thread;
76 self
77 }
78}
79
80pub struct XdgSurfaceHandle<'a> {
81 raw: NonNull<c_void>,
82 _marker: PhantomData<&'a ()>,
83}
84
85impl<'a> XdgSurfaceHandle<'a> {
86 pub unsafe fn borrow_raw(raw: NonNull<c_void>) -> Self {
88 Self { raw, _marker: PhantomData }
89 }
90
91 pub fn as_raw(&self) -> NonNull<c_void> {
93 self.raw
94 }
95}
96
97pub trait HasXdgSurfaceHandle {
98 fn xdg_surface_handle(&self) -> Result<XdgSurfaceHandle<'_>, HandleError>;
99}
100
101pub struct XdgToplevelHandle<'a> {
102 raw: NonNull<c_void>,
103 _marker: PhantomData<&'a ()>,
104}
105
106impl<'a> XdgToplevelHandle<'a> {
107 pub unsafe fn borrow_raw(raw: NonNull<c_void>) -> Self {
109 Self { raw, _marker: PhantomData }
110 }
111
112 pub fn as_raw(&self) -> NonNull<c_void> {
114 self.raw
115 }
116}
117
118pub trait HasXdgToplevelHandle {
119 fn xdg_toplevel_handle(&self) -> Result<XdgToplevelHandle<'_>, HandleError>;
120}
121
122pub trait WindowExtWayland {
126 fn xdg_surface_handle<'a>(&'a self) -> Option<&dyn HasXdgSurfaceHandle>;
127 fn xdg_toplevel_handle<'a>(&'a self) -> Option<&dyn HasXdgToplevelHandle>;
128 fn window_state(&self) -> Option<WindowState>;
130}
131
132impl WindowExtWayland for dyn CoreWindow + '_ {
133 fn xdg_surface_handle(&self) -> Option<&dyn HasXdgSurfaceHandle> {
134 let window = self.as_any().downcast_ref::<crate::platform_impl::wayland::Window>();
135 window.map(|w| w as &dyn HasXdgSurfaceHandle)
136 }
137
138 fn xdg_toplevel_handle(&self) -> Option<&dyn HasXdgToplevelHandle> {
139 let window = self.as_any().downcast_ref::<crate::platform_impl::wayland::Window>();
140 window.map(|w| w as &dyn HasXdgToplevelHandle)
141 }
142
143 fn window_state(&self) -> Option<WindowState> {
144 let window = self.as_any().downcast_ref::<crate::platform_impl::wayland::Window>();
145 window.and_then(|w| w.xdg_window_state())
146 }
147}
148
149pub trait WindowAttributesExtWayland {
151 fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
159}
160
161impl WindowAttributesExtWayland for WindowAttributes {
162 #[inline]
163 fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
164 self.platform_specific.name =
165 Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into()));
166 self
167 }
168}
169
170pub trait MonitorHandleExtWayland {
172 fn native_id(&self) -> u32;
174}
175
176impl MonitorHandleExtWayland for MonitorHandle {
177 #[inline]
178 fn native_id(&self) -> u32 {
179 self.inner.native_identifier()
180 }
181}