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