1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/views/apps/chrome_native_app_window_views_mac.h"
#import <Cocoa/Cocoa.h>
#import "base/mac/scoped_nsobject.h"
#import "base/mac/sdk_forward_declarations.h"
#include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h"
#import "chrome/browser/ui/views/apps/app_window_native_widget_mac.h"
#import "chrome/browser/ui/views/apps/native_app_window_frame_view_mac.h"
#import "ui/gfx/mac/coordinate_conversion.h"
// This observer is used to get NSWindow notifications. We need to monitor
// zoom and full screen events to store the correct bounds to Restore() to.
@interface ResizeNotificationObserver : NSObject {
@private
// Weak. Owns us.
ChromeNativeAppWindowViewsMac* nativeAppWindow_;
}
- (id)initForNativeAppWindow:(ChromeNativeAppWindowViewsMac*)nativeAppWindow;
- (void)onWindowWillStartLiveResize:(NSNotification*)notification;
- (void)onWindowWillExitFullScreen:(NSNotification*)notification;
- (void)onWindowDidExitFullScreen:(NSNotification*)notification;
- (void)stopObserving;
@end
@implementation ResizeNotificationObserver
- (id)initForNativeAppWindow:(ChromeNativeAppWindowViewsMac*)nativeAppWindow {
if ((self = [super init])) {
nativeAppWindow_ = nativeAppWindow;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onWindowWillStartLiveResize:)
name:NSWindowWillStartLiveResizeNotification
object:static_cast<ui::BaseWindow*>(nativeAppWindow)
->GetNativeWindow()];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onWindowWillExitFullScreen:)
name:NSWindowWillExitFullScreenNotification
object:static_cast<ui::BaseWindow*>(nativeAppWindow)
->GetNativeWindow()];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onWindowDidExitFullScreen:)
name:NSWindowDidExitFullScreenNotification
object:static_cast<ui::BaseWindow*>(nativeAppWindow)
->GetNativeWindow()];
}
return self;
}
- (void)onWindowWillStartLiveResize:(NSNotification*)notification {
nativeAppWindow_->OnWindowWillStartLiveResize();
}
- (void)onWindowWillExitFullScreen:(NSNotification*)notification {
nativeAppWindow_->OnWindowWillExitFullScreen();
}
- (void)onWindowDidExitFullScreen:(NSNotification*)notification {
nativeAppWindow_->OnWindowDidExitFullScreen();
}
- (void)stopObserving {
[[NSNotificationCenter defaultCenter] removeObserver:self];
nativeAppWindow_ = nullptr;
}
@end
namespace {
bool NSWindowIsMaximized(NSWindow* window) {
// -[NSWindow isZoomed] only works if the zoom button is enabled.
if ([[window standardWindowButton:NSWindowZoomButton] isEnabled])
return [window isZoomed];
// We don't attempt to distinguish between a window that has been explicitly
// maximized versus one that has just been dragged by the user to fill the
// screen. This is the same behavior as -[NSWindow isZoomed] above.
return NSEqualRects([window frame], [[window screen] visibleFrame]);
}
} // namespace
ChromeNativeAppWindowViewsMac::ChromeNativeAppWindowViewsMac() {}
ChromeNativeAppWindowViewsMac::~ChromeNativeAppWindowViewsMac() {
[nswindow_observer_ stopObserving];
}
void ChromeNativeAppWindowViewsMac::OnWindowWillStartLiveResize() {
if (!NSWindowIsMaximized(GetNativeWindow()) && !in_fullscreen_transition_)
bounds_before_maximize_ = [GetNativeWindow() frame];
}
void ChromeNativeAppWindowViewsMac::OnWindowWillExitFullScreen() {
in_fullscreen_transition_ = true;
}
void ChromeNativeAppWindowViewsMac::OnWindowDidExitFullScreen() {
in_fullscreen_transition_ = false;
}
void ChromeNativeAppWindowViewsMac::OnBeforeWidgetInit(
const extensions::AppWindow::CreateParams& create_params,
views::Widget::InitParams* init_params,
views::Widget* widget) {
DCHECK(!init_params->native_widget);
init_params->remove_standard_frame = IsFrameless();
init_params->native_widget = new AppWindowNativeWidgetMac(widget, this);
ChromeNativeAppWindowViews::OnBeforeWidgetInit(create_params, init_params,
widget);
}
views::NonClientFrameView*
ChromeNativeAppWindowViewsMac::CreateStandardDesktopAppFrame() {
return new NativeAppWindowFrameViewMac(widget(), this);
}
views::NonClientFrameView*
ChromeNativeAppWindowViewsMac::CreateNonStandardAppFrame() {
return new NativeAppWindowFrameViewMac(widget(), this);
}
bool ChromeNativeAppWindowViewsMac::IsMaximized() const {
return !IsMinimized() && !IsFullscreen() &&
NSWindowIsMaximized(GetNativeWindow());
}
gfx::Rect ChromeNativeAppWindowViewsMac::GetRestoredBounds() const {
if (NSWindowIsMaximized(GetNativeWindow()))
return gfx::ScreenRectFromNSRect(bounds_before_maximize_);
return ChromeNativeAppWindowViews::GetRestoredBounds();
}
void ChromeNativeAppWindowViewsMac::Show() {
UnhideWithoutActivation();
ChromeNativeAppWindowViews::Show();
}
void ChromeNativeAppWindowViewsMac::ShowInactive() {
if (is_hidden_with_app_)
return;
ChromeNativeAppWindowViews::ShowInactive();
}
void ChromeNativeAppWindowViewsMac::Activate() {
UnhideWithoutActivation();
ChromeNativeAppWindowViews::Activate();
}
void ChromeNativeAppWindowViewsMac::Maximize() {
if (IsFullscreen())
return;
NSWindow* window = GetNativeWindow();
if (!NSWindowIsMaximized(window))
[window setFrame:[[window screen] visibleFrame] display:YES animate:YES];
if (IsMinimized())
[window deminiaturize:nil];
}
void ChromeNativeAppWindowViewsMac::Restore() {
NSWindow* window = GetNativeWindow();
if (NSWindowIsMaximized(window))
[window setFrame:bounds_before_maximize_ display:YES animate:YES];
ChromeNativeAppWindowViews::Restore();
}
void ChromeNativeAppWindowViewsMac::FlashFrame(bool flash) {
apps::ExtensionAppShimHandler::RequestUserAttentionForWindow(
app_window(), flash ? apps::APP_SHIM_ATTENTION_CRITICAL
: apps::APP_SHIM_ATTENTION_CANCEL);
}
void ChromeNativeAppWindowViewsMac::OnWidgetCreated(views::Widget* widget) {
nswindow_observer_.reset(
[[ResizeNotificationObserver alloc] initForNativeAppWindow:this]);
}
void ChromeNativeAppWindowViewsMac::ShowWithApp() {
is_hidden_with_app_ = false;
if (!app_window()->is_hidden())
ShowInactive();
}
void ChromeNativeAppWindowViewsMac::HideWithApp() {
is_hidden_with_app_ = true;
ChromeNativeAppWindowViews::Hide();
}
void ChromeNativeAppWindowViewsMac::UnhideWithoutActivation() {
if (is_hidden_with_app_) {
apps::ExtensionAppShimHandler::UnhideWithoutActivationForWindow(
app_window());
is_hidden_with_app_ = false;
}
}
|