screenshot_ui: Move selection when holding Space

This commit is contained in:
Ivan Molodetskikh
2025-06-12 15:16:29 +03:00
parent 2f18d8e328
commit a23ce10311
2 changed files with 52 additions and 2 deletions
+4
View File
@@ -396,6 +396,10 @@ impl State {
return FilterResult::Intercept(None); return FilterResult::Intercept(None);
} }
if let Some(Keysym::space) = raw {
this.niri.screenshot_ui.set_space_down(pressed);
}
let bindings = &this.niri.config.borrow().binds; let bindings = &this.niri.config.borrow().binds;
let res = should_intercept_key( let res = should_intercept_key(
&mut this.niri.suppressed_keys, &mut this.niri.suppressed_keys,
+47 -1
View File
@@ -72,6 +72,8 @@ pub enum Button {
touch_slot: Option<TouchSlot>, touch_slot: Option<TouchSlot>,
on_capture_button: bool, on_capture_button: bool,
last_pos: (Output, Point<i32, Physical>), last_pos: (Output, Point<i32, Physical>),
/// Offset from the selection.1 corner to cursor grab point when moving the selection.
move_cursor_offset: Option<Point<i32, Physical>>,
}, },
} }
@@ -267,6 +269,26 @@ impl ScreenshotUi {
matches!(self, ScreenshotUi::Open { .. }) matches!(self, ScreenshotUi::Open { .. })
} }
pub fn set_space_down(&mut self, down: bool) {
if let Self::Open {
selection,
button:
Button::Down {
move_cursor_offset,
last_pos,
..
},
..
} = self
{
if down {
*move_cursor_offset = Some(last_pos.1 - selection.1);
} else {
*move_cursor_offset = None;
}
}
}
pub fn move_left(&mut self) { pub fn move_left(&mut self) {
let Self::Open { let Self::Open {
selection: (output, a, b), selection: (output, a, b),
@@ -725,7 +747,12 @@ impl ScreenshotUi {
} }
pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> { pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> {
if !matches!(self, Self::Open { .. }) { let Self::Open { button, .. } = self else {
return None;
};
// Pressing Space while the button is down goes into origin moving rather than capture.
if matches!(button, Button::Down { .. }) && raw == Keysym::space {
return None; return None;
} }
@@ -757,11 +784,13 @@ impl ScreenshotUi {
pub fn pointer_motion(&mut self, point: Point<i32, Physical>, slot: Option<TouchSlot>) { pub fn pointer_motion(&mut self, point: Point<i32, Physical>, slot: Option<TouchSlot>) {
let Self::Open { let Self::Open {
selection, selection,
output_data,
button: button:
Button::Down { Button::Down {
touch_slot, touch_slot,
on_capture_button, on_capture_button,
last_pos, last_pos,
move_cursor_offset,
}, },
.. ..
} = self } = self
@@ -779,7 +808,21 @@ impl ScreenshotUi {
return; return;
} }
if let Some(cursor_offset) = move_cursor_offset {
// The cursor offset is relative to selection.1.
let delta = point - (selection.1 + *cursor_offset);
let desired = rect_from_corner_points(selection.1 + delta, selection.2 + delta);
let bounds = Rectangle::from_size(output_data[&selection.0].size - desired.size);
let clamped_loc = desired.loc.constrain(bounds);
let delta = clamped_loc - rect_from_corner_points(selection.1, selection.2).loc;
selection.1 += delta;
selection.2 += delta;
} else {
selection.2 = point; selection.2 = point;
}
self.update_buffers(); self.update_buffers();
} }
@@ -818,6 +861,7 @@ impl ScreenshotUi {
touch_slot: slot, touch_slot: slot,
on_capture_button: true, on_capture_button: true,
last_pos: (output, point), last_pos: (output, point),
move_cursor_offset: None,
}; };
return false; return false;
} }
@@ -827,6 +871,7 @@ impl ScreenshotUi {
touch_slot: slot, touch_slot: slot,
on_capture_button: false, on_capture_button: false,
last_pos: (output.clone(), point), last_pos: (output.clone(), point),
move_cursor_offset: None,
}; };
*selection = (output, point, point); *selection = (output, point, point);
@@ -851,6 +896,7 @@ impl ScreenshotUi {
touch_slot, touch_slot,
on_capture_button, on_capture_button,
ref last_pos, ref last_pos,
..
} = *button } = *button
else { else {
return None; return None;