screenshot_ui: Refactor mouse down + touch slot state

This commit is contained in:
Ivan Molodetskikh
2025-05-09 15:10:00 +03:00
parent 99bf2df2b4
commit d207cd385b
+28 -22
View File
@@ -56,8 +56,7 @@ pub enum ScreenshotUi {
Open { Open {
selection: (Output, Point<i32, Physical>, Point<i32, Physical>), selection: (Output, Point<i32, Physical>, Point<i32, Physical>),
output_data: HashMap<Output, OutputData>, output_data: HashMap<Output, OutputData>,
mouse_down: bool, button: Button,
touch_slot: Option<TouchSlot>,
show_pointer: bool, show_pointer: bool,
open_anim: Animation, open_anim: Animation,
clock: Clock, clock: Clock,
@@ -65,6 +64,12 @@ pub enum ScreenshotUi {
}, },
} }
#[derive(Clone, Copy)]
enum Button {
Up,
Down { touch_slot: Option<TouchSlot> },
}
pub struct OutputData { pub struct OutputData {
size: Size<i32, Physical>, size: Size<i32, Physical>,
scale: f64, scale: f64,
@@ -89,6 +94,12 @@ niri_render_elements! {
} }
} }
impl Button {
fn is_down(&self) -> bool {
matches!(self, Self::Down { .. })
}
}
impl ScreenshotUi { impl ScreenshotUi {
pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self { pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self {
Self::Closed { Self::Closed {
@@ -194,8 +205,7 @@ impl ScreenshotUi {
*self = Self::Open { *self = Self::Open {
selection, selection,
output_data, output_data,
mouse_down: false, button: Button::Up,
touch_slot: None,
show_pointer, show_pointer,
open_anim, open_anim,
clock: clock.clone(), clock: clock.clone(),
@@ -491,7 +501,7 @@ impl ScreenshotUi {
let Self::Open { let Self::Open {
output_data, output_data,
show_pointer, show_pointer,
mouse_down, button,
open_anim, open_anim,
.. ..
} = self } = self
@@ -520,7 +530,7 @@ impl ScreenshotUi {
.to_f64() .to_f64()
.to_logical(scale); .to_logical(scale);
let alpha = if *mouse_down { 0.3 } else { 0.9 }; let alpha = if button.is_down() { 0.3 } else { 0.9 };
let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer(
buffer.clone(), buffer.clone(),
@@ -665,8 +675,7 @@ 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,
mouse_down: true, button: Button::Down { touch_slot },
touch_slot,
.. ..
} = self } = self
else { else {
@@ -690,15 +699,15 @@ impl ScreenshotUi {
let Self::Open { let Self::Open {
selection, selection,
output_data, output_data,
mouse_down, show_pointer,
touch_slot, button,
.. ..
} = self } = self
else { else {
return false; return false;
}; };
if *mouse_down { if button.is_down() {
return false; return false;
} }
@@ -706,9 +715,8 @@ impl ScreenshotUi {
return false; return false;
} }
*mouse_down = true; *button = Button::Down { touch_slot: slot };
*selection = (output, point, point); *selection = (output, point, point);
*touch_slot = slot;
self.update_buffers(); self.update_buffers();
@@ -719,24 +727,22 @@ impl ScreenshotUi {
let Self::Open { let Self::Open {
selection, selection,
output_data, output_data,
mouse_down, button,
touch_slot,
.. ..
} = self } = self
else { else {
return false; return false;
}; };
if !*mouse_down { let Button::Down { touch_slot } = *button else {
return false;
};
if touch_slot != slot {
return false; return false;
} }
if *touch_slot != slot { *button = Button::Up;
return false;
}
*mouse_down = false;
*touch_slot = None;
// Check if the resulting selection is zero-sized, and try to come up with a small // Check if the resulting selection is zero-sized, and try to come up with a small
// default rectangle. // default rectangle.