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
3 // found in the LICENSE file.
5 #import "ui/views/cocoa/native_widget_mac_nswindow.h"
7 #include "base/mac/foundation_util.h"
8 #import "ui/base/cocoa/user_interface_item_command_handler.h"
9 #import "ui/views/cocoa/views_nswindow_delegate.h"
10 #include "ui/views/controls/menu/menu_controller.h"
11 #include "ui/views/widget/native_widget_mac.h"
12 #include "ui/views/widget/widget_delegate.h"
14 @interface NativeWidgetMacNSWindow ()
15 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate;
16 - (views::Widget*)viewsWidget;
17 - (BOOL)hasViewsMenuActive;
19 // Private API on NSWindow, determines whether the title is drawn on the title
20 // bar. The title is still visible in menus, Expose, etc.
21 - (BOOL)_isTitleHidden;
24 @implementation NativeWidgetMacNSWindow {
26 base::scoped_nsobject<CommandDispatcher> commandDispatcher_;
27 base::scoped_nsprotocol<id<UserInterfaceItemCommandHandler>> commandHandler_;
30 - (instancetype)initWithContentRect:(NSRect)contentRect
31 styleMask:(NSUInteger)windowStyle
32 backing:(NSBackingStoreType)bufferingType
33 defer:(BOOL)deferCreation {
34 if ((self = [super initWithContentRect:contentRect
37 defer:deferCreation])) {
38 commandDispatcher_.reset([[CommandDispatcher alloc] initWithOwner:self]);
45 - (void)setCommandDispatcherDelegate:(id<CommandDispatcherDelegate>)delegate {
46 [commandDispatcher_ setDelegate:delegate];
51 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate {
52 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]);
55 - (views::Widget*)viewsWidget {
56 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget();
59 - (BOOL)hasViewsMenuActive {
60 views::MenuController* menuController =
61 views::MenuController::GetActiveInstance();
62 return menuController && menuController->owner() == [self viewsWidget];
65 // NSWindow overrides.
67 - (BOOL)_isTitleHidden {
71 return ![self viewsWidget]->widget_delegate()->ShouldShowWindowTitle();
74 // Ignore [super canBecome{Key,Main}Window]. The default is NO for windows with
75 // NSBorderlessWindowMask, which is not the desired behavior.
76 // Note these can be called via -[NSWindow close] while the widget is being torn
77 // down, so check for a delegate.
78 - (BOOL)canBecomeKeyWindow {
79 return [self delegate] && [self viewsWidget]->CanActivate();
82 - (BOOL)canBecomeMainWindow {
86 // Dialogs shouldn't take large shadows away from their parent window.
87 views::Widget* widget = [self viewsWidget];
88 return widget->CanActivate() && !widget->IsDialogBox();
91 // Override sendEvent to allow key events to be forwarded to a toolkit-views
92 // menu while it is active, and while still allowing any native subview to
93 // retain firstResponder status.
94 - (void)sendEvent:(NSEvent*)event {
95 // Let CommandDispatcher check if this is a redispatched event.
96 if ([commandDispatcher_ preSendEvent:event])
99 NSEventType type = [event type];
100 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) {
101 [super sendEvent:event];
105 // Send to the menu, after converting the event into an action message using
107 if (type == NSKeyDown)
108 [[self contentView] keyDown:event];
110 [[self contentView] keyUp:event];
113 // Override display, since this is the first opportunity Cocoa gives to detect
114 // a visibility change in some cases. For example, restoring from the dock first
115 // calls -[NSWindow display] before any NSWindowDelegate functions and before
116 // ordering the window (and without actually calling -[NSWindow deminiaturize]).
117 // By notifying the delegate that a display is about to occur, it can apply a
118 // correct visibility state, before [super display] requests a draw of the
119 // contentView. -[NSWindow isVisible] can still report NO at this point, so this
120 // gives the delegate time to apply correct visibility before the draw occurs.
122 [[self viewsNSWindowDelegate] onWindowWillDisplay];
126 // Override window order functions to intercept other visibility changes. This
127 // is needed in addition to the -[NSWindow display] override because Cocoa
128 // hardly ever calls display, and reports -[NSWindow isVisible] incorrectly
129 // when ordering in a window for the first time.
130 - (void)orderWindow:(NSWindowOrderingMode)orderingMode
131 relativeTo:(NSInteger)otherWindowNumber {
132 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode];
133 [super orderWindow:orderingMode relativeTo:otherWindowNumber];
134 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil];
137 // NSResponder implementation.
139 - (BOOL)performKeyEquivalent:(NSEvent*)event {
140 return [commandDispatcher_ performKeyEquivalent:event];
143 - (void)cursorUpdate:(NSEvent*)theEvent {
144 // The cursor provided by the delegate should only be applied within the
145 // content area. This is because we rely on the contentView to track the
146 // mouse cursor and forward cursorUpdate: messages up the responder chain.
147 // The cursorUpdate: isn't handled in BridgedContentView because views-style
148 // SetCapture() conflicts with the way tracking events are processed for
149 // the view during a drag. Since the NSWindow is still in the responder chain
150 // overriding cursorUpdate: here handles both cases.
151 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) {
152 [super cursorUpdate:theEvent];
156 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor];
160 [super cursorUpdate:theEvent];
163 // CommandDispatchingWindow implementation.
165 - (void)setCommandHandler:(id<UserInterfaceItemCommandHandler>)commandHandler {
166 commandHandler_.reset([commandHandler retain]);
169 - (BOOL)redispatchKeyEvent:(NSEvent*)event {
170 return [commandDispatcher_ redispatchKeyEvent:event];
173 - (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event {
174 return [super performKeyEquivalent:event];
177 - (void)commandDispatch:(id)sender {
178 [commandHandler_ commandDispatch:sender window:self];
181 - (void)commandDispatchUsingKeyModifiers:(id)sender {
182 [commandHandler_ commandDispatchUsingKeyModifiers:sender window:self];
185 // NSWindow overrides (NSUserInterfaceItemValidations implementation)
187 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
188 // Since this class implements these selectors, |super| will always say they
189 // are enabled. Only use [super] to validate other selectors. If there is no
190 // command handler, defer to AppController.
191 if ([item action] == @selector(commandDispatch:) ||
192 [item action] == @selector(commandDispatchUsingKeyModifiers:)) {
193 return commandHandler_
194 ? [commandHandler_ validateUserInterfaceItem:item window:self]
195 : [[NSApp delegate] validateUserInterfaceItem:item];
198 return [super validateUserInterfaceItem:item];