OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "base/mac/mac_util.h" |
8 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
9 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
10 #include "skia/ext/skia_utils_mac.h" | 11 #include "skia/ext/skia_utils_mac.h" |
11 #include "ui/base/ime/input_method.h" | 12 #include "ui/base/ime/input_method.h" |
12 #include "ui/base/ime/text_input_client.h" | 13 #include "ui/base/ime/text_input_client.h" |
13 #include "ui/compositor/canvas_painter.h" | 14 #include "ui/compositor/canvas_painter.h" |
14 #import "ui/events/cocoa/cocoa_event_utils.h" | 15 #import "ui/events/cocoa/cocoa_event_utils.h" |
15 #include "ui/events/keycodes/dom/dom_code.h" | 16 #include "ui/events/keycodes/dom/dom_code.h" |
16 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 17 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
17 #include "ui/gfx/canvas_paint_mac.h" | 18 #include "ui/gfx/canvas_paint_mac.h" |
18 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
19 #import "ui/gfx/mac/coordinate_conversion.h" | 20 #import "ui/gfx/mac/coordinate_conversion.h" |
| 21 #include "ui/gfx/path.h" |
| 22 #import "ui/gfx/path_mac.h" |
| 23 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
20 #include "ui/strings/grit/ui_strings.h" | 24 #include "ui/strings/grit/ui_strings.h" |
21 #include "ui/views/controls/menu/menu_config.h" | 25 #include "ui/views/controls/menu/menu_config.h" |
22 #include "ui/views/controls/menu/menu_controller.h" | 26 #include "ui/views/controls/menu/menu_controller.h" |
23 #include "ui/views/view.h" | 27 #include "ui/views/view.h" |
24 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
25 | 29 |
26 using views::MenuController; | 30 using views::MenuController; |
27 | 31 |
28 namespace { | 32 namespace { |
29 | 33 |
| 34 // Returns true if all four corners of |rect| are contained inside |path|. |
| 35 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { |
| 36 return [path containsPoint:rect.origin] && |
| 37 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
| 38 rect.origin.y)] && |
| 39 [path containsPoint:NSMakePoint(rect.origin.x, |
| 40 rect.origin.y + rect.size.height)] && |
| 41 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
| 42 rect.origin.y + rect.size.height)]; |
| 43 } |
| 44 |
30 // Convert a |point| in |source_window|'s AppKit coordinate system (origin at | 45 // Convert a |point| in |source_window|'s AppKit coordinate system (origin at |
31 // the bottom left of the window) to |target_window|'s content rect, with the | 46 // the bottom left of the window) to |target_window|'s content rect, with the |
32 // origin at the top left of the content area. | 47 // origin at the top left of the content area. |
33 // If |source_window| is nil, |point| will be treated as screen coordinates. | 48 // If |source_window| is nil, |point| will be treated as screen coordinates. |
34 gfx::Point MovePointToWindow(const NSPoint& point, | 49 gfx::Point MovePointToWindow(const NSPoint& point, |
35 NSWindow* source_window, | 50 NSWindow* source_window, |
36 NSWindow* target_window) { | 51 NSWindow* target_window) { |
37 NSPoint point_in_screen = source_window | 52 NSPoint point_in_screen = source_window |
38 ? [source_window convertBaseToScreen:point] | 53 ? [source_window convertBaseToScreen:point] |
39 : point; | 54 : point; |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 views::View::ConvertPointToTarget(hostedView_, view, &viewPoint); | 260 views::View::ConvertPointToTarget(hostedView_, view, &viewPoint); |
246 if (!view->GetTooltipText(viewPoint, &newTooltipText)) | 261 if (!view->GetTooltipText(viewPoint, &newTooltipText)) |
247 DCHECK(newTooltipText.empty()); | 262 DCHECK(newTooltipText.empty()); |
248 } | 263 } |
249 if (newTooltipText != lastTooltipText_) { | 264 if (newTooltipText != lastTooltipText_) { |
250 std::swap(newTooltipText, lastTooltipText_); | 265 std::swap(newTooltipText, lastTooltipText_); |
251 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; | 266 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; |
252 } | 267 } |
253 } | 268 } |
254 | 269 |
| 270 - (void)updateWindowMask { |
| 271 DCHECK(![self inLiveResize]); |
| 272 DCHECK(base::mac::IsOSMavericksOrEarlier()); |
| 273 DCHECK(hostedView_); |
| 274 |
| 275 views::Widget* widget = hostedView_->GetWidget(); |
| 276 if (!widget->non_client_view()) |
| 277 return; |
| 278 |
| 279 const NSRect frameRect = [self bounds]; |
| 280 gfx::Path mask; |
| 281 widget->non_client_view()->GetWindowMask(gfx::Size(frameRect.size), &mask); |
| 282 if (mask.isEmpty()) |
| 283 return; |
| 284 |
| 285 windowMask_.reset([gfx::CreateNSBezierPathFromSkPath(mask) retain]); |
| 286 |
| 287 // Convert to AppKit coordinate system. |
| 288 NSAffineTransform* flipTransform = [NSAffineTransform transform]; |
| 289 [flipTransform translateXBy:0.0 yBy:frameRect.size.height]; |
| 290 [flipTransform scaleXBy:1.0 yBy:-1.0]; |
| 291 [windowMask_ transformUsingAffineTransform:flipTransform]; |
| 292 } |
| 293 |
255 // BridgedContentView private implementation. | 294 // BridgedContentView private implementation. |
256 | 295 |
257 - (void)handleKeyEvent:(NSEvent*)theEvent { | 296 - (void)handleKeyEvent:(NSEvent*)theEvent { |
258 if (!hostedView_) | 297 if (!hostedView_) |
259 return; | 298 return; |
260 | 299 |
261 DCHECK(theEvent); | 300 DCHECK(theEvent); |
262 ui::KeyEvent event(theEvent); | 301 ui::KeyEvent event(theEvent); |
263 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) | 302 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) |
264 return; | 303 return; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 if (window) | 425 if (window) |
387 newSize = [window contentRectForFrameRect:[window frame]].size; | 426 newSize = [window contentRectForFrameRect:[window frame]].size; |
388 | 427 |
389 [super setFrameSize:newSize]; | 428 [super setFrameSize:newSize]; |
390 if (!hostedView_) | 429 if (!hostedView_) |
391 return; | 430 return; |
392 | 431 |
393 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 432 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
394 } | 433 } |
395 | 434 |
| 435 - (void)viewDidEndLiveResize { |
| 436 [super viewDidEndLiveResize]; |
| 437 |
| 438 // We prevent updating the window mask and clipping the border around the |
| 439 // view, during a live resize. Hence update the window mask and redraw the |
| 440 // view after resize has completed. |
| 441 if (base::mac::IsOSMavericksOrEarlier()) { |
| 442 [self updateWindowMask]; |
| 443 [self setNeedsDisplay:YES]; |
| 444 } |
| 445 } |
| 446 |
396 - (void)drawRect:(NSRect)dirtyRect { | 447 - (void)drawRect:(NSRect)dirtyRect { |
397 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to | 448 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to |
398 // suppress calls to this when the window is known to be hidden. | 449 // suppress calls to this when the window is known to be hidden. |
399 if (!hostedView_) | 450 if (!hostedView_) |
400 return; | 451 return; |
401 | 452 |
402 if (drawMenuBackgroundForBlur_) { | 453 if (drawMenuBackgroundForBlur_) { |
403 const CGFloat radius = views::MenuConfig::instance().corner_radius; | 454 const CGFloat radius = views::MenuConfig::instance().corner_radius; |
404 [skia::SkColorToSRGBNSColor(0x01000000) set]; | 455 [skia::SkColorToSRGBNSColor(0x01000000) set]; |
405 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] | 456 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] |
406 xRadius:radius | 457 xRadius:radius |
407 yRadius:radius] fill]; | 458 yRadius:radius] fill]; |
408 } | 459 } |
409 | 460 |
| 461 // On OS versions earlier than Yosemite, to generate a drop shadow, we set an |
| 462 // opaque background. This causes windows with non rectangular shapes to have |
| 463 // square corners. To get around this, fill the path outside the window |
| 464 // boundary with clearColor and tell Cococa to regenerate drop shadow. See |
| 465 // crbug.com/543671. |
| 466 if (windowMask_ && ![self inLiveResize] && |
| 467 !IsRectInsidePath(dirtyRect, windowMask_)) { |
| 468 DCHECK(base::mac::IsOSMavericksOrEarlier()); |
| 469 gfx::ScopedNSGraphicsContextSaveGState state; |
| 470 |
| 471 // The outer rectangular path corresponding to the window. |
| 472 NSBezierPath* outerPath = [NSBezierPath bezierPathWithRect:[self bounds]]; |
| 473 |
| 474 [outerPath appendBezierPath:windowMask_]; |
| 475 [outerPath setWindingRule:NSEvenOddWindingRule]; |
| 476 [[NSGraphicsContext currentContext] |
| 477 setCompositingOperation:NSCompositeCopy]; |
| 478 [[NSColor clearColor] set]; |
| 479 |
| 480 // Fill the region between windowMask_ and its outer rectangular path |
| 481 // with clear color. This causes the window to have the shape described |
| 482 // by windowMask_. |
| 483 [outerPath fill]; |
| 484 // Regerate drop shadow around the window boundary. |
| 485 [[self window] invalidateShadow]; |
| 486 } |
| 487 |
410 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). | 488 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). |
411 if (hostedView_->GetWidget()->GetLayer()) | 489 if (hostedView_->GetWidget()->GetLayer()) |
412 return; | 490 return; |
413 | 491 |
414 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); | 492 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
415 hostedView_->GetWidget()->OnNativeWidgetPaint( | 493 hostedView_->GetWidget()->OnNativeWidgetPaint( |
416 ui::CanvasPainter(&canvas, 1.f).context()); | 494 ui::CanvasPainter(&canvas, 1.f).context()); |
417 } | 495 } |
418 | 496 |
419 - (NSTextInputContext*)inputContext { | 497 - (NSTextInputContext*)inputContext { |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 } | 904 } |
827 | 905 |
828 return [super accessibilityAttributeValue:attribute]; | 906 return [super accessibilityAttributeValue:attribute]; |
829 } | 907 } |
830 | 908 |
831 - (id)accessibilityHitTest:(NSPoint)point { | 909 - (id)accessibilityHitTest:(NSPoint)point { |
832 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 910 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
833 } | 911 } |
834 | 912 |
835 @end | 913 @end |
OLD | NEW |