forge
Explore
Log in

Don't you dare go Hollow

Add a clickable button to capture the screenshot

Browse files at this commit

0763c7e1962bf838ae3bbc65474a829603e3f9f6Ivan MolodetskikhParent: fb5c520
Allows tablet-, touch- and mouse-only confirmation.
src/input/mod.rsmodified+18 -6
@@ -2561,8 +2561,12 @@ impl State {
25612561 self.niri.queue_redraw_all();
25622562 }
25632563 }
2564- } else if self.niri.screenshot_ui.pointer_up(None) {
2565- self.niri.queue_redraw_all();
2564+ } else if let Some(capture) = self.niri.screenshot_ui.pointer_up(None) {
2565+ if capture {
2566+ self.confirm_screenshot(true);
2567+ } else {
2568+ self.niri.queue_redraw_all();
2569+ }
25662570 }
25672571 }
25682572
@@ -3129,8 +3133,12 @@ impl State {
31293133 }
31303134 }
31313135 TabletToolTipState::Up => {
3132- if self.niri.screenshot_ui.pointer_up(None) {
3133- self.niri.queue_redraw_all();
3136+ if let Some(capture) = self.niri.screenshot_ui.pointer_up(None) {
3137+ if capture {
3138+ self.confirm_screenshot(true);
3139+ } else {
3140+ self.niri.queue_redraw_all();
3141+ }
31343142 }
31353143
31363144 tool.tip_up(event.time_msec());
@@ -3639,8 +3647,12 @@ impl State {
36393647 };
36403648 let slot = evt.slot();
36413649
3642- if self.niri.screenshot_ui.pointer_up(Some(slot)) {
3643- self.niri.queue_redraw_all();
3650+ if let Some(capture) = self.niri.screenshot_ui.pointer_up(Some(slot)) {
3651+ if capture {
3652+ self.confirm_screenshot(true);
3653+ } else {
3654+ self.niri.queue_redraw_all();
3655+ }
36443656 }
36453657
36463658 let serial = SERIAL_COUNTER.next_serial();
src/ui/screenshot_ui.rsmodified+148 -27
@@ -1,6 +1,7 @@
11 use std::cell::RefCell;
22 use std::cmp::{max, min};
33 use std::collections::HashMap;
4+use std::f64::consts::TAU;
45 use std::iter::zip;
56 use std::rc::Rc;
67
@@ -18,7 +19,7 @@ use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
1819 use smithay::backend::renderer::{ExportMem, Texture as _};
1920 use smithay::input::keyboard::{Keysym, ModifiersState};
2021 use smithay::output::{Output, WeakOutput};
21-use smithay::utils::{Physical, Point, Rectangle, Scale, Size, Transform};
22+use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform};
2223
2324 use crate::animation::{Animation, Clock};
2425 use crate::layout::floating::DIRECTIONAL_MOVE_PX;
@@ -32,6 +33,7 @@ use crate::utils::to_physical_precise_round;
3233 const SELECTION_BORDER: i32 = 2;
3334
3435 const PADDING: i32 = 8;
36+const RADIUS: i32 = 16;
3537 const FONT: &str = "sans 14px";
3638 const BORDER: i32 = 4;
3739 const TEXT_HIDE_P: &str =
@@ -64,10 +66,13 @@ pub enum ScreenshotUi {
6466 },
6567 }
6668
67-#[derive(Clone, Copy)]
68-enum Button {
69+pub enum Button {
6970 Up,
70- Down { touch_slot: Option<TouchSlot> },
71+ Down {
72+ touch_slot: Option<TouchSlot>,
73+ on_capture_button: bool,
74+ last_pos: (Output, Point<i32, Physical>),
75+ },
7176 }
7277
7378 pub struct OutputData {
@@ -98,6 +103,16 @@ impl Button {
98103 fn is_down(&self) -> bool {
99104 matches!(self, Self::Down { .. })
100105 }
106+
107+ fn is_dragging_selection(&self) -> bool {
108+ matches!(
109+ self,
110+ Self::Down {
111+ on_capture_button: false,
112+ ..
113+ }
114+ )
115+ }
101116 }
102117
103118 impl ScreenshotUi {
@@ -521,17 +536,15 @@ impl ScreenshotUi {
521536 // The help panel goes on top.
522537 if let Some((show, hide)) = &output_data.panel {
523538 let buffer = if *show_pointer { hide } else { show };
524-
525- let size = buffer.texture().size();
526- let padding: i32 = to_physical_precise_round(scale, PADDING);
527- let x = max(0, (output_data.size.w - size.w) / 2);
528- let y = max(0, output_data.size.h - size.h - padding * 2);
529- let location = Point::<_, Physical>::from((x, y))
539+ let alpha = if button.is_dragging_selection() {
540+ 0.3
541+ } else {
542+ 0.9
543+ };
544+ let location = panel_location(output_data, buffer.texture().size())
530545 .to_f64()
531546 .to_logical(scale);
532547
533- let alpha = if button.is_down() { 0.3 } else { 0.9 };
534-
535548 let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer(
536549 buffer.clone(),
537550 location,
@@ -675,7 +688,12 @@ impl ScreenshotUi {
675688 pub fn pointer_motion(&mut self, point: Point<i32, Physical>, slot: Option<TouchSlot>) {
676689 let Self::Open {
677690 selection,
678- button: Button::Down { touch_slot },
691+ button:
692+ Button::Down {
693+ touch_slot,
694+ on_capture_button,
695+ last_pos,
696+ },
679697 ..
680698 } = self
681699 else {
@@ -686,6 +704,12 @@ impl ScreenshotUi {
686704 return;
687705 }
688706
707+ last_pos.1 = point;
708+
709+ if *on_capture_button {
710+ return;
711+ }
712+
689713 selection.2 = point;
690714 self.update_buffers();
691715 }
@@ -711,11 +735,30 @@ impl ScreenshotUi {
711735 return false;
712736 }
713737
714- if !output_data.contains_key(&output) {
738+ let Some(output_data) = output_data.get(&output) else {
715739 return false;
740+ };
741+
742+ if let Some((show, hide)) = &output_data.panel {
743+ let buffer = if *show_pointer { hide } else { show };
744+ let panel_size = buffer.texture().size();
745+ let location = panel_location(output_data, panel_size);
746+
747+ if is_within_capture_button(output_data.scale, panel_size, point - location) {
748+ *button = Button::Down {
749+ touch_slot: slot,
750+ on_capture_button: true,
751+ last_pos: (output, point),
752+ };
753+ return false;
754+ }
716755 }
717756
718- *button = Button::Down { touch_slot: slot };
757+ *button = Button::Down {
758+ touch_slot: slot,
759+ on_capture_button: false,
760+ last_pos: (output.clone(), point),
761+ };
719762 *selection = (output, point, point);
720763
721764 self.update_buffers();
@@ -723,27 +766,54 @@ impl ScreenshotUi {
723766 true
724767 }
725768
726- pub fn pointer_up(&mut self, slot: Option<TouchSlot>) -> bool {
769+ pub fn pointer_up(&mut self, slot: Option<TouchSlot>) -> Option<bool> {
727770 let Self::Open {
728771 selection,
729772 output_data,
730773 button,
774+ show_pointer,
731775 ..
732776 } = self
733777 else {
734- return false;
778+ return None;
735779 };
736780
737- let Button::Down { touch_slot } = *button else {
738- return false;
781+ let Button::Down {
782+ touch_slot,
783+ on_capture_button,
784+ ref last_pos,
785+ } = *button
786+ else {
787+ return None;
739788 };
740789
741790 if touch_slot != slot {
742- return false;
791+ return None;
743792 }
744793
794+ let last_pos = last_pos.clone();
745795 *button = Button::Up;
746796
797+ // Check if we released still on the capture button.
798+ if on_capture_button {
799+ let (output, point) = last_pos;
800+
801+ #[allow(clippy::question_mark)]
802+ let Some(output_data) = output_data.get(&output) else {
803+ return None;
804+ };
805+
806+ if let Some((show, hide)) = &output_data.panel {
807+ let buffer = if *show_pointer { hide } else { show };
808+ let panel_size = buffer.texture().size();
809+ let location = panel_location(output_data, panel_size);
810+
811+ if is_within_capture_button(output_data.scale, panel_size, point - location) {
812+ return Some(true);
813+ }
814+ }
815+ }
816+
747817 // Check if the resulting selection is zero-sized, and try to come up with a small
748818 // default rectangle.
749819 let (output, a, b) = selection;
@@ -762,7 +832,7 @@ impl ScreenshotUi {
762832
763833 self.update_buffers();
764834
765- true
835+ Some(false)
766836 }
767837 }
768838
@@ -853,6 +923,29 @@ pub fn rect_from_corner_points(
853923 Rectangle::from_extremities((x1, y1), (x2 + 1, y2 + 1))
854924 }
855925
926+fn panel_location(output_data: &OutputData, panel_size: Size<i32, Buffer>) -> Point<i32, Physical> {
927+ let scale = output_data.scale;
928+ let padding: i32 = to_physical_precise_round(scale, PADDING);
929+ let x = max(0, (output_data.size.w - panel_size.w) / 2);
930+ let y = max(0, output_data.size.h - panel_size.h - padding * 2);
931+ Point::from((x, y))
932+}
933+
934+fn is_within_capture_button(
935+ scale: f64,
936+ panel_size: Size<i32, Buffer>,
937+ pos_within_panel: Point<i32, Physical>,
938+) -> bool {
939+ let padding: i32 = to_physical_precise_round(scale, PADDING);
940+ let radius = to_physical_precise_round::<i32>(scale, RADIUS) - 2;
941+
942+ let xc = padding + radius;
943+ let yc = panel_size.h / 2;
944+ let pos = pos_within_panel;
945+
946+ (pos.x - xc) * (pos.x - xc) + (pos.y - yc) * (pos.y - yc) <= radius * radius
947+}
948+
856949 fn render_panel(
857950 renderer: &mut GlesRenderer,
858951 scale: f64,
@@ -861,6 +954,11 @@ fn render_panel(
861954 let _span = tracy_client::span!("screenshot_ui::render_panel");
862955
863956 let padding: i32 = to_physical_precise_round(scale, PADDING);
957+ // Keep the border width even to avoid blurry edges.
958+ let border_width = (f64::from(BORDER) / 2. * scale).round() * 2.;
959+ let half_border_width = (border_width / 2.) as i32;
960+ let radius: i32 = to_physical_precise_round(scale, RADIUS);
961+ let circle_stroke: f64 = to_physical_precise_round(scale, 2.);
864962
865963 // Add 2 px of spacing to separate the backgrounds of the "Space" and "P" keys.
866964 let spacing = to_physical_precise_round::<i32>(scale, 2) * 1024;
@@ -873,12 +971,14 @@ fn render_panel(
873971 let layout = pangocairo::functions::create_layout(&cr);
874972 layout.context().set_round_glyph_positions(false);
875973 layout.set_font_description(Some(&font));
876- layout.set_alignment(Alignment::Center);
974+ layout.set_alignment(Alignment::Left);
877975 layout.set_markup(text);
878976 layout.set_spacing(spacing);
879977
880978 let (mut width, mut height) = layout.pixel_size();
881- width += padding * 2;
979+
980+ width += padding + radius * 2 + padding - half_border_width + padding;
981+ height = max(height, radius * 2);
882982 height += padding * 2;
883983
884984 let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?;
@@ -886,11 +986,33 @@ fn render_panel(
886986 cr.set_source_rgb(0.1, 0.1, 0.1);
887987 cr.paint()?;
888988
889- cr.move_to(padding.into(), padding.into());
989+ let padding = f64::from(padding);
990+ let half_border_width = f64::from(half_border_width);
991+ let r = f64::from(radius);
992+
993+ let yc = f64::from(height / 2);
994+
995+ cr.new_sub_path();
996+ cr.arc(padding + r, yc, r, 0., TAU);
997+ cr.set_source_rgb(1., 1., 1.);
998+ cr.fill()?;
999+
1000+ cr.new_sub_path();
1001+ cr.arc(padding + r, yc, r - circle_stroke, 0., TAU);
1002+ cr.set_source_rgb(0.1, 0.1, 0.1);
1003+ cr.fill()?;
1004+
1005+ cr.new_sub_path();
1006+ cr.arc(padding + r, yc, r - circle_stroke * 2., 0., TAU);
1007+ cr.set_source_rgb(1., 1., 1.);
1008+ cr.fill()?;
1009+
1010+ cr.move_to(padding + r * 2. + padding - half_border_width, padding);
1011+
8901012 let layout = pangocairo::functions::create_layout(&cr);
8911013 layout.context().set_round_glyph_positions(false);
8921014 layout.set_font_description(Some(&font));
893- layout.set_alignment(Alignment::Center);
1015+ layout.set_alignment(Alignment::Left);
8941016 layout.set_markup(text);
8951017 layout.set_spacing(spacing);
8961018
@@ -903,8 +1025,7 @@ fn render_panel(
9031025 cr.line_to(0., height.into());
9041026 cr.line_to(0., 0.);
9051027 cr.set_source_rgb(0.3, 0.3, 0.3);
906- // Keep the border width even to avoid blurry edges.
907- cr.set_line_width((f64::from(BORDER) / 2. * scale).round() * 2.);
1028+ cr.set_line_width(border_width);
9081029 cr.stroke()?;
9091030 drop(cr);
9101031