Skip to content

Commit

Permalink
improv(sctk): per-surface cursor position tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Jul 10, 2024
1 parent 5ebfaaf commit 26a8598
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions sctk/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ use sctk::{
registry::RegistryState,
seat::SeatState,
session_lock::SessionLockState,
shell::{wlr_layer::LayerShell, xdg::XdgShell, WaylandSurface},
shell::{
wlr_layer::{LayerShell, LayerSurface},
xdg::XdgShell,
WaylandSurface,
},
shm::Shm,
};
use sctk::{
Expand Down Expand Up @@ -292,8 +296,8 @@ where
F: FnMut(IcedSctkEvent<T>, &SctkState<T>, &mut ControlFlow),
{
let mut control_flow = ControlFlow::Poll;
let mut cursor_position = PhysicalPosition::new(0, 0);
let mut scale_factor = 1.0;

let mut cursor_position = HashMap::<_, LogicalPosition<f64>>::new();

callback(
IcedSctkEvent::NewEvents(StartCause::Init),
Expand Down Expand Up @@ -580,27 +584,15 @@ where
for event in sctk_event_sink_back_buffer.drain(..) {
match event {
SctkEvent::PointerEvent { ref variant, .. } => {
cursor_position = LogicalPosition::new(
variant.position.0,
variant.position.1,
)
.to_physical(scale_factor);
sticky_exit_callback(
IcedSctkEvent::SctkEvent(event),
&self.state,
&mut control_flow,
&mut callback,
)
}
let surface_id = variant.surface.id();

SctkEvent::ScaleFactorChanged { ref factor, .. } => {
scale_factor = *factor;
sticky_exit_callback(
IcedSctkEvent::SctkEvent(event),
&self.state,
&mut control_flow,
&mut callback,
)
cursor_position.insert(
surface_id,
LogicalPosition::new(
variant.position.0,
variant.position.1,
),
);
}

SctkEvent::PopupEvent {
Expand Down Expand Up @@ -631,13 +623,18 @@ where
&mut callback,
);
}
None => continue,
None => (),
};

continue;
}

SctkEvent::LayerSurfaceEvent {
variant: LayerSurfaceEventVariant::Done,
id,
} => {
cursor_position.remove(&id.id());

if let Some(i) =
self.state.layer_surfaces.iter().position(|l| {
l.surface.wl_surface().id() == id.id()
Expand All @@ -656,7 +653,10 @@ where
&mut callback,
);
}

continue;
}

SctkEvent::WindowEvent {
variant: WindowEventVariant::Close,
id,
Expand All @@ -680,14 +680,18 @@ where
&mut callback,
);
}

continue;
}
_ => sticky_exit_callback(
IcedSctkEvent::SctkEvent(event),
&self.state,
&mut control_flow,
&mut callback,
),
_ => (),
}

sticky_exit_callback(
IcedSctkEvent::SctkEvent(event),
&self.state,
&mut control_flow,
&mut callback,
)
}

// handle events indirectly via callback to the user.
Expand Down Expand Up @@ -965,7 +969,15 @@ where
},
platform_specific::wayland::window::Action::ShowWindowMenu { id } => {
if let (Some(window), Some((seat, last_press))) = (self.state.windows.iter_mut().find(|w| w.id == id), self.state.seats.first().and_then(|seat| seat.last_ptr_press.map(|p| (&seat.seat, p.2)))) {
let PhysicalPosition { x, y } = cursor_position;
let surface_id = window.window.wl_surface().id();

let cursor_position = cursor_position.get(&surface_id)
.cloned()
.unwrap_or_default();

// Cursor position does not need to be scaled here.
let PhysicalPosition { x, y } = cursor_position.to_physical::<i32>(1.0);

window.window.xdg_toplevel().show_window_menu(seat, last_press, x as i32, y as i32);
to_commit.insert(id, window.window.wl_surface().clone());
}
Expand Down

0 comments on commit 26a8598

Please sign in to comment.