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 commandDispatcher_.reset([[CommandDispatcher alloc] initWithOwner:self]); |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 // Public methods. |
| 39 |
| 40 - (void)setCommandDispatcherDelegate:(id<CommandDispatcherDelegate>)delegate { |
| 41 [commandDispatcher_ setDelegate:delegate]; |
| 42 } |
| 43 |
| 44 // Private methods. |
| 45 |
25 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate { | 46 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate { |
26 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]); | 47 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]); |
27 } | 48 } |
28 | 49 |
29 - (views::Widget*)viewsWidget { | 50 - (views::Widget*)viewsWidget { |
30 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget(); | 51 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget(); |
31 } | 52 } |
32 | 53 |
33 - (BOOL)hasViewsMenuActive { | 54 - (BOOL)hasViewsMenuActive { |
34 views::MenuController* menuController = | 55 views::MenuController* menuController = |
35 views::MenuController::GetActiveInstance(); | 56 views::MenuController::GetActiveInstance(); |
36 return menuController && menuController->owner() == [self viewsWidget]; | 57 return menuController && menuController->owner() == [self viewsWidget]; |
37 } | 58 } |
38 | 59 |
| 60 // NSWindow overrides. |
| 61 |
39 - (BOOL)_isTitleHidden { | 62 - (BOOL)_isTitleHidden { |
40 if (![self delegate]) | 63 if (![self delegate]) |
41 return NO; | 64 return NO; |
42 | 65 |
43 return ![self viewsWidget]->widget_delegate()->ShouldShowWindowTitle(); | 66 return ![self viewsWidget]->widget_delegate()->ShouldShowWindowTitle(); |
44 } | 67 } |
45 | 68 |
46 // Ignore [super canBecome{Key,Main}Window]. The default is NO for windows with | 69 // Ignore [super canBecome{Key,Main}Window]. The default is NO for windows with |
47 // NSBorderlessWindowMask, which is not the desired behavior. | 70 // NSBorderlessWindowMask, which is not the desired behavior. |
48 // Note these can be called via -[NSWindow close] while the widget is being torn | 71 // Note these can be called via -[NSWindow close] while the widget is being torn |
49 // down, so check for a delegate. | 72 // down, so check for a delegate. |
50 - (BOOL)canBecomeKeyWindow { | 73 - (BOOL)canBecomeKeyWindow { |
51 return [self delegate] && [self viewsWidget]->CanActivate(); | 74 return [self delegate] && [self viewsWidget]->CanActivate(); |
52 } | 75 } |
53 | 76 |
54 - (BOOL)canBecomeMainWindow { | 77 - (BOOL)canBecomeMainWindow { |
55 if (![self delegate]) | 78 if (![self delegate]) |
56 return NO; | 79 return NO; |
57 | 80 |
58 // Dialogs shouldn't take large shadows away from their parent window. | 81 // Dialogs shouldn't take large shadows away from their parent window. |
59 views::Widget* widget = [self viewsWidget]; | 82 views::Widget* widget = [self viewsWidget]; |
60 return widget->CanActivate() && !widget->IsDialogBox(); | 83 return widget->CanActivate() && !widget->IsDialogBox(); |
61 } | 84 } |
62 | 85 |
63 // Override sendEvent to allow key events to be forwarded to a toolkit-views | 86 // 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 | 87 // menu while it is active, and while still allowing any native subview to |
65 // retain firstResponder status. | 88 // retain firstResponder status. |
66 - (void)sendEvent:(NSEvent*)event { | 89 - (void)sendEvent:(NSEvent*)event { |
| 90 // Let CommandDispatcher check if this is a redispatched event. |
| 91 if ([commandDispatcher_ preSendEvent:event]) |
| 92 return; |
| 93 |
67 NSEventType type = [event type]; | 94 NSEventType type = [event type]; |
68 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) { | 95 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) { |
69 [super sendEvent:event]; | 96 [super sendEvent:event]; |
70 return; | 97 return; |
71 } | 98 } |
72 | 99 |
73 // Send to the menu, after converting the event into an action message using | 100 // Send to the menu, after converting the event into an action message using |
74 // the content view. | 101 // the content view. |
75 if (type == NSKeyDown) | 102 if (type == NSKeyDown) |
76 [[self contentView] keyDown:event]; | 103 [[self contentView] keyDown:event]; |
(...skipping 20 matching lines...) Expand all Loading... |
97 // when ordering in a window for the first time. | 124 // when ordering in a window for the first time. |
98 - (void)orderWindow:(NSWindowOrderingMode)orderingMode | 125 - (void)orderWindow:(NSWindowOrderingMode)orderingMode |
99 relativeTo:(NSInteger)otherWindowNumber { | 126 relativeTo:(NSInteger)otherWindowNumber { |
100 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode]; | 127 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode]; |
101 [super orderWindow:orderingMode relativeTo:otherWindowNumber]; | 128 [super orderWindow:orderingMode relativeTo:otherWindowNumber]; |
102 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil]; | 129 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil]; |
103 } | 130 } |
104 | 131 |
105 // NSResponder implementation. | 132 // NSResponder implementation. |
106 | 133 |
| 134 - (BOOL)performKeyEquivalent:(NSEvent*)event { |
| 135 return [commandDispatcher_ performKeyEquivalent:event]; |
| 136 } |
| 137 |
107 - (void)cursorUpdate:(NSEvent*)theEvent { | 138 - (void)cursorUpdate:(NSEvent*)theEvent { |
108 // The cursor provided by the delegate should only be applied within the | 139 // 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 | 140 // content area. This is because we rely on the contentView to track the |
110 // mouse cursor and forward cursorUpdate: messages up the responder chain. | 141 // mouse cursor and forward cursorUpdate: messages up the responder chain. |
111 // The cursorUpdate: isn't handled in BridgedContentView because views-style | 142 // The cursorUpdate: isn't handled in BridgedContentView because views-style |
112 // SetCapture() conflicts with the way tracking events are processed for | 143 // 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 | 144 // the view during a drag. Since the NSWindow is still in the responder chain |
114 // overriding cursorUpdate: here handles both cases. | 145 // overriding cursorUpdate: here handles both cases. |
115 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) { | 146 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) { |
116 [super cursorUpdate:theEvent]; | 147 [super cursorUpdate:theEvent]; |
117 return; | 148 return; |
118 } | 149 } |
119 | 150 |
120 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor]; | 151 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor]; |
121 if (cursor) | 152 if (cursor) |
122 [cursor set]; | 153 [cursor set]; |
123 else | 154 else |
124 [super cursorUpdate:theEvent]; | 155 [super cursorUpdate:theEvent]; |
125 } | 156 } |
126 | 157 |
| 158 // CommandDispatcher implementation. |
| 159 |
| 160 @synthesize commandHandler; |
| 161 |
| 162 - (BOOL)redispatchKeyEvent:(NSEvent*)event { |
| 163 return [commandDispatcher_ redispatchKeyEvent:event]; |
| 164 } |
| 165 |
| 166 - (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event { |
| 167 return [super performKeyEquivalent:event]; |
| 168 } |
| 169 |
| 170 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { |
| 171 return [[self commandHandler] validateUserInterfaceItem:item window:self]; |
| 172 } |
| 173 |
| 174 - (void)commandDispatch:(id)sender { |
| 175 [[self commandHandler] commandDispatch:sender window:self]; |
| 176 } |
| 177 |
| 178 - (void)commandDispatchUsingKeyModifiers:(id)sender { |
| 179 [[self commandHandler] commandDispatchUsingKeyModifiers:sender window:self]; |
| 180 } |
| 181 |
127 @end | 182 @end |
OLD | NEW |