From d34662661b2ad8e3a529c04830023dd1c9ecaadf Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 26 Mar 2024 13:48:56 -0700 Subject: [PATCH] CosmicWindow: Use `Option` instead of a `Focus::None` Slighter harder to convert to a `u8`, but allows matching over only the non-None variants, and is more idiomatic. --- src/shell/element/window.rs | 73 ++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/src/shell/element/window.rs b/src/shell/element/window.rs index 33461b29..6ff5b52d 100644 --- a/src/shell/element/window.rs +++ b/src/shell/element/window.rs @@ -94,8 +94,7 @@ impl fmt::Debug for CosmicWindowInternal { #[repr(u8)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Focus { - None, - Header, + Header = 1, ResizeTop, ResizeLeft, ResizeRight, @@ -107,16 +106,19 @@ pub enum Focus { } impl CosmicWindowInternal { - pub fn swap_focus(&self, focus: Focus) -> Focus { - unsafe { - std::mem::transmute::( - self.pointer_entered.swap(focus as u8, Ordering::SeqCst), - ) + pub fn swap_focus(&self, focus: Option) -> Option { + let value = focus.map_or(0, |x| x as u8); + match self.pointer_entered.swap(value, Ordering::SeqCst) { + 0 => None, + focus => unsafe { Some(std::mem::transmute::(focus)) }, } } - pub fn current_focus(&self) -> Focus { - unsafe { std::mem::transmute::(self.pointer_entered.load(Ordering::SeqCst)) } + pub fn current_focus(&self) -> Option { + match self.pointer_entered.load(Ordering::SeqCst) { + 0 => None, + focus => unsafe { Some(std::mem::transmute::(focus)) }, + } } pub fn has_ssd(&self, pending: bool) -> bool { @@ -138,7 +140,7 @@ impl CosmicWindow { window, mask: Arc::new(Mutex::new(None)), activated: Arc::new(AtomicBool::new(false)), - pointer_entered: Arc::new(AtomicU8::new(Focus::None as u8)), + pointer_entered: Arc::new(AtomicU8::new(0)), last_seat: Arc::new(Mutex::new(None)), last_title: Arc::new(Mutex::new(last_title)), }, @@ -613,43 +615,32 @@ impl PointerTarget for CosmicWindow { if p.has_ssd(false) { let geo = p.window.geometry(); let loc = event.location.to_i32_round::(); - let (old_focus, shape) = if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x < 0 { - ( - p.swap_focus(Focus::ResizeTopLeft), - CursorShape::NorthWestResize, - ) + let (next, shape) = if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x < 0 { + (Focus::ResizeTopLeft, CursorShape::NorthWestResize) } else if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x >= geo.size.w { - ( - p.swap_focus(Focus::ResizeTopRight), - CursorShape::NorthEastResize, - ) + (Focus::ResizeTopRight, CursorShape::NorthEastResize) } else if loc.y - geo.loc.y < 0 { - (p.swap_focus(Focus::ResizeTop), CursorShape::NorthResize) + (Focus::ResizeTop, CursorShape::NorthResize) } else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h && loc.x - geo.loc.x < 0 { - ( - p.swap_focus(Focus::ResizeBottomLeft), - CursorShape::SouthWestResize, - ) + (Focus::ResizeBottomLeft, CursorShape::SouthWestResize) } else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h && loc.x - geo.loc.x >= geo.size.w { - ( - p.swap_focus(Focus::ResizeBottomRight), - CursorShape::SouthEastResize, - ) + (Focus::ResizeBottomRight, CursorShape::SouthEastResize) } else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h { - (p.swap_focus(Focus::ResizeBottom), CursorShape::SouthResize) + (Focus::ResizeBottom, CursorShape::SouthResize) } else if loc.x - geo.loc.x < 0 { - (p.swap_focus(Focus::ResizeLeft), CursorShape::WestResize) + (Focus::ResizeLeft, CursorShape::WestResize) } else if loc.x - geo.loc.x >= geo.size.w { - (p.swap_focus(Focus::ResizeRight), CursorShape::EastResize) + (Focus::ResizeRight, CursorShape::EastResize) } else if loc.y - geo.loc.y < SSD_HEIGHT { - (p.swap_focus(Focus::Header), CursorShape::Default) + (Focus::Header, CursorShape::Default) } else { return; }; + let old_focus = p.swap_focus(Some(next)); - assert_eq!(old_focus, Focus::None); + assert_eq!(old_focus, None); let cursor_state = seat.user_data().get::().unwrap(); cursor_state.set_shape(shape); @@ -693,7 +684,7 @@ impl PointerTarget for CosmicWindow { return; }; - let _previous = p.swap_focus(next); + let _previous = p.swap_focus(Some(next)); let cursor_state = seat.user_data().get::().unwrap(); cursor_state.set_shape(shape); @@ -719,14 +710,13 @@ impl PointerTarget for CosmicWindow { fn button(&self, seat: &Seat, data: &mut State, event: &ButtonEvent) { match self.0.with_program(|p| p.current_focus()) { - Focus::Header => { + Some(Focus::Header) => { self.0.with_program(|p| { *p.last_seat.lock().unwrap() = Some((seat.clone(), event.serial)); }); PointerTarget::button(&self.0, seat, data, event) } - Focus::None => {} - x => { + Some(x) => { let serial = event.serial; let seat = seat.clone(); let Some(surface) = self.wl_surface() else { @@ -747,24 +737,25 @@ impl PointerTarget for CosmicWindow { Focus::ResizeBottomRight => ResizeEdge::BOTTOM_RIGHT, Focus::ResizeLeft => ResizeEdge::LEFT, Focus::ResizeRight => ResizeEdge::RIGHT, - Focus::Header | Focus::None => unreachable!(), + Focus::Header => unreachable!(), }, ) }); } + None => {} } } fn axis(&self, seat: &Seat, data: &mut State, frame: AxisFrame) { match self.0.with_program(|p| p.current_focus()) { - Focus::Header => PointerTarget::axis(&self.0, seat, data, frame), + Some(Focus::Header) => PointerTarget::axis(&self.0, seat, data, frame), _ => {} } } fn frame(&self, seat: &Seat, data: &mut State) { match self.0.with_program(|p| p.current_focus()) { - Focus::Header => PointerTarget::frame(&self.0, seat, data), + Some(Focus::Header) => PointerTarget::frame(&self.0, seat, data), _ => {} } } @@ -773,7 +764,7 @@ impl PointerTarget for CosmicWindow { self.0.with_program(|p| { let cursor_state = seat.user_data().get::().unwrap(); cursor_state.set_shape(CursorShape::Default); - let _previous = p.swap_focus(Focus::None); + let _previous = p.swap_focus(None); }); PointerTarget::leave(&self.0, seat, data, serial, time) }