From f9e81d4c1d41b96bf9aa04ef9d8365d59945b9ce Mon Sep 17 00:00:00 2001 From: Ottatop Date: Sat, 23 Dec 2023 22:17:35 -0600 Subject: [PATCH] Use `space_elements` to remove boilerplate Unsure if this helped or not because I had to add `_ => unreachable!()` everywhere I matched on `WindowElement` --- src/api/handlers.rs | 2 + src/focus.rs | 1 + src/grab/resize_grab.rs | 2 + src/input.rs | 1 + src/layout.rs | 1 + src/render.rs | 1 + src/window.rs | 136 +++++++++++-------------------------- src/window/blocker.rs | 1 + src/window/window_state.rs | 8 ++- 9 files changed, 55 insertions(+), 98 deletions(-) diff --git a/src/api/handlers.rs b/src/api/handlers.rs index b022463e6..e978655dc 100644 --- a/src/api/handlers.rs +++ b/src/api/handlers.rs @@ -79,6 +79,7 @@ impl State { surface.close().expect("failed to close x11 win"); } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } } @@ -508,6 +509,7 @@ impl State { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { (Some(surface.class()), Some(surface.title())) } + _ => unreachable!(), }); let focused = window.as_ref().and_then(|win| { diff --git a/src/focus.rs b/src/focus.rs index 97a62027d..34ade3efe 100644 --- a/src/focus.rs +++ b/src/focus.rs @@ -374,6 +374,7 @@ impl WaylandFocus for FocusTarget { ) => surface.same_client_as(object_id), FocusTarget::Popup(popup) => popup.wl_surface().id().same_client_as(object_id), FocusTarget::LayerSurface(surf) => surf.wl_surface().id().same_client_as(object_id), + _ => unreachable!(), } } } diff --git a/src/grab/resize_grab.rs b/src/grab/resize_grab.rs index 06a0e7575..3b14adc03 100644 --- a/src/grab/resize_grab.rs +++ b/src/grab/resize_grab.rs @@ -179,6 +179,7 @@ impl PointerGrab for ResizeSurfaceGrab { .expect("failed to configure x11 win"); } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } @@ -235,6 +236,7 @@ impl PointerGrab for ResizeSurfaceGrab { }); } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } } diff --git a/src/input.rs b/src/input.rs index 8ec4d7426..18e025974 100644 --- a/src/input.rs +++ b/src/input.rs @@ -347,6 +347,7 @@ impl State { // INFO: do i need to configure this? } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), }); keyboard.set_focus(self, None, serial); } diff --git a/src/layout.rs b/src/layout.rs index bf00833dd..f12b6d2d9 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -175,6 +175,7 @@ impl State { // filtered out up there somewhere unreachable!(); } + _ => unreachable!(), } } }); diff --git a/src/render.rs b/src/render.rs index 6abc854a9..a6190e98c 100644 --- a/src/render.rs +++ b/src/render.rs @@ -78,6 +78,7 @@ where WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { surface.render_elements(renderer, location, scale, alpha) } + _ => unreachable!(), } .into_iter() .map(C::from) diff --git a/src/window.rs b/src/window.rs index 54f9330a3..9c797831a 100644 --- a/src/window.rs +++ b/src/window.rs @@ -8,7 +8,6 @@ use std::{cell::RefCell, time::Duration}; use smithay::{ backend::input::KeyState, desktop::{ - space::SpaceElement, utils::{ send_dmabuf_feedback_surface_tree, send_frames_surface_tree, take_presentation_feedback_surface_tree, with_surfaces_surface_tree, @@ -26,7 +25,8 @@ use smithay::{ wayland_protocols::wp::presentation_time::server::wp_presentation_feedback, wayland_server::protocol::wl_surface::WlSurface, }, - utils::{user_data::UserDataMap, IsAlive, Logical, Point, Rectangle, Serial}, + space_elements, + utils::{user_data::UserDataMap, Logical, Rectangle, Serial}, wayland::{ compositor::{self, SurfaceData}, dmabuf::DmabufFeedback, @@ -42,17 +42,29 @@ use self::window_state::{LocationRequestState, WindowElementState}; pub mod window_state; -/// The different types of windows. -#[derive(Debug, Clone, PartialEq)] -pub enum WindowElement { +space_elements! { + /// The different types of windows. + #[derive(Debug, Clone, PartialEq)] + pub WindowElement; /// This is a native Wayland window. - Wayland(Window), + Wayland = Window, /// This is an Xwayland window. - X11(X11Surface), + X11 = X11Surface, /// This is an Xwayland override redirect window, which should not be messed with. - X11OverrideRedirect(X11Surface), + X11OverrideRedirect = X11Surface, } +// /// The different types of windows. +// #[derive(Debug, Clone, PartialEq)] +// pub enum WindowElement { +// /// This is a native Wayland window. +// Wayland(Window), +// /// This is an Xwayland window. +// X11(X11Surface), +// /// This is an Xwayland override redirect window, which should not be messed with. +// X11OverrideRedirect(X11Surface), +// } + impl WindowElement { pub fn with_surfaces(&self, processor: F) where @@ -65,6 +77,7 @@ impl WindowElement { with_surfaces_surface_tree(&surface, processor); } } + _ => unreachable!(), } } @@ -93,6 +106,7 @@ impl WindowElement { ); } } + _ => unreachable!(), } } @@ -123,6 +137,7 @@ impl WindowElement { ); } } + _ => unreachable!(), } } @@ -153,6 +168,7 @@ impl WindowElement { ); } } + _ => unreachable!(), } } @@ -162,6 +178,7 @@ impl WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { surface.wl_surface() } + _ => unreachable!(), } } @@ -171,6 +188,7 @@ impl WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { surface.user_data() } + _ => unreachable!(), } } @@ -196,6 +214,7 @@ impl WindowElement { .expect("failed to configure x11 win"); } } + _ => unreachable!(), } self.with_state(|state| { state.loc_request_state = LocationRequestState::Sent(new_geo.loc); @@ -219,6 +238,7 @@ impl WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { Some(surface.class()) } + _ => unreachable!(), } } @@ -239,6 +259,7 @@ impl WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { Some(surface.title()) } + _ => unreachable!(), } } @@ -317,17 +338,6 @@ impl WindowElement { } } -impl IsAlive for WindowElement { - fn alive(&self) -> bool { - match self { - WindowElement::Wayland(window) => window.alive(), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - surface.alive() - } - } - } -} - impl PointerTarget for WindowElement { fn frame(&self, seat: &Seat, data: &mut State) { match self { @@ -335,6 +345,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { surface.frame(seat, data) } + _ => unreachable!(), } } @@ -345,6 +356,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::enter(surface, seat, data, event) } + _ => unreachable!(), } } @@ -355,6 +367,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::motion(surface, seat, data, event) } + _ => unreachable!(), } } @@ -372,6 +385,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::relative_motion(surface, seat, data, event); } + _ => unreachable!(), } } @@ -387,6 +401,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::button(surface, seat, data, event) } + _ => unreachable!(), } } @@ -397,6 +412,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::axis(surface, seat, data, frame) } + _ => unreachable!(), } } @@ -409,6 +425,7 @@ impl PointerTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { PointerTarget::leave(surface, seat, data, serial, time) } + _ => unreachable!(), } } @@ -500,6 +517,7 @@ impl KeyboardTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { KeyboardTarget::enter(surface, seat, data, keys, serial) } + _ => unreachable!(), } } @@ -509,6 +527,7 @@ impl KeyboardTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { KeyboardTarget::leave(surface, seat, data, serial) } + _ => unreachable!(), } } @@ -528,6 +547,7 @@ impl KeyboardTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { KeyboardTarget::key(surface, seat, data, key, state, serial, time); } + _ => unreachable!(), } } @@ -545,83 +565,7 @@ impl KeyboardTarget for WindowElement { WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { KeyboardTarget::modifiers(surface, seat, data, modifiers, serial); } - } - } -} - -impl SpaceElement for WindowElement { - fn geometry(&self) -> Rectangle { - // TODO: ssd - match self { - WindowElement::Wayland(window) => SpaceElement::geometry(window), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::geometry(surface) - } - } - } - - fn bbox(&self) -> Rectangle { - // TODO: ssd - match self { - WindowElement::Wayland(window) => SpaceElement::bbox(window), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::bbox(surface) - } - } - } - - fn is_in_input_region(&self, point: &Point) -> bool { - // TODO: ssd - match self { - WindowElement::Wayland(window) => SpaceElement::is_in_input_region(window, point), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::is_in_input_region(surface, point) - } - } - } - - fn z_index(&self) -> u8 { - match self { - WindowElement::Wayland(window) => SpaceElement::z_index(window), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::z_index(surface) - } - } - } - - fn set_activate(&self, activated: bool) { - match self { - WindowElement::Wayland(window) => SpaceElement::set_activate(window, activated), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::set_activate(surface, activated) - } - } - } - - fn output_enter(&self, output: &Output, overlap: Rectangle) { - match self { - WindowElement::Wayland(window) => SpaceElement::output_enter(window, output, overlap), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::output_enter(surface, output, overlap) - } - } - } - - fn output_leave(&self, output: &Output) { - match self { - WindowElement::Wayland(window) => SpaceElement::output_leave(window, output), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::output_leave(surface, output) - } - } - } - - fn refresh(&self) { - match self { - WindowElement::Wayland(window) => SpaceElement::refresh(window), - WindowElement::X11(surface) | WindowElement::X11OverrideRedirect(surface) => { - SpaceElement::refresh(surface) - } + _ => unreachable!(), } } } diff --git a/src/window/blocker.rs b/src/window/blocker.rs index 550edb171..18e10b99a 100644 --- a/src/window/blocker.rs +++ b/src/window/blocker.rs @@ -52,6 +52,7 @@ impl TiledWindowBlocker { }) } WindowElement::X11(_) | WindowElement::X11OverrideRedirect(_) => true, + _ => unreachable!(), }); tracing::debug!( diff --git a/src/window/window_state.rs b/src/window/window_state.rs index 429eb263c..544daea6d 100644 --- a/src/window/window_state.rs +++ b/src/window/window_state.rs @@ -125,6 +125,7 @@ impl WindowElement { .expect("failed to set x11 win to not fullscreen"); } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } FullscreenOrMaximized::Fullscreen => { @@ -170,7 +171,8 @@ impl WindowElement { .set_fullscreen(false) .expect("failed to set x11 win to not fullscreen"); } - Self::X11OverrideRedirect(_) => (), + WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } FullscreenOrMaximized::Maximized => { @@ -211,7 +213,8 @@ impl WindowElement { .set_fullscreen(false) .expect("failed to set x11 win to not fullscreen"); } - Self::X11OverrideRedirect(_) => (), + WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } @@ -238,6 +241,7 @@ impl WindowElement { .expect("failed to set x11 win to not fullscreen"); } WindowElement::X11OverrideRedirect(_) => (), + _ => unreachable!(), } } }