Skip to content

Commit

Permalink
sctk: Map subsurface pointer events to parent surface, with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Feb 27, 2024
1 parent 749ee84 commit 100ff89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
34 changes: 28 additions & 6 deletions sctk/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,22 +437,30 @@ where
variant,
..
} => {
let mut offset = (0., 0.);
let (state, _native_id) = match surface_ids
.get(&variant.surface.id())
.and_then(|id| states.get_mut(&id.inner()).map(|state| (state, id)))
{
Some(s) => s,
None => continue,
None => {
if let Some((x_offset, y_offset, id)) = subsurface_ids.get(&variant.surface.id()) {
offset = (f64::from(*x_offset), f64::from(*y_offset));
states.get_mut(&id.inner()).map(|state| (state, id)).unwrap()
} else {
continue
}
},
};
match variant.kind {
PointerEventKind::Enter { .. } => {
state.set_cursor_position(Some(LogicalPosition { x: variant.position.0, y: variant.position.1 }));
state.set_cursor_position(Some(LogicalPosition { x: variant.position.0 + offset.0, y: variant.position.1 + offset.1 }));
}
PointerEventKind::Leave { .. } => {
state.set_cursor_position(None);
}
PointerEventKind::Motion { .. } => {
state.set_cursor_position(Some(LogicalPosition { x: variant.position.0, y: variant.position.1 }));
state.set_cursor_position(Some(LogicalPosition { x: variant.position.0 + offset.0, y: variant.position.1 + offset.1 }));
}
PointerEventKind::Press { .. }
| PointerEventKind::Release { .. }
Expand Down Expand Up @@ -952,6 +960,7 @@ where
&mut mods,
&surface_ids,
&destroyed_surface_ids,
&subsurface_ids,
) {
runtime.broadcast(native_event, Status::Ignored);
}
Expand Down Expand Up @@ -1017,6 +1026,7 @@ where
if event_is_for_surface(
&sctk_events[i],
object_id,
state,
has_kbd_focus,
) {
filtered_sctk.push(sctk_events.remove(i));
Expand All @@ -1034,6 +1044,7 @@ where
&mut mods,
&surface_ids,
&destroyed_surface_ids,
&subsurface_ids,
)
})
.collect();
Expand Down Expand Up @@ -2217,15 +2228,26 @@ where
}

// Determine if `SctkEvent` is for surface with given object id.
fn event_is_for_surface(
fn event_is_for_surface<'a, A, C>(
evt: &SctkEvent,
object_id: &ObjectId,
state: &State<A, C>,
has_kbd_focus: bool,
) -> bool {
) -> bool
where
A: Application + 'static,
<A as Program>::Theme: StyleSheet,
C: Compositor,
{
match evt {
SctkEvent::SeatEvent { id, .. } => &id.id() == object_id,
SctkEvent::PointerEvent { variant, .. } => {
&variant.surface.id() == object_id
let event_object_id = variant.surface.id();
&event_object_id == object_id
|| state
.subsurfaces
.iter()
.any(|s| s.wl_surface.id() == event_object_id)
}
SctkEvent::KeyboardEvent { variant, .. } => match variant {
KeyboardEventVariant::Leave(id) => &id.id() == object_id,
Expand Down
12 changes: 10 additions & 2 deletions sctk/src/sctk_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ impl SctkEvent {
modifiers: &mut Modifiers,
surface_ids: &HashMap<ObjectId, SurfaceIdWrapper>,
destroyed_surface_ids: &HashMap<ObjectId, SurfaceIdWrapper>,
subsurface_ids: &HashMap<ObjectId, (i32, i32, SurfaceIdWrapper)>,
) -> Vec<iced_runtime::core::Event> {
match self {
// TODO Ashley: Platform specific multi-seat events?
Expand All @@ -409,11 +410,18 @@ impl SctkEvent {
)]
}
PointerEventKind::Motion { .. } => {
let offset = if let Some((x_offset, y_offset, _)) =
subsurface_ids.get(&variant.surface.id())
{
(*x_offset, *y_offset)
} else {
(0, 0)
};
vec![iced_runtime::core::Event::Mouse(
mouse::Event::CursorMoved {
position: Point::new(
variant.position.0 as f32,
variant.position.1 as f32,
variant.position.0 as f32 + offset.0 as f32,
variant.position.1 as f32 + offset.1 as f32,
),
},
)]
Expand Down

0 comments on commit 100ff89

Please sign in to comment.