| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/cocoa/extensions/shell_window_cocoa.h" | |
| 6 | |
| 7 #include "base/mac/mac_util.h" | |
| 8 #include "base/sys_string_conversions.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/cocoa/browser_window_utils.h" | |
| 11 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | |
| 12 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa
.h" | |
| 13 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h" | |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 15 #include "chrome/common/extensions/extension.h" | |
| 16 #include "content/public/browser/native_web_keyboard_event.h" | |
| 17 #include "content/public/browser/render_widget_host_view.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "content/public/browser/web_contents_view.h" | |
| 20 #include "third_party/skia/include/core/SkRegion.h" | |
| 21 | |
| 22 @interface NSWindow (NSPrivateApis) | |
| 23 - (void)setBottomCornerRounded:(BOOL)rounded; | |
| 24 @end | |
| 25 | |
| 26 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | |
| 27 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
| 28 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
| 29 | |
| 30 @interface NSWindow (LionSDKDeclarations) | |
| 31 - (void)toggleFullScreen:(id)sender; | |
| 32 @end | |
| 33 | |
| 34 #endif // MAC_OS_X_VERSION_10_7 | |
| 35 | |
| 36 @implementation ShellWindowController | |
| 37 | |
| 38 @synthesize shellWindow = shellWindow_; | |
| 39 | |
| 40 - (void)windowWillClose:(NSNotification*)notification { | |
| 41 if (shellWindow_) | |
| 42 shellWindow_->WindowWillClose(); | |
| 43 } | |
| 44 | |
| 45 - (void)windowDidBecomeKey:(NSNotification*)notification { | |
| 46 if (shellWindow_) | |
| 47 shellWindow_->WindowDidBecomeKey(); | |
| 48 } | |
| 49 | |
| 50 - (void)windowDidResignKey:(NSNotification*)notification { | |
| 51 if (shellWindow_) | |
| 52 shellWindow_->WindowDidResignKey(); | |
| 53 } | |
| 54 | |
| 55 - (void)windowDidResize:(NSNotification*)notification { | |
| 56 if (shellWindow_) | |
| 57 shellWindow_->WindowDidResize(); | |
| 58 } | |
| 59 | |
| 60 - (void)windowDidMove:(NSNotification*)notification { | |
| 61 if (shellWindow_) | |
| 62 shellWindow_->WindowDidMove(); | |
| 63 } | |
| 64 | |
| 65 - (void)gtm_systemRequestsVisibilityForView:(NSView*)view { | |
| 66 [[self window] makeKeyAndOrderFront:self]; | |
| 67 } | |
| 68 | |
| 69 - (GTMWindowSheetController*)sheetController { | |
| 70 if (!sheetController_.get()) { | |
| 71 sheetController_.reset([[GTMWindowSheetController alloc] | |
| 72 initWithWindow:[self window] | |
| 73 delegate:self]); | |
| 74 } | |
| 75 return sheetController_; | |
| 76 } | |
| 77 | |
| 78 - (void)executeCommand:(int)command { | |
| 79 // No-op, swallow the event. | |
| 80 } | |
| 81 | |
| 82 - (BOOL)handledByExtensionCommand:(NSEvent*)event { | |
| 83 if (shellWindow_) | |
| 84 return shellWindow_->HandledByExtensionCommand(event); | |
| 85 return NO; | |
| 86 } | |
| 87 | |
| 88 @end | |
| 89 | |
| 90 // This is really a method on NSGrayFrame, so it should only be called on the | |
| 91 // view passed into -[NSWindow drawCustomFrameRect:forView:]. | |
| 92 @interface NSView (PrivateMethods) | |
| 93 - (CGFloat)roundedCornerRadius; | |
| 94 @end | |
| 95 | |
| 96 @interface ShellNSWindow : ChromeEventProcessingWindow | |
| 97 | |
| 98 - (void)drawCustomFrameRect:(NSRect)rect forView:(NSView*)view; | |
| 99 | |
| 100 @end | |
| 101 | |
| 102 @implementation ShellNSWindow | |
| 103 | |
| 104 - (void)drawCustomFrameRect:(NSRect)rect forView:(NSView*)view { | |
| 105 [[NSBezierPath bezierPathWithRect:rect] addClip]; | |
| 106 [[NSColor clearColor] set]; | |
| 107 NSRectFill(rect); | |
| 108 | |
| 109 // Set up our clip. | |
| 110 CGFloat cornerRadius = 4.0; | |
| 111 if ([view respondsToSelector:@selector(roundedCornerRadius)]) | |
| 112 cornerRadius = [view roundedCornerRadius]; | |
| 113 [[NSBezierPath bezierPathWithRoundedRect:[view bounds] | |
| 114 xRadius:cornerRadius | |
| 115 yRadius:cornerRadius] addClip]; | |
| 116 [[NSColor whiteColor] set]; | |
| 117 NSRectFill(rect); | |
| 118 } | |
| 119 | |
| 120 @end | |
| 121 | |
| 122 @interface ShellFramelessNSWindow : ShellNSWindow | |
| 123 | |
| 124 @end | |
| 125 | |
| 126 @implementation ShellFramelessNSWindow | |
| 127 | |
| 128 + (NSRect)frameRectForContentRect:(NSRect)contentRect | |
| 129 styleMask:(NSUInteger)mask { | |
| 130 return contentRect; | |
| 131 } | |
| 132 | |
| 133 + (NSRect)contentRectForFrameRect:(NSRect)frameRect | |
| 134 styleMask:(NSUInteger)mask { | |
| 135 return frameRect; | |
| 136 } | |
| 137 | |
| 138 - (NSRect)frameRectForContentRect:(NSRect)contentRect { | |
| 139 return contentRect; | |
| 140 } | |
| 141 | |
| 142 - (NSRect)contentRectForFrameRect:(NSRect)frameRect { | |
| 143 return frameRect; | |
| 144 } | |
| 145 | |
| 146 @end | |
| 147 | |
| 148 @interface ControlRegionView : NSView { | |
| 149 @private | |
| 150 ShellWindowCocoa* shellWindow_; // Weak; owns self. | |
| 151 } | |
| 152 | |
| 153 @end | |
| 154 | |
| 155 @implementation ControlRegionView | |
| 156 | |
| 157 - (id)initWithShellWindow:(ShellWindowCocoa*)shellWindow { | |
| 158 if ((self = [super init])) | |
| 159 shellWindow_ = shellWindow; | |
| 160 return self; | |
| 161 } | |
| 162 | |
| 163 - (BOOL)mouseDownCanMoveWindow { | |
| 164 return NO; | |
| 165 } | |
| 166 | |
| 167 - (NSView*)hitTest:(NSPoint)aPoint { | |
| 168 if (shellWindow_->use_system_drag() || | |
| 169 !shellWindow_->IsWithinDraggableRegion(aPoint)) { | |
| 170 return nil; | |
| 171 } | |
| 172 return self; | |
| 173 } | |
| 174 | |
| 175 - (void)mouseDown:(NSEvent*)event { | |
| 176 shellWindow_->HandleMouseEvent(event); | |
| 177 } | |
| 178 | |
| 179 - (void)mouseDragged:(NSEvent*)event { | |
| 180 shellWindow_->HandleMouseEvent(event); | |
| 181 } | |
| 182 | |
| 183 @end | |
| 184 | |
| 185 @interface NSView (WebContentsView) | |
| 186 - (void)setMouseDownCanMoveWindow:(BOOL)can_move; | |
| 187 @end | |
| 188 | |
| 189 ShellWindowCocoa::ShellWindowCocoa(ShellWindow* shell_window, | |
| 190 const ShellWindow::CreateParams& params) | |
| 191 : shell_window_(shell_window), | |
| 192 has_frame_(params.frame == ShellWindow::CreateParams::FRAME_CHROME), | |
| 193 attention_request_id_(0), | |
| 194 use_system_drag_(true) { | |
| 195 // Flip coordinates based on the primary screen. | |
| 196 NSRect main_screen_rect = [[[NSScreen screens] objectAtIndex:0] frame]; | |
| 197 NSRect cocoa_bounds = NSMakeRect(params.bounds.x(), | |
| 198 NSHeight(main_screen_rect) - params.bounds.y() - params.bounds.height(), | |
| 199 params.bounds.width(), params.bounds.height()); | |
| 200 | |
| 201 // If coordinates are < 0, center window on primary screen | |
| 202 if (params.bounds.x() == INT_MIN) { | |
| 203 cocoa_bounds.origin.x = | |
| 204 floor((NSWidth(main_screen_rect) - NSWidth(cocoa_bounds)) / 2); | |
| 205 } | |
| 206 if (params.bounds.y() == INT_MIN) { | |
| 207 cocoa_bounds.origin.y = | |
| 208 floor((NSHeight(main_screen_rect) - NSHeight(cocoa_bounds)) / 2); | |
| 209 } | |
| 210 | |
| 211 NSUInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | | |
| 212 NSMiniaturizableWindowMask | NSResizableWindowMask | | |
| 213 NSTexturedBackgroundWindowMask; | |
| 214 scoped_nsobject<NSWindow> window; | |
| 215 if (has_frame_) { | |
| 216 window.reset([[ShellNSWindow alloc] | |
| 217 initWithContentRect:cocoa_bounds | |
| 218 styleMask:style_mask | |
| 219 backing:NSBackingStoreBuffered | |
| 220 defer:NO]); | |
| 221 } else { | |
| 222 window.reset([[ShellFramelessNSWindow alloc] | |
| 223 initWithContentRect:cocoa_bounds | |
| 224 styleMask:style_mask | |
| 225 backing:NSBackingStoreBuffered | |
| 226 defer:NO]); | |
| 227 } | |
| 228 [window setTitle:base::SysUTF8ToNSString(extension()->name())]; | |
| 229 min_size_ = params.minimum_size; | |
| 230 if (min_size_.width() || min_size_.height()) { | |
| 231 [window setContentMinSize: | |
| 232 NSMakeSize(min_size_.width(), min_size_.height())]; | |
| 233 } | |
| 234 max_size_ = params.maximum_size; | |
| 235 if (max_size_.width() || max_size_.height()) { | |
| 236 CGFloat max_width = max_size_.width() ? max_size_.width() : CGFLOAT_MAX; | |
| 237 CGFloat max_height = max_size_.height() ? max_size_.height() : CGFLOAT_MAX; | |
| 238 [window setContentMaxSize:NSMakeSize(max_width, max_height)]; | |
| 239 } | |
| 240 | |
| 241 if (base::mac::IsOSSnowLeopard() && | |
| 242 [window respondsToSelector:@selector(setBottomCornerRounded:)]) | |
| 243 [window setBottomCornerRounded:NO]; | |
| 244 | |
| 245 window_controller_.reset( | |
| 246 [[ShellWindowController alloc] initWithWindow:window.release()]); | |
| 247 | |
| 248 NSView* view = web_contents()->GetView()->GetNativeView(); | |
| 249 [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
| 250 | |
| 251 // By default, the whole frameless window is not draggable. | |
| 252 if (!has_frame_) { | |
| 253 gfx::Rect window_bounds( | |
| 254 0, 0, NSWidth(cocoa_bounds), NSHeight(cocoa_bounds)); | |
| 255 system_drag_exclude_areas_.push_back(window_bounds); | |
| 256 } | |
| 257 | |
| 258 InstallView(); | |
| 259 | |
| 260 [[window_controller_ window] setDelegate:window_controller_]; | |
| 261 [window_controller_ setShellWindow:this]; | |
| 262 | |
| 263 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryCocoa( | |
| 264 shell_window_->profile(), | |
| 265 window, | |
| 266 extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY, | |
| 267 shell_window)); | |
| 268 } | |
| 269 | |
| 270 void ShellWindowCocoa::InstallView() { | |
| 271 NSView* view = web_contents()->GetView()->GetNativeView(); | |
| 272 if (has_frame_) { | |
| 273 [view setFrame:[[window() contentView] bounds]]; | |
| 274 [[window() contentView] addSubview:view]; | |
| 275 if (!max_size_.IsEmpty() && min_size_ == max_size_) { | |
| 276 [[window() standardWindowButton:NSWindowZoomButton] setEnabled:NO]; | |
| 277 [window() setShowsResizeIndicator:NO]; | |
| 278 } | |
| 279 } else { | |
| 280 // TODO(jeremya): find a cleaner way to send this information to the | |
| 281 // WebContentsViewCocoa view. | |
| 282 DCHECK([view | |
| 283 respondsToSelector:@selector(setMouseDownCanMoveWindow:)]); | |
| 284 [view setMouseDownCanMoveWindow:YES]; | |
| 285 | |
| 286 NSView* frameView = [[window() contentView] superview]; | |
| 287 [view setFrame:[frameView bounds]]; | |
| 288 [frameView addSubview:view]; | |
| 289 | |
| 290 [[window() standardWindowButton:NSWindowZoomButton] setHidden:YES]; | |
| 291 [[window() standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; | |
| 292 [[window() standardWindowButton:NSWindowCloseButton] setHidden:YES]; | |
| 293 | |
| 294 // Some third-party OS X utilities check the zoom button's enabled state to | |
| 295 // determine whether to show custom UI on hover, so we disable it here to | |
| 296 // prevent them from doing so in a frameless app window. | |
| 297 [[window() standardWindowButton:NSWindowZoomButton] setEnabled:NO]; | |
| 298 | |
| 299 InstallDraggableRegionViews(); | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 void ShellWindowCocoa::UninstallView() { | |
| 304 NSView* view = web_contents()->GetView()->GetNativeView(); | |
| 305 [view removeFromSuperview]; | |
| 306 } | |
| 307 | |
| 308 bool ShellWindowCocoa::IsActive() const { | |
| 309 return [window() isKeyWindow]; | |
| 310 } | |
| 311 | |
| 312 bool ShellWindowCocoa::IsMaximized() const { | |
| 313 return [window() isZoomed]; | |
| 314 } | |
| 315 | |
| 316 bool ShellWindowCocoa::IsMinimized() const { | |
| 317 return [window() isMiniaturized]; | |
| 318 } | |
| 319 | |
| 320 bool ShellWindowCocoa::IsFullscreen() const { | |
| 321 return is_fullscreen_; | |
| 322 } | |
| 323 | |
| 324 void ShellWindowCocoa::SetFullscreen(bool fullscreen) { | |
| 325 if (fullscreen == is_fullscreen_) | |
| 326 return; | |
| 327 is_fullscreen_ = fullscreen; | |
| 328 | |
| 329 if (base::mac::IsOSLionOrLater()) { | |
| 330 [window() toggleFullScreen:nil]; | |
| 331 return; | |
| 332 } | |
| 333 | |
| 334 DCHECK(base::mac::IsOSSnowLeopard()); | |
| 335 | |
| 336 // Fade to black. | |
| 337 const CGDisplayReservationInterval kFadeDurationSeconds = 0.6; | |
| 338 bool did_fade_out = false; | |
| 339 CGDisplayFadeReservationToken token; | |
| 340 if (CGAcquireDisplayFadeReservation(kFadeDurationSeconds, &token) == | |
| 341 kCGErrorSuccess) { | |
| 342 did_fade_out = true; | |
| 343 CGDisplayFade(token, kFadeDurationSeconds / 2, kCGDisplayBlendNormal, | |
| 344 kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, /*synchronous=*/true); | |
| 345 } | |
| 346 | |
| 347 // Since frameless windows insert the WebContentsView into the NSThemeFrame | |
| 348 // ([[window contentView] superview]), and since that NSThemeFrame is | |
| 349 // destroyed and recreated when we change the styleMask of the window, we | |
| 350 // need to remove the view from the window when we change the style, and | |
| 351 // add it back afterwards. | |
| 352 UninstallView(); | |
| 353 if (fullscreen) { | |
| 354 restored_bounds_ = [window() frame]; | |
| 355 [window() setStyleMask:NSBorderlessWindowMask]; | |
| 356 [window() setFrame:[window() | |
| 357 frameRectForContentRect:[[window() screen] frame]] | |
| 358 display:YES]; | |
| 359 base::mac::RequestFullScreen(base::mac::kFullScreenModeAutoHideAll); | |
| 360 } else { | |
| 361 base::mac::ReleaseFullScreen(base::mac::kFullScreenModeAutoHideAll); | |
| 362 NSUInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | | |
| 363 NSMiniaturizableWindowMask | NSResizableWindowMask | | |
| 364 NSTexturedBackgroundWindowMask; | |
| 365 [window() setStyleMask:style_mask]; | |
| 366 [window() setFrame:restored_bounds_ display:YES]; | |
| 367 } | |
| 368 InstallView(); | |
| 369 | |
| 370 // Fade back in. | |
| 371 if (did_fade_out) { | |
| 372 CGDisplayFade(token, kFadeDurationSeconds / 2, kCGDisplayBlendSolidColor, | |
| 373 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, /*synchronous=*/false); | |
| 374 CGReleaseDisplayFadeReservation(token); | |
| 375 } | |
| 376 } | |
| 377 | |
| 378 bool ShellWindowCocoa::IsFullscreenOrPending() const { | |
| 379 return is_fullscreen_; | |
| 380 } | |
| 381 | |
| 382 gfx::NativeWindow ShellWindowCocoa::GetNativeWindow() { | |
| 383 return window(); | |
| 384 } | |
| 385 | |
| 386 gfx::Rect ShellWindowCocoa::GetRestoredBounds() const { | |
| 387 // Flip coordinates based on the primary screen. | |
| 388 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
| 389 NSRect frame = [window() frame]; | |
| 390 gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame)); | |
| 391 bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame)); | |
| 392 return bounds; | |
| 393 } | |
| 394 | |
| 395 gfx::Rect ShellWindowCocoa::GetBounds() const { | |
| 396 return GetRestoredBounds(); | |
| 397 } | |
| 398 | |
| 399 void ShellWindowCocoa::Show() { | |
| 400 [window_controller_ showWindow:nil]; | |
| 401 [window() makeKeyAndOrderFront:window_controller_]; | |
| 402 } | |
| 403 | |
| 404 void ShellWindowCocoa::ShowInactive() { | |
| 405 [window() orderFront:window_controller_]; | |
| 406 } | |
| 407 | |
| 408 void ShellWindowCocoa::Hide() { | |
| 409 [window() orderOut:window_controller_]; | |
| 410 } | |
| 411 | |
| 412 void ShellWindowCocoa::Close() { | |
| 413 [window() performClose:nil]; | |
| 414 } | |
| 415 | |
| 416 void ShellWindowCocoa::Activate() { | |
| 417 [BrowserWindowUtils activateWindowForController:window_controller_]; | |
| 418 } | |
| 419 | |
| 420 void ShellWindowCocoa::Deactivate() { | |
| 421 // TODO(jcivelli): http://crbug.com/51364 Implement me. | |
| 422 NOTIMPLEMENTED(); | |
| 423 } | |
| 424 | |
| 425 void ShellWindowCocoa::Maximize() { | |
| 426 // Zoom toggles so only call if not already maximized. | |
| 427 if (!IsMaximized()) | |
| 428 [window() zoom:window_controller_]; | |
| 429 } | |
| 430 | |
| 431 void ShellWindowCocoa::Minimize() { | |
| 432 [window() miniaturize:window_controller_]; | |
| 433 } | |
| 434 | |
| 435 void ShellWindowCocoa::Restore() { | |
| 436 if (IsMaximized()) | |
| 437 [window() zoom:window_controller_]; // Toggles zoom mode. | |
| 438 else if (IsMinimized()) | |
| 439 [window() deminiaturize:window_controller_]; | |
| 440 } | |
| 441 | |
| 442 void ShellWindowCocoa::SetBounds(const gfx::Rect& bounds) { | |
| 443 // Enforce minimum/maximum bounds. | |
| 444 gfx::Rect checked_bounds = bounds; | |
| 445 | |
| 446 NSSize min_size = [window() minSize]; | |
| 447 if (bounds.width() < min_size.width) | |
| 448 checked_bounds.set_width(min_size.width); | |
| 449 if (bounds.height() < min_size.height) | |
| 450 checked_bounds.set_height(min_size.height); | |
| 451 NSSize max_size = [window() maxSize]; | |
| 452 if (checked_bounds.width() > max_size.width) | |
| 453 checked_bounds.set_width(max_size.width); | |
| 454 if (checked_bounds.height() > max_size.height) | |
| 455 checked_bounds.set_height(max_size.height); | |
| 456 | |
| 457 NSRect cocoa_bounds = NSMakeRect(checked_bounds.x(), 0, | |
| 458 checked_bounds.width(), | |
| 459 checked_bounds.height()); | |
| 460 // Flip coordinates based on the primary screen. | |
| 461 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
| 462 cocoa_bounds.origin.y = NSHeight([screen frame]) - checked_bounds.bottom(); | |
| 463 | |
| 464 [window() setFrame:cocoa_bounds display:YES]; | |
| 465 } | |
| 466 | |
| 467 void ShellWindowCocoa::UpdateWindowIcon() { | |
| 468 // TODO(junmin): implement. | |
| 469 } | |
| 470 | |
| 471 void ShellWindowCocoa::UpdateWindowTitle() { | |
| 472 string16 title = shell_window_->GetTitle(); | |
| 473 [window() setTitle:base::SysUTF16ToNSString(title)]; | |
| 474 } | |
| 475 | |
| 476 void ShellWindowCocoa::UpdateDraggableRegions( | |
| 477 const std::vector<extensions::DraggableRegion>& regions) { | |
| 478 // Draggable region is not supported for non-frameless window. | |
| 479 if (has_frame_) | |
| 480 return; | |
| 481 | |
| 482 // To use system drag, the window has to be marked as draggable with | |
| 483 // non-draggable areas being excluded via overlapping views. | |
| 484 // 1) If no draggable area is provided, the window is not draggable at all. | |
| 485 // 2) If only one draggable area is given, as this is the most common | |
| 486 // case, use the system drag. The non-draggable areas that are opposite of | |
| 487 // the draggable area are computed. | |
| 488 // 3) Otherwise, use the custom drag. As such, we lose the capability to | |
| 489 // support some features like snapping into other space. | |
| 490 | |
| 491 // Determine how to perform the drag by counting the number of draggable | |
| 492 // areas. | |
| 493 const extensions::DraggableRegion* draggable_area = NULL; | |
| 494 use_system_drag_ = true; | |
| 495 for (std::vector<extensions::DraggableRegion>::const_iterator iter = | |
| 496 regions.begin(); | |
| 497 iter != regions.end(); | |
| 498 ++iter) { | |
| 499 if (iter->draggable) { | |
| 500 // If more than one draggable area is found, use custom drag. | |
| 501 if (draggable_area) { | |
| 502 use_system_drag_ = false; | |
| 503 break; | |
| 504 } | |
| 505 draggable_area = &(*iter); | |
| 506 } | |
| 507 } | |
| 508 | |
| 509 if (use_system_drag_) | |
| 510 UpdateDraggableRegionsForSystemDrag(regions, draggable_area); | |
| 511 else | |
| 512 UpdateDraggableRegionsForCustomDrag(regions); | |
| 513 | |
| 514 InstallDraggableRegionViews(); | |
| 515 } | |
| 516 | |
| 517 void ShellWindowCocoa::UpdateDraggableRegionsForSystemDrag( | |
| 518 const std::vector<extensions::DraggableRegion>& regions, | |
| 519 const extensions::DraggableRegion* draggable_area) { | |
| 520 NSView* web_view = web_contents()->GetView()->GetNativeView(); | |
| 521 NSInteger web_view_width = NSWidth([web_view bounds]); | |
| 522 NSInteger web_view_height = NSHeight([web_view bounds]); | |
| 523 | |
| 524 system_drag_exclude_areas_.clear(); | |
| 525 | |
| 526 // The whole window is not draggable if no draggable area is given. | |
| 527 if (!draggable_area) { | |
| 528 gfx::Rect window_bounds(0, 0, web_view_width, web_view_height); | |
| 529 system_drag_exclude_areas_.push_back(window_bounds); | |
| 530 return; | |
| 531 } | |
| 532 | |
| 533 // Otherwise, there is only one draggable area. Compute non-draggable areas | |
| 534 // that are the opposite of the given draggable area, combined with the | |
| 535 // remaining provided non-draggable areas. | |
| 536 | |
| 537 // Copy all given non-draggable areas. | |
| 538 for (std::vector<extensions::DraggableRegion>::const_iterator iter = | |
| 539 regions.begin(); | |
| 540 iter != regions.end(); | |
| 541 ++iter) { | |
| 542 if (!iter->draggable) | |
| 543 system_drag_exclude_areas_.push_back(iter->bounds); | |
| 544 } | |
| 545 | |
| 546 gfx::Rect draggable_bounds = draggable_area->bounds; | |
| 547 gfx::Rect non_draggable_bounds; | |
| 548 | |
| 549 // Add the non-draggable area above the given draggable area. | |
| 550 if (draggable_bounds.y() > 0) { | |
| 551 non_draggable_bounds.SetRect(0, | |
| 552 0, | |
| 553 web_view_width, | |
| 554 draggable_bounds.y() - 1); | |
| 555 system_drag_exclude_areas_.push_back(non_draggable_bounds); | |
| 556 } | |
| 557 | |
| 558 // Add the non-draggable area below the given draggable area. | |
| 559 if (draggable_bounds.bottom() < web_view_height) { | |
| 560 non_draggable_bounds.SetRect(0, | |
| 561 draggable_bounds.bottom() + 1, | |
| 562 web_view_width, | |
| 563 web_view_height - draggable_bounds.bottom()); | |
| 564 system_drag_exclude_areas_.push_back(non_draggable_bounds); | |
| 565 } | |
| 566 | |
| 567 // Add the non-draggable area to the left of the given draggable area. | |
| 568 if (draggable_bounds.x() > 0) { | |
| 569 non_draggable_bounds.SetRect(0, | |
| 570 draggable_bounds.y(), | |
| 571 draggable_bounds.x() - 1, | |
| 572 draggable_bounds.height()); | |
| 573 system_drag_exclude_areas_.push_back(non_draggable_bounds); | |
| 574 } | |
| 575 | |
| 576 // Add the non-draggable area to the right of the given draggable area. | |
| 577 if (draggable_bounds.right() < web_view_width) { | |
| 578 non_draggable_bounds.SetRect(draggable_bounds.right() + 1, | |
| 579 draggable_bounds.y(), | |
| 580 web_view_width - draggable_bounds.right(), | |
| 581 draggable_bounds.height()); | |
| 582 system_drag_exclude_areas_.push_back(non_draggable_bounds); | |
| 583 } | |
| 584 } | |
| 585 | |
| 586 void ShellWindowCocoa::UpdateDraggableRegionsForCustomDrag( | |
| 587 const std::vector<extensions::DraggableRegion>& regions) { | |
| 588 // We still need one ControlRegionView to cover the whole window such that | |
| 589 // mouse events could be captured. | |
| 590 NSView* web_view = web_contents()->GetView()->GetNativeView(); | |
| 591 gfx::Rect window_bounds( | |
| 592 0, 0, NSWidth([web_view bounds]), NSHeight([web_view bounds])); | |
| 593 system_drag_exclude_areas_.clear(); | |
| 594 system_drag_exclude_areas_.push_back(window_bounds); | |
| 595 | |
| 596 // Aggregate the draggable areas and non-draggable areas such that hit test | |
| 597 // could be performed easily. | |
| 598 draggable_region_.reset(ShellWindow::RawDraggableRegionsToSkRegion(regions)); | |
| 599 } | |
| 600 | |
| 601 void ShellWindowCocoa::HandleKeyboardEvent( | |
| 602 const content::NativeWebKeyboardEvent& event) { | |
| 603 if (event.skip_in_browser || | |
| 604 event.type == content::NativeWebKeyboardEvent::Char) { | |
| 605 return; | |
| 606 } | |
| 607 [window() redispatchKeyEvent:event.os_event]; | |
| 608 } | |
| 609 | |
| 610 void ShellWindowCocoa::InstallDraggableRegionViews() { | |
| 611 DCHECK(!has_frame_); | |
| 612 | |
| 613 // All ControlRegionViews should be added as children of the WebContentsView, | |
| 614 // because WebContentsView will be removed and re-added when entering and | |
| 615 // leaving fullscreen mode. | |
| 616 NSView* webView = web_contents()->GetView()->GetNativeView(); | |
| 617 NSInteger webViewHeight = NSHeight([webView bounds]); | |
| 618 | |
| 619 // Remove all ControlRegionViews that are added last time. | |
| 620 // Note that [webView subviews] returns the view's mutable internal array and | |
| 621 // it should be copied to avoid mutating the original array while enumerating | |
| 622 // it. | |
| 623 scoped_nsobject<NSArray> subviews([[webView subviews] copy]); | |
| 624 for (NSView* subview in subviews.get()) | |
| 625 if ([subview isKindOfClass:[ControlRegionView class]]) | |
| 626 [subview removeFromSuperview]; | |
| 627 | |
| 628 // Create and add ControlRegionView for each region that needs to be excluded | |
| 629 // from the dragging. | |
| 630 for (std::vector<gfx::Rect>::const_iterator iter = | |
| 631 system_drag_exclude_areas_.begin(); | |
| 632 iter != system_drag_exclude_areas_.end(); | |
| 633 ++iter) { | |
| 634 scoped_nsobject<NSView> controlRegion( | |
| 635 [[ControlRegionView alloc] initWithShellWindow:this]); | |
| 636 [controlRegion setFrame:NSMakeRect(iter->x(), | |
| 637 webViewHeight - iter->bottom(), | |
| 638 iter->width(), | |
| 639 iter->height())]; | |
| 640 [webView addSubview:controlRegion]; | |
| 641 } | |
| 642 } | |
| 643 | |
| 644 void ShellWindowCocoa::FlashFrame(bool flash) { | |
| 645 if (flash) { | |
| 646 attention_request_id_ = [NSApp requestUserAttention:NSInformationalRequest]; | |
| 647 } else { | |
| 648 [NSApp cancelUserAttentionRequest:attention_request_id_]; | |
| 649 attention_request_id_ = 0; | |
| 650 } | |
| 651 } | |
| 652 | |
| 653 bool ShellWindowCocoa::IsAlwaysOnTop() const { | |
| 654 return false; | |
| 655 } | |
| 656 | |
| 657 void ShellWindowCocoa::WindowWillClose() { | |
| 658 [window_controller_ setShellWindow:NULL]; | |
| 659 shell_window_->SaveWindowPosition(); | |
| 660 shell_window_->OnNativeClose(); | |
| 661 } | |
| 662 | |
| 663 void ShellWindowCocoa::WindowDidBecomeKey() { | |
| 664 content::RenderWidgetHostView* rwhv = | |
| 665 web_contents()->GetRenderWidgetHostView(); | |
| 666 if (rwhv) | |
| 667 rwhv->SetActive(true); | |
| 668 } | |
| 669 | |
| 670 void ShellWindowCocoa::WindowDidResignKey() { | |
| 671 // If our app is still active and we're still the key window, ignore this | |
| 672 // message, since it just means that a menu extra (on the "system status bar") | |
| 673 // was activated; we'll get another |-windowDidResignKey| if we ever really | |
| 674 // lose key window status. | |
| 675 if ([NSApp isActive] && ([NSApp keyWindow] == window())) | |
| 676 return; | |
| 677 | |
| 678 content::RenderWidgetHostView* rwhv = | |
| 679 web_contents()->GetRenderWidgetHostView(); | |
| 680 if (rwhv) | |
| 681 rwhv->SetActive(false); | |
| 682 } | |
| 683 | |
| 684 void ShellWindowCocoa::WindowDidResize() { | |
| 685 shell_window_->SaveWindowPosition(); | |
| 686 } | |
| 687 | |
| 688 void ShellWindowCocoa::WindowDidMove() { | |
| 689 shell_window_->SaveWindowPosition(); | |
| 690 } | |
| 691 | |
| 692 bool ShellWindowCocoa::HandledByExtensionCommand(NSEvent* event) { | |
| 693 return extension_keybinding_registry_->ProcessKeyEvent( | |
| 694 content::NativeWebKeyboardEvent(event)); | |
| 695 } | |
| 696 | |
| 697 void ShellWindowCocoa::HandleMouseEvent(NSEvent* event) { | |
| 698 if ([event type] == NSLeftMouseDown) { | |
| 699 last_mouse_location_ = | |
| 700 [window() convertBaseToScreen:[event locationInWindow]]; | |
| 701 } else if ([event type] == NSLeftMouseDragged) { | |
| 702 NSPoint current_mouse_location = | |
| 703 [window() convertBaseToScreen:[event locationInWindow]]; | |
| 704 NSPoint frame_origin = [window() frame].origin; | |
| 705 frame_origin.x += current_mouse_location.x - last_mouse_location_.x; | |
| 706 frame_origin.y += current_mouse_location.y - last_mouse_location_.y; | |
| 707 [window() setFrameOrigin:frame_origin]; | |
| 708 last_mouse_location_ = current_mouse_location; | |
| 709 } | |
| 710 } | |
| 711 | |
| 712 bool ShellWindowCocoa::IsWithinDraggableRegion(NSPoint point) const { | |
| 713 if (!draggable_region_) | |
| 714 return false; | |
| 715 NSView* webView = web_contents()->GetView()->GetNativeView(); | |
| 716 NSInteger webViewHeight = NSHeight([webView bounds]); | |
| 717 // |draggable_region_| is stored in local platform-indepdent coordiate system | |
| 718 // while |point| is in local Cocoa coordinate system. Do the conversion | |
| 719 // to match these two. | |
| 720 return draggable_region_->contains(point.x, webViewHeight - point.y); | |
| 721 } | |
| 722 | |
| 723 ShellWindowCocoa::~ShellWindowCocoa() { | |
| 724 } | |
| 725 | |
| 726 ShellNSWindow* ShellWindowCocoa::window() const { | |
| 727 NSWindow* window = [window_controller_ window]; | |
| 728 CHECK(!window || [window isKindOfClass:[ShellNSWindow class]]); | |
| 729 return static_cast<ShellNSWindow*>(window); | |
| 730 } | |
| 731 | |
| 732 // static | |
| 733 NativeShellWindow* NativeShellWindow::Create( | |
| 734 ShellWindow* shell_window, const ShellWindow::CreateParams& params) { | |
| 735 return new ShellWindowCocoa(shell_window, params); | |
| 736 } | |
| OLD | NEW |