| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "base/mac/mac_util.h" | 8 #import "base/mac/mac_util.h" |
| 9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
| 10 #import "base/mac/sdk_forward_declarations.h" | 10 #import "base/mac/sdk_forward_declarations.h" |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 268 |
| 269 @interface BridgedContentView () | 269 @interface BridgedContentView () |
| 270 | 270 |
| 271 // Returns the active menu controller corresponding to |hostedView_|, | 271 // Returns the active menu controller corresponding to |hostedView_|, |
| 272 // nil otherwise. | 272 // nil otherwise. |
| 273 - (MenuController*)activeMenuController; | 273 - (MenuController*)activeMenuController; |
| 274 | 274 |
| 275 // Passes |event| to the InputMethod for dispatch. | 275 // Passes |event| to the InputMethod for dispatch. |
| 276 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 276 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
| 277 | 277 |
| 278 // Allows accelerators to be handled at different points in AppKit key event |
| 279 // dispatch. Checks for an unhandled event passed in to -keyDown: and passes it |
| 280 // to the Widget for processing. Returns YES if the Widget handles it. |
| 281 - (BOOL)handleUnhandledKeyDownAsKeyEvent; |
| 282 |
| 278 // Handles an NSResponder Action Message by mapping it to a corresponding text | 283 // Handles an NSResponder Action Message by mapping it to a corresponding text |
| 279 // editing command from ui_strings.grd and, when not being sent to a | 284 // editing command from ui_strings.grd and, when not being sent to a |
| 280 // TextInputClient, the keyCode that toolkit-views expects internally. | 285 // TextInputClient, the keyCode that toolkit-views expects internally. |
| 281 // For example, moveToLeftEndOfLine: would pass ui::VKEY_HOME in non-RTL locales | 286 // For example, moveToLeftEndOfLine: would pass ui::VKEY_HOME in non-RTL locales |
| 282 // even though the Home key on Mac defaults to moveToBeginningOfDocument:. | 287 // even though the Home key on Mac defaults to moveToBeginningOfDocument:. |
| 283 // This approach also allows action messages a user | 288 // This approach also allows action messages a user |
| 284 // may have remapped in ~/Library/KeyBindings/DefaultKeyBinding.dict to be | 289 // may have remapped in ~/Library/KeyBindings/DefaultKeyBinding.dict to be |
| 285 // catered for. | 290 // catered for. |
| 286 // Note: default key bindings in Mac can be read from StandardKeyBinding.dict | 291 // Note: default key bindings in Mac can be read from StandardKeyBinding.dict |
| 287 // which lives in /System/Library/Frameworks/AppKit.framework/Resources. Do | 292 // which lives in /System/Library/Frameworks/AppKit.framework/Resources. Do |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 if (!hostedView_) | 453 if (!hostedView_) |
| 449 return; | 454 return; |
| 450 | 455 |
| 451 DCHECK(event); | 456 DCHECK(event); |
| 452 if (DispatchEventToMenu([self activeMenuController], event)) | 457 if (DispatchEventToMenu([self activeMenuController], event)) |
| 453 return; | 458 return; |
| 454 | 459 |
| 455 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(event); | 460 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(event); |
| 456 } | 461 } |
| 457 | 462 |
| 463 - (BOOL)handleUnhandledKeyDownAsKeyEvent { |
| 464 if (!keyDownEvent_) |
| 465 return NO; |
| 466 |
| 467 ui::KeyEvent event(keyDownEvent_); |
| 468 [self handleKeyEvent:&event]; |
| 469 keyDownEvent_ = nil; |
| 470 return event.handled(); |
| 471 } |
| 472 |
| 458 - (void)handleAction:(ui::TextEditCommand)command | 473 - (void)handleAction:(ui::TextEditCommand)command |
| 459 keyCode:(ui::KeyboardCode)keyCode | 474 keyCode:(ui::KeyboardCode)keyCode |
| 460 domCode:(ui::DomCode)domCode | 475 domCode:(ui::DomCode)domCode |
| 461 eventFlags:(int)eventFlags { | 476 eventFlags:(int)eventFlags { |
| 462 if (!hostedView_) | 477 if (!hostedView_) |
| 463 return; | 478 return; |
| 464 | 479 |
| 465 // Generate a synthetic event with the keycode toolkit-views expects. | 480 // Generate a synthetic event with the keycode toolkit-views expects. |
| 466 ui::KeyEvent event(ui::ET_KEY_PRESSED, keyCode, domCode, eventFlags); | 481 ui::KeyEvent event(ui::ET_KEY_PRESSED, keyCode, domCode, eventFlags); |
| 467 | 482 |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 case ui::TEXT_INPUT_TYPE_NONE: | 773 case ui::TEXT_INPUT_TYPE_NONE: |
| 759 case ui::TEXT_INPUT_TYPE_PASSWORD: | 774 case ui::TEXT_INPUT_TYPE_PASSWORD: |
| 760 return nil; | 775 return nil; |
| 761 default: | 776 default: |
| 762 return [super inputContext]; | 777 return [super inputContext]; |
| 763 } | 778 } |
| 764 } | 779 } |
| 765 | 780 |
| 766 // NSResponder implementation. | 781 // NSResponder implementation. |
| 767 | 782 |
| 783 - (BOOL)_wantsKeyDownForEvent:(NSEvent*)event { |
| 784 // This is a SPI that AppKit apparently calls after |performKeyEquivalent:| |
| 785 // returned NO. If this function returns |YES|, Cocoa sends the event to |
| 786 // |keyDown:| instead of doing other things with it. Ctrl-tab will be sent |
| 787 // to us instead of doing key view loop control, ctrl-left/right get handled |
| 788 // correctly, etc. |
| 789 // (However, there are still some keys that Cocoa swallows, e.g. the key |
| 790 // equivalent that Cocoa uses for toggling the input language. In this case, |
| 791 // that's actually a good thing, though -- see http://crbug.com/26115 .) |
| 792 return YES; |
| 793 } |
| 794 |
| 768 - (void)keyDown:(NSEvent*)theEvent { | 795 - (void)keyDown:(NSEvent*)theEvent { |
| 769 // Convert the event into an action message, according to OSX key mappings. | 796 // Convert the event into an action message, according to OSX key mappings. |
| 770 keyDownEvent_ = theEvent; | 797 keyDownEvent_ = theEvent; |
| 771 [self interpretKeyEvents:@[ theEvent ]]; | 798 [self interpretKeyEvents:@[ theEvent ]]; |
| 772 keyDownEvent_ = nil; | 799 |
| 800 // If |keyDownEvent_| wasn't cleared during -interpretKeyEvents:, it wasn't |
| 801 // handled. Give Widget accelerators a chance to handle it. |
| 802 [self handleUnhandledKeyDownAsKeyEvent]; |
| 803 DCHECK(!keyDownEvent_); |
| 773 } | 804 } |
| 774 | 805 |
| 775 - (void)keyUp:(NSEvent*)theEvent { | 806 - (void)keyUp:(NSEvent*)theEvent { |
| 776 ui::KeyEvent event(theEvent); | 807 ui::KeyEvent event(theEvent); |
| 777 [self handleKeyEvent:&event]; | 808 [self handleKeyEvent:&event]; |
| 778 } | 809 } |
| 779 | 810 |
| 780 - (void)scrollWheel:(NSEvent*)theEvent { | 811 - (void)scrollWheel:(NSEvent*)theEvent { |
| 781 if (!hostedView_) | 812 if (!hostedView_) |
| 782 return; | 813 return; |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1281 } | 1312 } |
| 1282 | 1313 |
| 1283 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { | 1314 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { |
| 1284 NOTIMPLEMENTED(); | 1315 NOTIMPLEMENTED(); |
| 1285 return 0; | 1316 return 0; |
| 1286 } | 1317 } |
| 1287 | 1318 |
| 1288 - (void)doCommandBySelector:(SEL)selector { | 1319 - (void)doCommandBySelector:(SEL)selector { |
| 1289 // Like the renderer, handle insert action messages as a regular key dispatch. | 1320 // Like the renderer, handle insert action messages as a regular key dispatch. |
| 1290 // This ensures, e.g., insertTab correctly changes focus between fields. | 1321 // This ensures, e.g., insertTab correctly changes focus between fields. |
| 1291 if (keyDownEvent_ && [NSStringFromSelector(selector) hasPrefix:@"insert"]) { | 1322 if (keyDownEvent_ && [NSStringFromSelector(selector) hasPrefix:@"insert"]) |
| 1292 ui::KeyEvent event(keyDownEvent_); | 1323 return; // Handle in -keyDown:. |
| 1293 [self handleKeyEvent:&event]; | 1324 |
| 1325 if ([self respondsToSelector:selector]) { |
| 1326 [self performSelector:selector withObject:nil]; |
| 1327 keyDownEvent_ = nil; |
| 1294 return; | 1328 return; |
| 1295 } | 1329 } |
| 1296 | 1330 |
| 1297 if ([self respondsToSelector:selector]) | 1331 // For events that AppKit sends via doCommandBySelector:, first attempt to |
| 1298 [self performSelector:selector withObject:nil]; | 1332 // handle as a Widget accelerator. Forward along the responder chain only if |
| 1299 else | 1333 // the Widget doesn't handle it. |
| 1334 if (![self handleUnhandledKeyDownAsKeyEvent]) |
| 1300 [[self nextResponder] doCommandBySelector:selector]; | 1335 [[self nextResponder] doCommandBySelector:selector]; |
| 1301 } | 1336 } |
| 1302 | 1337 |
| 1303 - (NSRect)firstRectForCharacterRange:(NSRange)range | 1338 - (NSRect)firstRectForCharacterRange:(NSRange)range |
| 1304 actualRange:(NSRangePointer)actualNSRange { | 1339 actualRange:(NSRangePointer)actualNSRange { |
| 1305 gfx::Range actualRange; | 1340 gfx::Range actualRange; |
| 1306 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, | 1341 gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, |
| 1307 gfx::Range(range), &actualRange); | 1342 gfx::Range(range), &actualRange); |
| 1308 if (actualNSRange) | 1343 if (actualNSRange) |
| 1309 *actualNSRange = actualRange.ToNSRange(); | 1344 *actualNSRange = actualRange.ToNSRange(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1432 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1467 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 1433 } | 1468 } |
| 1434 | 1469 |
| 1435 - (id)accessibilityFocusedUIElement { | 1470 - (id)accessibilityFocusedUIElement { |
| 1436 if (!hostedView_) | 1471 if (!hostedView_) |
| 1437 return nil; | 1472 return nil; |
| 1438 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; | 1473 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; |
| 1439 } | 1474 } |
| 1440 | 1475 |
| 1441 @end | 1476 @end |
| OLD | NEW |