screenshot_ui: Move selection with a second touch too
Browse files at this commit97dfd2b1a00bcb9b765a8fa92ce39d2e0c79abc2Parent: 730eab01 changed file +56 -10
- src/ui/screenshot_ui.rs +56 -10
src/ui/screenshot_ui.rsmodified+56 -10
| @@ -66,14 +66,22 @@ pub enum ScreenshotUi { | ||
| 66 | 66 | }, |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | +/// State for moving the selection (as opposed to just drawing). | |
| 70 | +pub struct MoveState { | |
| 71 | + // Cursor offset from selection.1 when starting the move. | |
| 72 | + pointer_offset: Point<i32, Physical>, | |
| 73 | + // If the move is initiated by a touch, this is the slot. If `None`, the move was initiated by | |
| 74 | + // holding Space. | |
| 75 | + touch_slot: Option<TouchSlot>, | |
| 76 | +} | |
| 77 | + | |
| 69 | 78 | pub enum Button { |
| 70 | 79 | Up, |
| 71 | 80 | Down { |
| 72 | 81 | touch_slot: Option<TouchSlot>, |
| 73 | 82 | on_capture_button: bool, |
| 74 | 83 | last_pos: (Output, Point<i32, Physical>), |
| 75 | - /// Offset from the selection.1 corner to cursor grab point when moving the selection. | |
| 76 | - move_cursor_offset: Option<Point<i32, Physical>>, | |
| 84 | + move_state: Option<MoveState>, | |
| 77 | 85 | }, |
| 78 | 86 | } |
| 79 | 87 | |
| @@ -274,7 +282,7 @@ impl ScreenshotUi { | ||
| 274 | 282 | selection, |
| 275 | 283 | button: |
| 276 | 284 | Button::Down { |
| 277 | - move_cursor_offset, | |
| 285 | + move_state, | |
| 278 | 286 | last_pos, |
| 279 | 287 | .. |
| 280 | 288 | }, |
| @@ -282,9 +290,20 @@ impl ScreenshotUi { | ||
| 282 | 290 | } = self |
| 283 | 291 | { |
| 284 | 292 | if down { |
| 285 | - *move_cursor_offset = Some(last_pos.1 - selection.1); | |
| 293 | + if move_state.is_none() { | |
| 294 | + *move_state = Some(MoveState { | |
| 295 | + pointer_offset: last_pos.1 - selection.1, | |
| 296 | + touch_slot: None, | |
| 297 | + }); | |
| 298 | + } | |
| 286 | 299 | } else { |
| 287 | - *move_cursor_offset = None; | |
| 300 | + // Only clear if moving with Space. | |
| 301 | + if let Some(MoveState { | |
| 302 | + touch_slot: None, .. | |
| 303 | + }) = move_state | |
| 304 | + { | |
| 305 | + *move_state = None; | |
| 306 | + } | |
| 288 | 307 | } |
| 289 | 308 | } |
| 290 | 309 | } |
| @@ -790,7 +809,7 @@ impl ScreenshotUi { | ||
| 790 | 809 | touch_slot, |
| 791 | 810 | on_capture_button, |
| 792 | 811 | last_pos, |
| 793 | - move_cursor_offset, | |
| 812 | + move_state, | |
| 794 | 813 | }, |
| 795 | 814 | .. |
| 796 | 815 | } = self |
| @@ -808,9 +827,9 @@ impl ScreenshotUi { | ||
| 808 | 827 | return; |
| 809 | 828 | } |
| 810 | 829 | |
| 811 | - if let Some(cursor_offset) = move_cursor_offset { | |
| 830 | + if let Some(move_state) = move_state { | |
| 812 | 831 | // The cursor offset is relative to selection.1. |
| 813 | - let delta = point - (selection.1 + *cursor_offset); | |
| 832 | + let delta = point - (selection.1 + move_state.pointer_offset); | |
| 814 | 833 | |
| 815 | 834 | let desired = rect_from_corner_points(selection.1 + delta, selection.2 + delta); |
| 816 | 835 | let bounds = Rectangle::from_size(output_data[&selection.0].size - desired.size); |
| @@ -843,6 +862,24 @@ impl ScreenshotUi { | ||
| 843 | 862 | return false; |
| 844 | 863 | }; |
| 845 | 864 | |
| 865 | + // Check if this is a second touch (different slot) while already dragging. | |
| 866 | + if let Some(new_slot) = slot { | |
| 867 | + if let Button::Down { | |
| 868 | + on_capture_button: false, | |
| 869 | + move_state, | |
| 870 | + last_pos, | |
| 871 | + .. | |
| 872 | + } = button | |
| 873 | + { | |
| 874 | + if move_state.is_none() { | |
| 875 | + *move_state = Some(MoveState { | |
| 876 | + pointer_offset: last_pos.1 - selection.1, | |
| 877 | + touch_slot: Some(new_slot), | |
| 878 | + }); | |
| 879 | + } | |
| 880 | + } | |
| 881 | + } | |
| 882 | + | |
| 846 | 883 | if button.is_down() { |
| 847 | 884 | return false; |
| 848 | 885 | } |
| @@ -861,7 +898,7 @@ impl ScreenshotUi { | ||
| 861 | 898 | touch_slot: slot, |
| 862 | 899 | on_capture_button: true, |
| 863 | 900 | last_pos: (output, point), |
| 864 | - move_cursor_offset: None, | |
| 901 | + move_state: None, | |
| 865 | 902 | }; |
| 866 | 903 | return false; |
| 867 | 904 | } |
| @@ -871,7 +908,7 @@ impl ScreenshotUi { | ||
| 871 | 908 | touch_slot: slot, |
| 872 | 909 | on_capture_button: false, |
| 873 | 910 | last_pos: (output.clone(), point), |
| 874 | - move_cursor_offset: None, | |
| 911 | + move_state: None, | |
| 875 | 912 | }; |
| 876 | 913 | *selection = (output, point, point); |
| 877 | 914 | |
| @@ -896,12 +933,21 @@ impl ScreenshotUi { | ||
| 896 | 933 | touch_slot, |
| 897 | 934 | on_capture_button, |
| 898 | 935 | ref last_pos, |
| 936 | + ref mut move_state, | |
| 899 | 937 | .. |
| 900 | 938 | } = *button |
| 901 | 939 | else { |
| 902 | 940 | return None; |
| 903 | 941 | }; |
| 904 | 942 | |
| 943 | + // Check if this is a move touch and if so, stop the move. | |
| 944 | + if let Some(state) = move_state { | |
| 945 | + if state.touch_slot.is_some_and(|m_slot| Some(m_slot) == slot) { | |
| 946 | + *move_state = None; | |
| 947 | + return None; | |
| 948 | + } | |
| 949 | + }; | |
| 950 | + | |
| 905 | 951 | if touch_slot != slot { |
| 906 | 952 | return None; |
| 907 | 953 | } |