[awm2] Track mouse state changes
@@ -147,10 +147,18 @@ impl DesktopElement for Window {
}
}
+#[derive(Debug)]
+enum MouseStateChange {
+ LeftClickBegan,
+ LeftClickEnded,
+ Moved(Point, Point),
+}
+
struct MouseState {
pos: Point,
desktop_size: Size,
size: Size,
+ left_click_down: bool,
}
impl MouseState {
@@ -159,10 +167,12 @@ impl MouseState {
pos,
size: Size::new(14, 14),
desktop_size,
+ left_click_down: false,
}
}
- fn handle_update(&mut self, packet: &MousePacket) {
+ fn handle_update(&mut self, packet: &MousePacket) -> Vec<MouseStateChange> {
+ let mut out = vec![];
self.pos.x += packet.rel_x as isize;
self.pos.y += packet.rel_y as isize;
@@ -171,6 +181,30 @@ impl MouseState {
self.pos.y = max(self.pos.y, 0);
self.pos.x = min(self.pos.x, self.desktop_size.width - 4);
self.pos.y = min(self.pos.y, self.desktop_size.height - 10);
+
+ if packet.rel_x != 0 || packet.rel_y != 0 {
+ out.push(MouseStateChange::Moved(
+ self.pos,
+ Point::new(packet.rel_x as isize, packet.rel_y as isize),
+ ));
+ }
+
+ // Is the left button clicked?
+ if packet.status & (1 << 0) != 0 {
+ // Were we already tracking a left click?
+ if !self.left_click_down {
+ self.left_click_down = true;
+ out.push(MouseStateChange::LeftClickBegan);
+ }
+ } else {
+ // Did we just release a left click?
+ if self.left_click_down {
+ self.left_click_down = false;
+ out.push(MouseStateChange::LeftClickEnded);
+ }
+ }
+
+ out
}
fn frame(&self) -> Rect {
@@ -593,19 +627,14 @@ impl Desktop {
pub fn handle_mouse_update(&mut self, packet: &MousePacket) {
let old_mouse_frame = self.mouse_state.frame();
- // Previous mouse position should be redrawn
- /*
- self.compositor_state
- .queue_full_redraw(self.mouse_state.frame());
- */
- self.mouse_state.handle_update(packet);
+ let state_changes = self.mouse_state.handle_update(packet);
// Don't bother queueing the new mouse position to redraw
// For simplicity, the compositor will always draw the mouse over each frame
let new_mouse_frame = self.mouse_state.frame();
+ // Previous mouse position should be redrawn
let total_update_rect = old_mouse_frame.union(new_mouse_frame);
- //println!("old {old_mouse_frame} total_update {total_update_rect}");
self.compositor_state.queue_full_redraw(total_update_rect);
}
