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/views_nswindow_delegate.h"
7 #include "base/logging.h"
8 #import "ui/views/cocoa/bridged_content_view.h"
9 #import "ui/views/cocoa/bridged_native_widget.h"
10 #include "ui/views/widget/native_widget_mac.h"
12 @implementation ViewsNSWindowDelegate
14 - (id)initWithBridgedNativeWidget:(views::BridgedNativeWidget*)parent {
16 if ((self = [super init])) {
22 - (views::NativeWidgetMac*)nativeWidgetMac {
23 return parent_->native_widget_mac();
30 - (void)setCursor:(NSCursor*)newCursor {
31 if (cursor_.get() == newCursor)
34 cursor_.reset([newCursor retain]);
35 [parent_->ns_window() resetCursorRects];
38 - (void)onWindowOrderWillChange:(NSWindowOrderingMode)orderingMode {
39 parent_->OnVisibilityChangedTo(orderingMode != NSWindowOut);
42 - (void)onWindowOrderChanged:(NSNotification*)notification {
43 parent_->OnVisibilityChanged();
46 - (void)onWindowWillDisplay {
47 parent_->OnVisibilityChangedTo(true);
50 - (void)sheetDidEnd:(NSWindow*)sheet
51 returnCode:(NSInteger)returnCode
52 contextInfo:(void*)contextInfo {
54 parent_->OnWindowWillClose();
57 - (BOOL)shouldRepostPendingLeftMouseDown:(NSPoint)locationInWindow {
58 return parent_->ShouldRepostPendingLeftMouseDown(locationInWindow);
61 // NSWindowDelegate implementation.
63 - (void)windowDidFailToEnterFullScreen:(NSWindow*)window {
64 // Cocoa should already have sent an (unexpected) windowDidExitFullScreen:
65 // notification, and the attempt to get back into fullscreen should fail.
66 // Nothing to do except verify |parent_| is no longer trying to fullscreen.
67 DCHECK(!parent_->target_fullscreen_state());
70 - (void)windowDidFailToExitFullScreen:(NSWindow*)window {
71 // Unlike entering fullscreen, windowDidFailToExitFullScreen: is sent *before*
72 // windowDidExitFullScreen:. Also, failing to exit fullscreen just dumps the
73 // window out of fullscreen without an animation; still sending the expected,
74 // windowDidExitFullScreen: notification. So, again, nothing to do here.
75 DCHECK(!parent_->target_fullscreen_state());
78 - (void)windowDidResize:(NSNotification*)notification {
79 parent_->OnSizeChanged();
82 - (void)windowDidBecomeKey:(NSNotification*)notification {
83 parent_->OnWindowKeyStatusChangedTo(true);
86 - (void)windowDidResignKey:(NSNotification*)notification {
87 parent_->OnWindowKeyStatusChangedTo(false);
90 - (void)windowWillClose:(NSNotification*)notification {
91 DCHECK([parent_->ns_window() isEqual:[notification object]]);
92 parent_->OnWindowWillClose();
95 - (void)windowDidMiniaturize:(NSNotification*)notification {
96 parent_->OnVisibilityChanged();
99 - (void)windowDidDeminiaturize:(NSNotification*)notification {
100 parent_->OnVisibilityChanged();
103 - (void)windowDidChangeBackingProperties:(NSNotification*)notification {
104 parent_->OnBackingPropertiesChanged();
107 - (void)windowWillEnterFullScreen:(NSNotification*)notification {
108 parent_->OnFullscreenTransitionStart(true);
111 - (void)windowDidEnterFullScreen:(NSNotification*)notification {
112 parent_->OnFullscreenTransitionComplete(true);
115 - (void)windowWillExitFullScreen:(NSNotification*)notification {
116 parent_->OnFullscreenTransitionStart(false);
119 - (void)windowDidExitFullScreen:(NSNotification*)notification {
120 parent_->OnFullscreenTransitionComplete(false);
123 // Allow non-resizable windows (without NSResizableWindowMask) to fill the
124 // screen in fullscreen mode. This only happens when
125 // -[NSWindow toggleFullscreen:] is called since non-resizable windows have no
126 // fullscreen button. Without this they would only enter fullscreen at their
128 - (NSSize)window:(NSWindow*)window
129 willUseFullScreenContentSize:(NSSize)proposedSize {
133 // Override to correctly position modal dialogs.
134 - (NSRect)window:(NSWindow*)window
135 willPositionSheet:(NSWindow*)sheet
136 usingRect:(NSRect)defaultSheetLocation {
137 // As per NSWindowDelegate documentation, the origin indicates the top left
138 // point of the host frame in window coordinates. The width changes the
139 // animation from vertical to trapezoid if it is smaller than the width of the
140 // dialog. The height is ignored but should be set to zero.
141 return NSMakeRect(0, [self nativeWidgetMac]->SheetPositionY(),
142 NSWidth(defaultSheetLocation), 0);