forge
Explore
Log in

Don't you dare go Hollow

screenshot: make selection area modifiable via move/resize keybinds (#1279)

Browse files at this commit

7dc015e16b30be457a227601de85f085e5af1ddaUnverifiednyx and Ivan MolodetskikhParent: 0db48e2
* screenshot: make selection area modifiable via keybinds

* input: run fmt

* Reimplement screenshot UI binds in a better way

---------
src/input/mod.rsmodified+60 -11
@@ -740,14 +740,24 @@ impl State {
740 });740 });
741 }741 }
742 Action::MoveColumnLeft => {742 Action::MoveColumnLeft => {
743- self.niri.layout.move_left();743+ if self.niri.screenshot_ui.is_open() {
744- self.maybe_warp_cursor_to_focus();744+ self.niri.screenshot_ui.move_left();
745+ } else {
746+ self.niri.layout.move_left();
747+ self.maybe_warp_cursor_to_focus();
748+ }
749+
745 // FIXME: granular750 // FIXME: granular
746 self.niri.queue_redraw_all();751 self.niri.queue_redraw_all();
747 }752 }
748 Action::MoveColumnRight => {753 Action::MoveColumnRight => {
749- self.niri.layout.move_right();754+ if self.niri.screenshot_ui.is_open() {
750- self.maybe_warp_cursor_to_focus();755+ self.niri.screenshot_ui.move_right();
756+ } else {
757+ self.niri.layout.move_right();
758+ self.maybe_warp_cursor_to_focus();
759+ }
760+
751 // FIXME: granular761 // FIXME: granular
752 self.niri.queue_redraw_all();762 self.niri.queue_redraw_all();
753 }763 }
@@ -798,14 +808,24 @@ impl State {
798 self.niri.queue_redraw_all();808 self.niri.queue_redraw_all();
799 }809 }
800 Action::MoveWindowDown => {810 Action::MoveWindowDown => {
801- self.niri.layout.move_down();811+ if self.niri.screenshot_ui.is_open() {
802- self.maybe_warp_cursor_to_focus();812+ self.niri.screenshot_ui.move_down();
813+ } else {
814+ self.niri.layout.move_down();
815+ self.maybe_warp_cursor_to_focus();
816+ }
817+
803 // FIXME: granular818 // FIXME: granular
804 self.niri.queue_redraw_all();819 self.niri.queue_redraw_all();
805 }820 }
806 Action::MoveWindowUp => {821 Action::MoveWindowUp => {
807- self.niri.layout.move_up();822+ if self.niri.screenshot_ui.is_open() {
808- self.maybe_warp_cursor_to_focus();823+ self.niri.screenshot_ui.move_up();
824+ } else {
825+ self.niri.layout.move_up();
826+ self.maybe_warp_cursor_to_focus();
827+ }
828+
809 // FIXME: granular829 // FIXME: granular
810 self.niri.queue_redraw_all();830 self.niri.queue_redraw_all();
811 }831 }
@@ -1589,10 +1609,24 @@ impl State {
1589 }1609 }
1590 }1610 }
1591 Action::SetColumnWidth(change) => {1611 Action::SetColumnWidth(change) => {
1592- self.niri.layout.set_column_width(change);1612+ if self.niri.screenshot_ui.is_open() {
1613+ self.niri.screenshot_ui.set_width(change);
1614+
1615+ // FIXME: granular
1616+ self.niri.queue_redraw_all();
1617+ } else {
1618+ self.niri.layout.set_column_width(change);
1619+ }
1593 }1620 }
1594 Action::SetWindowWidth(change) => {1621 Action::SetWindowWidth(change) => {
1595- self.niri.layout.set_window_width(None, change);1622+ if self.niri.screenshot_ui.is_open() {
1623+ self.niri.screenshot_ui.set_width(change);
1624+
1625+ // FIXME: granular
1626+ self.niri.queue_redraw_all();
1627+ } else {
1628+ self.niri.layout.set_window_width(None, change);
1629+ }
1596 }1630 }
1597 Action::SetWindowWidthById { id, change } => {1631 Action::SetWindowWidthById { id, change } => {
1598 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);1632 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
@@ -1602,7 +1636,14 @@ impl State {
1602 }1636 }
1603 }1637 }
1604 Action::SetWindowHeight(change) => {1638 Action::SetWindowHeight(change) => {
1605- self.niri.layout.set_window_height(None, change);1639+ if self.niri.screenshot_ui.is_open() {
1640+ self.niri.screenshot_ui.set_height(change);
1641+
1642+ // FIXME: granular
1643+ self.niri.queue_redraw_all();
1644+ } else {
1645+ self.niri.layout.set_window_height(None, change);
1646+ }
1606 }1647 }
1607 Action::SetWindowHeightById { id, change } => {1648 Action::SetWindowHeightById { id, change } => {
1608 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);1649 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
@@ -3418,6 +3459,14 @@ fn allowed_during_screenshot(action: &Action) -> bool {
3418 | Action::Suspend3459 | Action::Suspend
3419 | Action::PowerOffMonitors3460 | Action::PowerOffMonitors
3420 | Action::PowerOnMonitors3461 | Action::PowerOnMonitors
3462+ // The screenshot UI can handle these.
3463+ | Action::MoveColumnLeft
3464+ | Action::MoveColumnRight
3465+ | Action::MoveWindowUp
3466+ | Action::MoveWindowDown
3467+ | Action::SetWindowWidth(_)
3468+ | Action::SetWindowHeight(_)
3469+ | Action::SetColumnWidth(_)
3421 )3470 )
3422 }3471 }
3423 3472
src/layout/floating.rsmodified+1 -1
@@ -26,7 +26,7 @@ use crate::utils::{
26 use crate::window::ResolvedWindowRules;26 use crate::window::ResolvedWindowRules;
27 27
28 /// By how many logical pixels the directional move commands move floating windows.28 /// By how many logical pixels the directional move commands move floating windows.
29-const DIRECTIONAL_MOVE_PX: f64 = 50.;29+pub const DIRECTIONAL_MOVE_PX: f64 = 50.;
30 30
31 /// Space for floating windows.31 /// Space for floating windows.
32 #[derive(Debug)]32 #[derive(Debug)]
src/ui/screenshot_ui.rsmodified+158 -0
@@ -7,6 +7,7 @@ use std::rc::Rc;
7 use anyhow::Context;7 use anyhow::Context;
8 use arrayvec::ArrayVec;8 use arrayvec::ArrayVec;
9 use niri_config::{Action, Config};9 use niri_config::{Action, Config};
10+use niri_ipc::SizeChange;
10 use pango::{Alignment, FontDescription};11 use pango::{Alignment, FontDescription};
11 use pangocairo::cairo::{self, ImageSurface};12 use pangocairo::cairo::{self, ImageSurface};
12 use smithay::backend::allocator::Fourcc;13 use smithay::backend::allocator::Fourcc;
@@ -20,6 +21,7 @@ use smithay::output::{Output, WeakOutput};
20 use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform};21 use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform};
21 22
22 use crate::animation::{Animation, Clock};23 use crate::animation::{Animation, Clock};
24+use crate::layout::floating::DIRECTIONAL_MOVE_PX;
23 use crate::niri_render_elements;25 use crate::niri_render_elements;
24 use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;26 use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
25 use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};27 use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
@@ -238,6 +240,162 @@ impl ScreenshotUi {
238 matches!(self, ScreenshotUi::Open { .. })240 matches!(self, ScreenshotUi::Open { .. })
239 }241 }
240 242
243+ pub fn move_left(&mut self) {
244+ let Self::Open {
245+ selection: (output, a, b),
246+ output_data,
247+ ..
248+ } = self
249+ else {
250+ return;
251+ };
252+
253+ let data = &output_data[output];
254+
255+ let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX);
256+ let delta = min(delta, min(a.x, b.x));
257+ a.x -= delta;
258+ b.x -= delta;
259+
260+ self.update_buffers();
261+ }
262+
263+ pub fn move_right(&mut self) {
264+ let Self::Open {
265+ selection: (output, a, b),
266+ output_data,
267+ ..
268+ } = self
269+ else {
270+ return;
271+ };
272+
273+ let data = &output_data[output];
274+
275+ let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX);
276+ let delta = min(delta, data.size.w - max(a.x, b.x) - 1);
277+ a.x += delta;
278+ b.x += delta;
279+
280+ self.update_buffers();
281+ }
282+
283+ pub fn move_up(&mut self) {
284+ let Self::Open {
285+ selection: (output, a, b),
286+ output_data,
287+ ..
288+ } = self
289+ else {
290+ return;
291+ };
292+
293+ let data = &output_data[output];
294+
295+ let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX);
296+ let delta = min(delta, min(a.y, b.y));
297+ a.y -= delta;
298+ b.y -= delta;
299+
300+ self.update_buffers();
301+ }
302+
303+ pub fn move_down(&mut self) {
304+ let Self::Open {
305+ selection: (output, a, b),
306+ output_data,
307+ ..
308+ } = self
309+ else {
310+ return;
311+ };
312+
313+ let data = &output_data[output];
314+
315+ let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX);
316+ let delta = min(delta, data.size.h - max(a.y, b.y) - 1);
317+ a.y += delta;
318+ b.y += delta;
319+
320+ self.update_buffers();
321+ }
322+
323+ pub fn set_width(&mut self, change: SizeChange) {
324+ let Self::Open {
325+ selection: (output, a, b),
326+ output_data,
327+ ..
328+ } = self
329+ else {
330+ return;
331+ };
332+
333+ let data = &output_data[output];
334+
335+ let available_size = f64::from(data.size.w);
336+ let current_size = max(a.x, b.x) + 1 - min(a.x, b.x);
337+
338+ let new_size = match change {
339+ SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed),
340+ SizeChange::SetProportion(prop) => {
341+ let prop = (prop / 100.).clamp(0., 1.);
342+ (available_size * prop).round() as i32
343+ }
344+ SizeChange::AdjustFixed(delta) => {
345+ let delta = to_physical_precise_round(data.scale, delta);
346+ current_size.saturating_add(delta)
347+ }
348+ SizeChange::AdjustProportion(delta) => {
349+ let current_prop = f64::from(current_size) / available_size;
350+ let prop = (current_prop + delta / 100.).clamp(0., 1.);
351+ (available_size * prop).round() as i32
352+ }
353+ };
354+ let new_size = new_size.clamp(1, data.size.w - min(a.x, b.x)) - 1;
355+ a.x = min(a.x, b.x);
356+ b.x = a.x + new_size;
357+
358+ self.update_buffers();
359+ }
360+
361+ pub fn set_height(&mut self, change: SizeChange) {
362+ let Self::Open {
363+ selection: (output, a, b),
364+ output_data,
365+ ..
366+ } = self
367+ else {
368+ return;
369+ };
370+
371+ let data = &output_data[output];
372+
373+ let available_size = f64::from(data.size.h);
374+ let current_size = max(a.y, b.y) + 1 - min(a.y, b.y);
375+
376+ let new_size = match change {
377+ SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed),
378+ SizeChange::SetProportion(prop) => {
379+ let prop = (prop / 100.).clamp(0., 1.);
380+ (available_size * prop).round() as i32
381+ }
382+ SizeChange::AdjustFixed(delta) => {
383+ let delta = to_physical_precise_round(data.scale, delta);
384+ current_size.saturating_add(delta)
385+ }
386+ SizeChange::AdjustProportion(delta) => {
387+ let current_prop = f64::from(current_size) / available_size;
388+ let prop = (current_prop + delta / 100.).clamp(0., 1.);
389+ (available_size * prop).round() as i32
390+ }
391+ };
392+ let new_size = new_size.clamp(1, data.size.h - min(a.y, b.y)) - 1;
393+ a.y = min(a.y, b.y);
394+ b.y = a.y + new_size;
395+
396+ self.update_buffers();
397+ }
398+
241 pub fn advance_animations(&mut self) {}399 pub fn advance_animations(&mut self) {}
242 400
243 pub fn are_animations_ongoing(&self) -> bool {401 pub fn are_animations_ongoing(&self) -> bool {