| 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 | } |