Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/ui/cocoa/fast_resize_view.h" | 5 #import "chrome/browser/ui/cocoa/fast_resize_view.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "ui/base/cocoa/animation_utils.h" | 12 #include "ui/base/cocoa/animation_utils.h" |
| 13 #include "ui/base/ui_base_switches.h" | 13 #include "ui/base/ui_base_switches.h" |
| 14 | 14 |
| 15 namespace { | |
| 16 | |
| 17 // The radius of the rounded corners. | |
| 18 const CGFloat kRoundedCornerRadius = 4; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 15 @interface FastResizeView (PrivateMethods) | 22 @interface FastResizeView (PrivateMethods) |
| 16 // Lays out this views subviews. If fast resize mode is on, does not resize any | 23 // Lays out this views subviews. If fast resize mode is on, does not resize any |
| 17 // subviews and instead pegs them to the top left. If fast resize mode is off, | 24 // subviews and instead pegs them to the top left. If fast resize mode is off, |
| 18 // sets the subviews' frame to be equal to this view's bounds. | 25 // sets the subviews' frame to be equal to this view's bounds. |
| 19 - (void)layoutSubviews; | 26 - (void)layoutSubviews; |
| 27 | |
| 28 // Creates a path whose bottom two corners are rounded. | |
| 29 // Caller takes ownership of the path. | |
| 30 - (CGPathRef)createRoundedBottomCornersPath:(NSSize)size; | |
| 31 | |
| 32 // Updates the path of the layer mask to reflect the current value of | |
| 33 // roundedBottomCorners_ and fastResizeMode_. | |
| 34 - (void)updateLayerMask; | |
| 20 @end | 35 @end |
| 21 | 36 |
| 22 @implementation FastResizeView | 37 @implementation FastResizeView |
| 23 | 38 |
| 24 - (id)initWithFrame:(NSRect)frameRect { | 39 - (id)initWithFrame:(NSRect)frameRect { |
| 25 if ((self = [super initWithFrame:frameRect])) { | 40 if ((self = [super initWithFrame:frameRect])) { |
| 26 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 41 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 27 switches::kDisableCoreAnimation)) { | 42 switches::kDisableCoreAnimation)) { |
| 28 ScopedCAActionDisabler disabler; | 43 ScopedCAActionDisabler disabler; |
| 29 base::scoped_nsobject<CALayer> layer([[CALayer alloc] init]); | 44 base::scoped_nsobject<CALayer> layer([[CALayer alloc] init]); |
| 30 [layer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; | 45 [layer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; |
| 31 [self setLayer:layer]; | 46 [self setLayer:layer]; |
| 32 [self setWantsLayer:YES]; | 47 [self setWantsLayer:YES]; |
| 48 | |
| 49 roundedBottomCorners_ = YES; | |
| 50 [self updateLayerMask]; | |
| 33 } | 51 } |
| 34 } | 52 } |
| 35 return self; | 53 return self; |
| 36 } | 54 } |
| 37 | 55 |
| 38 - (BOOL)isOpaque { | 56 - (BOOL)isOpaque { |
| 39 return YES; | 57 return YES; |
| 40 } | 58 } |
| 41 | 59 |
| 42 - (void)setFastResizeMode:(BOOL)fastResizeMode { | 60 - (void)setFastResizeMode:(BOOL)fastResizeMode { |
| 43 if (fastResizeMode_ == fastResizeMode) | 61 if (fastResizeMode_ == fastResizeMode) |
| 44 return; | 62 return; |
| 45 | 63 |
| 46 fastResizeMode_ = fastResizeMode; | 64 fastResizeMode_ = fastResizeMode; |
| 65 [self updateLayerMask]; | |
| 47 | 66 |
| 48 // Force a relayout when coming out of fast resize mode. | 67 // Force a relayout when coming out of fast resize mode. |
| 49 if (!fastResizeMode_) | 68 if (!fastResizeMode_) |
| 50 [self layoutSubviews]; | 69 [self layoutSubviews]; |
| 51 | 70 |
| 52 [self setNeedsDisplay:YES]; | 71 [self setNeedsDisplay:YES]; |
| 53 } | 72 } |
| 54 | 73 |
| 74 - (void)setRoundedBottomCorners:(BOOL)roundedBottomCorners { | |
| 75 if (roundedBottomCorners == roundedBottomCorners_) | |
| 76 return; | |
| 77 | |
| 78 roundedBottomCorners_ = roundedBottomCorners; | |
| 79 [self updateLayerMask]; | |
| 80 } | |
| 81 | |
| 55 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize { | 82 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize { |
| 56 [self layoutSubviews]; | 83 [self layoutSubviews]; |
| 57 } | 84 } |
| 58 | 85 |
| 59 - (void)drawRect:(NSRect)dirtyRect { | 86 - (void)drawRect:(NSRect)dirtyRect { |
| 60 // If we are in fast resize mode, our subviews may not completely cover our | 87 // If we are in fast resize mode, our subviews may not completely cover our |
| 61 // bounds, so we fill with white. If we are not in fast resize mode, we do | 88 // bounds, so we fill with white. If we are not in fast resize mode, we do |
| 62 // not need to draw anything. | 89 // not need to draw anything. |
| 63 if (!fastResizeMode_) | 90 if (!fastResizeMode_) |
| 64 return; | 91 return; |
| 65 | 92 |
| 66 [[NSColor whiteColor] set]; | 93 [[NSColor whiteColor] set]; |
| 67 NSRectFill(dirtyRect); | 94 NSRectFill(dirtyRect); |
| 68 } | 95 } |
| 69 | 96 |
| 97 // Every time the frame's size changes, the layer mask needs to be updated. | |
| 98 - (void)setFrameSize:(NSSize)newSize { | |
| 99 [super setFrameSize:newSize]; | |
| 100 [self updateLayerMask]; | |
| 101 } | |
| 102 | |
| 70 @end | 103 @end |
| 71 | 104 |
| 72 @implementation FastResizeView (PrivateMethods) | 105 @implementation FastResizeView (PrivateMethods) |
| 73 | 106 |
| 74 - (void)layoutSubviews { | 107 - (void)layoutSubviews { |
| 75 // There should never be more than one subview. There can be zero, if we are | 108 // There should never be more than one subview. There can be zero, if we are |
| 76 // in the process of switching tabs or closing the window. In those cases, no | 109 // in the process of switching tabs or closing the window. In those cases, no |
| 77 // layout is needed. | 110 // layout is needed. |
| 78 NSArray* subviews = [self subviews]; | 111 NSArray* subviews = [self subviews]; |
| 79 DCHECK([subviews count] <= 1); | 112 DCHECK([subviews count] <= 1); |
| 80 if ([subviews count] < 1) | 113 if ([subviews count] < 1) |
| 81 return; | 114 return; |
| 82 | 115 |
| 83 NSView* subview = [subviews objectAtIndex:0]; | 116 NSView* subview = [subviews objectAtIndex:0]; |
| 84 NSRect bounds = [self bounds]; | 117 NSRect bounds = [self bounds]; |
| 85 | 118 |
| 86 if (fastResizeMode_) { | 119 if (fastResizeMode_) { |
| 87 NSRect frame = [subview frame]; | 120 NSRect frame = [subview frame]; |
| 88 frame.origin.x = 0; | 121 frame.origin.x = 0; |
| 89 frame.origin.y = NSHeight(bounds) - NSHeight(frame); | 122 frame.origin.y = NSHeight(bounds) - NSHeight(frame); |
| 90 [subview setFrame:frame]; | 123 [subview setFrame:frame]; |
| 91 } else { | 124 } else { |
| 92 [subview setFrame:bounds]; | 125 [subview setFrame:bounds]; |
| 93 } | 126 } |
| 94 } | 127 } |
| 95 | 128 |
| 129 - (CGPathRef)createRoundedBottomCornersPath:(NSSize)size { | |
| 130 CGMutablePathRef path = CGPathCreateMutable(); | |
| 131 CGFloat width = size.width; | |
| 132 CGFloat height = size.height; | |
| 133 CGFloat cornerRadius = kRoundedCornerRadius; | |
| 134 | |
| 135 // Top left corner. | |
| 136 CGPathMoveToPoint(path, NULL, 0, height); | |
| 137 | |
| 138 // Top right corner. | |
| 139 CGPathAddLineToPoint(path, NULL, width, height); | |
| 140 | |
| 141 // Bottom right corner. | |
| 142 CGPathAddRelativeArc( | |
| 143 path, NULL, width - cornerRadius, cornerRadius, cornerRadius, 0, -M_PI_2); | |
| 144 | |
| 145 // Bottom left corner. | |
| 146 CGPathAddRelativeArc( | |
| 147 path, NULL, cornerRadius, cornerRadius, cornerRadius, -M_PI_2, -M_PI_2); | |
| 148 | |
| 149 // Draw line back to top-left corner. | |
| 150 CGPathAddLineToPoint(path, NULL, 0, height); | |
| 151 CGPathCloseSubpath(path); | |
| 152 return path; | |
| 153 } | |
| 154 | |
| 155 - (void)updateLayerMask { | |
| 156 if (fastResizeMode_ || !roundedBottomCorners_) { | |
| 157 [self layer].mask = nil; | |
| 158 layerMask_ = nil; | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 if (![self layer].mask) { | |
| 163 CAShapeLayer* maskLayer = [CAShapeLayer layer]; | |
| 164 [self layer].mask = maskLayer; | |
| 165 layerMask_ = maskLayer; | |
|
Andre
2014/08/12 23:06:16
optional nit: can do away with maskLayer local var
erikchen
2014/08/12 23:09:25
Done.
| |
| 166 } | |
| 167 | |
| 168 CGPathRef path = [self createRoundedBottomCornersPath:self.bounds.size]; | |
| 169 layerMask_.path = path; | |
| 170 CGPathRelease(path); | |
| 171 } | |
| 172 | |
| 96 @end | 173 @end |
| OLD | NEW |