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)]
#[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::<u8, Focus>(
self.pointer_entered.swap(focus as u8, Ordering::SeqCst),
)
pub fn swap_focus(&self, focus: Option<Focus>) -> Option<Focus> {
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::<u8, Focus>(focus)) },
}
}
pub fn current_focus(&self) -> Focus {
unsafe { std::mem::transmute::<u8, Focus>(self.pointer_entered.load(Ordering::SeqCst)) }
pub fn current_focus(&self) -> Option<Focus> {
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 {
@ -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<State> for CosmicWindow {
if p.has_ssd(false) {
let geo = p.window.geometry();
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 {
(
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::<CursorState>().unwrap();
cursor_state.set_shape(shape);
@ -693,7 +684,7 @@ impl PointerTarget<State> for CosmicWindow {
return;
};
let _previous = p.swap_focus(next);
let _previous = p.swap_focus(Some(next));
let cursor_state = seat.user_data().get::<CursorState>().unwrap();
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) {
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<State> 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<State>, 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<State>, 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<State> for CosmicWindow {
self.0.with_program(|p| {
let cursor_state = seat.user_data().get::<CursorState>().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)
}