Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "KeyboardEventManager.h" | 5 #include "KeyboardEventManager.h" |
| 6 | 6 |
| 7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
| 8 #include "core/editing/Editor.h" | 8 #include "core/editing/Editor.h" |
| 9 #include "core/events/KeyboardEvent.h" | 9 #include "core/events/KeyboardEvent.h" |
| 10 #include "core/html/HTMLDialogElement.h" | 10 #include "core/html/HTMLDialogElement.h" |
| 11 #include "core/input/EventHandler.h" | |
| 11 #include "core/input/EventHandlingUtil.h" | 12 #include "core/input/EventHandlingUtil.h" |
| 12 #include "core/input/ScrollManager.h" | 13 #include "core/input/ScrollManager.h" |
| 13 #include "core/layout/LayoutObject.h" | 14 #include "core/layout/LayoutObject.h" |
| 14 #include "core/layout/LayoutTextControlSingleLine.h" | 15 #include "core/layout/LayoutTextControlSingleLine.h" |
| 15 #include "core/loader/FrameLoaderClient.h" | 16 #include "core/loader/FrameLoaderClient.h" |
| 16 #include "core/page/ChromeClient.h" | 17 #include "core/page/ChromeClient.h" |
| 17 #include "core/page/FocusController.h" | 18 #include "core/page/FocusController.h" |
| 18 #include "core/page/Page.h" | 19 #include "core/page/Page.h" |
| 19 #include "core/page/SpatialNavigation.h" | 20 #include "core/page/SpatialNavigation.h" |
| 21 #include "platform/KeyboardCodes.h" | |
| 20 #include "platform/UserGestureIndicator.h" | 22 #include "platform/UserGestureIndicator.h" |
| 21 #include "platform/WindowsKeyboardCodes.h" | 23 #include "platform/WindowsKeyboardCodes.h" |
| 22 #include "public/platform/WebInputEvent.h" | 24 #include "public/platform/WebInputEvent.h" |
| 23 | 25 |
| 24 #if OS(WIN) | 26 #if OS(WIN) |
| 25 #include <windows.h> | 27 #include <windows.h> |
| 26 #elif OS(MACOSX) | 28 #elif OS(MACOSX) |
| 27 #import <Carbon/Carbon.h> | 29 #import <Carbon/Carbon.h> |
| 28 #endif | 30 #endif |
| 29 | 31 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 43 retVal = WebFocusTypeDown; | 45 retVal = WebFocusTypeDown; |
| 44 else if (key == "ArrowUp") | 46 else if (key == "ArrowUp") |
| 45 retVal = WebFocusTypeUp; | 47 retVal = WebFocusTypeUp; |
| 46 else if (key == "ArrowLeft") | 48 else if (key == "ArrowLeft") |
| 47 retVal = WebFocusTypeLeft; | 49 retVal = WebFocusTypeLeft; |
| 48 else if (key == "ArrowRight") | 50 else if (key == "ArrowRight") |
| 49 retVal = WebFocusTypeRight; | 51 retVal = WebFocusTypeRight; |
| 50 return retVal; | 52 return retVal; |
| 51 } | 53 } |
| 52 | 54 |
| 55 bool mapKeyCodeForScroll(int keyCode, | |
| 56 PlatformEvent::Modifiers modifiers, | |
| 57 ScrollDirection* scrollDirection, | |
| 58 ScrollGranularity* scrollGranularity) { | |
| 59 if (modifiers & PlatformEvent::ShiftKey || modifiers & PlatformEvent::MetaKey) | |
| 60 return false; | |
| 61 | |
| 62 if (modifiers & PlatformEvent::AltKey) { | |
| 63 // Alt-Up/Down should behave like PageUp/Down on Mac. (Note that Alt-keys | |
| 64 // on other platforms are suppressed due to isSystemKey being set.) | |
|
skobes
2016/10/26 17:57:44
Why do Alt-keys not set isSystemKey on Mac?
I won
aelias_OOO_until_Jul13
2016/10/26 19:27:39
On Mac, the system key is the meta key instead: ht
| |
| 65 if (keyCode == VKEY_UP) | |
| 66 keyCode = VKEY_PRIOR; | |
| 67 else if (keyCode == VKEY_DOWN) | |
| 68 keyCode = VKEY_NEXT; | |
| 69 else | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 if (modifiers & PlatformEvent::CtrlKey) { | |
| 74 // Match FF behavior in the sense that Ctrl+home/end are the only Ctrl | |
| 75 // key combinations which affect scrolling. | |
| 76 if (keyCode != VKEY_HOME && keyCode != VKEY_END) | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 switch (keyCode) { | |
| 81 case VKEY_LEFT: | |
| 82 *scrollDirection = ScrollLeftIgnoringWritingMode; | |
| 83 *scrollGranularity = ScrollByLine; | |
| 84 break; | |
| 85 case VKEY_RIGHT: | |
| 86 *scrollDirection = ScrollRightIgnoringWritingMode; | |
| 87 *scrollGranularity = ScrollByLine; | |
| 88 break; | |
| 89 case VKEY_UP: | |
| 90 *scrollDirection = ScrollUpIgnoringWritingMode; | |
| 91 *scrollGranularity = ScrollByLine; | |
| 92 break; | |
| 93 case VKEY_DOWN: | |
| 94 *scrollDirection = ScrollDownIgnoringWritingMode; | |
| 95 *scrollGranularity = ScrollByLine; | |
| 96 break; | |
| 97 case VKEY_HOME: | |
| 98 *scrollDirection = ScrollUpIgnoringWritingMode; | |
| 99 *scrollGranularity = ScrollByDocument; | |
| 100 break; | |
| 101 case VKEY_END: | |
| 102 *scrollDirection = ScrollDownIgnoringWritingMode; | |
| 103 *scrollGranularity = ScrollByDocument; | |
| 104 break; | |
| 105 case VKEY_PRIOR: // page up | |
| 106 *scrollDirection = ScrollUpIgnoringWritingMode; | |
| 107 *scrollGranularity = ScrollByPage; | |
| 108 break; | |
| 109 case VKEY_NEXT: // page down | |
| 110 *scrollDirection = ScrollDownIgnoringWritingMode; | |
| 111 *scrollGranularity = ScrollByPage; | |
| 112 break; | |
| 113 default: | |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 53 } // namespace | 120 } // namespace |
| 54 | 121 |
| 55 KeyboardEventManager::KeyboardEventManager(LocalFrame* frame, | 122 KeyboardEventManager::KeyboardEventManager(LocalFrame* frame, |
| 56 ScrollManager* scrollManager) | 123 ScrollManager* scrollManager) |
| 57 : m_frame(frame), m_scrollManager(scrollManager) {} | 124 : m_frame(frame), m_scrollManager(scrollManager) {} |
| 58 | 125 |
| 59 DEFINE_TRACE(KeyboardEventManager) { | 126 DEFINE_TRACE(KeyboardEventManager) { |
| 60 visitor->trace(m_frame); | 127 visitor->trace(m_frame); |
| 61 visitor->trace(m_scrollManager); | 128 visitor->trace(m_scrollManager); |
| 62 } | 129 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 // TODO(dtapuska): Replace this with isComposing support. crbug.com/625686 | 270 // TODO(dtapuska): Replace this with isComposing support. crbug.com/625686 |
| 204 if (event->keyCode() == kVKeyProcessKey) | 271 if (event->keyCode() == kVKeyProcessKey) |
| 205 return; | 272 return; |
| 206 if (event->key() == "Tab") { | 273 if (event->key() == "Tab") { |
| 207 defaultTabEventHandler(event); | 274 defaultTabEventHandler(event); |
| 208 } else if (event->key() == "Backspace") { | 275 } else if (event->key() == "Backspace") { |
| 209 defaultBackspaceEventHandler(event); | 276 defaultBackspaceEventHandler(event); |
| 210 } else if (event->key() == "Escape") { | 277 } else if (event->key() == "Escape") { |
| 211 defaultEscapeEventHandler(event); | 278 defaultEscapeEventHandler(event); |
| 212 } else { | 279 } else { |
| 213 WebFocusType type = focusDirectionForKey(event->key()); | 280 defaultArrowEventHandler(event, possibleFocusedNode); |
| 214 if (type != WebFocusTypeNone) | |
| 215 defaultArrowEventHandler(type, event); | |
| 216 } | 281 } |
| 217 } | 282 } |
| 218 if (event->type() == EventTypeNames::keypress) { | 283 if (event->type() == EventTypeNames::keypress) { |
| 219 m_frame->editor().handleKeyboardEvent(event); | 284 m_frame->editor().handleKeyboardEvent(event); |
| 220 if (event->defaultHandled()) | 285 if (event->defaultHandled()) |
| 221 return; | 286 return; |
| 222 if (event->charCode() == ' ') | 287 if (event->charCode() == ' ') |
| 223 defaultSpaceEventHandler(event, possibleFocusedNode); | 288 defaultSpaceEventHandler(event, possibleFocusedNode); |
| 224 } | 289 } |
| 225 } | 290 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 256 UseCounter::count(m_frame->document(), UseCounter::BackspaceNavigatedBack); | 321 UseCounter::count(m_frame->document(), UseCounter::BackspaceNavigatedBack); |
| 257 if (m_frame->page()->chromeClient().hadFormInteraction()) | 322 if (m_frame->page()->chromeClient().hadFormInteraction()) |
| 258 UseCounter::count(m_frame->document(), | 323 UseCounter::count(m_frame->document(), |
| 259 UseCounter::BackspaceNavigatedBackAfterFormInteraction); | 324 UseCounter::BackspaceNavigatedBackAfterFormInteraction); |
| 260 bool handledEvent = m_frame->loader().client()->navigateBackForward( | 325 bool handledEvent = m_frame->loader().client()->navigateBackForward( |
| 261 event->shiftKey() ? 1 : -1); | 326 event->shiftKey() ? 1 : -1); |
| 262 if (handledEvent) | 327 if (handledEvent) |
| 263 event->setDefaultHandled(); | 328 event->setDefaultHandled(); |
| 264 } | 329 } |
| 265 | 330 |
| 266 void KeyboardEventManager::defaultArrowEventHandler(WebFocusType focusType, | 331 void KeyboardEventManager::defaultArrowEventHandler(KeyboardEvent* event, |
| 267 KeyboardEvent* event) { | 332 Node* possibleFocusedNode) { |
| 268 DCHECK_EQ(event->type(), EventTypeNames::keydown); | 333 DCHECK_EQ(event->type(), EventTypeNames::keydown); |
| 269 | 334 |
| 270 if (event->ctrlKey() || event->metaKey() || event->shiftKey()) | |
| 271 return; | |
| 272 | |
| 273 Page* page = m_frame->page(); | 335 Page* page = m_frame->page(); |
| 274 if (!page) | 336 if (!page) |
| 275 return; | 337 return; |
| 276 | 338 |
| 277 if (!isSpatialNavigationEnabled(m_frame)) | 339 WebFocusType type = focusDirectionForKey(event->key()); |
| 340 if (event->ctrlKey() || event->metaKey() || event->shiftKey()) | |
|
skobes
2016/10/26 17:57:44
Maybe this check should be in focusDirectionForKey
aelias_OOO_until_Jul13
2016/10/26 19:27:39
Done.
| |
| 341 type = WebFocusTypeNone; | |
| 342 if (type != WebFocusTypeNone && isSpatialNavigationEnabled(m_frame) && | |
| 343 !m_frame->document()->inDesignMode()) { | |
| 344 if (page->focusController().advanceFocus(type)) { | |
| 345 event->setDefaultHandled(); | |
| 346 return; | |
| 347 } | |
| 348 } | |
| 349 | |
| 350 if (event->keyEvent()->isSystemKey) | |
| 278 return; | 351 return; |
| 279 | 352 |
| 280 // Arrows and other possible directional navigation keys can be used in design | 353 ScrollDirection scrollDirection; |
| 281 // mode editing. | 354 ScrollGranularity scrollGranularity; |
| 282 if (m_frame->document()->inDesignMode()) | 355 if (!mapKeyCodeForScroll(event->keyCode(), event->modifiers(), |
| 356 &scrollDirection, &scrollGranularity)) | |
| 283 return; | 357 return; |
| 284 | 358 |
| 285 if (page->focusController().advanceFocus(focusType)) | 359 if (m_scrollManager->bubblingScroll(scrollDirection, scrollGranularity, |
| 360 nullptr, possibleFocusedNode)) { | |
| 286 event->setDefaultHandled(); | 361 event->setDefaultHandled(); |
| 362 return; | |
| 363 } | |
| 287 } | 364 } |
| 288 | 365 |
| 289 void KeyboardEventManager::defaultTabEventHandler(KeyboardEvent* event) { | 366 void KeyboardEventManager::defaultTabEventHandler(KeyboardEvent* event) { |
| 290 DCHECK_EQ(event->type(), EventTypeNames::keydown); | 367 DCHECK_EQ(event->type(), EventTypeNames::keydown); |
| 291 | 368 |
| 292 // We should only advance focus on tabs if no special modifier keys are held | 369 // We should only advance focus on tabs if no special modifier keys are held |
| 293 // down. | 370 // down. |
| 294 if (event->ctrlKey() || event->metaKey()) | 371 if (event->ctrlKey() || event->metaKey()) |
| 295 return; | 372 return; |
| 296 | 373 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 if (currentModifiers & ::cmdKey) | 449 if (currentModifiers & ::cmdKey) |
| 373 modifiers |= WebInputEvent::MetaKey; | 450 modifiers |= WebInputEvent::MetaKey; |
| 374 #else | 451 #else |
| 375 // TODO(crbug.com/538289): Implement on other platforms. | 452 // TODO(crbug.com/538289): Implement on other platforms. |
| 376 return static_cast<WebInputEvent::Modifiers>(0); | 453 return static_cast<WebInputEvent::Modifiers>(0); |
| 377 #endif | 454 #endif |
| 378 return static_cast<WebInputEvent::Modifiers>(modifiers); | 455 return static_cast<WebInputEvent::Modifiers>(modifiers); |
| 379 } | 456 } |
| 380 | 457 |
| 381 } // namespace blink | 458 } // namespace blink |
| OLD | NEW |