Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: ui/events/blink/web_input_event_builders_win.cc

Issue 2573073003: Collapse the API surface on WebInputEvent via accessor functions. (Closed)
Patch Set: Fix nits Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/events/blink/web_input_event.cc ('k') | ui/events/blink/web_input_event_traits.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/events/blink/web_input_event_builders_win.h" 5 #include "ui/events/blink/web_input_event_builders_win.h"
6 6
7 #include "ui/display/win/screen_win.h" 7 #include "ui/display/win/screen_win.h"
8 #include "ui/events/blink/blink_event_util.h" 8 #include "ui/events/blink/blink_event_util.h"
9 #include "ui/events/event_utils.h" 9 #include "ui/events/event_utils.h"
10 10
11 using blink::WebInputEvent; 11 using blink::WebInputEvent;
12 using blink::WebKeyboardEvent; 12 using blink::WebKeyboardEvent;
13 using blink::WebMouseEvent; 13 using blink::WebMouseEvent;
14 using blink::WebMouseWheelEvent; 14 using blink::WebMouseWheelEvent;
15 15
16 namespace ui { 16 namespace ui {
17 17
18 static const unsigned long kDefaultScrollLinesPerWheelDelta = 3; 18 static const unsigned long kDefaultScrollLinesPerWheelDelta = 3;
19 static const unsigned long kDefaultScrollCharsPerWheelDelta = 1; 19 static const unsigned long kDefaultScrollCharsPerWheelDelta = 1;
20 20
21 WebKeyboardEvent WebKeyboardEventBuilder::Build(HWND hwnd, 21 WebKeyboardEvent WebKeyboardEventBuilder::Build(HWND hwnd,
22 UINT message, 22 UINT message,
23 WPARAM wparam, 23 WPARAM wparam,
24 LPARAM lparam, 24 LPARAM lparam,
25 double time_stamp) { 25 double time_stamp) {
26 WebKeyboardEvent result; 26 WebInputEvent::Type type = WebInputEvent::Undefined;
27 27 bool is_system_key = false;
28 result.timeStampSeconds = time_stamp;
29
30 result.windowsKeyCode = static_cast<int>(wparam);
31 // Record the scan code (along with other context bits) for this key event.
32 result.nativeKeyCode = static_cast<int>(lparam);
33
34 switch (message) { 28 switch (message) {
35 case WM_SYSKEYDOWN: 29 case WM_SYSKEYDOWN:
36 result.isSystemKey = true; 30 is_system_key = true;
37 // fallthrough 31 // fallthrough
38 case WM_KEYDOWN: 32 case WM_KEYDOWN:
39 result.type = WebInputEvent::RawKeyDown; 33 type = WebInputEvent::RawKeyDown;
40 break; 34 break;
41 case WM_SYSKEYUP: 35 case WM_SYSKEYUP:
42 result.isSystemKey = true; 36 is_system_key = true;
43 // fallthrough 37 // fallthrough
44 case WM_KEYUP: 38 case WM_KEYUP:
45 result.type = WebInputEvent::KeyUp; 39 type = WebInputEvent::KeyUp;
46 break; 40 break;
47 case WM_IME_CHAR: 41 case WM_IME_CHAR:
48 result.type = WebInputEvent::Char; 42 type = WebInputEvent::Char;
49 break; 43 break;
50 case WM_SYSCHAR: 44 case WM_SYSCHAR:
51 result.isSystemKey = true; 45 is_system_key = true;
52 // fallthrough 46 // fallthrough
53 case WM_CHAR: 47 case WM_CHAR:
54 result.type = WebInputEvent::Char; 48 type = WebInputEvent::Char;
55 break; 49 break;
56 default: 50 default:
57 NOTREACHED(); 51 NOTREACHED();
58 } 52 }
59 53
60 if (result.type == WebInputEvent::Char || 54 WebKeyboardEvent result(
61 result.type == WebInputEvent::RawKeyDown) { 55 type, ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState()),
56 time_stamp);
57 result.isSystemKey = is_system_key;
58 result.windowsKeyCode = static_cast<int>(wparam);
59 // Record the scan code (along with other context bits) for this key event.
60 result.nativeKeyCode = static_cast<int>(lparam);
61
62 if (result.type() == WebInputEvent::Char ||
63 result.type() == WebInputEvent::RawKeyDown) {
62 result.text[0] = result.windowsKeyCode; 64 result.text[0] = result.windowsKeyCode;
63 result.unmodifiedText[0] = result.windowsKeyCode; 65 result.unmodifiedText[0] = result.windowsKeyCode;
64 } 66 }
65 result.modifiers =
66 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState());
67 // NOTE: There doesn't seem to be a way to query the mouse button state in 67 // NOTE: There doesn't seem to be a way to query the mouse button state in
68 // this case. 68 // this case.
69 69
70 // Bit 30 of lParam represents the "previous key state". If set, the key was 70 // Bit 30 of lParam represents the "previous key state". If set, the key was
71 // already down, therefore this is an auto-repeat. Only apply this to key 71 // already down, therefore this is an auto-repeat. Only apply this to key
72 // down events, to match DOM semantics. 72 // down events, to match DOM semantics.
73 if ((result.type == WebInputEvent::RawKeyDown) && (lparam & 0x40000000)) 73 if ((result.type() == WebInputEvent::RawKeyDown) && (lparam & 0x40000000))
74 result.modifiers |= WebInputEvent::IsAutoRepeat; 74 result.setModifiers(result.modifiers() | WebInputEvent::IsAutoRepeat);
75 75
76 return result; 76 return result;
77 } 77 }
78 78
79 // WebMouseEvent -------------------------------------------------------------- 79 // WebMouseEvent --------------------------------------------------------------
80 80
81 static int g_last_click_count = 0; 81 static int g_last_click_count = 0;
82 static double g_last_click_time = 0; 82 static double g_last_click_time = 0;
83 83
84 static LPARAM GetRelativeCursorPos(HWND hwnd) { 84 static LPARAM GetRelativeCursorPos(HWND hwnd) {
85 POINT pos = {-1, -1}; 85 POINT pos = {-1, -1};
86 GetCursorPos(&pos); 86 GetCursorPos(&pos);
87 ScreenToClient(hwnd, &pos); 87 ScreenToClient(hwnd, &pos);
88 return MAKELPARAM(pos.x, pos.y); 88 return MAKELPARAM(pos.x, pos.y);
89 } 89 }
90 90
91 WebMouseEvent WebMouseEventBuilder::Build( 91 WebMouseEvent WebMouseEventBuilder::Build(
92 HWND hwnd, 92 HWND hwnd,
93 UINT message, 93 UINT message,
94 WPARAM wparam, 94 WPARAM wparam,
95 LPARAM lparam, 95 LPARAM lparam,
96 double time_stamp, 96 double time_stamp,
97 blink::WebPointerProperties::PointerType pointer_type) { 97 blink::WebPointerProperties::PointerType pointer_type) {
98 WebMouseEvent result; 98 WebInputEvent::Type type = WebInputEvent::Type::Undefined;
99 99 WebMouseEvent::Button button = WebMouseEvent::Button::NoButton;
100 switch (message) { 100 switch (message) {
101 case WM_MOUSEMOVE: 101 case WM_MOUSEMOVE:
102 result.type = WebInputEvent::MouseMove; 102 type = WebInputEvent::MouseMove;
103 if (wparam & MK_LBUTTON) 103 if (wparam & MK_LBUTTON)
104 result.button = WebMouseEvent::Button::Left; 104 button = WebMouseEvent::Button::Left;
105 else if (wparam & MK_MBUTTON) 105 else if (wparam & MK_MBUTTON)
106 result.button = WebMouseEvent::Button::Middle; 106 button = WebMouseEvent::Button::Middle;
107 else if (wparam & MK_RBUTTON) 107 else if (wparam & MK_RBUTTON)
108 result.button = WebMouseEvent::Button::Right; 108 button = WebMouseEvent::Button::Right;
109 else 109 else
110 result.button = WebMouseEvent::Button::NoButton; 110 button = WebMouseEvent::Button::NoButton;
111 break; 111 break;
112 case WM_MOUSELEAVE: 112 case WM_MOUSELEAVE:
113 case WM_NCMOUSELEAVE: 113 case WM_NCMOUSELEAVE:
114 // TODO(rbyers): This should be MouseLeave but is disabled temporarily. 114 // TODO(rbyers): This should be MouseLeave but is disabled temporarily.
115 // See http://crbug.com/450631 115 // See http://crbug.com/450631
116 result.type = WebInputEvent::MouseMove; 116 type = WebInputEvent::MouseMove;
117 result.button = WebMouseEvent::Button::NoButton; 117 button = WebMouseEvent::Button::NoButton;
118 // set the current mouse position (relative to the client area of the 118 // set the current mouse position (relative to the client area of the
119 // current window) since none is specified for this event 119 // current window) since none is specified for this event
120 lparam = GetRelativeCursorPos(hwnd); 120 lparam = GetRelativeCursorPos(hwnd);
121 break; 121 break;
122 case WM_LBUTTONDOWN: 122 case WM_LBUTTONDOWN:
123 case WM_LBUTTONDBLCLK: 123 case WM_LBUTTONDBLCLK:
124 result.type = WebInputEvent::MouseDown; 124 type = WebInputEvent::MouseDown;
125 result.button = WebMouseEvent::Button::Left; 125 button = WebMouseEvent::Button::Left;
126 break; 126 break;
127 case WM_MBUTTONDOWN: 127 case WM_MBUTTONDOWN:
128 case WM_MBUTTONDBLCLK: 128 case WM_MBUTTONDBLCLK:
129 result.type = WebInputEvent::MouseDown; 129 type = WebInputEvent::MouseDown;
130 result.button = WebMouseEvent::Button::Middle; 130 button = WebMouseEvent::Button::Middle;
131 break; 131 break;
132 case WM_RBUTTONDOWN: 132 case WM_RBUTTONDOWN:
133 case WM_RBUTTONDBLCLK: 133 case WM_RBUTTONDBLCLK:
134 result.type = WebInputEvent::MouseDown; 134 type = WebInputEvent::MouseDown;
135 result.button = WebMouseEvent::Button::Right; 135 button = WebMouseEvent::Button::Right;
136 break; 136 break;
137 case WM_LBUTTONUP: 137 case WM_LBUTTONUP:
138 result.type = WebInputEvent::MouseUp; 138 type = WebInputEvent::MouseUp;
139 result.button = WebMouseEvent::Button::Left; 139 button = WebMouseEvent::Button::Left;
140 break; 140 break;
141 case WM_MBUTTONUP: 141 case WM_MBUTTONUP:
142 result.type = WebInputEvent::MouseUp; 142 type = WebInputEvent::MouseUp;
143 result.button = WebMouseEvent::Button::Middle; 143 button = WebMouseEvent::Button::Middle;
144 break; 144 break;
145 case WM_RBUTTONUP: 145 case WM_RBUTTONUP:
146 result.type = WebInputEvent::MouseUp; 146 type = WebInputEvent::MouseUp;
147 result.button = WebMouseEvent::Button::Right; 147 button = WebMouseEvent::Button::Right;
148 break; 148 break;
149 default: 149 default:
150 NOTREACHED(); 150 NOTREACHED();
151 } 151 }
152 152
153 result.timeStampSeconds = time_stamp; 153 // set modifiers:
154 int modifiers =
155 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState());
156 if (wparam & MK_CONTROL)
157 modifiers |= WebInputEvent::ControlKey;
158 if (wparam & MK_SHIFT)
159 modifiers |= WebInputEvent::ShiftKey;
160 if (wparam & MK_LBUTTON)
161 modifiers |= WebInputEvent::LeftButtonDown;
162 if (wparam & MK_MBUTTON)
163 modifiers |= WebInputEvent::MiddleButtonDown;
164 if (wparam & MK_RBUTTON)
165 modifiers |= WebInputEvent::RightButtonDown;
166
167 WebMouseEvent result(type, modifiers, time_stamp);
154 result.pointerType = pointer_type; 168 result.pointerType = pointer_type;
169 result.button = button;
155 170
156 // set position fields: 171 // set position fields:
157 172
158 result.x = static_cast<short>(LOWORD(lparam)); 173 result.x = static_cast<short>(LOWORD(lparam));
159 result.y = static_cast<short>(HIWORD(lparam)); 174 result.y = static_cast<short>(HIWORD(lparam));
160 result.windowX = result.x; 175 result.windowX = result.x;
161 result.windowY = result.y; 176 result.windowY = result.y;
162 177
163 POINT global_point = {result.x, result.y}; 178 POINT global_point = {result.x, result.y};
164 ClientToScreen(hwnd, &global_point); 179 ClientToScreen(hwnd, &global_point);
165 180
166 // We need to convert the global point back to DIP before using it. 181 // We need to convert the global point back to DIP before using it.
167 gfx::Point dip_global_point = display::win::ScreenWin::ScreenToDIPPoint( 182 gfx::Point dip_global_point = display::win::ScreenWin::ScreenToDIPPoint(
168 gfx::Point(global_point.x, global_point.y)); 183 gfx::Point(global_point.x, global_point.y));
169 184
170 result.globalX = dip_global_point.x(); 185 result.globalX = dip_global_point.x();
171 result.globalY = dip_global_point.y(); 186 result.globalY = dip_global_point.y();
172 187
173 // calculate number of clicks: 188 // calculate number of clicks:
174 189
175 // This differs slightly from the WebKit code in WebKit/win/WebView.cpp 190 // This differs slightly from the WebKit code in WebKit/win/WebView.cpp
176 // where their original code looks buggy. 191 // where their original code looks buggy.
177 static int last_click_position_x; 192 static int last_click_position_x;
178 static int last_click_position_y; 193 static int last_click_position_y;
179 static WebMouseEvent::Button last_click_button = WebMouseEvent::Button::Left; 194 static WebMouseEvent::Button last_click_button = WebMouseEvent::Button::Left;
180 195
181 double current_time = result.timeStampSeconds; 196 double current_time = result.timeStampSeconds();
182 bool cancel_previous_click = 197 bool cancel_previous_click =
183 (abs(last_click_position_x - result.x) > 198 (abs(last_click_position_x - result.x) >
184 (::GetSystemMetrics(SM_CXDOUBLECLK) / 2)) || 199 (::GetSystemMetrics(SM_CXDOUBLECLK) / 2)) ||
185 (abs(last_click_position_y - result.y) > 200 (abs(last_click_position_y - result.y) >
186 (::GetSystemMetrics(SM_CYDOUBLECLK) / 2)) || 201 (::GetSystemMetrics(SM_CYDOUBLECLK) / 2)) ||
187 ((current_time - g_last_click_time) * 1000.0 > ::GetDoubleClickTime()); 202 ((current_time - g_last_click_time) * 1000.0 > ::GetDoubleClickTime());
188 203
189 if (result.type == WebInputEvent::MouseDown) { 204 if (result.type() == WebInputEvent::MouseDown) {
190 if (!cancel_previous_click && (result.button == last_click_button)) { 205 if (!cancel_previous_click && (result.button == last_click_button)) {
191 ++g_last_click_count; 206 ++g_last_click_count;
192 } else { 207 } else {
193 g_last_click_count = 1; 208 g_last_click_count = 1;
194 last_click_position_x = result.x; 209 last_click_position_x = result.x;
195 last_click_position_y = result.y; 210 last_click_position_y = result.y;
196 } 211 }
197 g_last_click_time = current_time; 212 g_last_click_time = current_time;
198 last_click_button = result.button; 213 last_click_button = result.button;
199 } else if (result.type == WebInputEvent::MouseMove || 214 } else if (result.type() == WebInputEvent::MouseMove ||
200 result.type == WebInputEvent::MouseLeave) { 215 result.type() == WebInputEvent::MouseLeave) {
201 if (cancel_previous_click) { 216 if (cancel_previous_click) {
202 g_last_click_count = 0; 217 g_last_click_count = 0;
203 last_click_position_x = 0; 218 last_click_position_x = 0;
204 last_click_position_y = 0; 219 last_click_position_y = 0;
205 g_last_click_time = 0; 220 g_last_click_time = 0;
206 } 221 }
207 } 222 }
208 result.clickCount = g_last_click_count; 223 result.clickCount = g_last_click_count;
209 224
210 // set modifiers:
211 result.modifiers =
212 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState());
213 if (wparam & MK_CONTROL)
214 result.modifiers |= WebInputEvent::ControlKey;
215 if (wparam & MK_SHIFT)
216 result.modifiers |= WebInputEvent::ShiftKey;
217 if (wparam & MK_LBUTTON)
218 result.modifiers |= WebInputEvent::LeftButtonDown;
219 if (wparam & MK_MBUTTON)
220 result.modifiers |= WebInputEvent::MiddleButtonDown;
221 if (wparam & MK_RBUTTON)
222 result.modifiers |= WebInputEvent::RightButtonDown;
223 225
224 return result; 226 return result;
225 } 227 }
226 228
227 // WebMouseWheelEvent --------------------------------------------------------- 229 // WebMouseWheelEvent ---------------------------------------------------------
228 230
229 WebMouseWheelEvent WebMouseWheelEventBuilder::Build( 231 WebMouseWheelEvent WebMouseWheelEventBuilder::Build(
230 HWND hwnd, 232 HWND hwnd,
231 UINT message, 233 UINT message,
232 WPARAM wparam, 234 WPARAM wparam,
233 LPARAM lparam, 235 LPARAM lparam,
234 double time_stamp, 236 double time_stamp,
235 blink::WebPointerProperties::PointerType pointer_type) { 237 blink::WebPointerProperties::PointerType pointer_type) {
236 WebMouseWheelEvent result; 238 WebMouseWheelEvent result(
237 239 WebInputEvent::MouseWheel,
238 result.type = WebInputEvent::MouseWheel; 240 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState()),
239 241 time_stamp);
240 result.timeStampSeconds = time_stamp;
241 242
242 result.button = WebMouseEvent::Button::NoButton; 243 result.button = WebMouseEvent::Button::NoButton;
243
244 result.pointerType = pointer_type; 244 result.pointerType = pointer_type;
245 245
246 // Get key state, coordinates, and wheel delta from event. 246 // Get key state, coordinates, and wheel delta from event.
247 UINT key_state; 247 UINT key_state;
248 float wheel_delta; 248 float wheel_delta;
249 bool horizontal_scroll = false; 249 bool horizontal_scroll = false;
250 if ((message == WM_VSCROLL) || (message == WM_HSCROLL)) { 250 if ((message == WM_VSCROLL) || (message == WM_HSCROLL)) {
251 // Synthesize mousewheel event from a scroll event. This is needed to 251 // Synthesize mousewheel event from a scroll event. This is needed to
252 // simulate middle mouse scrolling in some laptops. Use GetAsyncKeyState 252 // simulate middle mouse scrolling in some laptops. Use GetAsyncKeyState
253 // for key state since we are synthesizing the input event. 253 // for key state since we are synthesizing the input event.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 // distinguish these from real mouse wheels (crbug.com/545234). 298 // distinguish these from real mouse wheels (crbug.com/545234).
299 wheel_delta = static_cast<float>(GET_WHEEL_DELTA_WPARAM(wparam)); 299 wheel_delta = static_cast<float>(GET_WHEEL_DELTA_WPARAM(wparam));
300 300
301 if (message == WM_MOUSEHWHEEL) { 301 if (message == WM_MOUSEHWHEEL) {
302 horizontal_scroll = true; 302 horizontal_scroll = true;
303 wheel_delta = -wheel_delta; // Windows is <- -/+ ->, WebKit <- +/- ->. 303 wheel_delta = -wheel_delta; // Windows is <- -/+ ->, WebKit <- +/- ->.
304 } 304 }
305 } 305 }
306 306
307 // Set modifiers based on key state. 307 // Set modifiers based on key state.
308 result.modifiers = 308 int modifiers = result.modifiers();
309 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState());
310 if (key_state & MK_SHIFT) 309 if (key_state & MK_SHIFT)
311 result.modifiers |= WebInputEvent::ShiftKey; 310 modifiers |= WebInputEvent::ShiftKey;
312 if (key_state & MK_CONTROL) 311 if (key_state & MK_CONTROL)
313 result.modifiers |= WebInputEvent::ControlKey; 312 modifiers |= WebInputEvent::ControlKey;
314 if (key_state & MK_LBUTTON) 313 if (key_state & MK_LBUTTON)
315 result.modifiers |= WebInputEvent::LeftButtonDown; 314 modifiers |= WebInputEvent::LeftButtonDown;
316 if (key_state & MK_MBUTTON) 315 if (key_state & MK_MBUTTON)
317 result.modifiers |= WebInputEvent::MiddleButtonDown; 316 modifiers |= WebInputEvent::MiddleButtonDown;
318 if (key_state & MK_RBUTTON) 317 if (key_state & MK_RBUTTON)
319 result.modifiers |= WebInputEvent::RightButtonDown; 318 modifiers |= WebInputEvent::RightButtonDown;
319 result.setModifiers(modifiers);
320 320
321 // Set coordinates by translating event coordinates from screen to client. 321 // Set coordinates by translating event coordinates from screen to client.
322 POINT client_point = {result.globalX, result.globalY}; 322 POINT client_point = {result.globalX, result.globalY};
323 MapWindowPoints(0, hwnd, &client_point, 1); 323 MapWindowPoints(0, hwnd, &client_point, 1);
324 result.x = client_point.x; 324 result.x = client_point.x;
325 result.y = client_point.y; 325 result.y = client_point.y;
326 result.windowX = result.x; 326 result.windowX = result.x;
327 result.windowY = result.y; 327 result.windowY = result.y;
328 328
329 // Convert wheel delta amount to a number of pixels to scroll. 329 // Convert wheel delta amount to a number of pixels to scroll.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 result.wheelTicksX = wheel_delta; 365 result.wheelTicksX = wheel_delta;
366 } else { 366 } else {
367 result.deltaY = scroll_delta; 367 result.deltaY = scroll_delta;
368 result.wheelTicksY = wheel_delta; 368 result.wheelTicksY = wheel_delta;
369 } 369 }
370 370
371 return result; 371 return result;
372 } 372 }
373 373
374 } // namespace ui 374 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/blink/web_input_event.cc ('k') | ui/events/blink/web_input_event_traits.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698