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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
// Copyright (c) 2012 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/cocoa/constrained_window/constrained_window_sheet_controller.h"
#include <map>
#include "base/logging.h"
#include "base/mac/sdk_forward_declarations.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
#include "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_info.h"
#import "chrome/browser/ui/cocoa/web_contents_modal_dialog_host_cocoa.h"
namespace {
// Maps parent windows to sheet controllers.
NSMutableDictionary* g_sheetControllers;
// Get a value for the given window that can be used as a key in a dictionary.
NSValue* GetKeyForParentWindow(NSWindow* parent_window) {
return [NSValue valueWithNonretainedObject:parent_window];
}
// Returns the bounds to use when showing a sheet for a given parent view. This
// returns a rect in window coordinates.
NSRect GetSheetParentBoundsForParentView(NSView* view) {
// If the devtools view is open, it shrinks the size of the WebContents, so go
// up the hierarchy to the devtools container view to avoid that. Note that
// the devtools view is always in the hierarchy even if it is not open or it
// is detached.
NSView* devtools_view = [[[view superview] superview] superview];
if (devtools_view)
view = devtools_view;
return [view convertRect:[view bounds] toView:nil];
}
} // namespace
// An invisible overlay window placed on top of the sheet's parent view.
// This window blocks interaction with the underlying view.
@interface CWSheetOverlayWindow : NSWindow {
base::scoped_nsobject<ConstrainedWindowSheetController> controller_;
}
@end
@interface ConstrainedWindowSheetController ()
- (id)initWithParentWindow:(NSWindow*)parentWindow;
- (ConstrainedWindowSheetInfo*)findSheetInfoForParentView:(NSView*)parentView;
- (ConstrainedWindowSheetInfo*)
findSheetInfoForSheet:(id<ConstrainedWindowSheet>)sheet;
- (void)onParentWindowWillClose:(NSNotification*)note;
- (void)onParentWindowSizeDidChange:(NSNotification*)note;
- (void)updateSheetPosition:(NSView*)parentView;
- (NSRect)overlayWindowFrameForParentView:(NSView*)parentView;
- (NSPoint)originForSheetSize:(NSSize)sheetSize
inContainerRect:(NSRect)containerRect;
- (void)onOverlayWindowMouseDown:(CWSheetOverlayWindow*)overlayWindow;
- (void)closeSheet:(ConstrainedWindowSheetInfo*)info
withAnimation:(BOOL)withAnimation;
@end
@implementation CWSheetOverlayWindow
- (id)initWithContentRect:(NSRect)rect
controller:(ConstrainedWindowSheetController*)controller {
if ((self = [super initWithContentRect:rect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO])) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setIgnoresMouseEvents:NO];
[self setReleasedWhenClosed:NO];
controller_.reset([controller retain]);
}
return self;
}
- (void)mouseDown:(NSEvent*)event {
[controller_ onOverlayWindowMouseDown:self];
}
@end
@implementation ConstrainedWindowSheetController
+ (ConstrainedWindowSheetController*)
controllerForParentWindow:(NSWindow*)parentWindow {
DCHECK(parentWindow);
ConstrainedWindowSheetController* controller =
[g_sheetControllers objectForKey:GetKeyForParentWindow(parentWindow)];
if (controller)
return controller;
base::scoped_nsobject<ConstrainedWindowSheetController> new_controller(
[[ConstrainedWindowSheetController alloc]
initWithParentWindow:parentWindow]);
if (!g_sheetControllers)
g_sheetControllers = [[NSMutableDictionary alloc] init];
[g_sheetControllers setObject:new_controller
forKey:GetKeyForParentWindow(parentWindow)];
return new_controller;
}
+ (ConstrainedWindowSheetController*)
controllerForSheet:(id<ConstrainedWindowSheet>)sheet {
for (ConstrainedWindowSheetController* controller in
[g_sheetControllers objectEnumerator]) {
if ([controller findSheetInfoForSheet:sheet])
return controller;
}
return nil;
}
+ (id<ConstrainedWindowSheet>)sheetForOverlayWindow:(NSWindow*)overlayWindow {
for (ConstrainedWindowSheetController* controller in
[g_sheetControllers objectEnumerator]) {
for (ConstrainedWindowSheetInfo* info in controller->sheets_.get()) {
if ([overlayWindow isEqual:[info overlayWindow]])
return [info sheet];
}
}
return nil;
}
- (id)initWithParentWindow:(NSWindow*)parentWindow {
if ((self = [super init])) {
parentWindow_.reset([parentWindow retain]);
sheets_.reset([[NSMutableArray alloc] init]);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onParentWindowWillClose:)
name:NSWindowWillCloseNotification
object:parentWindow_];
}
return self;
}
- (web_modal::WebContentsModalDialogHost*)dialogHost {
if (!dialogHost_)
dialogHost_.reset(new WebContentsModalDialogHostCocoa(self));
return dialogHost_.get();
}
- (NSWindow*)parentWindow {
return parentWindow_.get();
}
- (void)showSheet:(id<ConstrainedWindowSheet>)sheet
forParentView:(NSView*)parentView {
DCHECK(sheet);
DCHECK(parentView);
// At maximum one active view is allowed.
DCHECK(!activeView_.get() || [activeView_ isEqual:parentView]);
if (!activeView_.get())
activeView_.reset([parentView retain]);
// This function can be called multiple times for the same
// |parentView|, so sheet info could be created already.
ConstrainedWindowSheetInfo* existingInfo =
[self findSheetInfoForParentView:activeView_];
if (existingInfo) {
DCHECK([[existingInfo sheet] isEqual:sheet]);
[self updateSheetPosition:activeView_];
[existingInfo showSheet];
return;
}
// Observe the parent window's size.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onParentWindowSizeDidChange:)
name:NSWindowDidResizeNotification
object:parentWindow_];
// Create an invisible overlay window.
NSRect rect = [self overlayWindowFrameForParentView:parentView];
base::scoped_nsobject<NSWindow> overlayWindow(
[[CWSheetOverlayWindow alloc] initWithContentRect:rect controller:self]);
[parentWindow_ addChildWindow:overlayWindow
ordered:NSWindowAbove];
// Add an entry for the sheet.
base::scoped_nsobject<ConstrainedWindowSheetInfo> info(
[[ConstrainedWindowSheetInfo alloc] initWithSheet:sheet
parentView:parentView
overlayWindow:overlayWindow]);
[sheets_ addObject:info];
// Show the sheet.
[info showSheet];
}
- (void)hideSheet:(id<ConstrainedWindowSheet>)sheet {
ConstrainedWindowSheetInfo* info = [self findSheetInfoForSheet:sheet];
// Method can be called for already hidden sheet. http://crbug.com/589074.
if ([[info parentView] isEqual:activeView_]) {
[info hideSheet];
activeView_.reset();
}
}
- (void)hideSheetForFullscreenTransition {
if (ConstrainedWindowSheetInfo* sheetInfo =
[self findSheetInfoForParentView:activeView_]) {
[sheetInfo hideSheet];
isSheetHiddenForFullscreen_ = YES;
}
}
- (void)unhideSheetForFullscreenTransition {
isSheetHiddenForFullscreen_ = NO;
if (ConstrainedWindowSheetInfo* sheetInfo =
[self findSheetInfoForParentView:activeView_]) {
[self showSheet:[sheetInfo sheet] forParentView:activeView_];
}
}
- (NSPoint)originForSheet:(id<ConstrainedWindowSheet>)sheet
withWindowSize:(NSSize)size {
ConstrainedWindowSheetInfo* info = [self findSheetInfoForSheet:sheet];
DCHECK(info);
NSRect containerRect =
[self overlayWindowFrameForParentView:[info parentView]];
return [self originForSheetSize:size inContainerRect:containerRect];
}
- (void)closeSheet:(id<ConstrainedWindowSheet>)sheet {
ConstrainedWindowSheetInfo* info = [self findSheetInfoForSheet:sheet];
DCHECK(info);
[self closeSheet:info withAnimation:YES];
}
- (void)pulseSheet:(id<ConstrainedWindowSheet>)sheet {
ConstrainedWindowSheetInfo* info = [self findSheetInfoForSheet:sheet];
DCHECK(info);
if ([activeView_ isEqual:[info parentView]])
[[info sheet] pulseSheet];
}
- (int)sheetCount {
return [sheets_ count];
}
- (NSSize)overlayWindowSizeForParentView:(NSView*)parentView {
return [self overlayWindowFrameForParentView:parentView].size;
}
- (ConstrainedWindowSheetInfo*)findSheetInfoForParentView:(NSView*)parentView {
for (ConstrainedWindowSheetInfo* info in sheets_.get()) {
if ([parentView isEqual:[info parentView]])
return info;
}
return NULL;
}
- (ConstrainedWindowSheetInfo*)
findSheetInfoForSheet:(id<ConstrainedWindowSheet>)sheet {
for (ConstrainedWindowSheetInfo* info in sheets_.get()) {
if ([sheet isEqual:[info sheet]])
return info;
}
return NULL;
}
- (void)onParentWindowWillClose:(NSNotification*)note {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSWindowWillCloseNotification
object:parentWindow_];
// Close all sheets.
NSArray* sheets = [NSArray arrayWithArray:sheets_];
for (ConstrainedWindowSheetInfo* info in sheets)
[self closeSheet:info withAnimation:NO];
dialogHost_.reset();
// Delete this instance.
[g_sheetControllers removeObjectForKey:GetKeyForParentWindow(parentWindow_)];
if (![g_sheetControllers count]) {
[g_sheetControllers release];
g_sheetControllers = nil;
}
}
- (void)onParentWindowSizeDidChange:(NSNotification*)note {
if (isSheetHiddenForFullscreen_)
return;
[self updateSheetPosition:activeView_];
}
- (void)updateSheetPosition:(NSView*)parentView {
ConstrainedWindowSheetInfo* info =
[self findSheetInfoForParentView:parentView];
if (!info)
return;
NSRect rect = [self overlayWindowFrameForParentView:parentView];
[[info overlayWindow] setFrame:rect display:YES];
[[info sheet] updateSheetPosition];
}
- (NSRect)overlayWindowFrameForParentView:(NSView*)parentView {
NSRect viewFrame = GetSheetParentBoundsForParentView(parentView);
id<NSWindowDelegate> delegate = [[parentView window] delegate];
if ([delegate respondsToSelector:@selector(window:
willPositionSheet:
usingRect:)]) {
NSRect sheetFrame = NSZeroRect;
// This API needs Y to be the distance from the bottom of the overlay to
// the top of the sheet. X, width, and height are ignored.
sheetFrame.origin.y = NSMaxY(viewFrame);
NSRect customSheetFrame = [delegate window:[parentView window]
willPositionSheet:nil
usingRect:sheetFrame];
viewFrame.size.height += NSMinY(customSheetFrame) - NSMinY(sheetFrame);
}
viewFrame = [[parentView window] convertRectToScreen:viewFrame];
return viewFrame;
}
- (NSPoint)originForSheetSize:(NSSize)sheetSize
inContainerRect:(NSRect)containerRect {
NSPoint origin;
origin.x = roundf(NSMinX(containerRect) +
(NSWidth(containerRect) - sheetSize.width) / 2.0);
origin.y = NSMaxY(containerRect) + 5 - sheetSize.height;
return origin;
}
- (void)onOverlayWindowMouseDown:(CWSheetOverlayWindow*)overlayWindow {
for (ConstrainedWindowSheetInfo* curInfo in sheets_.get()) {
if ([overlayWindow isEqual:[curInfo overlayWindow]]) {
[self pulseSheet:[curInfo sheet]];
[[curInfo sheet] makeSheetKeyAndOrderFront];
break;
}
}
}
- (void)closeSheet:(ConstrainedWindowSheetInfo*)info
withAnimation:(BOOL)withAnimation {
if (![sheets_ containsObject:info])
return;
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSWindowDidResizeNotification
object:parentWindow_];
if ([activeView_ isEqual:[info parentView]])
activeView_.reset();
[parentWindow_ removeChildWindow:[info overlayWindow]];
[[info sheet] closeSheetWithAnimation:withAnimation];
[[info overlayWindow] close];
[sheets_ removeObject:info];
}
@end
|