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 68f77d3 commit 0cf77e6
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions sctk/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use sctk::{
registry::RegistryState,

Check failure on line 47 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 47 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
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 +292,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 +580,12 @@ where
for event in sctk_event_sink_back_buffer.drain(..) {
match event {
SctkEvent::PointerEvent { ref variant, .. } => {

Check failure on line 582 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 582 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
cursor_position = LogicalPosition::new(
let surface_id = variant.surface.id();

cursor_position.insert(surface_id, 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,
)
}

SctkEvent::ScaleFactorChanged { ref factor, .. } => {
scale_factor = *factor;
sticky_exit_callback(
IcedSctkEvent::SctkEvent(event),
&self.state,
&mut control_flow,
&mut callback,
)
));
}

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

Check failure on line 619 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 619 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
};

continue
}

SctkEvent::LayerSurfaceEvent {
variant: LayerSurfaceEventVariant::Done,
id,
} => {

Check failure on line 628 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 628 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
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 +647,10 @@ where
&mut callback,
);

Check failure on line 648 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 648 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
}

continue
}

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

Check failure on line 675 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs

Check failure on line 675 in sctk/src/event_loop/mod.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/sctk/src/event_loop/mod.rs
}

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 +963,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 0cf77e6

Please sign in to comment.