| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 views::View::ConvertPointToTarget(hostedView_, view, &viewPoint); | 173 views::View::ConvertPointToTarget(hostedView_, view, &viewPoint); |
| 159 if (!view->GetTooltipText(viewPoint, &newTooltipText)) | 174 if (!view->GetTooltipText(viewPoint, &newTooltipText)) |
| 160 DCHECK(newTooltipText.empty()); | 175 DCHECK(newTooltipText.empty()); |
| 161 } | 176 } |
| 162 if (newTooltipText != lastTooltipText_) { | 177 if (newTooltipText != lastTooltipText_) { |
| 163 std::swap(newTooltipText, lastTooltipText_); | 178 std::swap(newTooltipText, lastTooltipText_); |
| 164 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; | 179 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; |
| 165 } | 180 } |
| 166 } | 181 } |
| 167 | 182 |
| 183 - (void)updateWindowMask { |
| 184 DCHECK(![self inLiveResize]); |
| 185 DCHECK(base::mac::IsOSMavericksOrEarlier()); |
| 186 DCHECK(hostedView_); |
| 187 |
| 188 views::Widget* widget = hostedView_->GetWidget(); |
| 189 if (!widget->non_client_view()) |
| 190 return; |
| 191 |
| 192 const NSRect frameRect = [self bounds]; |
| 193 gfx::Path mask; |
| 194 widget->non_client_view()->GetWindowMask(gfx::Size(frameRect.size), &mask); |
| 195 if (mask.isEmpty()) |
| 196 return; |
| 197 |
| 198 windowMask_.reset([gfx::CreateNSBezierPathFromSkPath(mask) retain]); |
| 199 |
| 200 // Convert to AppKit coordinate system. |
| 201 NSAffineTransform* flipTransform = [NSAffineTransform transform]; |
| 202 [flipTransform translateXBy:0.0 yBy:frameRect.size.height]; |
| 203 [flipTransform scaleXBy:1.0 yBy:-1.0]; |
| 204 [windowMask_ transformUsingAffineTransform:flipTransform]; |
| 205 } |
| 206 |
| 168 // BridgedContentView private implementation. | 207 // BridgedContentView private implementation. |
| 169 | 208 |
| 170 - (void)handleKeyEvent:(NSEvent*)theEvent { | 209 - (void)handleKeyEvent:(NSEvent*)theEvent { |
| 171 if (!hostedView_) | 210 if (!hostedView_) |
| 172 return; | 211 return; |
| 173 | 212 |
| 174 DCHECK(theEvent); | 213 DCHECK(theEvent); |
| 175 ui::KeyEvent event(theEvent); | 214 ui::KeyEvent event(theEvent); |
| 176 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) | 215 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) |
| 177 return; | 216 return; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 if (window) | 338 if (window) |
| 300 newSize = [window contentRectForFrameRect:[window frame]].size; | 339 newSize = [window contentRectForFrameRect:[window frame]].size; |
| 301 | 340 |
| 302 [super setFrameSize:newSize]; | 341 [super setFrameSize:newSize]; |
| 303 if (!hostedView_) | 342 if (!hostedView_) |
| 304 return; | 343 return; |
| 305 | 344 |
| 306 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 345 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
| 307 } | 346 } |
| 308 | 347 |
| 348 - (void)viewDidEndLiveResize { |
| 349 [super viewDidEndLiveResize]; |
| 350 |
| 351 // We prevent updating the window mask and clipping the border around the |
| 352 // view, during a live resize. Hence update the window mask and redraw the |
| 353 // view after resize has completed. |
| 354 if (base::mac::IsOSMavericksOrEarlier()) { |
| 355 [self updateWindowMask]; |
| 356 [self setNeedsDisplay:YES]; |
| 357 } |
| 358 } |
| 359 |
| 309 - (void)drawRect:(NSRect)dirtyRect { | 360 - (void)drawRect:(NSRect)dirtyRect { |
| 310 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to | 361 // Note that BridgedNativeWidget uses -[NSWindow setAutodisplay:NO] to |
| 311 // suppress calls to this when the window is known to be hidden. | 362 // suppress calls to this when the window is known to be hidden. |
| 312 if (!hostedView_) | 363 if (!hostedView_) |
| 313 return; | 364 return; |
| 314 | 365 |
| 315 if (drawMenuBackgroundForBlur_) { | 366 if (drawMenuBackgroundForBlur_) { |
| 316 const CGFloat radius = views::MenuConfig::instance().corner_radius; | 367 const CGFloat radius = views::MenuConfig::instance().corner_radius; |
| 317 [skia::SkColorToSRGBNSColor(0x01000000) set]; | 368 [skia::SkColorToSRGBNSColor(0x01000000) set]; |
| 318 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] | 369 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] |
| 319 xRadius:radius | 370 xRadius:radius |
| 320 yRadius:radius] fill]; | 371 yRadius:radius] fill]; |
| 321 } | 372 } |
| 322 | 373 |
| 374 // On OS versions earlier than Yosemite, to generate a drop shadow, we set an |
| 375 // opaque background. This causes windows with non rectangular shapes to have |
| 376 // square corners. To get around this, fill the path outside the window |
| 377 // boundary with clearColor and tell Cococa to regenerate drop shadow. See |
| 378 // crbug.com/543671. |
| 379 if (windowMask_ && ![self inLiveResize] && |
| 380 !IsRectInsidePath(dirtyRect, windowMask_)) { |
| 381 DCHECK(base::mac::IsOSMavericksOrEarlier()); |
| 382 gfx::ScopedNSGraphicsContextSaveGState state; |
| 383 |
| 384 // The outer rectangular path corresponding to the window. |
| 385 NSBezierPath* outerPath = [NSBezierPath bezierPathWithRect:[self bounds]]; |
| 386 |
| 387 [outerPath appendBezierPath:windowMask_]; |
| 388 [outerPath setWindingRule:NSEvenOddWindingRule]; |
| 389 [[NSGraphicsContext currentContext] |
| 390 setCompositingOperation:NSCompositeCopy]; |
| 391 [[NSColor clearColor] set]; |
| 392 |
| 393 // Fill the region between windowMask_ and its outer rectangular path |
| 394 // with clear color. This causes the window to have the shape described |
| 395 // by windowMask_. |
| 396 [outerPath fill]; |
| 397 // Regerate drop shadow around the window boundary. |
| 398 [[self window] invalidateShadow]; |
| 399 } |
| 400 |
| 323 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). | 401 // If there's a layer, painting occurs in BridgedNativeWidget::OnPaintLayer(). |
| 324 if (hostedView_->GetWidget()->GetLayer()) | 402 if (hostedView_->GetWidget()->GetLayer()) |
| 325 return; | 403 return; |
| 326 | 404 |
| 327 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); | 405 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
| 328 hostedView_->GetWidget()->OnNativeWidgetPaint( | 406 hostedView_->GetWidget()->OnNativeWidgetPaint( |
| 329 ui::CanvasPainter(&canvas, 1.f).context()); | 407 ui::CanvasPainter(&canvas, 1.f).context()); |
| 330 } | 408 } |
| 331 | 409 |
| 332 - (NSTextInputContext*)inputContext { | 410 - (NSTextInputContext*)inputContext { |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 } | 813 } |
| 736 | 814 |
| 737 return [super accessibilityAttributeValue:attribute]; | 815 return [super accessibilityAttributeValue:attribute]; |
| 738 } | 816 } |
| 739 | 817 |
| 740 - (id)accessibilityHitTest:(NSPoint)point { | 818 - (id)accessibilityHitTest:(NSPoint)point { |
| 741 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 819 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 742 } | 820 } |
| 743 | 821 |
| 744 @end | 822 @end |
| OLD | NEW |