feat(touch): long press opens context menus as right click
- unclaimed gpui long press becomes right mouse down/up at the touch point - claims the long press, so release is no tap and the finger does not pan - installed once in AppShell, elements with own long press handling win - test: long press right-clicks without click or scroll Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -2,6 +2,7 @@ pub mod card_chart;
2
2
pub mod channel_tabs;
3
3
pub mod draft_style;
4
4
pub mod layout;
5
pub mod long_press;
5
6
pub mod reaction_proposal_pill;
6
7
pub mod render;
7
8
pub mod scroll_edge;
@@ -2,6 +2,7 @@ pub mod card_chart;2
pub mod channel_tabs;2
pub mod channel_tabs;3
pub mod draft_style;3
pub mod draft_style;4
pub mod layout;4
pub mod layout;5
pub mod long_press;5
pub mod reaction_proposal_pill;6
pub mod reaction_proposal_pill;6
pub mod render;7
pub mod render;7
pub mod scroll_edge;8
pub mod scroll_edge;@@ -0,0 +1,40 @@
1
use gpui_kit::*;
2
3
pub fn long_press_right_click() -> impl IntoElement {
4
canvas(
5
|_, _, _| {},
6
|_, _, window, _| {
7
window.on_mouse_event(|event: &LongPressEvent, phase, window, cx| {
8
if phase != DispatchPhase::Bubble
9
|| event.phase != TouchPhase::Started
10
|| window.default_prevented()
11
{
12
return;
13
}
14
window.prevent_default();
15
let position = event.start_position;
16
window.defer(cx, move |window, cx| {
17
window.dispatch_event(
18
PlatformInput::MouseDown(MouseDownEvent {
19
button: MouseButton::Right,
20
position,
21
modifiers: Modifiers::default(),
22
click_count: 1,
23
first_mouse: false,
24
}),
25
cx,
26
);
27
window.dispatch_event(
28
PlatformInput::MouseUp(MouseUpEvent {
29
button: MouseButton::Right,
30
position,
31
modifiers: Modifiers::default(),
32
click_count: 1,
33
}),
34
cx,
35
);
36
});
37
});
38
},
39
)
40
}
@@ -0,0 +1,40 @@1
use gpui_kit::*;2
3
pub fn long_press_right_click() -> impl IntoElement {4
canvas(5
|_, _, _| {},6
|_, _, window, _| {7
window.on_mouse_event(|event: &LongPressEvent, phase, window, cx| {8
if phase != DispatchPhase::Bubble9
|| event.phase != TouchPhase::Started10
|| window.default_prevented()11
{12
return;13
}14
window.prevent_default();15
let position = event.start_position;16
window.defer(cx, move |window, cx| {17
window.dispatch_event(18
PlatformInput::MouseDown(MouseDownEvent {19
button: MouseButton::Right,20
position,21
modifiers: Modifiers::default(),22
click_count: 1,23
first_mouse: false,24
}),25
cx,26
);27
window.dispatch_event(28
PlatformInput::MouseUp(MouseUpEvent {29
button: MouseButton::Right,30
position,31
modifiers: Modifiers::default(),32
click_count: 1,33
}),34
cx,35
);36
});37
});38
},39
)40
}@@ -2,36 +2,50 @@ use std::cell::Cell;
2
2
use std::rc::Rc;
3
3
use std::time::Duration;
4
4
5
use crate::long_press::long_press_right_click;
5
6
use gpui_kit::{
6
Context, InteractiveElement as _, IntoElement, ListAlignment, ListState, ParentElement as _,
7
Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _, TestAppContext, TouchEvent,
8
TouchId, TouchPhase, VisualTestContext, Window, div, list, point, px,
7
Context, InteractiveElement as _, IntoElement, ListAlignment, ListState, MouseButton,
8
ParentElement as _, Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _,
9
TestAppContext, TouchEvent, TouchId, TouchPhase, VisualTestContext, Window, div, list, point,
10
px,
9
11
};
10
12
11
13
const ROW_HEIGHT: f32 = 40.;
12
14
const ROW_COUNT: usize = 60;
13
15
16
#[derive(Clone, Default)]
17
struct Clicks {
18
left: Rc<Cell<usize>>,
19
right: Rc<Cell<usize>>,
20
}
21
14
22
struct ScrollContainer {
15
23
scroll_handle: ScrollHandle,
16
clicks: Rc<Cell<usize>>,
24
clicks: Clicks,
17
25
}
18
26
19
27
impl Render for ScrollContainer {
20
28
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
21
29
let clicks = self.clicks.clone();
22
div()
23
.id("scroll-container")
24
.size_full()
25
.overflow_y_scroll()
26
.track_scroll(&self.scroll_handle)
27
.children((0..ROW_COUNT).map(|row_index| {
28
let clicks = clicks.clone();
29
div()
30
.id(row_index)
31
.h(px(ROW_HEIGHT))
32
.w_full()
33
.on_click(move |_, _, _| clicks.set(clicks.get() + 1))
34
}))
30
div().size_full().child(long_press_right_click()).child(
31
div()
32
.id("scroll-container")
33
.size_full()
34
.overflow_y_scroll()
35
.track_scroll(&self.scroll_handle)
36
.children((0..ROW_COUNT).map(|row_index| {
37
let left = clicks.left.clone();
38
let right = clicks.right.clone();
39
div()
40
.id(row_index)
41
.h(px(ROW_HEIGHT))
42
.w_full()
43
.on_click(move |_, _, _| left.set(left.get() + 1))
44
.on_mouse_down(MouseButton::Right, move |_, _, _| {
45
right.set(right.get() + 1)
46
})
47
})),
48
)
35
49
}
36
50
}
37
51
@@ -91,10 +105,10 @@ impl Finger<'_> {
91
105
92
106
fn open_scroll_container(
93
107
cx: &mut TestAppContext,
94
) -> (ScrollHandle, Rc<Cell<usize>>, &mut VisualTestContext) {
108
) -> (ScrollHandle, Clicks, &mut VisualTestContext) {
95
109
cx.update(gpui_kit::init);
96
110
let scroll_handle = ScrollHandle::new();
97
let clicks = Rc::new(Cell::new(0));
111
let clicks = Clicks::default();
98
112
let (_, window_cx) = cx.add_window_view({
99
113
let scroll_handle = scroll_handle.clone();
100
114
let clicks = clicks.clone();
@@ -132,7 +146,7 @@ fn finger_pan_scrolls_the_container_under_the_finger_by_pixels(cx: &mut TestAppC
132
146
(-120.5..=-100.).contains(&offset_y),
133
147
"a 120 px slow pan scrolls about 120 px, got {offset_y}"
134
148
);
135
assert_eq!(clicks.get(), 0, "a pan never clicks");
149
assert_eq!(clicks.left.get(), 0, "a pan never clicks");
136
150
}
137
151
138
152
#[gpui_kit::test]
@@ -160,7 +174,7 @@ fn tap_clicks_the_row_under_the_finger(cx: &mut TestAppContext) {
160
174
touch_id: TouchId(1),
161
175
};
162
176
finger.tap(100., 60.);
163
assert_eq!(clicks.get(), 1);
177
assert_eq!(clicks.left.get(), 1);
164
178
}
165
179
166
180
#[gpui_kit::test]
@@ -184,3 +198,24 @@ fn finger_pan_scrolls_a_virtual_list(cx: &mut TestAppContext) {
184
198
"a 200 px slow pan scrolls the list about 200 px, got {scrolled}"
185
199
);
186
200
}
201
202
#[gpui_kit::test]
203
fn long_press_without_a_handler_right_clicks_instead_of_click_or_scroll(cx: &mut TestAppContext) {
204
let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);
205
let mut finger = Finger {
206
window_cx,
207
touch_id: TouchId(1),
208
};
209
finger.touch(TouchPhase::Started, 100., 60.);
210
finger
211
.window_cx
212
.executor()
213
.advance_clock(Duration::from_millis(600));
214
finger.window_cx.run_until_parked();
215
finger.touch(TouchPhase::Moved, 100., 20.);
216
finger.touch(TouchPhase::Ended, 100., 20.);
217
finger.window_cx.run_until_parked();
218
assert_eq!(clicks.right.get(), 1);
219
assert_eq!(clicks.left.get(), 0);
220
assert_eq!(f32::from(scroll_handle.offset().y), 0.);
221
}
@@ -2,36 +2,50 @@ use std::cell::Cell;2
use std::rc::Rc;2
use std::rc::Rc;3
use std::time::Duration;3
use std::time::Duration;4
4
5
use crate::long_press::long_press_right_click;5
use gpui_kit::{6
use gpui_kit::{6
Context, InteractiveElement as _, IntoElement, ListAlignment, ListState, ParentElement as _,7
Context, InteractiveElement as _, IntoElement, ListAlignment, ListState, MouseButton,7
Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _, TestAppContext, TouchEvent,8
ParentElement as _, Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _,8
TouchId, TouchPhase, VisualTestContext, Window, div, list, point, px,9
TestAppContext, TouchEvent, TouchId, TouchPhase, VisualTestContext, Window, div, list, point,10
px,9
};11
};10
12
11
const ROW_HEIGHT: f32 = 40.;13
const ROW_HEIGHT: f32 = 40.;12
const ROW_COUNT: usize = 60;14
const ROW_COUNT: usize = 60;13
15
16
#[derive(Clone, Default)]17
struct Clicks {18
left: Rc<Cell<usize>>,19
right: Rc<Cell<usize>>,20
}21
14
struct ScrollContainer {22
struct ScrollContainer {15
scroll_handle: ScrollHandle,23
scroll_handle: ScrollHandle,16
clicks: Rc<Cell<usize>>,24
clicks: Clicks,17
}25
}18
26
19
impl Render for ScrollContainer {27
impl Render for ScrollContainer {20
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {28
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {21
let clicks = self.clicks.clone();29
let clicks = self.clicks.clone();22
div()30
div().size_full().child(long_press_right_click()).child(23
.id("scroll-container")31
div()24
.size_full()32
.id("scroll-container")25
.overflow_y_scroll()33
.size_full()26
.track_scroll(&self.scroll_handle)34
.overflow_y_scroll()27
.children((0..ROW_COUNT).map(|row_index| {35
.track_scroll(&self.scroll_handle)28
let clicks = clicks.clone();36
.children((0..ROW_COUNT).map(|row_index| {29
div()37
let left = clicks.left.clone();30
.id(row_index)38
let right = clicks.right.clone();31
.h(px(ROW_HEIGHT))39
div()32
.w_full()40
.id(row_index)33
.on_click(move |_, _, _| clicks.set(clicks.get() + 1))41
.h(px(ROW_HEIGHT))34
}))42
.w_full()43
.on_click(move |_, _, _| left.set(left.get() + 1))44
.on_mouse_down(MouseButton::Right, move |_, _, _| {45
right.set(right.get() + 1)46
})47
})),48
)35
}49
}36
}50
}37
51
@@ -91,10 +105,10 @@ impl Finger<'_> {91
105
92
fn open_scroll_container(106
fn open_scroll_container(93
cx: &mut TestAppContext,107
cx: &mut TestAppContext,94
) -> (ScrollHandle, Rc<Cell<usize>>, &mut VisualTestContext) {108
) -> (ScrollHandle, Clicks, &mut VisualTestContext) {95
cx.update(gpui_kit::init);109
cx.update(gpui_kit::init);96
let scroll_handle = ScrollHandle::new();110
let scroll_handle = ScrollHandle::new();97
let clicks = Rc::new(Cell::new(0));111
let clicks = Clicks::default();98
let (_, window_cx) = cx.add_window_view({112
let (_, window_cx) = cx.add_window_view({99
let scroll_handle = scroll_handle.clone();113
let scroll_handle = scroll_handle.clone();100
let clicks = clicks.clone();114
let clicks = clicks.clone();@@ -132,7 +146,7 @@ fn finger_pan_scrolls_the_container_under_the_finger_by_pixels(cx: &mut TestAppC132
(-120.5..=-100.).contains(&offset_y),146
(-120.5..=-100.).contains(&offset_y),133
"a 120 px slow pan scrolls about 120 px, got {offset_y}"147
"a 120 px slow pan scrolls about 120 px, got {offset_y}"134
);148
);135
assert_eq!(clicks.get(), 0, "a pan never clicks");149
assert_eq!(clicks.left.get(), 0, "a pan never clicks");136
}150
}137
151
138
#[gpui_kit::test]152
#[gpui_kit::test]@@ -160,7 +174,7 @@ fn tap_clicks_the_row_under_the_finger(cx: &mut TestAppContext) {160
touch_id: TouchId(1),174
touch_id: TouchId(1),161
};175
};162
finger.tap(100., 60.);176
finger.tap(100., 60.);163
assert_eq!(clicks.get(), 1);177
assert_eq!(clicks.left.get(), 1);164
}178
}165
179
166
#[gpui_kit::test]180
#[gpui_kit::test]@@ -184,3 +198,24 @@ fn finger_pan_scrolls_a_virtual_list(cx: &mut TestAppContext) {184
"a 200 px slow pan scrolls the list about 200 px, got {scrolled}"198
"a 200 px slow pan scrolls the list about 200 px, got {scrolled}"185
);199
);186
}200
}201
202
#[gpui_kit::test]203
fn long_press_without_a_handler_right_clicks_instead_of_click_or_scroll(cx: &mut TestAppContext) {204
let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);205
let mut finger = Finger {206
window_cx,207
touch_id: TouchId(1),208
};209
finger.touch(TouchPhase::Started, 100., 60.);210
finger211
.window_cx212
.executor()213
.advance_clock(Duration::from_millis(600));214
finger.window_cx.run_until_parked();215
finger.touch(TouchPhase::Moved, 100., 20.);216
finger.touch(TouchPhase::Ended, 100., 20.);217
finger.window_cx.run_until_parked();218
assert_eq!(clicks.right.get(), 1);219
assert_eq!(clicks.left.get(), 0);220
assert_eq!(f32::from(scroll_handle.offset().y), 0.);221
}@@ -1098,6 +1098,7 @@ impl Render for AppShell {
1098
1098
.text_size(px(14.))
1099
1099
.child(crate::frame_log::probe("first"))
1100
1100
.child(user_activity::input_probe(self.user_input.clone()))
1101
.child(app_ui_kit::long_press::long_press_right_click())
1101
1102
.child(
1102
1103
v_flex()
1103
1104
.debug_selector(|| "shell".into())
@@ -1098,6 +1098,7 @@ impl Render for AppShell {1098
.text_size(px(14.))1098
.text_size(px(14.))1099
.child(crate::frame_log::probe("first"))1099
.child(crate::frame_log::probe("first"))1100
.child(user_activity::input_probe(self.user_input.clone()))1100
.child(user_activity::input_probe(self.user_input.clone()))1101
.child(app_ui_kit::long_press::long_press_right_click())1101
.child(1102
.child(1102
v_flex()1103
v_flex()1103
.debug_selector(|| "shell".into())1104
.debug_selector(|| "shell".into())