| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/ui/cocoa/gradient_button_cell.h" | 5 #include "chrome/browser/ui/cocoa/gradient_button_cell.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 ThemeProperties::COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE) : | 483 ThemeProperties::COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE) : |
| 484 [NSColor colorWithCalibratedWhite:0.0 | 484 [NSColor colorWithCalibratedWhite:0.0 |
| 485 alpha:0.3 * outerStrokeAlphaMult_]; | 485 alpha:0.3 * outerStrokeAlphaMult_]; |
| 486 } | 486 } |
| 487 [strokeColor setStroke]; | 487 [strokeColor setStroke]; |
| 488 | 488 |
| 489 [innerPath setLineWidth:1]; | 489 [innerPath setLineWidth:1]; |
| 490 [innerPath stroke]; | 490 [innerPath stroke]; |
| 491 } | 491 } |
| 492 | 492 |
| 493 - (CGFloat)insetInView:(NSView*)controlView { |
| 494 return [controlView cr_lineWidth]; |
| 495 } |
| 496 |
| 493 // TODO(viettrungluu): clean this up. | 497 // TODO(viettrungluu): clean this up. |
| 494 // (Private) | 498 // (Private) |
| 495 - (void)getDrawParamsForFrame:(NSRect)cellFrame | 499 - (void)getDrawParamsForFrame:(NSRect)cellFrame |
| 496 inView:(NSView*)controlView | 500 inView:(NSView*)controlView |
| 497 innerFrame:(NSRect*)returnInnerFrame | 501 innerFrame:(NSRect*)returnInnerFrame |
| 498 innerPath:(NSBezierPath**)returnInnerPath | 502 innerPath:(NSBezierPath**)returnInnerPath |
| 499 clipPath:(NSBezierPath**)returnClipPath { | 503 clipPath:(NSBezierPath**)returnClipPath { |
| 500 const CGFloat kLineWidth = [controlView cr_lineWidth]; | 504 const CGFloat lineWidth = [self insetInView:controlView]; |
| 501 const CGFloat kHalfLineWidth = kLineWidth / 2.0; | 505 const CGFloat halfLineWidth = lineWidth / 2.0; |
| 502 | 506 |
| 503 NSRect drawFrame = NSZeroRect; | 507 NSRect drawFrame = NSZeroRect; |
| 504 NSRect innerFrame = NSZeroRect; | 508 NSRect innerFrame = NSZeroRect; |
| 505 CGFloat cornerRadius = 2; | 509 CGFloat cornerRadius = 2; |
| 506 if (![self isMaterialDesignButtonType]) { | 510 if (![self isMaterialDesignButtonType]) { |
| 507 drawFrame = NSInsetRect(cellFrame, 1.5 * kLineWidth, 1.5 * kLineWidth); | 511 drawFrame = NSInsetRect(cellFrame, 1.5 * lineWidth, 1.5 * lineWidth); |
| 508 innerFrame = NSInsetRect(cellFrame, kLineWidth, kLineWidth); | 512 innerFrame = NSInsetRect(cellFrame, lineWidth, lineWidth); |
| 509 cornerRadius = 3; | 513 cornerRadius = 3; |
| 510 } else { | 514 } else { |
| 511 drawFrame = cellFrame; | 515 drawFrame = cellFrame; |
| 512 // Hover and click paths are always 20pt tall, regardless of the button's | 516 // Hover and click paths are always 20pt tall, regardless of the button's |
| 513 // height. | 517 // height. |
| 514 drawFrame.size.height = 20; | 518 drawFrame.size.height = 20; |
| 515 innerFrame = NSInsetRect(drawFrame, kLineWidth, kLineWidth); | 519 innerFrame = NSInsetRect(drawFrame, lineWidth, lineWidth); |
| 516 } | 520 } |
| 517 | 521 |
| 518 ButtonType type = [[(NSControl*)controlView cell] tag]; | 522 ButtonType type = [[(NSControl*)controlView cell] tag]; |
| 519 switch (type) { | 523 switch (type) { |
| 520 case kMiddleButtonType: | 524 case kMiddleButtonType: |
| 521 drawFrame.size.width += 20; | 525 drawFrame.size.width += 20; |
| 522 innerFrame.size.width += 2; | 526 innerFrame.size.width += 2; |
| 523 // Fallthrough | 527 // Fallthrough |
| 524 case kRightButtonType: | 528 case kRightButtonType: |
| 525 drawFrame.origin.x -= 20; | 529 drawFrame.origin.x -= 20; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 543 drawFrame.origin.y += | 547 drawFrame.origin.y += |
| 544 [self hoverBackgroundVerticalOffsetInControlView:controlView]; | 548 [self hoverBackgroundVerticalOffsetInControlView:controlView]; |
| 545 | 549 |
| 546 if ([self tag] == kMaterialMenuButtonTypeWithLimitedClickFeedback) { | 550 if ([self tag] == kMaterialMenuButtonTypeWithLimitedClickFeedback) { |
| 547 *returnInnerPath = [NSBezierPath bezierPathWithRect:drawFrame]; | 551 *returnInnerPath = [NSBezierPath bezierPathWithRect:drawFrame]; |
| 548 } else { | 552 } else { |
| 549 *returnInnerPath = [NSBezierPath bezierPathWithRoundedRect:drawFrame | 553 *returnInnerPath = [NSBezierPath bezierPathWithRoundedRect:drawFrame |
| 550 xRadius:cornerRadius | 554 xRadius:cornerRadius |
| 551 yRadius:cornerRadius]; | 555 yRadius:cornerRadius]; |
| 552 } | 556 } |
| 553 [*returnInnerPath setLineWidth:kLineWidth]; | 557 [*returnInnerPath setLineWidth:lineWidth]; |
| 554 } | 558 } |
| 555 if (returnClipPath) { | 559 if (returnClipPath) { |
| 556 DCHECK(*returnClipPath == nil); | 560 DCHECK(*returnClipPath == nil); |
| 557 NSRect clipPathRect = | 561 NSRect clipPathRect = |
| 558 NSInsetRect(drawFrame, -kHalfLineWidth, -kHalfLineWidth); | 562 NSInsetRect(drawFrame, -halfLineWidth, -halfLineWidth); |
| 559 *returnClipPath = [NSBezierPath | 563 *returnClipPath = |
| 560 bezierPathWithRoundedRect:clipPathRect | 564 [NSBezierPath bezierPathWithRoundedRect:clipPathRect |
| 561 xRadius:cornerRadius + kHalfLineWidth | 565 xRadius:cornerRadius + halfLineWidth |
| 562 yRadius:cornerRadius + kHalfLineWidth]; | 566 yRadius:cornerRadius + halfLineWidth]; |
| 563 } | 567 } |
| 564 } | 568 } |
| 565 | 569 |
| 566 // TODO(viettrungluu): clean this up. | 570 // TODO(viettrungluu): clean this up. |
| 567 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 571 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 568 NSRect innerFrame; | 572 NSRect innerFrame; |
| 569 NSBezierPath* innerPath = nil; | 573 NSBezierPath* innerPath = nil; |
| 570 [self getDrawParamsForFrame:cellFrame | 574 [self getDrawParamsForFrame:cellFrame |
| 571 inView:controlView | 575 inView:controlView |
| 572 innerFrame:&innerFrame | 576 innerFrame:&innerFrame |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 NSDivideRect(cellFrame, &gradientPart, &solidPart, gradientWidth, NSMaxXEdge); | 740 NSDivideRect(cellFrame, &gradientPart, &solidPart, gradientWidth, NSMaxXEdge); |
| 737 | 741 |
| 738 // Draw non-gradient part without transparency layer, as light text on a dark | 742 // Draw non-gradient part without transparency layer, as light text on a dark |
| 739 // background looks bad with a gradient layer. | 743 // background looks bad with a gradient layer. |
| 740 NSPoint textOffset = NSZeroPoint; | 744 NSPoint textOffset = NSZeroPoint; |
| 741 { | 745 { |
| 742 gfx::ScopedNSGraphicsContextSaveGState scopedGState; | 746 gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
| 743 if (shouldClipTheTitle) | 747 if (shouldClipTheTitle) |
| 744 [NSBezierPath clipRect:solidPart]; | 748 [NSBezierPath clipRect:solidPart]; |
| 745 | 749 |
| 746 CGFloat textLeft = [self textStartXOffset]; | |
| 747 | 750 |
| 748 // For some reason, the height of cellFrame as passed in is totally bogus. | 751 // For some reason, the height of cellFrame as passed in is totally bogus. |
| 749 // For vertical centering purposes, we need the bounds of the containing | 752 // For vertical centering purposes, we need the bounds of the containing |
| 750 // view. | 753 // view. |
| 751 NSRect buttonFrame = [[self controlView] frame]; | 754 NSRect buttonFrame = [[self controlView] frame]; |
| 752 | 755 |
| 753 // Call the vertical offset to match native NSButtonCell's version. | 756 // Call the vertical offset to match native NSButtonCell's version. |
| 754 textOffset = NSMakePoint(textLeft, | 757 textOffset = NSMakePoint( |
| 755 (NSHeight(buttonFrame) - size.height) / 2 + | 758 NSMinX(cellFrame), |
| 756 [self verticalTextOffset]); | 759 (NSHeight(buttonFrame) - size.height) / 2 + [self verticalTextOffset]); |
| 757 // WIth Material Design we want an ellipsis if the title is too long to fit, | 760 // WIth Material Design we want an ellipsis if the title is too long to fit, |
| 758 // so have to use drawInRect: instead of drawAtPoint:. | 761 // so have to use drawInRect: instead of drawAtPoint:. |
| 759 if (isModeMaterial) { | 762 if (isModeMaterial) { |
| 760 NSRect textBounds = NSMakeRect(textOffset.x, | 763 NSRect textFrame = NSMakeRect(textOffset.x, textOffset.y, |
| 761 textOffset.y, | 764 NSWidth(cellFrame), NSHeight(buttonFrame)); |
| 762 NSWidth(buttonFrame) - textOffset.x, | 765 [title drawInRect:textFrame]; |
| 763 NSHeight(buttonFrame)); | |
| 764 [title drawInRect:textBounds]; | |
| 765 } else { | 766 } else { |
| 766 [title drawAtPoint:textOffset]; | 767 [title drawAtPoint:textOffset]; |
| 767 } | 768 } |
| 768 } | 769 } |
| 769 | 770 |
| 770 if (!shouldClipTheTitle) | 771 if (!shouldClipTheTitle) |
| 771 return cellFrame; | 772 return cellFrame; |
| 772 | 773 |
| 773 // Draw the gradient part with a transparency layer. This makes the text look | 774 // Draw the gradient part with a transparency layer. This makes the text look |
| 774 // suboptimal, but since it fades out, that's ok. | 775 // suboptimal, but since it fades out, that's ok. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 options:options | 846 options:options |
| 846 owner:self | 847 owner:self |
| 847 userInfo:nil]); | 848 userInfo:nil]); |
| 848 if (isMouseInside_ != mouseInView) { | 849 if (isMouseInside_ != mouseInView) { |
| 849 [self setMouseInside:mouseInView animate:NO]; | 850 [self setMouseInside:mouseInView animate:NO]; |
| 850 [controlView setNeedsDisplay:YES]; | 851 [controlView setNeedsDisplay:YES]; |
| 851 } | 852 } |
| 852 } | 853 } |
| 853 | 854 |
| 854 @end | 855 @end |
| OLD | NEW |