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/views/cocoa/views_nswindow_delegate.h"
9 #include "ui/views/controls/menu/menu_controller.h"
10 #include "ui/views/widget/native_widget_mac.h"
11 #include "ui/views/widget/widget_delegate.h"
13 @interface NativeWidgetMacNSWindow ()
14 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate;
15 - (views::Widget*)viewsWidget;
16 - (BOOL)hasViewsMenuActive;
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.
20 - (BOOL)_isTitleHidden;
23 @implementation NativeWidgetMacNSWindow
25 - (ViewsNSWindowDelegate*)viewsNSWindowDelegate {
26 return base::mac::ObjCCastStrict<ViewsNSWindowDelegate>([self delegate]);
29 - (views::Widget*)viewsWidget {
30 return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget();
33 - (BOOL)hasViewsMenuActive {
34 views::MenuController* menuController =
35 views::MenuController::GetActiveInstance();
36 return menuController && menuController->owner() == [self viewsWidget];
39 - (BOOL)_isTitleHidden {
43 return ![self viewsWidget]->widget_delegate()->ShouldShowWindowTitle();
46 // Ignore [super canBecome{Key,Main}Window]. The default is NO for windows with
47 // NSBorderlessWindowMask, which is not the desired behavior.
48 // Note these can be called via -[NSWindow close] while the widget is being torn
49 // down, so check for a delegate.
50 - (BOOL)canBecomeKeyWindow {
51 return [self delegate] && [self viewsWidget]->CanActivate();
54 - (BOOL)canBecomeMainWindow {
58 // Dialogs shouldn't take large shadows away from their parent window.
59 views::Widget* widget = [self viewsWidget];
60 return widget->CanActivate() && !widget->IsDialogBox();
63 // 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
65 // retain firstResponder status.
66 - (void)sendEvent:(NSEvent*)event {
67 NSEventType type = [event type];
68 if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) {
69 [super sendEvent:event];
73 // Send to the menu, after converting the event into an action message using
75 if (type == NSKeyDown)
76 [[self contentView] keyDown:event];
78 [[self contentView] keyUp:event];
81 // Override display, since this is the first opportunity Cocoa gives to detect
82 // a visibility change in some cases. For example, restoring from the dock first
83 // calls -[NSWindow display] before any NSWindowDelegate functions and before
84 // ordering the window (and without actually calling -[NSWindow deminiaturize]).
85 // By notifying the delegate that a display is about to occur, it can apply a
86 // correct visibility state, before [super display] requests a draw of the
87 // contentView. -[NSWindow isVisible] can still report NO at this point, so this
88 // gives the delegate time to apply correct visibility before the draw occurs.
90 [[self viewsNSWindowDelegate] onWindowWillDisplay];
94 // Override window order functions to intercept other visibility changes. This
95 // is needed in addition to the -[NSWindow display] override because Cocoa
96 // hardly ever calls display, and reports -[NSWindow isVisible] incorrectly
97 // when ordering in a window for the first time.
98 - (void)orderWindow:(NSWindowOrderingMode)orderingMode
99 relativeTo:(NSInteger)otherWindowNumber {
100 [[self viewsNSWindowDelegate] onWindowOrderWillChange:orderingMode];
101 [super orderWindow:orderingMode relativeTo:otherWindowNumber];
102 [[self viewsNSWindowDelegate] onWindowOrderChanged:nil];
105 // NSResponder implementation.
107 - (void)cursorUpdate:(NSEvent*)theEvent {
108 // 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
110 // mouse cursor and forward cursorUpdate: messages up the responder chain.
111 // The cursorUpdate: isn't handled in BridgedContentView because views-style
112 // 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
114 // overriding cursorUpdate: here handles both cases.
115 if (!NSPointInRect([theEvent locationInWindow], [[self contentView] frame])) {
116 [super cursorUpdate:theEvent];
120 NSCursor* cursor = [[self viewsNSWindowDelegate] cursor];
124 [super cursorUpdate:theEvent];