From 7dc015e16b30be457a227601de85f085e5af1dda Mon Sep 17 00:00:00 2001 From: nyx Date: Sat, 29 Mar 2025 03:40:21 -0400 Subject: screenshot: make selection area modifiable via move/resize keybinds (#1279) * screenshot: make selection area modifiable via keybinds * input: run fmt * Reimplement screenshot UI binds in a better way --------- Co-authored-by: Ivan Molodetskikh --- src/ui/screenshot_ui.rs | 158 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) (limited to 'src/ui') diff --git a/src/ui/screenshot_ui.rs b/src/ui/screenshot_ui.rs index 14675f7b..46a8d1b6 100644 --- a/src/ui/screenshot_ui.rs +++ b/src/ui/screenshot_ui.rs @@ -7,6 +7,7 @@ use std::rc::Rc; use anyhow::Context; use arrayvec::ArrayVec; use niri_config::{Action, Config}; +use niri_ipc::SizeChange; use pango::{Alignment, FontDescription}; use pangocairo::cairo::{self, ImageSurface}; use smithay::backend::allocator::Fourcc; @@ -20,6 +21,7 @@ use smithay::output::{Output, WeakOutput}; use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform}; use crate::animation::{Animation, Clock}; +use crate::layout::floating::DIRECTIONAL_MOVE_PX; use crate::niri_render_elements; use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; @@ -238,6 +240,162 @@ impl ScreenshotUi { matches!(self, ScreenshotUi::Open { .. }) } + pub fn move_left(&mut self) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); + let delta = min(delta, min(a.x, b.x)); + a.x -= delta; + b.x -= delta; + + self.update_buffers(); + } + + pub fn move_right(&mut self) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); + let delta = min(delta, data.size.w - max(a.x, b.x) - 1); + a.x += delta; + b.x += delta; + + self.update_buffers(); + } + + pub fn move_up(&mut self) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); + let delta = min(delta, min(a.y, b.y)); + a.y -= delta; + b.y -= delta; + + self.update_buffers(); + } + + pub fn move_down(&mut self) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); + let delta = min(delta, data.size.h - max(a.y, b.y) - 1); + a.y += delta; + b.y += delta; + + self.update_buffers(); + } + + pub fn set_width(&mut self, change: SizeChange) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let available_size = f64::from(data.size.w); + let current_size = max(a.x, b.x) + 1 - min(a.x, b.x); + + let new_size = match change { + SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed), + SizeChange::SetProportion(prop) => { + let prop = (prop / 100.).clamp(0., 1.); + (available_size * prop).round() as i32 + } + SizeChange::AdjustFixed(delta) => { + let delta = to_physical_precise_round(data.scale, delta); + current_size.saturating_add(delta) + } + SizeChange::AdjustProportion(delta) => { + let current_prop = f64::from(current_size) / available_size; + let prop = (current_prop + delta / 100.).clamp(0., 1.); + (available_size * prop).round() as i32 + } + }; + let new_size = new_size.clamp(1, data.size.w - min(a.x, b.x)) - 1; + a.x = min(a.x, b.x); + b.x = a.x + new_size; + + self.update_buffers(); + } + + pub fn set_height(&mut self, change: SizeChange) { + let Self::Open { + selection: (output, a, b), + output_data, + .. + } = self + else { + return; + }; + + let data = &output_data[output]; + + let available_size = f64::from(data.size.h); + let current_size = max(a.y, b.y) + 1 - min(a.y, b.y); + + let new_size = match change { + SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed), + SizeChange::SetProportion(prop) => { + let prop = (prop / 100.).clamp(0., 1.); + (available_size * prop).round() as i32 + } + SizeChange::AdjustFixed(delta) => { + let delta = to_physical_precise_round(data.scale, delta); + current_size.saturating_add(delta) + } + SizeChange::AdjustProportion(delta) => { + let current_prop = f64::from(current_size) / available_size; + let prop = (current_prop + delta / 100.).clamp(0., 1.); + (available_size * prop).round() as i32 + } + }; + let new_size = new_size.clamp(1, data.size.h - min(a.y, b.y)) - 1; + a.y = min(a.y, b.y); + b.y = a.y + new_size; + + self.update_buffers(); + } + pub fn advance_animations(&mut self) {} pub fn are_animations_ongoing(&self) -> bool { -- cgit