Skip to content

Commit

Permalink
Add restore_focus argument to PointerInnerHandle::unset_grab
Browse files Browse the repository at this point in the history
Used in `ClickGrab` to prevent `motion` events from occurring with every
`button` event. Otherwise, behavior should be unchanged. This matches
the argument taken by `KeyboardInnerHandle::unset_grab`.

This seems like the simplest solution. It would also be possible to add
a method to the `PointerGrab` trait indicating if focus should be
restored, but that's complicated since `unset_grab` can't access the
grab when it's `Borrowed`, so it would have to add a bool to
`GrabStatus::Borrowed`, etc.

This still doesn't send a `frame`, but since this takes a serial and a
time, it probably will be sent along with other pointer events, and
hopefully part of a `frame`. The Wayland spec isn't all that specific
about when things can/should be part of a `frame`...

Calling `motion` is also incorrect with pointer constraints, but grabs
other than `ClickGrab` generally shouldn't exist while a constraint is
active. It would be good to enforce that some way.

Fixes #1148.
  • Loading branch information
ids1024 committed Oct 18, 2023
1 parent ec56970 commit 2458103
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions anvil/src/shell/grabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for MoveSurfaceG
handle.button(data, event);
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, true);
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for ResizeSurfac

// It is impossible to get `min_size` and `max_size` of dead toplevel, so we return early.
if !self.window.alive() {
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, true);
return;
}

Expand Down Expand Up @@ -346,7 +346,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for ResizeSurfac
handle.button(data, event);
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, true);

// If toplevel is dead, we can't resize it, so we return early.
if !self.window.alive() {
Expand Down
6 changes: 3 additions & 3 deletions src/desktop/wayland/popup/grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ where
event: &MotionEvent,
) {
if self.popup_grab.has_ended() {
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, true);
self.popup_grab.unset_keyboard_grab(data, event.serial);
return;
}
Expand Down Expand Up @@ -591,7 +591,7 @@ where
let state = event.state;

if self.popup_grab.has_ended() {
handle.unset_grab(data, serial, time);
handle.unset_grab(data, serial, time, true);
handle.button(data, event);
self.popup_grab.unset_keyboard_grab(data, serial);
return;
Expand All @@ -610,7 +610,7 @@ where
.unwrap_or(false)
{
let _ = self.popup_grab.ungrab(PopupUngrabStrategy::All);
handle.unset_grab(data, serial, time);
handle.unset_grab(data, serial, time, true);
handle.button(data, event);
self.popup_grab.unset_keyboard_grab(data, serial);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/input/pointer/grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl<D: SeatHandler + 'static> PointerGrab<D> for ClickGrab<D> {
handle.button(data, event);
if handle.current_pressed().is_empty() {
// no more buttons are pressed, release the grab
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, false);
}
}

Expand Down
43 changes: 25 additions & 18 deletions src/input/pointer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ impl<D: SeatHandler + 'static> PointerHandle<D> {
#[instrument(level = "debug", parent = &self.span, skip(self, data))]
pub fn unset_grab(&self, data: &mut D, serial: Serial, time: u32) {
let seat = self.get_seat(data);
self.inner.lock().unwrap().unset_grab(data, &seat, serial, time);
self.inner
.lock()
.unwrap()
.unset_grab(data, &seat, serial, time, true);
}

/// Check if this pointer is currently grabbed with this serial
Expand Down Expand Up @@ -460,9 +463,11 @@ impl<'a, D: SeatHandler + 'static> PointerInnerHandle<'a, D> {

/// Remove any current grab on this pointer, resetting it to the default behavior
///
/// This will also restore the focus of the underlying pointer
pub fn unset_grab(&mut self, data: &mut D, serial: Serial, time: u32) {
self.inner.unset_grab(data, self.seat, serial, time);
/// This will also restore the focus of the underlying pointer if restore_focus
/// is [`true`]
pub fn unset_grab(&mut self, data: &mut D, serial: Serial, time: u32, restore_focus: bool) {
self.inner
.unset_grab(data, self.seat, serial, time, restore_focus);
}

/// Access the current focus of this pointer
Expand Down Expand Up @@ -678,21 +683,23 @@ impl<D: SeatHandler + 'static> PointerInternal<D> {
}
}

fn unset_grab(&mut self, data: &mut D, seat: &Seat<D>, serial: Serial, time: u32) {
fn unset_grab(&mut self, data: &mut D, seat: &Seat<D>, serial: Serial, time: u32, restore_focus: bool) {
self.grab = GrabStatus::None;
// restore the focus
let location = self.location;
let focus = self.pending_focus.clone();
self.motion(
data,
seat,
focus,
&MotionEvent {
location,
serial,
time,
},
);
if restore_focus {
// restore the focus
let location = self.location;
let focus = self.pending_focus.clone();
self.motion(
data,
seat,
focus,
&MotionEvent {
location,
serial,
time,
},
);
}
}

fn motion(
Expand Down
2 changes: 1 addition & 1 deletion src/wayland/selection/data_device/dnd_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ where
}
}
}
handle.unset_grab(data, event.serial, event.time);
handle.unset_grab(data, event.serial, event.time, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wayland/selection/data_device/server_dnd_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
}
}
}
handle.unset_grab(data, serial, time);
handle.unset_grab(data, serial, time, true);
}
}

Expand Down

0 comments on commit 2458103

Please sign in to comment.