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