BlueGit

ict rusty-teams

feat(touch): touchscreen pan, fling and tap on Windows

- patch gpui-pre-windows: WM_POINTER* from PT_TOUCH become PlatformInput::Touch, no mouse promotion
- gpui core recognizer does slop, tap, pixel pan, axis lock and fling momentum
- pen, mouse and touchpad (DirectManipulation) unchanged
- build-windows.sh applies scripts/gpui-windows-patches/*.patch
- tests: raw touch drives overflow scroll and list, fling, tap, pan without click

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-17 · 6 weeks ago

Hash
4a1548f62c590c4e6930fb0db63f994d249de570
Parent
770acee0
4 files changed +369 −0
crates/app-ui-kit/src/lib.rs +3 −0
@@ -8,3 +8,6 @@ pub mod scroll_edge;
8 8 pub mod settings_controls;
9 9 pub mod spinner;
10 10 pub mod widgets;
11
12 #[cfg(test)]
13 mod touch_scroll;
@@ -8,3 +8,6 @@ pub mod scroll_edge;
8pub mod settings_controls;
8pub mod settings_controls;
9pub mod spinner;
9pub mod spinner;
10pub mod widgets;
10pub mod widgets;
11
12#[cfg(test)]
13mod touch_scroll;
crates/app-ui-kit/src/touch_scroll.rs added +186 −0
@@ -0,0 +1,186 @@
1 use std::cell::Cell;
2 use std::rc::Rc;
3 use std::time::Duration;
4
5 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,
9 };
10
11 const ROW_HEIGHT: f32 = 40.;
12 const ROW_COUNT: usize = 60;
13
14 struct ScrollContainer {
15 scroll_handle: ScrollHandle,
16 clicks: Rc<Cell<usize>>,
17 }
18
19 impl Render for ScrollContainer {
20 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
21 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 }))
35 }
36 }
37
38 struct VirtualList {
39 list_state: ListState,
40 }
41
42 impl Render for VirtualList {
43 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
44 list(self.list_state.clone(), |row_index, _, _| {
45 div()
46 .id(row_index)
47 .h(px(ROW_HEIGHT))
48 .w_full()
49 .into_any_element()
50 })
51 .size_full()
52 }
53 }
54
55 struct Finger<'a> {
56 window_cx: &'a mut VisualTestContext,
57 touch_id: TouchId,
58 }
59
60 impl Finger<'_> {
61 fn touch(&mut self, phase: TouchPhase, x: f32, y: f32) {
62 self.window_cx.simulate_event(TouchEvent {
63 id: self.touch_id,
64 phase,
65 position: point(px(x), px(y)),
66 predicted_position: None,
67 force: None,
68 });
69 self.window_cx
70 .executor()
71 .advance_clock(Duration::from_millis(8));
72 }
73
74 fn pan(&mut self, from_y: f32, to_y: f32, steps: usize) {
75 self.touch(TouchPhase::Started, 100., from_y);
76 for step in 1..=steps {
77 let progress = step as f32 / steps as f32;
78 self.touch(TouchPhase::Moved, 100., from_y + (to_y - from_y) * progress);
79 }
80 self.touch(TouchPhase::Ended, 100., to_y);
81 }
82
83 fn tap(&mut self, x: f32, y: f32) {
84 self.touch(TouchPhase::Started, x, y);
85 self.touch(TouchPhase::Ended, x, y);
86 self.window_cx
87 .executor()
88 .advance_clock(Duration::from_millis(500));
89 }
90 }
91
92 fn open_scroll_container(
93 cx: &mut TestAppContext,
94 ) -> (ScrollHandle, Rc<Cell<usize>>, &mut VisualTestContext) {
95 cx.update(gpui_kit::init);
96 let scroll_handle = ScrollHandle::new();
97 let clicks = Rc::new(Cell::new(0));
98 let (_, window_cx) = cx.add_window_view({
99 let scroll_handle = scroll_handle.clone();
100 let clicks = clicks.clone();
101 move |_, _| ScrollContainer {
102 scroll_handle,
103 clicks,
104 }
105 });
106 window_cx.update(|window, cx| window.draw(cx).clear(cx));
107 (scroll_handle, clicks, window_cx)
108 }
109
110 fn run_frames(window_cx: &mut VisualTestContext, frame_count: usize) {
111 for _ in 0..frame_count {
112 window_cx
113 .executor()
114 .advance_clock(Duration::from_millis(16));
115 window_cx.update(|window, cx| {
116 window.simulate_next_frame(cx);
117 window.draw(cx).clear(cx);
118 });
119 }
120 }
121
122 #[gpui_kit::test]
123 fn finger_pan_scrolls_the_container_under_the_finger_by_pixels(cx: &mut TestAppContext) {
124 let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);
125 let mut finger = Finger {
126 window_cx,
127 touch_id: TouchId(1),
128 };
129 finger.pan(300., 180., 30);
130 let offset_y = f32::from(scroll_handle.offset().y);
131 assert!(
132 (-120.5..=-100.).contains(&offset_y),
133 "a 120 px slow pan scrolls about 120 px, got {offset_y}"
134 );
135 assert_eq!(clicks.get(), 0, "a pan never clicks");
136 }
137
138 #[gpui_kit::test]
139 fn fling_keeps_scrolling_after_release(cx: &mut TestAppContext) {
140 let (scroll_handle, _, window_cx) = open_scroll_container(cx);
141 let mut finger = Finger {
142 window_cx,
143 touch_id: TouchId(1),
144 };
145 finger.pan(300., 100., 5);
146 let released_at = f32::from(scroll_handle.offset().y);
147 run_frames(finger.window_cx, 30);
148 let coasted_to = f32::from(scroll_handle.offset().y);
149 assert!(
150 coasted_to < released_at - 50.,
151 "momentum continues the scroll after release: {released_at} -> {coasted_to}"
152 );
153 }
154
155 #[gpui_kit::test]
156 fn tap_clicks_the_row_under_the_finger(cx: &mut TestAppContext) {
157 let (_, clicks, window_cx) = open_scroll_container(cx);
158 let mut finger = Finger {
159 window_cx,
160 touch_id: TouchId(1),
161 };
162 finger.tap(100., 60.);
163 assert_eq!(clicks.get(), 1);
164 }
165
166 #[gpui_kit::test]
167 fn finger_pan_scrolls_a_virtual_list(cx: &mut TestAppContext) {
168 cx.update(gpui_kit::init);
169 let list_state = ListState::new(ROW_COUNT, ListAlignment::Top, px(200.));
170 let (_, window_cx) = cx.add_window_view({
171 let list_state = list_state.clone();
172 move |_, _| VirtualList { list_state }
173 });
174 window_cx.update(|window, cx| window.draw(cx).clear(cx));
175 let mut finger = Finger {
176 window_cx,
177 touch_id: TouchId(1),
178 };
179 finger.pan(300., 100., 30);
180 let scroll_top = list_state.logical_scroll_top();
181 let scrolled = scroll_top.item_ix as f32 * ROW_HEIGHT + f32::from(scroll_top.offset_in_item);
182 assert!(
183 (180.0..=200.5).contains(&scrolled),
184 "a 200 px slow pan scrolls the list about 200 px, got {scrolled}"
185 );
186 }
@@ -0,0 +1,186 @@
1use std::cell::Cell;
2use std::rc::Rc;
3use std::time::Duration;
4
5use 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,
9};
10
11const ROW_HEIGHT: f32 = 40.;
12const ROW_COUNT: usize = 60;
13
14struct ScrollContainer {
15 scroll_handle: ScrollHandle,
16 clicks: Rc<Cell<usize>>,
17}
18
19impl Render for ScrollContainer {
20 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
21 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 }))
35 }
36}
37
38struct VirtualList {
39 list_state: ListState,
40}
41
42impl Render for VirtualList {
43 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
44 list(self.list_state.clone(), |row_index, _, _| {
45 div()
46 .id(row_index)
47 .h(px(ROW_HEIGHT))
48 .w_full()
49 .into_any_element()
50 })
51 .size_full()
52 }
53}
54
55struct Finger<'a> {
56 window_cx: &'a mut VisualTestContext,
57 touch_id: TouchId,
58}
59
60impl Finger<'_> {
61 fn touch(&mut self, phase: TouchPhase, x: f32, y: f32) {
62 self.window_cx.simulate_event(TouchEvent {
63 id: self.touch_id,
64 phase,
65 position: point(px(x), px(y)),
66 predicted_position: None,
67 force: None,
68 });
69 self.window_cx
70 .executor()
71 .advance_clock(Duration::from_millis(8));
72 }
73
74 fn pan(&mut self, from_y: f32, to_y: f32, steps: usize) {
75 self.touch(TouchPhase::Started, 100., from_y);
76 for step in 1..=steps {
77 let progress = step as f32 / steps as f32;
78 self.touch(TouchPhase::Moved, 100., from_y + (to_y - from_y) * progress);
79 }
80 self.touch(TouchPhase::Ended, 100., to_y);
81 }
82
83 fn tap(&mut self, x: f32, y: f32) {
84 self.touch(TouchPhase::Started, x, y);
85 self.touch(TouchPhase::Ended, x, y);
86 self.window_cx
87 .executor()
88 .advance_clock(Duration::from_millis(500));
89 }
90}
91
92fn open_scroll_container(
93 cx: &mut TestAppContext,
94) -> (ScrollHandle, Rc<Cell<usize>>, &mut VisualTestContext) {
95 cx.update(gpui_kit::init);
96 let scroll_handle = ScrollHandle::new();
97 let clicks = Rc::new(Cell::new(0));
98 let (_, window_cx) = cx.add_window_view({
99 let scroll_handle = scroll_handle.clone();
100 let clicks = clicks.clone();
101 move |_, _| ScrollContainer {
102 scroll_handle,
103 clicks,
104 }
105 });
106 window_cx.update(|window, cx| window.draw(cx).clear(cx));
107 (scroll_handle, clicks, window_cx)
108}
109
110fn run_frames(window_cx: &mut VisualTestContext, frame_count: usize) {
111 for _ in 0..frame_count {
112 window_cx
113 .executor()
114 .advance_clock(Duration::from_millis(16));
115 window_cx.update(|window, cx| {
116 window.simulate_next_frame(cx);
117 window.draw(cx).clear(cx);
118 });
119 }
120}
121
122#[gpui_kit::test]
123fn finger_pan_scrolls_the_container_under_the_finger_by_pixels(cx: &mut TestAppContext) {
124 let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);
125 let mut finger = Finger {
126 window_cx,
127 touch_id: TouchId(1),
128 };
129 finger.pan(300., 180., 30);
130 let offset_y = f32::from(scroll_handle.offset().y);
131 assert!(
132 (-120.5..=-100.).contains(&offset_y),
133 "a 120 px slow pan scrolls about 120 px, got {offset_y}"
134 );
135 assert_eq!(clicks.get(), 0, "a pan never clicks");
136}
137
138#[gpui_kit::test]
139fn fling_keeps_scrolling_after_release(cx: &mut TestAppContext) {
140 let (scroll_handle, _, window_cx) = open_scroll_container(cx);
141 let mut finger = Finger {
142 window_cx,
143 touch_id: TouchId(1),
144 };
145 finger.pan(300., 100., 5);
146 let released_at = f32::from(scroll_handle.offset().y);
147 run_frames(finger.window_cx, 30);
148 let coasted_to = f32::from(scroll_handle.offset().y);
149 assert!(
150 coasted_to < released_at - 50.,
151 "momentum continues the scroll after release: {released_at} -> {coasted_to}"
152 );
153}
154
155#[gpui_kit::test]
156fn tap_clicks_the_row_under_the_finger(cx: &mut TestAppContext) {
157 let (_, clicks, window_cx) = open_scroll_container(cx);
158 let mut finger = Finger {
159 window_cx,
160 touch_id: TouchId(1),
161 };
162 finger.tap(100., 60.);
163 assert_eq!(clicks.get(), 1);
164}
165
166#[gpui_kit::test]
167fn finger_pan_scrolls_a_virtual_list(cx: &mut TestAppContext) {
168 cx.update(gpui_kit::init);
169 let list_state = ListState::new(ROW_COUNT, ListAlignment::Top, px(200.));
170 let (_, window_cx) = cx.add_window_view({
171 let list_state = list_state.clone();
172 move |_, _| VirtualList { list_state }
173 });
174 window_cx.update(|window, cx| window.draw(cx).clear(cx));
175 let mut finger = Finger {
176 window_cx,
177 touch_id: TouchId(1),
178 };
179 finger.pan(300., 100., 30);
180 let scroll_top = list_state.logical_scroll_top();
181 let scrolled = scroll_top.item_ix as f32 * ROW_HEIGHT + f32::from(scroll_top.offset_in_item);
182 assert!(
183 (180.0..=200.5).contains(&scrolled),
184 "a 200 px slow pan scrolls the list about 200 px, got {scrolled}"
185 );
186}
scripts/build-windows.sh +3 −0
@@ -37,6 +37,9 @@ text = re.sub(r'fn main\(\) \{.*?\n\}\n', 'fn main() {\n #[cfg(not(debug_asse
37 37 text = re.sub(r'(pub fn find_latest_windows_sdk_binary\(.*?\) -> Result<[^{]*\{).*?(\n /// You can set)', r'\1\n let _ = binary;\n Ok(None)\n }\n\2', text, count=1, flags=re.S)
38 38 open(path, 'w').write(text)
39 39 PY
40 for patchFile in "$repoRoot"/scripts/gpui-windows-patches/*.patch; do
41 patch --directory="$stagingDir" --strip=1 --forward --batch --no-backup-if-mismatch --quiet < "$patchFile"
42 done
40 43 # A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it.
41 44 if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then
42 45 rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir"
@@ -37,6 +37,9 @@ text = re.sub(r'fn main\(\) \{.*?\n\}\n', 'fn main() {\n #[cfg(not(debug_asse
37text = re.sub(r'(pub fn find_latest_windows_sdk_binary\(.*?\) -> Result<[^{]*\{).*?(\n /// You can set)', r'\1\n let _ = binary;\n Ok(None)\n }\n\2', text, count=1, flags=re.S)
37text = re.sub(r'(pub fn find_latest_windows_sdk_binary\(.*?\) -> Result<[^{]*\{).*?(\n /// You can set)', r'\1\n let _ = binary;\n Ok(None)\n }\n\2', text, count=1, flags=re.S)
38open(path, 'w').write(text)
38open(path, 'w').write(text)
39PY
39PY
40for patchFile in "$repoRoot"/scripts/gpui-windows-patches/*.patch; do
41 patch --directory="$stagingDir" --strip=1 --forward --batch --no-backup-if-mismatch --quiet < "$patchFile"
42done
40# A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it.
43# A fresh copy gets new mtimes, and Cargo then rebuilds gpui and everything above it.
41if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then
44if ! diff -rq "$stagingDir" "$patchedDir" >/dev/null 2>&1; then
42 rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir"
45 rm -rf "$patchedDir"; mkdir -p "$(dirname "$patchedDir")"; cp -rp "$stagingDir" "$patchedDir"
scripts/gpui-windows-patches/touch-input.patch added +177 −0
@@ -0,0 +1,177 @@
1 diff -ruN a/src/events.rs b/src/events.rs
2 --- a/src/events.rs
3 +++ b/src/events.rs
4 @@ -10,7 +10,7 @@
5 UI::{
6 Controls::*,
7 HiDpi::*,
8 - Input::{Ime::*, KeyboardAndMouse::*},
9 + Input::{Ime::*, KeyboardAndMouse::*, Pointer::GetPointerType},
10 WindowsAndMessaging::*,
11 },
12 },
13 @@ -31,6 +31,7 @@
14 pub(crate) const WM_GPUI_END_SESSION: u32 = WM_USER + 9;
15
16 const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
17 +const POINTER_MESSAGE_FLAG_CANCELED: u16 = 0x8000;
18
19 /// Coordinates window draws on the UI thread. Owned by the platform and
20 /// shared with every window (like `WindowsPlatformState::cursor_visible`),
21 @@ -149,6 +150,10 @@
22 }
23 WM_MOUSEWHEEL => self.handle_mouse_wheel_msg(handle, wparam, lparam),
24 WM_MOUSEHWHEEL => self.handle_mouse_horizontal_wheel_msg(handle, wparam, lparam),
25 + WM_POINTERDOWN => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Started),
26 + WM_POINTERUPDATE => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Moved),
27 + WM_POINTERUP => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Ended),
28 + WM_POINTERCAPTURECHANGED => self.handle_touch_pointer_capture_changed_msg(wparam),
29 WM_SYSKEYUP => self.handle_syskeyup_msg(wparam, lparam),
30 WM_KEYUP => self.handle_keyup_msg(wparam, lparam),
31 WM_GPUI_KEYDOWN => self.handle_keydown_msg(wparam, lparam),
32 @@ -647,6 +652,62 @@
33 if handled { Some(0) } else { Some(1) }
34 }
35
36 + fn handle_touch_pointer_msg(
37 + &self,
38 + handle: HWND,
39 + wparam: WPARAM,
40 + lparam: LPARAM,
41 + phase: TouchPhase,
42 + ) -> Option<isize> {
43 + let pointer_id = wparam.loword() as u32;
44 + let mut pointer_type = POINTER_INPUT_TYPE::default();
45 + unsafe { GetPointerType(pointer_id, &mut pointer_type) }.ok()?;
46 + if pointer_type != PT_TOUCH {
47 + return None;
48 + }
49 + let phase = if phase == TouchPhase::Ended
50 + && wparam.hiword() & POINTER_MESSAGE_FLAG_CANCELED != 0
51 + {
52 + TouchPhase::Cancelled
53 + } else {
54 + phase
55 + };
56 + let mut cursor_point = POINT {
57 + x: lparam.signed_loword().into(),
58 + y: lparam.signed_hiword().into(),
59 + };
60 + unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
61 + let position = logical_point(
62 + cursor_point.x as f32,
63 + cursor_point.y as f32,
64 + self.state.scale_factor.get(),
65 + );
66 + if let Some(touch_id) = self.state.touch_pointers.track(pointer_id, phase, position) {
67 + self.dispatch_touch(touch_id, phase, position);
68 + }
69 + Some(0)
70 + }
71 +
72 + fn handle_touch_pointer_capture_changed_msg(&self, wparam: WPARAM) -> Option<isize> {
73 + let (touch_id, position) = self.state.touch_pointers.cancel(wparam.loword() as u32)?;
74 + self.dispatch_touch(touch_id, TouchPhase::Cancelled, position);
75 + Some(0)
76 + }
77 +
78 + fn dispatch_touch(&self, touch_id: TouchId, phase: TouchPhase, position: Point<Pixels>) {
79 + let Some(mut func) = self.state.callbacks.input.take() else {
80 + return;
81 + };
82 + func(PlatformInput::Touch(TouchEvent {
83 + id: touch_id,
84 + phase,
85 + position,
86 + predicted_position: None,
87 + force: None,
88 + }));
89 + self.state.callbacks.input.set(Some(func));
90 + }
91 +
92 fn retrieve_caret_position(&self) -> Option<POINT> {
93 self.with_input_handler_and_scale_factor(|input_handler, scale_factor| {
94 let caret_range = input_handler.selected_text_range(false)?;
95 diff -ruN a/src/gpui_windows.rs b/src/gpui_windows.rs
96 --- a/src/gpui_windows.rs
97 +++ b/src/gpui_windows.rs
98 @@ -15,6 +15,7 @@
99 mod platform;
100 mod system_notifications;
101 mod system_settings;
102 +mod touch;
103 mod util;
104 mod vsync;
105 mod window;
106 @@ -33,6 +34,7 @@
107 pub(crate) use platform::*;
108 pub(crate) use system_notifications::*;
109 pub(crate) use system_settings::*;
110 +pub(crate) use touch::*;
111 pub(crate) use util::*;
112 pub(crate) use vsync::*;
113 pub(crate) use window::*;
114 diff -ruN a/src/touch.rs b/src/touch.rs
115 --- a/src/touch.rs
116 +++ b/src/touch.rs
117 @@ -0,0 +1,41 @@
118 +use std::cell::{Cell, RefCell};
119 +use std::collections::HashMap;
120 +
121 +use gpui::{Pixels, Point, TouchId, TouchPhase};
122 +
123 +#[derive(Default)]
124 +pub(crate) struct TouchPointers {
125 + next_touch_id: Cell<u64>,
126 + active: RefCell<HashMap<u32, (TouchId, Point<Pixels>)>>,
127 +}
128 +
129 +impl TouchPointers {
130 + pub(crate) fn track(
131 + &self,
132 + pointer_id: u32,
133 + phase: TouchPhase,
134 + position: Point<Pixels>,
135 + ) -> Option<TouchId> {
136 + let mut active = self.active.borrow_mut();
137 + match phase {
138 + TouchPhase::Started => {
139 + let touch_id = TouchId(self.next_touch_id.get());
140 + self.next_touch_id.set(self.next_touch_id.get() + 1);
141 + active.insert(pointer_id, (touch_id, position));
142 + Some(touch_id)
143 + }
144 + TouchPhase::Moved => {
145 + let entry = active.get_mut(&pointer_id)?;
146 + entry.1 = position;
147 + Some(entry.0)
148 + }
149 + TouchPhase::Ended | TouchPhase::Cancelled => {
150 + active.remove(&pointer_id).map(|(touch_id, _)| touch_id)
151 + }
152 + }
153 + }
154 +
155 + pub(crate) fn cancel(&self, pointer_id: u32) -> Option<(TouchId, Point<Pixels>)> {
156 + self.active.borrow_mut().remove(&pointer_id)
157 + }
158 +}
159 diff -ruN a/src/window.rs b/src/window.rs
160 --- a/src/window.rs
161 +++ b/src/window.rs
162 @@ -63,6 +63,7 @@
163 pub hovered: Cell<bool>,
164 pub last_visibility: Cell<Option<WindowVisibility>>,
165 pub direct_manipulation: DirectManipulationHandler,
166 + pub touch_pointers: TouchPointers,
167
168 pub renderer: RefCell<DirectXRenderer>,
169 /// Set when the next `draw_window` call must be treated as a forced
170 @@ -190,6 +191,7 @@
171 invalidate_devices,
172 draw_coordinator,
173 direct_manipulation,
174 + touch_pointers: TouchPointers::default(),
175 a11y: RefCell::new(None),
176 })
177 }
@@ -0,0 +1,177 @@
1diff -ruN a/src/events.rs b/src/events.rs
2--- a/src/events.rs
3+++ b/src/events.rs
4@@ -10,7 +10,7 @@
5 UI::{
6 Controls::*,
7 HiDpi::*,
8- Input::{Ime::*, KeyboardAndMouse::*},
9+ Input::{Ime::*, KeyboardAndMouse::*, Pointer::GetPointerType},
10 WindowsAndMessaging::*,
11 },
12 },
13@@ -31,6 +31,7 @@
14 pub(crate) const WM_GPUI_END_SESSION: u32 = WM_USER + 9;
15
16 const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
17+const POINTER_MESSAGE_FLAG_CANCELED: u16 = 0x8000;
18
19 /// Coordinates window draws on the UI thread. Owned by the platform and
20 /// shared with every window (like `WindowsPlatformState::cursor_visible`),
21@@ -149,6 +150,10 @@
22 }
23 WM_MOUSEWHEEL => self.handle_mouse_wheel_msg(handle, wparam, lparam),
24 WM_MOUSEHWHEEL => self.handle_mouse_horizontal_wheel_msg(handle, wparam, lparam),
25+ WM_POINTERDOWN => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Started),
26+ WM_POINTERUPDATE => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Moved),
27+ WM_POINTERUP => self.handle_touch_pointer_msg(handle, wparam, lparam, TouchPhase::Ended),
28+ WM_POINTERCAPTURECHANGED => self.handle_touch_pointer_capture_changed_msg(wparam),
29 WM_SYSKEYUP => self.handle_syskeyup_msg(wparam, lparam),
30 WM_KEYUP => self.handle_keyup_msg(wparam, lparam),
31 WM_GPUI_KEYDOWN => self.handle_keydown_msg(wparam, lparam),
32@@ -647,6 +652,62 @@
33 if handled { Some(0) } else { Some(1) }
34 }
35
36+ fn handle_touch_pointer_msg(
37+ &self,
38+ handle: HWND,
39+ wparam: WPARAM,
40+ lparam: LPARAM,
41+ phase: TouchPhase,
42+ ) -> Option<isize> {
43+ let pointer_id = wparam.loword() as u32;
44+ let mut pointer_type = POINTER_INPUT_TYPE::default();
45+ unsafe { GetPointerType(pointer_id, &mut pointer_type) }.ok()?;
46+ if pointer_type != PT_TOUCH {
47+ return None;
48+ }
49+ let phase = if phase == TouchPhase::Ended
50+ && wparam.hiword() & POINTER_MESSAGE_FLAG_CANCELED != 0
51+ {
52+ TouchPhase::Cancelled
53+ } else {
54+ phase
55+ };
56+ let mut cursor_point = POINT {
57+ x: lparam.signed_loword().into(),
58+ y: lparam.signed_hiword().into(),
59+ };
60+ unsafe { ScreenToClient(handle, &mut cursor_point).ok().log_err() };
61+ let position = logical_point(
62+ cursor_point.x as f32,
63+ cursor_point.y as f32,
64+ self.state.scale_factor.get(),
65+ );
66+ if let Some(touch_id) = self.state.touch_pointers.track(pointer_id, phase, position) {
67+ self.dispatch_touch(touch_id, phase, position);
68+ }
69+ Some(0)
70+ }
71+
72+ fn handle_touch_pointer_capture_changed_msg(&self, wparam: WPARAM) -> Option<isize> {
73+ let (touch_id, position) = self.state.touch_pointers.cancel(wparam.loword() as u32)?;
74+ self.dispatch_touch(touch_id, TouchPhase::Cancelled, position);
75+ Some(0)
76+ }
77+
78+ fn dispatch_touch(&self, touch_id: TouchId, phase: TouchPhase, position: Point<Pixels>) {
79+ let Some(mut func) = self.state.callbacks.input.take() else {
80+ return;
81+ };
82+ func(PlatformInput::Touch(TouchEvent {
83+ id: touch_id,
84+ phase,
85+ position,
86+ predicted_position: None,
87+ force: None,
88+ }));
89+ self.state.callbacks.input.set(Some(func));
90+ }
91+
92 fn retrieve_caret_position(&self) -> Option<POINT> {
93 self.with_input_handler_and_scale_factor(|input_handler, scale_factor| {
94 let caret_range = input_handler.selected_text_range(false)?;
95diff -ruN a/src/gpui_windows.rs b/src/gpui_windows.rs
96--- a/src/gpui_windows.rs
97+++ b/src/gpui_windows.rs
98@@ -15,6 +15,7 @@
99 mod platform;
100 mod system_notifications;
101 mod system_settings;
102+mod touch;
103 mod util;
104 mod vsync;
105 mod window;
106@@ -33,6 +34,7 @@
107 pub(crate) use platform::*;
108 pub(crate) use system_notifications::*;
109 pub(crate) use system_settings::*;
110+pub(crate) use touch::*;
111 pub(crate) use util::*;
112 pub(crate) use vsync::*;
113 pub(crate) use window::*;
114diff -ruN a/src/touch.rs b/src/touch.rs
115--- a/src/touch.rs
116+++ b/src/touch.rs
117@@ -0,0 +1,41 @@
118+use std::cell::{Cell, RefCell};
119+use std::collections::HashMap;
120+
121+use gpui::{Pixels, Point, TouchId, TouchPhase};
122+
123+#[derive(Default)]
124+pub(crate) struct TouchPointers {
125+ next_touch_id: Cell<u64>,
126+ active: RefCell<HashMap<u32, (TouchId, Point<Pixels>)>>,
127+}
128+
129+impl TouchPointers {
130+ pub(crate) fn track(
131+ &self,
132+ pointer_id: u32,
133+ phase: TouchPhase,
134+ position: Point<Pixels>,
135+ ) -> Option<TouchId> {
136+ let mut active = self.active.borrow_mut();
137+ match phase {
138+ TouchPhase::Started => {
139+ let touch_id = TouchId(self.next_touch_id.get());
140+ self.next_touch_id.set(self.next_touch_id.get() + 1);
141+ active.insert(pointer_id, (touch_id, position));
142+ Some(touch_id)
143+ }
144+ TouchPhase::Moved => {
145+ let entry = active.get_mut(&pointer_id)?;
146+ entry.1 = position;
147+ Some(entry.0)
148+ }
149+ TouchPhase::Ended | TouchPhase::Cancelled => {
150+ active.remove(&pointer_id).map(|(touch_id, _)| touch_id)
151+ }
152+ }
153+ }
154+
155+ pub(crate) fn cancel(&self, pointer_id: u32) -> Option<(TouchId, Point<Pixels>)> {
156+ self.active.borrow_mut().remove(&pointer_id)
157+ }
158+}
159diff -ruN a/src/window.rs b/src/window.rs
160--- a/src/window.rs
161+++ b/src/window.rs
162@@ -63,6 +63,7 @@
163 pub hovered: Cell<bool>,
164 pub last_visibility: Cell<Option<WindowVisibility>>,
165 pub direct_manipulation: DirectManipulationHandler,
166+ pub touch_pointers: TouchPointers,
167
168 pub renderer: RefCell<DirectXRenderer>,
169 /// Set when the next `draw_window` call must be treated as a forced
170@@ -190,6 +191,7 @@
171 invalidate_devices,
172 draw_coordinator,
173 direct_manipulation,
174+ touch_pointers: TouchPointers::default(),
175 a11y: RefCell::new(None),
176 })
177 }