1use std::cell::{Ref, RefCell, RefMut};
2use std::ffi::c_void;
3use std::ptr::NonNull;
4use std::rc::Rc;
5use std::sync::Arc;
6
7use blade_graphics as gpu;
8use collections::HashMap;
9use futures::channel::oneshot::Receiver;
10
11use raw_window_handle as rwh;
12use wayland_backend::client::ObjectId;
13use wayland_client::WEnum;
14use wayland_client::{protocol::wl_surface, Proxy};
15use wayland_protocols::wp::fractional_scale::v1::client::wp_fractional_scale_v1;
16use wayland_protocols::wp::viewporter::client::wp_viewport;
17use wayland_protocols::xdg::decoration::zv1::client::zxdg_toplevel_decoration_v1;
18use wayland_protocols::xdg::shell::client::xdg_surface;
19use wayland_protocols::xdg::shell::client::xdg_toplevel::{self};
20use wayland_protocols_plasma::blur::client::org_kde_kwin_blur;
21
22use crate::platform::blade::{BladeRenderer, BladeSurfaceConfig};
23use crate::platform::linux::wayland::display::WaylandDisplay;
24use crate::platform::linux::wayland::serial::SerialKind;
25use crate::platform::{PlatformAtlas, PlatformInputHandler, PlatformWindow};
26use crate::scene::Scene;
27use crate::{
28 px, size, AnyWindowHandle, Bounds, Globals, Modifiers, Output, Pixels, PlatformDisplay,
29 PlatformInput, Point, PromptLevel, Size, WaylandClientStatePtr, WindowAppearance,
30 WindowBackgroundAppearance, WindowBounds, WindowParams,
31};
32
33#[derive(Default)]
34pub(crate) struct Callbacks {
35 request_frame: Option<Box<dyn FnMut()>>,
36 input: Option<Box<dyn FnMut(crate::PlatformInput) -> crate::DispatchEventResult>>,
37 active_status_change: Option<Box<dyn FnMut(bool)>>,
38 resize: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
39 moved: Option<Box<dyn FnMut()>>,
40 should_close: Option<Box<dyn FnMut() -> bool>>,
41 close: Option<Box<dyn FnOnce()>>,
42 appearance_changed: Option<Box<dyn FnMut()>>,
43}
44
45struct RawWindow {
46 window: *mut c_void,
47 display: *mut c_void,
48}
49
50impl rwh::HasWindowHandle for RawWindow {
51 fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> {
52 let window = NonNull::new(self.window).unwrap();
53 let handle = rwh::WaylandWindowHandle::new(window);
54 Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) })
55 }
56}
57impl rwh::HasDisplayHandle for RawWindow {
58 fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> {
59 let display = NonNull::new(self.display).unwrap();
60 let handle = rwh::WaylandDisplayHandle::new(display);
61 Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) })
62 }
63}
64
65pub struct WaylandWindowState {
66 xdg_surface: xdg_surface::XdgSurface,
67 acknowledged_first_configure: bool,
68 pub surface: wl_surface::WlSurface,
69 decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>,
70 appearance: WindowAppearance,
71 blur: Option<org_kde_kwin_blur::OrgKdeKwinBlur>,
72 toplevel: xdg_toplevel::XdgToplevel,
73 viewport: Option<wp_viewport::WpViewport>,
74 outputs: HashMap<ObjectId, Output>,
75 display: Option<(ObjectId, Output)>,
76 globals: Globals,
77 renderer: BladeRenderer,
78 bounds: Bounds<Pixels>,
79 scale: f32,
80 input_handler: Option<PlatformInputHandler>,
81 decoration_state: WaylandDecorationState,
82 fullscreen: bool,
83 maximized: bool,
84 windowed_bounds: Bounds<Pixels>,
85 client: WaylandClientStatePtr,
86 handle: AnyWindowHandle,
87 active: bool,
88}
89
90#[derive(Clone)]
91pub struct WaylandWindowStatePtr {
92 state: Rc<RefCell<WaylandWindowState>>,
93 callbacks: Rc<RefCell<Callbacks>>,
94}
95
96impl WaylandWindowState {
97 #[allow(clippy::too_many_arguments)]
98 pub(crate) fn new(
99 handle: AnyWindowHandle,
100 surface: wl_surface::WlSurface,
101 xdg_surface: xdg_surface::XdgSurface,
102 toplevel: xdg_toplevel::XdgToplevel,
103 decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>,
104 appearance: WindowAppearance,
105 viewport: Option<wp_viewport::WpViewport>,
106 client: WaylandClientStatePtr,
107 globals: Globals,
108 options: WindowParams,
109 ) -> anyhow::Result<Self> {
110 let raw = RawWindow {
111 window: surface.id().as_ptr().cast::<c_void>(),
112 display: surface
113 .backend()
114 .upgrade()
115 .unwrap()
116 .display_ptr()
117 .cast::<c_void>(),
118 };
119 let gpu = Arc::new(
120 unsafe {
121 gpu::Context::init_windowed(
122 &raw,
123 gpu::ContextDesc {
124 validation: false,
125 capture: false,
126 overlay: false,
127 },
128 )
129 }
130 .map_err(|e| anyhow::anyhow!("{:?}", e))?,
131 );
132 let config = BladeSurfaceConfig {
133 size: gpu::Extent {
134 width: options.bounds.size.width.0 as u32,
135 height: options.bounds.size.height.0 as u32,
136 depth: 1,
137 },
138 transparent: options.window_background != WindowBackgroundAppearance::Opaque,
139 };
140
141 Ok(Self {
142 xdg_surface,
143 acknowledged_first_configure: false,
144 surface,
145 decoration,
146 blur: None,
147 toplevel,
148 viewport,
149 globals,
150 outputs: HashMap::default(),
151 display: None,
152 renderer: BladeRenderer::new(gpu, config),
153 bounds: options.bounds,
154 scale: 1.0,
155 input_handler: None,
156 decoration_state: WaylandDecorationState::Client,
157 fullscreen: false,
158 maximized: false,
159 windowed_bounds: options.bounds,
160 client,
161 appearance,
162 handle,
163 active: false,
164 })
165 }
166}
167
168pub(crate) struct WaylandWindow(pub WaylandWindowStatePtr);
169pub enum ImeInput {
170 InsertText(String),
171 SetMarkedText(String),
172 UnmarkText,
173 DeleteText,
174}
175
176impl Drop for WaylandWindow {
177 fn drop(&mut self) {
178 let mut state = self.0.state.borrow_mut();
179 let surface_id = state.surface.id();
180 let client = state.client.clone();
181
182 state.renderer.destroy();
183 if let Some(decoration) = &state.decoration {
184 decoration.destroy();
185 }
186 if let Some(blur) = &state.blur {
187 blur.release();
188 }
189 state.toplevel.destroy();
190 if let Some(viewport) = &state.viewport {
191 viewport.destroy();
192 }
193 state.xdg_surface.destroy();
194 state.surface.destroy();
195
196 let state_ptr = self.0.clone();
197 state
198 .globals
199 .executor
200 .spawn(async move {
201 state_ptr.close();
202 client.drop_window(&surface_id)
203 })
204 .detach();
205 drop(state);
206 }
207}
208
209impl WaylandWindow {
210 fn borrow(&self) -> Ref<WaylandWindowState> {
211 self.0.state.borrow()
212 }
213
214 fn borrow_mut(&self) -> RefMut<WaylandWindowState> {
215 self.0.state.borrow_mut()
216 }
217
218 pub fn new(
219 handle: AnyWindowHandle,
220 globals: Globals,
221 client: WaylandClientStatePtr,
222 params: WindowParams,
223 appearance: WindowAppearance,
224 ) -> anyhow::Result<(Self, ObjectId)> {
225 let surface = globals.compositor.create_surface(&globals.qh, ());
226 let xdg_surface = globals
227 .wm_base
228 .get_xdg_surface(&surface, &globals.qh, surface.id());
229 let toplevel = xdg_surface.get_toplevel(&globals.qh, surface.id());
230 toplevel.set_min_size(200, 200);
231
232 if let Some(fractional_scale_manager) = globals.fractional_scale_manager.as_ref() {
233 fractional_scale_manager.get_fractional_scale(&surface, &globals.qh, surface.id());
234 }
235
236 // Attempt to set up window decorations based on the requested configuration
237 let decoration = globals
238 .decoration_manager
239 .as_ref()
240 .map(|decoration_manager| {
241 let decoration = decoration_manager.get_toplevel_decoration(
242 &toplevel,
243 &globals.qh,
244 surface.id(),
245 );
246 decoration.set_mode(zxdg_toplevel_decoration_v1::Mode::ClientSide);
247 decoration
248 });
249
250 let viewport = globals
251 .viewporter
252 .as_ref()
253 .map(|viewporter| viewporter.get_viewport(&surface, &globals.qh, ()));
254
255 let this = Self(WaylandWindowStatePtr {
256 state: Rc::new(RefCell::new(WaylandWindowState::new(
257 handle,
258 surface.clone(),
259 xdg_surface,
260 toplevel,
261 decoration,
262 appearance,
263 viewport,
264 client,
265 globals,
266 params,
267 )?)),
268 callbacks: Rc::new(RefCell::new(Callbacks::default())),
269 });
270
271 // Kick things off
272 surface.commit();
273
274 Ok((this, surface.id()))
275 }
276}
277
278impl WaylandWindowStatePtr {
279 pub fn handle(&self) -> AnyWindowHandle {
280 self.state.borrow().handle
281 }
282
283 pub fn surface(&self) -> wl_surface::WlSurface {
284 self.state.borrow().surface.clone()
285 }
286
287 pub fn ptr_eq(&self, other: &Self) -> bool {
288 Rc::ptr_eq(&self.state, &other.state)
289 }
290
291 pub fn frame(&self, request_frame_callback: bool) {
292 if request_frame_callback {
293 let state = self.state.borrow_mut();
294 state.surface.frame(&state.globals.qh, state.surface.id());
295 drop(state);
296 }
297 let mut cb = self.callbacks.borrow_mut();
298 if let Some(fun) = cb.request_frame.as_mut() {
299 fun();
300 }
301 }
302
303 pub fn handle_xdg_surface_event(&self, event: xdg_surface::Event) {
304 match event {
305 xdg_surface::Event::Configure { serial } => {
306 let mut state = self.state.borrow_mut();
307 state.xdg_surface.ack_configure(serial);
308 let request_frame_callback = !state.acknowledged_first_configure;
309 state.acknowledged_first_configure = true;
310 drop(state);
311 self.frame(request_frame_callback);
312 }
313 _ => {}
314 }
315 }
316
317 pub fn handle_toplevel_decoration_event(&self, event: zxdg_toplevel_decoration_v1::Event) {
318 match event {
319 zxdg_toplevel_decoration_v1::Event::Configure { mode } => match mode {
320 WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ServerSide) => {
321 self.set_decoration_state(WaylandDecorationState::Server)
322 }
323 WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ClientSide) => {
324 self.set_decoration_state(WaylandDecorationState::Client)
325 }
326 WEnum::Value(_) => {
327 log::warn!("Unknown decoration mode");
328 }
329 WEnum::Unknown(v) => {
330 log::warn!("Unknown decoration mode: {}", v);
331 }
332 },
333 _ => {}
334 }
335 }
336
337 pub fn handle_fractional_scale_event(&self, event: wp_fractional_scale_v1::Event) {
338 match event {
339 wp_fractional_scale_v1::Event::PreferredScale { scale } => {
340 self.rescale(scale as f32 / 120.0);
341 }
342 _ => {}
343 }
344 }
345
346 pub fn handle_toplevel_event(&self, event: xdg_toplevel::Event) -> bool {
347 match event {
348 xdg_toplevel::Event::Configure {
349 width,
350 height,
351 states,
352 } => {
353 let mut size = if width == 0 || height == 0 {
354 None
355 } else {
356 Some(size(px(width as f32), px(height as f32)))
357 };
358
359 let fullscreen = states.contains(&(xdg_toplevel::State::Fullscreen as u8));
360 let maximized = states.contains(&(xdg_toplevel::State::Maximized as u8));
361
362 let mut state = self.state.borrow_mut();
363 let got_unmaximized = state.maximized && !maximized;
364 state.fullscreen = fullscreen;
365 state.maximized = maximized;
366
367 if got_unmaximized {
368 size = Some(state.windowed_bounds.size);
369 } else if !fullscreen && !maximized {
370 if let Some(size) = size {
371 state.windowed_bounds = Bounds {
372 origin: Point::default(),
373 size,
374 };
375 }
376 }
377
378 drop(state);
379 if let Some(size) = size {
380 self.resize(size);
381 }
382
383 false
384 }
385 xdg_toplevel::Event::Close => {
386 let mut cb = self.callbacks.borrow_mut();
387 if let Some(mut should_close) = cb.should_close.take() {
388 let result = (should_close)();
389 cb.should_close = Some(should_close);
390 if result {
391 drop(cb);
392 self.close();
393 }
394 result
395 } else {
396 true
397 }
398 }
399 _ => false,
400 }
401 }
402
403 pub fn handle_surface_event(
404 &self,
405 event: wl_surface::Event,
406 outputs: HashMap<ObjectId, Output>,
407 ) {
408 let mut state = self.state.borrow_mut();
409
410 match event {
411 wl_surface::Event::Enter { output } => {
412 let id = output.id();
413
414 let Some(output) = outputs.get(&id) else {
415 return;
416 };
417
418 state.outputs.insert(id, output.clone());
419
420 let scale = primary_output_scale(&mut state);
421
422 // We use `PreferredBufferScale` instead to set the scale if it's available
423 if state.surface.version() < wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE {
424 state.surface.set_buffer_scale(scale);
425 drop(state);
426 self.rescale(scale as f32);
427 }
428 }
429 wl_surface::Event::Leave { output } => {
430 state.outputs.remove(&output.id());
431
432 let scale = primary_output_scale(&mut state);
433
434 // We use `PreferredBufferScale` instead to set the scale if it's available
435 if state.surface.version() < wl_surface::EVT_PREFERRED_BUFFER_SCALE_SINCE {
436 state.surface.set_buffer_scale(scale);
437 drop(state);
438 self.rescale(scale as f32);
439 }
440 }
441 wl_surface::Event::PreferredBufferScale { factor } => {
442 // We use `WpFractionalScale` instead to set the scale if it's available
443 if state.globals.fractional_scale_manager.is_none() {
444 state.surface.set_buffer_scale(factor);
445 drop(state);
446 self.rescale(factor as f32);
447 }
448 }
449 _ => {}
450 }
451 }
452
453 pub fn handle_ime(&self, ime: ImeInput) {
454 let mut state = self.state.borrow_mut();
455 if let Some(mut input_handler) = state.input_handler.take() {
456 drop(state);
457 match ime {
458 ImeInput::InsertText(text) => {
459 input_handler.replace_text_in_range(None, &text);
460 }
461 ImeInput::SetMarkedText(text) => {
462 input_handler.replace_and_mark_text_in_range(None, &text, None);
463 }
464 ImeInput::UnmarkText => {
465 input_handler.unmark_text();
466 }
467 ImeInput::DeleteText => {
468 if let Some(marked) = input_handler.marked_text_range() {
469 input_handler.replace_text_in_range(Some(marked), "");
470 }
471 }
472 }
473 self.state.borrow_mut().input_handler = Some(input_handler);
474 }
475 }
476
477 pub fn get_ime_area(&self) -> Option<Bounds<Pixels>> {
478 let mut state = self.state.borrow_mut();
479 let mut bounds: Option<Bounds<Pixels>> = None;
480 if let Some(mut input_handler) = state.input_handler.take() {
481 drop(state);
482 if let Some(range) = input_handler.selected_text_range() {
483 bounds = input_handler.bounds_for_range(range);
484 }
485 self.state.borrow_mut().input_handler = Some(input_handler);
486 }
487 bounds
488 }
489
490 pub fn set_size_and_scale(&self, size: Option<Size<Pixels>>, scale: Option<f32>) {
491 let (size, scale) = {
492 let mut state = self.state.borrow_mut();
493 if size.map_or(true, |size| size == state.bounds.size)
494 && scale.map_or(true, |scale| scale == state.scale)
495 {
496 return;
497 }
498 if let Some(size) = size {
499 state.bounds.size = size;
500 }
501 if let Some(scale) = scale {
502 state.scale = scale;
503 }
504 let device_bounds = state.bounds.to_device_pixels(state.scale);
505 state.renderer.update_drawable_size(device_bounds.size);
506 (state.bounds.size, state.scale)
507 };
508
509 if let Some(ref mut fun) = self.callbacks.borrow_mut().resize {
510 fun(size, scale);
511 }
512
513 {
514 let state = self.state.borrow();
515 if let Some(viewport) = &state.viewport {
516 viewport.set_destination(size.width.0 as i32, size.height.0 as i32);
517 }
518 }
519 }
520
521 pub fn resize(&self, size: Size<Pixels>) {
522 self.set_size_and_scale(Some(size), None);
523 }
524
525 pub fn rescale(&self, scale: f32) {
526 self.set_size_and_scale(None, Some(scale));
527 }
528
529 /// Notifies the window of the state of the decorations.
530 ///
531 /// # Note
532 ///
533 /// This API is indirectly called by the wayland compositor and
534 /// not meant to be called by a user who wishes to change the state
535 /// of the decorations. This is because the state of the decorations
536 /// is managed by the compositor and not the client.
537 pub fn set_decoration_state(&self, state: WaylandDecorationState) {
538 self.state.borrow_mut().decoration_state = state;
539 }
540
541 pub fn close(&self) {
542 let mut callbacks = self.callbacks.borrow_mut();
543 if let Some(fun) = callbacks.close.take() {
544 fun()
545 }
546 }
547
548 pub fn handle_input(&self, input: PlatformInput) {
549 if let Some(ref mut fun) = self.callbacks.borrow_mut().input {
550 if !fun(input.clone()).propagate {
551 return;
552 }
553 }
554 if let PlatformInput::KeyDown(event) = input {
555 if let Some(ime_key) = &event.keystroke.ime_key {
556 let mut state = self.state.borrow_mut();
557 if let Some(mut input_handler) = state.input_handler.take() {
558 drop(state);
559 input_handler.replace_text_in_range(None, ime_key);
560 self.state.borrow_mut().input_handler = Some(input_handler);
561 }
562 }
563 }
564 }
565
566 pub fn set_focused(&self, focus: bool) {
567 self.state.borrow_mut().active = focus;
568 if let Some(ref mut fun) = self.callbacks.borrow_mut().active_status_change {
569 fun(focus);
570 }
571 }
572
573 pub fn set_appearance(&mut self, appearance: WindowAppearance) {
574 self.state.borrow_mut().appearance = appearance;
575
576 let mut callbacks = self.callbacks.borrow_mut();
577 if let Some(ref mut fun) = callbacks.appearance_changed {
578 (fun)()
579 }
580 }
581}
582
583fn primary_output_scale(state: &mut RefMut<WaylandWindowState>) -> i32 {
584 let mut scale = 1;
585 let mut current_output = state.display.take();
586 for (id, output) in state.outputs.iter() {
587 if let Some((_, output_data)) = ¤t_output {
588 if output.scale > output_data.scale {
589 current_output = Some((id.clone(), output.clone()));
590 }
591 } else {
592 current_output = Some((id.clone(), output.clone()));
593 }
594 scale = scale.max(output.scale);
595 }
596 state.display = current_output;
597 scale
598}
599
600impl rwh::HasWindowHandle for WaylandWindow {
601 fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> {
602 unimplemented!()
603 }
604}
605impl rwh::HasDisplayHandle for WaylandWindow {
606 fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> {
607 unimplemented!()
608 }
609}
610
611impl PlatformWindow for WaylandWindow {
612 fn bounds(&self) -> Bounds<Pixels> {
613 self.borrow().bounds
614 }
615
616 fn is_maximized(&self) -> bool {
617 self.borrow().maximized
618 }
619
620 fn window_bounds(&self) -> WindowBounds {
621 let state = self.borrow();
622 if state.fullscreen {
623 WindowBounds::Fullscreen(state.windowed_bounds)
624 } else if state.maximized {
625 WindowBounds::Maximized(state.windowed_bounds)
626 } else {
627 drop(state);
628 WindowBounds::Windowed(self.bounds())
629 }
630 }
631
632 fn content_size(&self) -> Size<Pixels> {
633 self.borrow().bounds.size
634 }
635
636 fn scale_factor(&self) -> f32 {
637 self.borrow().scale
638 }
639
640 fn appearance(&self) -> WindowAppearance {
641 self.borrow().appearance
642 }
643
644 fn display(&self) -> Option<Rc<dyn PlatformDisplay>> {
645 let state = self.borrow();
646 state.display.as_ref().map(|(id, display)| {
647 Rc::new(WaylandDisplay {
648 id: id.clone(),
649 name: display.name.clone(),
650 bounds: display.bounds.to_pixels(state.scale),
651 }) as Rc<dyn PlatformDisplay>
652 })
653 }
654
655 fn mouse_position(&self) -> Point<Pixels> {
656 self.borrow()
657 .client
658 .get_client()
659 .borrow()
660 .mouse_location
661 .unwrap_or_default()
662 }
663
664 fn modifiers(&self) -> Modifiers {
665 self.borrow().client.get_client().borrow().modifiers
666 }
667
668 fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
669 self.borrow_mut().input_handler = Some(input_handler);
670 }
671
672 fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
673 self.borrow_mut().input_handler.take()
674 }
675
676 fn prompt(
677 &self,
678 _level: PromptLevel,
679 _msg: &str,
680 _detail: Option<&str>,
681 _answers: &[&str],
682 ) -> Option<Receiver<usize>> {
683 None
684 }
685
686 fn activate(&self) {
687 log::info!("Wayland does not support this API");
688 }
689
690 fn is_active(&self) -> bool {
691 self.borrow().active
692 }
693
694 fn set_title(&mut self, title: &str) {
695 self.borrow().toplevel.set_title(title.to_string());
696 }
697
698 fn set_app_id(&mut self, app_id: &str) {
699 self.borrow().toplevel.set_app_id(app_id.to_owned());
700 }
701
702 fn set_background_appearance(&mut self, background_appearance: WindowBackgroundAppearance) {
703 let opaque = background_appearance == WindowBackgroundAppearance::Opaque;
704 let mut state = self.borrow_mut();
705 state.renderer.update_transparency(!opaque);
706
707 let region = state
708 .globals
709 .compositor
710 .create_region(&state.globals.qh, ());
711 region.add(0, 0, i32::MAX, i32::MAX);
712
713 if opaque {
714 // Promise the compositor that this region of the window surface
715 // contains no transparent pixels. This allows the compositor to
716 // do skip whatever is behind the surface for better performance.
717 state.surface.set_opaque_region(Some(®ion));
718 } else {
719 state.surface.set_opaque_region(None);
720 }
721
722 if let Some(ref blur_manager) = state.globals.blur_manager {
723 if background_appearance == WindowBackgroundAppearance::Blurred {
724 if state.blur.is_none() {
725 let blur = blur_manager.create(&state.surface, &state.globals.qh, ());
726 blur.set_region(Some(®ion));
727 state.blur = Some(blur);
728 }
729 state.blur.as_ref().unwrap().commit();
730 } else {
731 // It probably doesn't hurt to clear the blur for opaque windows
732 blur_manager.unset(&state.surface);
733 if let Some(b) = state.blur.take() {
734 b.release()
735 }
736 }
737 }
738
739 region.destroy();
740 }
741
742 fn set_edited(&mut self, _edited: bool) {
743 log::info!("ignoring macOS specific set_edited");
744 }
745
746 fn show_character_palette(&self) {
747 log::info!("ignoring macOS specific show_character_palette");
748 }
749
750 fn minimize(&self) {
751 self.borrow().toplevel.set_minimized();
752 }
753
754 fn zoom(&self) {
755 let state = self.borrow();
756 if !state.maximized {
757 state.toplevel.set_maximized();
758 } else {
759 state.toplevel.unset_maximized();
760 }
761 }
762
763 fn toggle_fullscreen(&self) {
764 let mut state = self.borrow_mut();
765 if !state.fullscreen {
766 state.toplevel.set_fullscreen(None);
767 } else {
768 state.toplevel.unset_fullscreen();
769 }
770 }
771
772 fn is_fullscreen(&self) -> bool {
773 self.borrow().fullscreen
774 }
775
776 fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
777 self.0.callbacks.borrow_mut().request_frame = Some(callback);
778 }
779
780 fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>) {
781 self.0.callbacks.borrow_mut().input = Some(callback);
782 }
783
784 fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
785 self.0.callbacks.borrow_mut().active_status_change = Some(callback);
786 }
787
788 fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
789 self.0.callbacks.borrow_mut().resize = Some(callback);
790 }
791
792 fn on_moved(&self, callback: Box<dyn FnMut()>) {
793 self.0.callbacks.borrow_mut().moved = Some(callback);
794 }
795
796 fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
797 self.0.callbacks.borrow_mut().should_close = Some(callback);
798 }
799
800 fn on_close(&self, callback: Box<dyn FnOnce()>) {
801 self.0.callbacks.borrow_mut().close = Some(callback);
802 }
803
804 fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
805 self.0.callbacks.borrow_mut().appearance_changed = Some(callback);
806 }
807
808 fn draw(&self, scene: &Scene) {
809 let mut state = self.borrow_mut();
810 state.renderer.draw(scene);
811 }
812
813 fn completed_frame(&self) {
814 let mut state = self.borrow_mut();
815 state.surface.commit();
816 }
817
818 fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
819 let state = self.borrow();
820 state.renderer.sprite_atlas().clone()
821 }
822
823 fn show_window_menu(&self, position: Point<Pixels>) {
824 let state = self.borrow();
825 let serial = state.client.get_serial(SerialKind::MousePress);
826 state.toplevel.show_window_menu(
827 &state.globals.seat,
828 serial,
829 position.x.0 as i32,
830 position.y.0 as i32,
831 );
832 }
833
834 fn start_system_move(&self) {
835 let state = self.borrow();
836 let serial = state.client.get_serial(SerialKind::MousePress);
837 state.toplevel._move(&state.globals.seat, serial);
838 }
839
840 fn should_render_window_controls(&self) -> bool {
841 self.borrow().decoration_state == WaylandDecorationState::Client
842 }
843}
844
845#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
846pub enum WaylandDecorationState {
847 /// Decorations are to be provided by the client
848 Client,
849
850 /// Decorations are provided by the server
851 Server,
852}