CosmicWindow: Use Option<Focus> 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.
This commit is contained in:
Ian Douglas Scott 2024-03-26 13:48:56 -07:00 • committed by Victoria Brekenfeld
parent 9e5ce71e1c
commit d34662661b

View file

@ -94,8 +94,7 @@ impl fmt::Debug for CosmicWindowInternal {
#[repr(u8)] #[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Focus { pub enum Focus {
None, Header = 1,
Header,
ResizeTop, ResizeTop,
ResizeLeft, ResizeLeft,
ResizeRight, ResizeRight,
@ -107,16 +106,19 @@ pub enum Focus {
} }
impl CosmicWindowInternal { impl CosmicWindowInternal {
pub fn swap_focus(&self, focus: Focus) -> Focus { pub fn swap_focus(&self, focus: Option<Focus>) -> Option<Focus> {
unsafe { let value = focus.map_or(0, |x| x as u8);
std::mem::transmute::<u8, Focus>( match self.pointer_entered.swap(value, Ordering::SeqCst) {
self.pointer_entered.swap(focus as u8, Ordering::SeqCst), 0 => None,
) focus => unsafe { Some(std::mem::transmute::<u8, Focus>(focus)) },
} }
} }
pub fn current_focus(&self) -> Focus { pub fn current_focus(&self) -> Option<Focus> {
unsafe { std::mem::transmute::<u8, Focus>(self.pointer_entered.load(Ordering::SeqCst)) } match self.pointer_entered.load(Ordering::SeqCst) {
0 => None,
focus => unsafe { Some(std::mem::transmute::<u8, Focus>(focus)) },
}
} }
pub fn has_ssd(&self, pending: bool) -> bool { pub fn has_ssd(&self, pending: bool) -> bool {
@ -138,7 +140,7 @@ impl CosmicWindow {
window, window,
mask: Arc::new(Mutex::new(None)), mask: Arc::new(Mutex::new(None)),
activated: Arc::new(AtomicBool::new(false)), 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_seat: Arc::new(Mutex::new(None)),
last_title: Arc::new(Mutex::new(last_title)), last_title: Arc::new(Mutex::new(last_title)),
}, },
@ -613,43 +615,32 @@ impl PointerTarget<State> for CosmicWindow {
if p.has_ssd(false) { if p.has_ssd(false) {
let geo = p.window.geometry(); let geo = p.window.geometry();
let loc = event.location.to_i32_round::<i32>(); let loc = event.location.to_i32_round::<i32>();
let (old_focus, shape) = if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x < 0 { let (next, shape) = if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x < 0 {
( (Focus::ResizeTopLeft, CursorShape::NorthWestResize)
p.swap_focus(Focus::ResizeTopLeft),
CursorShape::NorthWestResize,
)
} else if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x >= geo.size.w { } else if loc.y - geo.loc.y < 0 && loc.x - geo.loc.x >= geo.size.w {
( (Focus::ResizeTopRight, CursorShape::NorthEastResize)
p.swap_focus(Focus::ResizeTopRight),
CursorShape::NorthEastResize,
)
} else if loc.y - geo.loc.y < 0 { } 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 { } else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h && loc.x - geo.loc.x < 0 {
( (Focus::ResizeBottomLeft, CursorShape::SouthWestResize)
p.swap_focus(Focus::ResizeBottomLeft),
CursorShape::SouthWestResize,
)
} else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h } else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h
&& loc.x - geo.loc.x >= geo.size.w && loc.x - geo.loc.x >= geo.size.w
{ {
( (Focus::ResizeBottomRight, CursorShape::SouthEastResize)
p.swap_focus(Focus::ResizeBottomRight),
CursorShape::SouthEastResize,
)
} else if loc.y - geo.loc.y >= SSD_HEIGHT + geo.size.h { } 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 { } 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 { } 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 { } else if loc.y - geo.loc.y < SSD_HEIGHT {
(p.swap_focus(Focus::Header), CursorShape::Default) (Focus::Header, CursorShape::Default)
} else { } else {
return; 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::<CursorState>().unwrap(); let cursor_state = seat.user_data().get::<CursorState>().unwrap();
cursor_state.set_shape(shape); cursor_state.set_shape(shape);
@ -693,7 +684,7 @@ impl PointerTarget<State> for CosmicWindow {
return; return;
}; };
let _previous = p.swap_focus(next); let _previous = p.swap_focus(Some(next));
let cursor_state = seat.user_data().get::<CursorState>().unwrap(); let cursor_state = seat.user_data().get::<CursorState>().unwrap();
cursor_state.set_shape(shape); cursor_state.set_shape(shape);
@ -719,14 +710,13 @@ impl PointerTarget<State> for CosmicWindow {
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) { fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
match self.0.with_program(|p| p.current_focus()) { match self.0.with_program(|p| p.current_focus()) {
Focus::Header => { Some(Focus::Header) => {
self.0.with_program(|p| { self.0.with_program(|p| {
*p.last_seat.lock().unwrap() = Some((seat.clone(), event.serial)); *p.last_seat.lock().unwrap() = Some((seat.clone(), event.serial));
}); });
PointerTarget::button(&self.0, seat, data, event) PointerTarget::button(&self.0, seat, data, event)
} }
Focus::None => {} Some(x) => {
x => {
let serial = event.serial; let serial = event.serial;
let seat = seat.clone(); let seat = seat.clone();
let Some(surface) = self.wl_surface() else { let Some(surface) = self.wl_surface() else {
@ -747,24 +737,25 @@ impl PointerTarget<State> for CosmicWindow {
Focus::ResizeBottomRight => ResizeEdge::BOTTOM_RIGHT, Focus::ResizeBottomRight => ResizeEdge::BOTTOM_RIGHT,
Focus::ResizeLeft => ResizeEdge::LEFT, Focus::ResizeLeft => ResizeEdge::LEFT,
Focus::ResizeRight => ResizeEdge::RIGHT, Focus::ResizeRight => ResizeEdge::RIGHT,
Focus::Header | Focus::None => unreachable!(), Focus::Header => unreachable!(),
}, },
) )
}); });
} }
None => {}
} }
} }
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) { fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
match self.0.with_program(|p| p.current_focus()) { 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<State>, data: &mut State) { fn frame(&self, seat: &Seat<State>, data: &mut State) {
match self.0.with_program(|p| p.current_focus()) { 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<State> for CosmicWindow {
self.0.with_program(|p| { self.0.with_program(|p| {
let cursor_state = seat.user_data().get::<CursorState>().unwrap(); let cursor_state = seat.user_data().get::<CursorState>().unwrap();
cursor_state.set_shape(CursorShape::Default); 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) PointerTarget::leave(&self.0, seat, data, serial, time)
} }