Chromium Code Reviews| Index: chrome/browser/ui/cocoa/gradient_button_cell.mm |
| diff --git a/chrome/browser/ui/cocoa/gradient_button_cell.mm b/chrome/browser/ui/cocoa/gradient_button_cell.mm |
| index 8d44cbd8217eff38f3205c490e360c7b185a6565..fd1ec7ac723a64ec1855a54d96a25e64c475d821 100644 |
| --- a/chrome/browser/ui/cocoa/gradient_button_cell.mm |
| +++ b/chrome/browser/ui/cocoa/gradient_button_cell.mm |
| @@ -206,6 +206,13 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| isMouseInside_ = flag; |
| if (pulseState_ != gradient_button_cell::kPulsingContinuous) { |
| if (animated) { |
| + // In Material Design, if the button is already fully on, don't pulse it |
| + // on again if the mouse is within its bounds. |
| + if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback && |
| + isMouseInside_ && pulseState_ == gradient_button_cell::kPulsedOn) { |
| + return; |
| + } |
| + |
| [self setPulseState:(isMouseInside_ ? gradient_button_cell::kPulsingOn : |
| gradient_button_cell::kPulsingOff)]; |
| } else { |
| @@ -319,6 +326,19 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| return trackingArea_ && isMouseInside_; |
| } |
| +- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView { |
| + if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback) { |
| + // THe user has just clicked down in the button. In Material Design, set the |
|
upolat
2016/05/09 23:16:45
typo: THe
shrike
2016/05/09 23:42:37
Done.
|
| + // pulsed (hover) state to off now so that if the user keeps the mouse held |
| + // down while dragging it out of the button's bounds, the button will draw |
| + // itself in its normal state. This is unrelated to dragging the button |
| + // in the button bar, which takes a different path through the code. |
| + [self setPulseState:gradient_button_cell::kPulsedOff]; |
| + } |
| + |
| + return [super startTrackingAt:startPoint inView:controlView]; |
| +} |
| + |
| // Since we have our own drawWithFrame:, we need to also have our own |
| // logic for determining when the mouse is inside for honoring this |
| // request. |
| @@ -348,6 +368,35 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| active:(BOOL)active |
| cellFrame:(NSRect)cellFrame |
| defaultGradient:(NSGradient*)defaultGradient { |
| + // For Material Design, draw a solid rounded rect behind the button, based on |
| + // the hover and pressed states. |
| + if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback) { |
| + const CGFloat kEightPercentAlpha = 0.08; |
| + const CGFloat kFourPercentAlpha = 0.04; |
| + |
| + // The alpha is always at least 8%. Default the color to black. |
| + CGFloat alpha = kEightPercentAlpha; |
| + CGFloat color = 0.0; |
| + // If a dark theme, incrase the opacity slightly and use white. |
| + if ([[controlView window] hasDarkTheme]) { |
| + alpha += kFourPercentAlpha; |
| + color = 1.0; |
| + } |
| + // If clicked or highlighted, the background is slightly more opaque. If not |
| + // clicked or highlighted, adjust the alpha by the animation fade in |
| + // percentage. |
| + if (showClickedGradient || showHighlightGradient) { |
| + alpha += kFourPercentAlpha; |
| + } else { |
| + alpha *= hoverAlpha; |
| + } |
| + |
| + // Fill the path. |
| + [[NSColor colorWithCalibratedWhite:color alpha:alpha] set]; |
| + [innerPath fill]; |
| + return; |
| + } |
| + |
| BOOL isFlatButton = [self showsBorderOnlyWhileMouseInside]; |
| // For flat (unbordered when not hovered) buttons, never use the toolbar |
| @@ -461,14 +510,17 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| innerFrame:(NSRect*)returnInnerFrame |
| innerPath:(NSBezierPath**)returnInnerPath |
| clipPath:(NSBezierPath**)returnClipPath { |
| - const CGFloat lineWidth = [controlView cr_lineWidth]; |
| - const CGFloat halfLineWidth = lineWidth / 2.0; |
| + const CGFloat kLineWidth = [controlView cr_lineWidth]; |
| + const CGFloat kHalfLineWidth = kLineWidth / 2.0; |
| - // Constants from Cole. Will kConstant them once the feedback loop |
| - // is complete. |
| - NSRect drawFrame = NSInsetRect(cellFrame, 1.5 * lineWidth, 1.5 * lineWidth); |
| - NSRect innerFrame = NSInsetRect(cellFrame, lineWidth, lineWidth); |
| - const CGFloat radius = 3; |
| + NSRect drawFrame = cellFrame; |
| + NSRect innerFrame = NSInsetRect(cellFrame, kLineWidth, kLineWidth); |
| + CGFloat cornerRadius = 2; |
| + if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback) { |
| + } else { |
| + drawFrame = NSInsetRect(cellFrame, 1.5 * kLineWidth, 1.5 * kLineWidth); |
| + cornerRadius = 3; |
| + } |
| ButtonType type = [[(NSControl*)controlView cell] tag]; |
| switch (type) { |
| @@ -496,18 +548,18 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| if (returnInnerPath) { |
| DCHECK(*returnInnerPath == nil); |
| *returnInnerPath = [NSBezierPath bezierPathWithRoundedRect:drawFrame |
| - xRadius:radius |
| - yRadius:radius]; |
| - [*returnInnerPath setLineWidth:lineWidth]; |
| + xRadius:cornerRadius |
| + yRadius:cornerRadius]; |
| + [*returnInnerPath setLineWidth:kLineWidth]; |
| } |
| if (returnClipPath) { |
| DCHECK(*returnClipPath == nil); |
| NSRect clipPathRect = |
| - NSInsetRect(drawFrame, -halfLineWidth, -halfLineWidth); |
| + NSInsetRect(drawFrame, -kHalfLineWidth, -kHalfLineWidth); |
| *returnClipPath = [NSBezierPath |
| bezierPathWithRoundedRect:clipPathRect |
| - xRadius:radius + halfLineWidth |
| - yRadius:radius + halfLineWidth]; |
| + xRadius:cornerRadius + kHalfLineWidth |
| + yRadius:cornerRadius + kHalfLineWidth]; |
| } |
| } |
| @@ -565,6 +617,12 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| [self drawInteriorWithFrame:innerFrame inView:controlView]; |
| } |
| +- (CGFloat)textStartXOffset { |
| + // 11 is the magic number needed to make this match the native |
| + // NSButtonCell's label display. |
| + return [[self image] size].width + 11; |
| +} |
| + |
| - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| const CGFloat lineWidth = [controlView cr_lineWidth]; |
| @@ -665,9 +723,7 @@ static const NSTimeInterval kAnimationContinuousCycleDuration = 0.4; |
| if (clipping) |
| [NSBezierPath clipRect:solidPart]; |
| - // 11 is the magic number needed to make this match the native |
| - // NSButtonCell's label display. |
| - CGFloat textLeft = [[self image] size].width + 11; |
| + CGFloat textLeft = [self textStartXOffset]; |
| // For some reason, the height of cellFrame as passed in is totally bogus. |
| // For vertical centering purposes, we need the bounds of the containing |