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/native_widget_mac_nswindow.h" | 5 #import "ui/views/cocoa/native_widget_mac_nswindow.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #import "ui/views/cocoa/views_nswindow_delegate.h" | 8 #import "ui/views/cocoa/views_nswindow_delegate.h" |
9 #include "ui/views/controls/menu/menu_controller.h" | 9 #include "ui/views/controls/menu/menu_controller.h" |
10 #include "ui/views/widget/native_widget_mac.h" | 10 #include "ui/views/widget/native_widget_mac.h" |
11 #include "ui/views/widget/widget_delegate.h" | 11 #include "ui/views/widget/widget_delegate.h" |
12 | 12 |
13 @interface NativeWidgetMacNSWindow () | 13 @interface NativeWidgetMacNSWindow () |
14 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate; | 14 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate; |
15 - (views::Widget*)viewsWidget; | 15 - (views::Widget*)viewsWidget; |
16 - (BOOL)hasViewsMenuActive; | 16 - (BOOL)hasViewsMenuActive; |
17 | 17 |
18 // Private API on NSWindow, determines whether the title is drawn on the title | 18 // Private API on NSWindow, determines whether the title is drawn on the title |
19 // bar. The title is still visible in menus, Expose, etc. | 19 // bar. The title is still visible in menus, Expose, etc. |
20 - (BOOL)_isTitleHidden; | 20 - (BOOL)_isTitleHidden; |
21 @end | 21 @end |
22 | 22 |
23 @implementation NativeWidgetMacNSWindow | 23 @implementation NativeWidgetMacNSWindow |
24 | 24 |
| 25 - (instancetype)initWithContentRect:(NSRect)contentRect |
| 26 styleMask:(NSUInteger)windowStyle |
| 27 backing:(NSBackingStoreType)bufferingType |
| 28 defer:(BOOL)deferCreation { |
| 29 if ((self = [super initWithContentRect:contentRect |
| 30 styleMask:windowStyle |
| 31 backing:bufferingType |
| 32 defer:deferCreation])) { |
| 33 commandDispatcherImpl_.reset( |
| 34 [[CommandDispatcherImpl alloc] initWithOwner:self]); |
| 35 } |
| 36 return self; |
| 37 } |
| 38 |
25 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate { | 39 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate { |
26 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]); | 40 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]); |
27 } | 41 } |
28 | 42 |
29 - (views::Widget*)viewsWidget { | 43 - (views::Widget*)viewsWidget { |
30 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget(); | 44 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget(); |
31 } | 45 } |
32 | 46 |
33 - (BOOL)hasViewsMenuActive { | 47 - (BOOL)hasViewsMenuActive { |
34 views::MenuController* menuController = | 48 views::MenuController* menuController = |
(...skipping 22 matching lines...) Expand all Loading... |
57 | 71 |
58 // Dialogs shouldn't take large shadows away from their parent window. | 72 // Dialogs shouldn't take large shadows away from their parent window. |
59 views::Widget* widget = [self viewsWidget]; | 73 views::Widget* widget = [self viewsWidget]; |
60 return widget->CanActivate() && !widget->IsDialogBox(); | 74 return widget->CanActivate() && !widget->IsDialogBox(); |
61 } | 75 } |
62 | 76 |
63 // Override sendEvent to allow key events to be forwarded to a toolkit-views | 77 // Override sendEvent to allow key events to be forwarded to a toolkit-views |
64 // menu while it is active, and while still allowing any native subview to | 78 // menu while it is active, and while still allowing any native subview to |
65 // retain firstResponder status. | 79 // retain firstResponder status. |
66 - (void)sendEvent:(NSEvent*)event { | 80 - (void)sendEvent:(NSEvent*)event { |
| 81 // Let CommandDispatcherImpl check if this is a redispatched event. |
| 82 if ([commandDispatcherImpl_ preSendEvent:event]) |
| 83 return; |
| 84 |
67 NSEventType type = [event type]; | 85 NSEventType type = [event type]; |
68 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) { | 86 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) { |
69 [super sendEvent:event]; | 87 [super sendEvent:event]; |
70 return; | 88 return; |
71 } | 89 } |
72 | 90 |
73 // Send to the menu, after converting the event into an action message using | 91 // Send to the menu, after converting the event into an action message using |
74 // the content view. | 92 // the content view. |
75 if (type == NSKeyDown) | 93 if (type == NSKeyDown) |
76 [[self contentView] keyDown:event]; | 94 [[self contentView] keyDown:event]; |
(...skipping 20 matching lines...) Expand all Loading... |
97 // when ordering in a window for the first time. | 115 // when ordering in a window for the first time. |
98 - (void)orderWindow:(NSWindowOrderingMode)orderingMode | 116 - (void)orderWindow:(NSWindowOrderingMode)orderingMode |
99 relativeTo:(NSInteger)otherWindowNumber { | 117 relativeTo:(NSInteger)otherWindowNumber { |
100 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode]; | 118 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode]; |
101 [super orderWindow:orderingMode relativeTo:otherWindowNumber]; | 119 [super orderWindow:orderingMode relativeTo:otherWindowNumber]; |
102 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil]; | 120 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil]; |
103 } | 121 } |
104 | 122 |
105 // NSResponder implementation. | 123 // NSResponder implementation. |
106 | 124 |
| 125 - (BOOL)performKeyEquivalent:(NSEvent*)event { |
| 126 return [commandDispatcherImpl_ performKeyEquivalent:event]; |
| 127 } |
| 128 |
107 - (void)cursorUpdate:(NSEvent*)theEvent { | 129 - (void)cursorUpdate:(NSEvent*)theEvent { |
108 // The cursor provided by the delegate should only be applied within the | 130 // The cursor provided by the delegate should only be applied within the |
109 // content area. This is because we rely on the contentView to track the | 131 // content area. This is because we rely on the contentView to track the |
110 // mouse cursor and forward cursorUpdate: messages up the responder chain. | 132 // mouse cursor and forward cursorUpdate: messages up the responder chain. |
111 // The cursorUpdate: isn't handled in BridgedContentView because views-style | 133 // The cursorUpdate: isn't handled in BridgedContentView because views-style |
112 // SetCapture() conflicts with the way tracking events are processed for | 134 // SetCapture() conflicts with the way tracking events are processed for |
113 // the view during a drag. Since the NSWindow is still in the responder chain | 135 // the view during a drag. Since the NSWindow is still in the responder chain |
114 // overriding cursorUpdate: here handles both cases. | 136 // overriding cursorUpdate: here handles both cases. |
115 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) { | 137 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) { |
116 [super cursorUpdate:theEvent]; | 138 [super cursorUpdate:theEvent]; |
117 return; | 139 return; |
118 } | 140 } |
119 | 141 |
120 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor]; | 142 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor]; |
121 if (cursor) | 143 if (cursor) |
122 [cursor set]; | 144 [cursor set]; |
123 else | 145 else |
124 [super cursorUpdate:theEvent]; | 146 [super cursorUpdate:theEvent]; |
125 } | 147 } |
126 | 148 |
| 149 // CommandDispatcher implementation. |
| 150 |
| 151 - (void)setCommandDispatcherDelegate:(id<CommandDispatcherDelegate>)delegate { |
| 152 [commandDispatcherImpl_ setDelegate:delegate]; |
| 153 } |
| 154 |
| 155 - (BOOL)redispatchKeyEvent:(NSEvent*)event { |
| 156 return [commandDispatcherImpl_ redispatchKeyEvent:event]; |
| 157 } |
| 158 |
| 159 - (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event { |
| 160 return [super performKeyEquivalent:event]; |
| 161 } |
| 162 |
127 @end | 163 @end |
OLD | NEW |