window.rs

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