Add a clickable button to capture the screenshot
Browse files at this commit0763c7e1962bf838ae3bbc65474a829603e3f9f6Parent: fb5c5202 changed files +166 -33
- src/input/mod.rs +18 -6
- src/ui/screenshot_ui.rs +148 -27
src/input/mod.rsmodified+18 -6
| @@ -2561,8 +2561,12 @@ impl State { | ||
| 2561 | 2561 | self.niri.queue_redraw_all(); |
| 2562 | 2562 | } |
| 2563 | 2563 | } |
| 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 | + } | |
| 2566 | 2570 | } |
| 2567 | 2571 | } |
| 2568 | 2572 | |
| @@ -3129,8 +3133,12 @@ impl State { | ||
| 3129 | 3133 | } |
| 3130 | 3134 | } |
| 3131 | 3135 | 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 | + } | |
| 3134 | 3142 | } |
| 3135 | 3143 | |
| 3136 | 3144 | tool.tip_up(event.time_msec()); |
| @@ -3639,8 +3647,12 @@ impl State { | ||
| 3639 | 3647 | }; |
| 3640 | 3648 | let slot = evt.slot(); |
| 3641 | 3649 | |
| 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 | + } | |
| 3644 | 3656 | } |
| 3645 | 3657 | |
| 3646 | 3658 | let serial = SERIAL_COUNTER.next_serial(); |
src/ui/screenshot_ui.rsmodified+148 -27
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | use std::cell::RefCell; |
| 2 | 2 | use std::cmp::{max, min}; |
| 3 | 3 | use std::collections::HashMap; |
| 4 | +use std::f64::consts::TAU; | |
| 4 | 5 | use std::iter::zip; |
| 5 | 6 | use std::rc::Rc; |
| 6 | 7 | |
| @@ -18,7 +19,7 @@ use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | ||
| 18 | 19 | use smithay::backend::renderer::{ExportMem, Texture as _}; |
| 19 | 20 | use smithay::input::keyboard::{Keysym, ModifiersState}; |
| 20 | 21 | 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}; | |
| 22 | 23 | |
| 23 | 24 | use crate::animation::{Animation, Clock}; |
| 24 | 25 | use crate::layout::floating::DIRECTIONAL_MOVE_PX; |
| @@ -32,6 +33,7 @@ use crate::utils::to_physical_precise_round; | ||
| 32 | 33 | const SELECTION_BORDER: i32 = 2; |
| 33 | 34 | |
| 34 | 35 | const PADDING: i32 = 8; |
| 36 | +const RADIUS: i32 = 16; | |
| 35 | 37 | const FONT: &str = "sans 14px"; |
| 36 | 38 | const BORDER: i32 = 4; |
| 37 | 39 | const TEXT_HIDE_P: &str = |
| @@ -64,10 +66,13 @@ pub enum ScreenshotUi { | ||
| 64 | 66 | }, |
| 65 | 67 | } |
| 66 | 68 | |
| 67 | -#[derive(Clone, Copy)] | |
| 68 | -enum Button { | |
| 69 | +pub enum Button { | |
| 69 | 70 | 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 | + }, | |
| 71 | 76 | } |
| 72 | 77 | |
| 73 | 78 | pub struct OutputData { |
| @@ -98,6 +103,16 @@ impl Button { | ||
| 98 | 103 | fn is_down(&self) -> bool { |
| 99 | 104 | matches!(self, Self::Down { .. }) |
| 100 | 105 | } |
| 106 | + | |
| 107 | + fn is_dragging_selection(&self) -> bool { | |
| 108 | + matches!( | |
| 109 | + self, | |
| 110 | + Self::Down { | |
| 111 | + on_capture_button: false, | |
| 112 | + .. | |
| 113 | + } | |
| 114 | + ) | |
| 115 | + } | |
| 101 | 116 | } |
| 102 | 117 | |
| 103 | 118 | impl ScreenshotUi { |
| @@ -521,17 +536,15 @@ impl ScreenshotUi { | ||
| 521 | 536 | // The help panel goes on top. |
| 522 | 537 | if let Some((show, hide)) = &output_data.panel { |
| 523 | 538 | 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()) | |
| 530 | 545 | .to_f64() |
| 531 | 546 | .to_logical(scale); |
| 532 | 547 | |
| 533 | - let alpha = if button.is_down() { 0.3 } else { 0.9 }; | |
| 534 | - | |
| 535 | 548 | let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 536 | 549 | buffer.clone(), |
| 537 | 550 | location, |
| @@ -675,7 +688,12 @@ impl ScreenshotUi { | ||
| 675 | 688 | pub fn pointer_motion(&mut self, point: Point<i32, Physical>, slot: Option<TouchSlot>) { |
| 676 | 689 | let Self::Open { |
| 677 | 690 | selection, |
| 678 | - button: Button::Down { touch_slot }, | |
| 691 | + button: | |
| 692 | + Button::Down { | |
| 693 | + touch_slot, | |
| 694 | + on_capture_button, | |
| 695 | + last_pos, | |
| 696 | + }, | |
| 679 | 697 | .. |
| 680 | 698 | } = self |
| 681 | 699 | else { |
| @@ -686,6 +704,12 @@ impl ScreenshotUi { | ||
| 686 | 704 | return; |
| 687 | 705 | } |
| 688 | 706 | |
| 707 | + last_pos.1 = point; | |
| 708 | + | |
| 709 | + if *on_capture_button { | |
| 710 | + return; | |
| 711 | + } | |
| 712 | + | |
| 689 | 713 | selection.2 = point; |
| 690 | 714 | self.update_buffers(); |
| 691 | 715 | } |
| @@ -711,11 +735,30 @@ impl ScreenshotUi { | ||
| 711 | 735 | return false; |
| 712 | 736 | } |
| 713 | 737 | |
| 714 | - if !output_data.contains_key(&output) { | |
| 738 | + let Some(output_data) = output_data.get(&output) else { | |
| 715 | 739 | 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 | + } | |
| 716 | 755 | } |
| 717 | 756 | |
| 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 | + }; | |
| 719 | 762 | *selection = (output, point, point); |
| 720 | 763 | |
| 721 | 764 | self.update_buffers(); |
| @@ -723,27 +766,54 @@ impl ScreenshotUi { | ||
| 723 | 766 | true |
| 724 | 767 | } |
| 725 | 768 | |
| 726 | - pub fn pointer_up(&mut self, slot: Option<TouchSlot>) -> bool { | |
| 769 | + pub fn pointer_up(&mut self, slot: Option<TouchSlot>) -> Option<bool> { | |
| 727 | 770 | let Self::Open { |
| 728 | 771 | selection, |
| 729 | 772 | output_data, |
| 730 | 773 | button, |
| 774 | + show_pointer, | |
| 731 | 775 | .. |
| 732 | 776 | } = self |
| 733 | 777 | else { |
| 734 | - return false; | |
| 778 | + return None; | |
| 735 | 779 | }; |
| 736 | 780 | |
| 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; | |
| 739 | 788 | }; |
| 740 | 789 | |
| 741 | 790 | if touch_slot != slot { |
| 742 | - return false; | |
| 791 | + return None; | |
| 743 | 792 | } |
| 744 | 793 | |
| 794 | + let last_pos = last_pos.clone(); | |
| 745 | 795 | *button = Button::Up; |
| 746 | 796 | |
| 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 | + | |
| 747 | 817 | // Check if the resulting selection is zero-sized, and try to come up with a small |
| 748 | 818 | // default rectangle. |
| 749 | 819 | let (output, a, b) = selection; |
| @@ -762,7 +832,7 @@ impl ScreenshotUi { | ||
| 762 | 832 | |
| 763 | 833 | self.update_buffers(); |
| 764 | 834 | |
| 765 | - true | |
| 835 | + Some(false) | |
| 766 | 836 | } |
| 767 | 837 | } |
| 768 | 838 | |
| @@ -853,6 +923,29 @@ pub fn rect_from_corner_points( | ||
| 853 | 923 | Rectangle::from_extremities((x1, y1), (x2 + 1, y2 + 1)) |
| 854 | 924 | } |
| 855 | 925 | |
| 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 | + | |
| 856 | 949 | fn render_panel( |
| 857 | 950 | renderer: &mut GlesRenderer, |
| 858 | 951 | scale: f64, |
| @@ -861,6 +954,11 @@ fn render_panel( | ||
| 861 | 954 | let _span = tracy_client::span!("screenshot_ui::render_panel"); |
| 862 | 955 | |
| 863 | 956 | 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.); | |
| 864 | 962 | |
| 865 | 963 | // Add 2 px of spacing to separate the backgrounds of the "Space" and "P" keys. |
| 866 | 964 | let spacing = to_physical_precise_round::<i32>(scale, 2) * 1024; |
| @@ -873,12 +971,14 @@ fn render_panel( | ||
| 873 | 971 | let layout = pangocairo::functions::create_layout(&cr); |
| 874 | 972 | layout.context().set_round_glyph_positions(false); |
| 875 | 973 | layout.set_font_description(Some(&font)); |
| 876 | - layout.set_alignment(Alignment::Center); | |
| 974 | + layout.set_alignment(Alignment::Left); | |
| 877 | 975 | layout.set_markup(text); |
| 878 | 976 | layout.set_spacing(spacing); |
| 879 | 977 | |
| 880 | 978 | 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); | |
| 882 | 982 | height += padding * 2; |
| 883 | 983 | |
| 884 | 984 | let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; |
| @@ -886,11 +986,33 @@ fn render_panel( | ||
| 886 | 986 | cr.set_source_rgb(0.1, 0.1, 0.1); |
| 887 | 987 | cr.paint()?; |
| 888 | 988 | |
| 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 | + | |
| 890 | 1012 | let layout = pangocairo::functions::create_layout(&cr); |
| 891 | 1013 | layout.context().set_round_glyph_positions(false); |
| 892 | 1014 | layout.set_font_description(Some(&font)); |
| 893 | - layout.set_alignment(Alignment::Center); | |
| 1015 | + layout.set_alignment(Alignment::Left); | |
| 894 | 1016 | layout.set_markup(text); |
| 895 | 1017 | layout.set_spacing(spacing); |
| 896 | 1018 | |
| @@ -903,8 +1025,7 @@ fn render_panel( | ||
| 903 | 1025 | cr.line_to(0., height.into()); |
| 904 | 1026 | cr.line_to(0., 0.); |
| 905 | 1027 | 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); | |
| 908 | 1029 | cr.stroke()?; |
| 909 | 1030 | drop(cr); |
| 910 | 1031 |