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" |
20 #include "ui/gfx/path.h" | |
21 #import "ui/gfx/path_mac.h" | |
22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | |
19 #include "ui/strings/grit/ui_strings.h" | 23 #include "ui/strings/grit/ui_strings.h" |
20 #include "ui/views/controls/menu/menu_config.h" | 24 #include "ui/views/controls/menu/menu_config.h" |
21 #include "ui/views/controls/menu/menu_controller.h" | 25 #include "ui/views/controls/menu/menu_controller.h" |
22 #include "ui/views/view.h" | 26 #include "ui/views/view.h" |
23 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
24 | 28 |
25 using views::MenuController; | 29 using views::MenuController; |
26 | 30 |
27 namespace { | 31 namespace { |
28 | 32 |
33 // Returns true if all four corners of |rect| are contained inside |path|. | |
34 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { | |
35 return [path containsPoint:rect.origin] && | |
36 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | |
37 rect.origin.y)] && | |
38 [path containsPoint:NSMakePoint(rect.origin.x, | |
39 rect.origin.y + rect.size.height)] && | |
40 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | |
41 rect.origin.y + rect.size.height)]; | |
42 } | |
43 | |
29 // Convert a |point| in |source_window|'s AppKit coordinate system (origin at | 44 // Convert a |point| in |source_window|'s AppKit coordinate system (origin at |
30 // the bottom left of the window) to |target_window|'s content rect, with the | 45 // the bottom left of the window) to |target_window|'s content rect, with the |
31 // origin at the top left of the content area. | 46 // origin at the top left of the content area. |
32 // If |source_window| is nil, |point| will be treated as screen coordinates. | 47 // If |source_window| is nil, |point| will be treated as screen coordinates. |
33 gfx::Point MovePointToWindow(const NSPoint& point, | 48 gfx::Point MovePointToWindow(const NSPoint& point, |
34 NSWindow* source_window, | 49 NSWindow* source_window, |
35 NSWindow* target_window) { | 50 NSWindow* target_window) { |
36 NSPoint point_in_screen = source_window | 51 NSPoint point_in_screen = source_window |
37 ? [source_window convertBaseToScreen:point] | 52 ? [source_window convertBaseToScreen:point] |
38 : point; | 53 : point; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 | 125 |
111 // Apple's documentation says that NSTrackingActiveAlways is incompatible | 126 // Apple's documentation says that NSTrackingActiveAlways is incompatible |
112 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. | 127 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. |
113 cursorTrackingArea_.reset([[CrTrackingArea alloc] | 128 cursorTrackingArea_.reset([[CrTrackingArea alloc] |
114 initWithRect:NSZeroRect | 129 initWithRect:NSZeroRect |
115 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | | 130 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | |
116 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect | 131 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect |
117 owner:self | 132 owner:self |
118 userInfo:nil]); | 133 userInfo:nil]); |
119 [self addTrackingArea:cursorTrackingArea_.get()]; | 134 [self addTrackingArea:cursorTrackingArea_.get()]; |
135 | |
136 // Get Notified whenever the view bounds or frame change. | |
137 [self setPostsBoundsChangedNotifications:YES]; | |
138 [self setPostsFrameChangedNotifications:YES]; | |
139 [[NSNotificationCenter defaultCenter] | |
140 addObserver:self | |
141 selector:@selector(boundsDidChangeNotification:) | |
142 name:NSViewBoundsDidChangeNotification | |
143 object:self]; | |
144 [[NSNotificationCenter defaultCenter] | |
145 addObserver:self | |
146 selector:@selector(boundsDidChangeNotification:) | |
147 name:NSViewFrameDidChangeNotification | |
148 object:self]; | |
120 } | 149 } |
121 return self; | 150 return self; |
122 } | 151 } |
123 | 152 |
124 - (void)clearView { | 153 - (void)clearView { |
125 textInputClient_ = nullptr; | 154 textInputClient_ = nullptr; |
126 hostedView_ = nullptr; | 155 hostedView_ = nullptr; |
127 [cursorTrackingArea_.get() clearOwner]; | 156 [cursorTrackingArea_.get() clearOwner]; |
128 [self removeTrackingArea:cursorTrackingArea_.get()]; | 157 [self removeTrackingArea:cursorTrackingArea_.get()]; |
129 } | 158 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 if (window) | 328 if (window) |
300 newSize = [window contentRectForFrameRect:[window frame]].size; | 329 newSize = [window contentRectForFrameRect:[window frame]].size; |
301 | 330 |
302 [super setFrameSize:newSize]; | 331 [super setFrameSize:newSize]; |
303 if (!hostedView_) | 332 if (!hostedView_) |
304 return; | 333 return; |
305 | 334 |
306 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 335 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
307 } | 336 } |
308 | 337 |
338 - (void)viewDidEndLiveResize { | |
339 // We prevent updating the window mask and clipping the border around the | |
340 // view, during a live resize. Hence update the window mask and redraw the | |
341 // view after resize has completed. | |
342 [super viewDidEndLiveResize]; | |
karandeepb
2016/02/04 03:39:28
This optimisation to prevent drawing during a live
| |
343 [self setWindowMask]; | |
344 [self setNeedsDisplay:YES]; | |
345 } | |
346 | |
309 - (void)drawRect:(NSRect)dirtyRect { | 347 - (void)drawRect:(NSRect)dirtyRect { |
310 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to | 348 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to |
311 // suppress calls to this when the window is known to be hidden. | 349 // suppress calls to this when the window is known to be hidden. |
312 if (!hostedView_) | 350 if (!hostedView_) |
313 return; | 351 return; |
314 | 352 |
315 if (drawMenuBackgroundForBlur_) { | 353 if (drawMenuBackgroundForBlur_) { |
316 const CGFloat radius = views::MenuConfig::instance().corner_radius; | 354 const CGFloat radius = views::MenuConfig::instance().corner_radius; |
317 [skia::SkColorToSRGBNSColor(0x01000000) set]; | 355 [skia::SkColorToSRGBNSColor(0x01000000) set]; |
318 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] | 356 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] |
319 xRadius:radius | 357 xRadius:radius |
320 yRadius:radius] fill]; | 358 yRadius:radius] fill]; |
321 } | 359 } |
322 | 360 |
361 // On OS versions earlier than Yosemite, to generate a drop shadow, we set an | |
362 // opaque background. This distorts windows with non rectangular shapes. To | |
363 // get around this, fill the path outside the window boundary with clearColor | |
364 // and tell Cococa to regenerate drop shadow. See crbug.com/543671. | |
365 if (windowMask_ && ![self inLiveResize] && | |
366 !IsRectInsidePath(dirtyRect, windowMask_)) { | |
367 gfx::ScopedNSGraphicsContextSaveGState state; | |
368 DLOG(WARNING) << "Drawing\n"; | |
369 // The outer rectangular path corresponding to the window. | |
370 NSBezierPath* outerPath = [NSBezierPath bezierPathWithRect:[self bounds]]; | |
371 | |
372 [outerPath appendBezierPath:windowMask_]; | |
373 [outerPath setWindingRule:NSEvenOddWindingRule]; | |
374 [[NSGraphicsContext currentContext] | |
375 setCompositingOperation:NSCompositeCopy]; | |
376 [[NSColor clearColor] set]; | |
377 | |
378 // Fill the region between windowMask_ and its outer rectangular path | |
379 // with clear color. This causes the window to have the shape described | |
380 // by windowMask_. | |
381 [outerPath fill]; | |
382 // Regerate drop shadow around the window boundary. | |
383 [[self window] invalidateShadow]; | |
384 } | |
385 | |
323 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). | 386 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). |
324 if (hostedView_->GetWidget()->GetLayer()) | 387 if (hostedView_->GetWidget()->GetLayer()) |
325 return; | 388 return; |
326 | 389 |
327 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); | 390 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
328 hostedView_->GetWidget()->OnNativeWidgetPaint( | 391 hostedView_->GetWidget()->OnNativeWidgetPaint( |
329 ui::CanvasPainter(&canvas, 1.f).context()); | 392 ui::CanvasPainter(&canvas, 1.f).context()); |
330 } | 393 } |
331 | 394 |
332 - (NSTextInputContext*)inputContext { | 395 - (NSTextInputContext*)inputContext { |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
734 return @[ hostedView_->GetNativeViewAccessible() ]; | 797 return @[ hostedView_->GetNativeViewAccessible() ]; |
735 } | 798 } |
736 | 799 |
737 return [super accessibilityAttributeValue:attribute]; | 800 return [super accessibilityAttributeValue:attribute]; |
738 } | 801 } |
739 | 802 |
740 - (id)accessibilityHitTest:(NSPoint)point { | 803 - (id)accessibilityHitTest:(NSPoint)point { |
741 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 804 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
742 } | 805 } |
743 | 806 |
807 // Update windowMask_ depending on the current view bounds. | |
808 - (void)setWindowMask { | |
809 // Ensure a live resize is not in progress. | |
810 DCHECK(![self inLiveResize]); | |
811 | |
812 views::Widget* widget = hostedView_->GetWidget(); | |
813 if (widget->non_client_view() && base::mac::IsOSMavericksOrEarlier()) { | |
814 const NSRect frameRect = [self bounds]; | |
815 gfx::Path mask; | |
816 widget->non_client_view()->GetWindowMask(gfx::Size(frameRect.size), &mask); | |
817 if (!mask.isEmpty()) { | |
818 windowMask_.reset([gfx::CreateNSBezierPathFromSkPath(mask) retain]); | |
819 | |
820 // Convert to AppKit coordinate system. | |
821 NSAffineTransform* flipTransform = [NSAffineTransform transform]; | |
822 [flipTransform translateXBy:0.0 yBy:frameRect.size.height]; | |
823 [flipTransform scaleXBy:1.0 yBy:-1.0]; | |
824 [windowMask_ transformUsingAffineTransform:flipTransform]; | |
825 } | |
826 } | |
827 } | |
828 | |
829 // Called whenever the view's bounds or frame change. Used to update | |
830 // windowMask_. | |
831 - (void)boundsDidChangeNotification:(NSNotification*)notification { | |
832 // Ensure that the notification was posted by this object. | |
833 DCHECK_EQ(notification.object, self); | |
834 // We don't update the window mask during a live resize, instead it is done | |
835 // after the resize is completed in viewDidEndLiveResize:. | |
836 if (![self inLiveResize]) | |
837 [self setWindowMask]; | |
838 } | |
839 | |
840 - (void)dealloc { | |
841 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
842 [super dealloc]; | |
843 } | |
844 | |
744 @end | 845 @end |
OLD | NEW |