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

Unified Diff: ui/views/cocoa/bridged_content_view.mm

Issue 809773006: MacViews: Intercept events for Menus (after AppKit has interpreted keystrokes) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@20141205-MacViews-AcceleratedWidget-PLUS-AddingLayers-fromcl-PLUS-bringup
Patch Set: rollback card unmask Created 6 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/events/keycodes/keyboard_code_conversion_mac.mm ('k') | ui/views/controls/menu/menu_controller.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/cocoa/bridged_content_view.mm
diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm
index e3b3d8e0700380423e6c6130153aaaf0dde4fbb3..a151529dea03a7809be9ec3b0725f9f92c891d21 100644
--- a/ui/views/cocoa/bridged_content_view.mm
+++ b/ui/views/cocoa/bridged_content_view.mm
@@ -8,12 +8,18 @@
#import "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "ui/base/ime/text_input_client.h"
+#import "ui/events/cocoa/cocoa_event_utils.h"
+#include "ui/events/keycodes/dom3/dom_code.h"
+#import "ui/events/keycodes/keyboard_code_conversion_mac.h"
#include "ui/gfx/canvas_paint_mac.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/strings/grit/ui_strings.h"
+#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
+using views::MenuController;
+
namespace {
// Convert a |point| in |source_window|'s AppKit coordinate system (origin at
@@ -42,6 +48,15 @@ gfx::Point MovePointToWindow(const NSPoint& point,
// the event to NativeWidgetMac for handling.
- (void)handleMouseEvent:(NSEvent*)theEvent;
+// Handles an NSResponder Action Message by mapping it to the keyCode that
+// toolkit-views expects internally. E.g. moveToBeginningOfLine: would pass
+// ui::VKEY_HOME even though the Home key on Mac defaults to
+// moveToBeginningOfDocument:. This approach also allows action messages a user
+// may have remapped in ~/Library/KeyBindings/DefaultKeyBinding.dict to be
+// catered for.
+- (void)handleActionAsKey:(ui::KeyboardCode)keyCode
+ domCode:(ui::DomCode)domCode;
+
// Execute a command on the currently focused TextInputClient.
// |commandId| should be a resource ID from ui_strings.grd.
- (void)doCommandByID:(int)commandId;
@@ -111,6 +126,23 @@ gfx::Point MovePointToWindow(const NSPoint& point,
hostedView_->GetWidget()->OnMouseEvent(&event);
}
+- (void)handleActionAsKey:(ui::KeyboardCode)keyCode
+ domCode:(ui::DomCode)domCode {
+ if (!hostedView_)
+ return;
+
+ ui::KeyEvent event(ui::ET_KEY_PRESSED, keyCode, domCode,
+ ui::EventFlagsFromModifiers([NSEvent modifierFlags]));
+
+ MenuController* menuController = MenuController::GetActiveInstance();
+ if (menuController &&
+ menuController->owner() == hostedView_->GetWidget() &&
+ menuController->OnWillDispatchEvent(event) == ui::POST_DISPATCH_NONE)
+ return;
+
+ hostedView_->GetWidget()->OnKeyEvent(&event);
+}
+
- (void)doCommandByID:(int)commandId {
if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId))
textInputClient_->ExecuteEditingCommand(commandId);
@@ -147,10 +179,7 @@ gfx::Point MovePointToWindow(const NSPoint& point,
// NSResponder implementation.
- (void)keyDown:(NSEvent*)theEvent {
- if (textInputClient_)
- [self interpretKeyEvents:@[ theEvent ]];
- else
- [super keyDown:theEvent];
+ [self interpretKeyEvents:@[ theEvent ]];
Andre 2014/12/19 06:41:57 How about when BridgedContentView is not first res
tapted 2015/01/28 11:18:06 So this was an excellent point :) it crossed my m
}
- (void)mouseDown:(NSEvent*)theEvent {
@@ -221,8 +250,19 @@ gfx::Point MovePointToWindow(const NSPoint& point,
}
- (void)insertText:(id)text {
- if (textInputClient_)
- textInputClient_->InsertText(base::SysNSStringToUTF16(text));
+ [self insertText:text replacementRange:NSMakeRange(NSNotFound, 0)];
+}
+
+- (void)moveUp:(id)sender {
+ [self handleActionAsKey:ui::VKEY_UP domCode:ui::DomCode::ARROW_UP];
+}
+
+- (void)moveDown:(id)sender {
+ [self handleActionAsKey:ui::VKEY_DOWN domCode:ui::DomCode::ARROW_DOWN];
+}
+
+- (void)cancelOperation:(id)sender {
+ [self handleActionAsKey:ui::VKEY_ESCAPE domCode:ui::DomCode::ESCAPE];
}
// Support for Services in context menus.
@@ -288,10 +328,12 @@ gfx::Point MovePointToWindow(const NSPoint& point,
}
- (void)doCommandBySelector:(SEL)selector {
- if ([self respondsToSelector:selector])
+ if ([self respondsToSelector:selector]) {
[self performSelector:selector withObject:nil];
- else
+ } else {
+ NSLog(@"Unmatched selector: %@\n", NSStringFromSelector(selector));
[[self nextResponder] doCommandBySelector:selector];
+ }
}
- (NSRect)firstRectForCharacterRange:(NSRange)range
@@ -305,11 +347,26 @@ gfx::Point MovePointToWindow(const NSPoint& point,
}
- (void)insertText:(id)text replacementRange:(NSRange)replacementRange {
- if (!textInputClient_)
+ if (!hostedView_)
return;
if ([text isKindOfClass:[NSAttributedString class]])
text = [text string];
+
+ MenuController* menuController = MenuController::GetActiveInstance();
+ if (menuController && menuController->owner() == hostedView_->GetWidget()) {
+ DCHECK([text length] > 0);
+ ui::KeyEvent event(ui::ET_KEY_PRESSED,
+ ui::KeyboardCodeFromCharCode([text characterAtIndex:0]),
+ ui::DomCode::NONE,
+ ui::EventFlagsFromModifiers([NSEvent modifierFlags]));
+ if (menuController->OnWillDispatchEvent(event) == ui::POST_DISPATCH_NONE)
+ return;
+ }
+
+ if (!textInputClient_)
+ return;
+
textInputClient_->DeleteRange(gfx::Range(replacementRange));
textInputClient_->InsertText(base::SysNSStringToUTF16(text));
}
« no previous file with comments | « ui/events/keycodes/keyboard_code_conversion_mac.mm ('k') | ui/views/controls/menu/menu_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698