| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/new_tab_button.h" | 5 #import "chrome/browser/ui/cocoa/new_tab_button.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/sdk_forward_declarations.h" | 8 #include "base/mac/sdk_forward_declarations.h" |
| 9 #import "chrome/browser/ui/cocoa/image_button_cell.h" | 9 #import "chrome/browser/ui/cocoa/image_button_cell.h" |
| 10 #include "chrome/browser/ui/cocoa/l10n_util.h" | 10 #include "chrome/browser/ui/cocoa/l10n_util.h" |
| 11 #include "chrome/browser/ui/cocoa/tabs/tab_view.h" | 11 #include "chrome/browser/ui/cocoa/tabs/tab_view.h" |
| 12 #include "chrome/grit/theme_resources.h" | 12 #include "chrome/grit/theme_resources.h" |
| 13 #include "ui/base/cocoa/nsgraphics_context_additions.h" | 13 #include "ui/base/cocoa/nsgraphics_context_additions.h" |
| 14 #include "ui/base/material_design/material_design_controller.h" | 14 #include "ui/base/material_design/material_design_controller.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/base/theme_provider.h" | 16 #include "ui/base/theme_provider.h" |
| 17 | 17 |
| 18 @class NewTabButtonCell; | 18 @class NewTabButtonCell; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 enum class OverlayOption { | 22 enum class OverlayOption { |
| 23 NONE, | 23 NONE, |
| 24 LIGHTEN, | 24 LIGHTEN, |
| 25 DARKEN, | 25 DARKEN, |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 const NSSize newTabButtonImageSize = { 34, 18 }; | 28 const NSSize newTabButtonImageSize = {34, 18}; |
| 29 | |
| 30 const CGFloat k7PercentAlpha = 0.07; | |
| 31 const CGFloat k8PercentAlpha = 0.08; | |
| 32 const CGFloat k10PercentAlpha = 0.1; | |
| 33 const CGFloat k20PercentAlpha = 0.2; | |
| 34 const CGFloat k25PercentAlpha = 0.25; | |
| 35 | 29 |
| 36 NSImage* GetMaskImageFromCell(NewTabButtonCell* aCell) { | 30 NSImage* GetMaskImageFromCell(NewTabButtonCell* aCell) { |
| 37 return [aCell imageForState:image_button_cell::kDefaultState view:nil]; | 31 return [aCell imageForState:image_button_cell::kDefaultState view:nil]; |
| 38 } | 32 } |
| 39 | 33 |
| 40 // Creates an NSImage with size |size| and bitmap image representations for both | 34 // Creates an NSImage with size |size| and bitmap image representations for both |
| 41 // 1x and 2x scale factors. |drawingHandler| is called once for every scale | 35 // 1x and 2x scale factors. |drawingHandler| is called once for every scale |
| 42 // factor. This is similar to -[NSImage imageWithSize:flipped:drawingHandler:], | 36 // factor. This is similar to -[NSImage imageWithSize:flipped:drawingHandler:], |
| 43 // but this function always evaluates drawingHandler eagerly, and it works on | 37 // but this function always evaluates drawingHandler eagerly, and it works on |
| 44 // 10.6 and 10.7. | 38 // 10.6 and 10.7. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 172 |
| 179 // Returns a new tab button image appropriate for the specified button state | 173 // Returns a new tab button image appropriate for the specified button state |
| 180 // (e.g. hover) and theme. In Material Design, the theme color affects the | 174 // (e.g. hover) and theme. In Material Design, the theme color affects the |
| 181 // button color. | 175 // button color. |
| 182 - (NSImage*)imageForState:(image_button_cell::ButtonState)state | 176 - (NSImage*)imageForState:(image_button_cell::ButtonState)state |
| 183 theme:(const ui::ThemeProvider*)theme; | 177 theme:(const ui::ThemeProvider*)theme; |
| 184 | 178 |
| 185 // Returns a new tab button image bezier path with the specified line width. | 179 // Returns a new tab button image bezier path with the specified line width. |
| 186 + (NSBezierPath*)newTabButtonBezierPathWithLineWidth:(CGFloat)lineWidth; | 180 + (NSBezierPath*)newTabButtonBezierPathWithLineWidth:(CGFloat)lineWidth; |
| 187 | 181 |
| 188 // NSCustomImageRep custom drawing method that renders the new tab button image. | 182 // Draws the new tab button image to |imageRep|, with either a normal stroke or |
| 189 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep; | 183 // a heavy stroke for increased visibility. |
| 184 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep |
| 185 withHeavyStroke:(BOOL)heavyStroke; |
| 186 |
| 187 // NSCustomImageRep custom drawing method shims for normal and heavy strokes |
| 188 // respectively. |
| 189 + (void)drawNewTabButtonImageWithNormalStroke: |
| 190 (NewTabButtonCustomImageRep*)imageRep; |
| 191 + (void)drawNewTabButtonImageWithHeavyStroke: |
| 192 (NewTabButtonCustomImageRep*)imageRep; |
| 190 | 193 |
| 191 // Returns a new tab button image filled with |fillColor|. | 194 // Returns a new tab button image filled with |fillColor|. |
| 192 - (NSImage*)imageWithFillColor:(NSColor*)fillColor; | 195 - (NSImage*)imageWithFillColor:(NSColor*)fillColor; |
| 193 | 196 |
| 194 @end | 197 @end |
| 195 | 198 |
| 196 @implementation NewTabButton | 199 @implementation NewTabButton |
| 197 | 200 |
| 198 + (Class)cellClass { | 201 + (Class)cellClass { |
| 199 return [NewTabButtonCell class]; | 202 return [NewTabButtonCell class]; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 fillColor = | 314 fillColor = |
| 312 theme->GetNSImageColorNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE); | 315 theme->GetNSImageColorNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE); |
| 313 break; | 316 break; |
| 314 | 317 |
| 315 default: | 318 default: |
| 316 fillColor = [NSColor redColor]; | 319 fillColor = [NSColor redColor]; |
| 317 // All states should be accounted for above. | 320 // All states should be accounted for above. |
| 318 NOTREACHED(); | 321 NOTREACHED(); |
| 319 } | 322 } |
| 320 | 323 |
| 324 SEL drawSelector = @selector(drawNewTabButtonImageWithNormalStroke:); |
| 325 if (theme && theme->ShouldIncreaseContrast()) |
| 326 drawSelector = @selector(drawNewTabButtonImageWithHeavyStroke:); |
| 327 |
| 321 base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep( | 328 base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep( |
| 322 [[NewTabButtonCustomImageRep alloc] | 329 [[NewTabButtonCustomImageRep alloc] |
| 323 initWithDrawSelector:@selector(drawNewTabButtonImage:) | 330 initWithDrawSelector:drawSelector |
| 324 delegate:[NewTabButton class]]); | 331 delegate:[NewTabButton class]]); |
| 325 [imageRep setDestView:self]; | 332 [imageRep setDestView:self]; |
| 326 [imageRep setFillColor:fillColor]; | 333 [imageRep setFillColor:fillColor]; |
| 327 [imageRep setPatternPhasePosition: | 334 [imageRep setPatternPhasePosition: |
| 328 [[self window] | 335 [[self window] |
| 329 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]]; | 336 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]]; |
| 330 [imageRep setOverlayOption:overlayOption]; | 337 [imageRep setOverlayOption:overlayOption]; |
| 331 | 338 |
| 332 NSImage* newTabButtonImage = | 339 NSImage* newTabButtonImage = |
| 333 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; | 340 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // Adjust by half the line width to get crisp lines. | 398 // Adjust by half the line width to get crisp lines. |
| 392 NSAffineTransform* transform = [NSAffineTransform transform]; | 399 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 393 [transform translateXBy:lineWidth / 2 yBy:lineWidth / 2]; | 400 [transform translateXBy:lineWidth / 2 yBy:lineWidth / 2]; |
| 394 [bezierPath transformUsingAffineTransform:transform]; | 401 [bezierPath transformUsingAffineTransform:transform]; |
| 395 | 402 |
| 396 [bezierPath setLineWidth:lineWidth]; | 403 [bezierPath setLineWidth:lineWidth]; |
| 397 | 404 |
| 398 return bezierPath; | 405 return bezierPath; |
| 399 } | 406 } |
| 400 | 407 |
| 401 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep { | 408 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep |
| 409 withHeavyStroke:(BOOL)heavyStroke { |
| 402 [[NSGraphicsContext currentContext] | 410 [[NSGraphicsContext currentContext] |
| 403 cr_setPatternPhase:[imageRep patternPhasePosition] | 411 cr_setPatternPhase:[imageRep patternPhasePosition] |
| 404 forView:[imageRep destView]]; | 412 forView:[imageRep destView]]; |
| 405 | 413 |
| 406 CGContextRef context = static_cast<CGContextRef>( | 414 CGContextRef context = static_cast<CGContextRef>( |
| 407 [[NSGraphicsContext currentContext] graphicsPort]); | 415 [[NSGraphicsContext currentContext] graphicsPort]); |
| 408 CGFloat lineWidth = LineWidthFromContext(context); | 416 CGFloat lineWidth = LineWidthFromContext(context); |
| 409 NSBezierPath* bezierPath = | 417 NSBezierPath* bezierPath = |
| 410 [self newTabButtonBezierPathWithLineWidth:lineWidth]; | 418 [self newTabButtonBezierPathWithLineWidth:lineWidth]; |
| 411 | 419 |
| 412 if ([imageRep fillColor]) { | 420 if ([imageRep fillColor]) { |
| 413 [[imageRep fillColor] set]; | 421 [[imageRep fillColor] set]; |
| 414 [bezierPath fill]; | 422 [bezierPath fill]; |
| 415 } | 423 } |
| 416 | 424 |
| 425 CGFloat alpha = heavyStroke ? 1.0 : 0.25; |
| 417 static NSColor* strokeColor = | 426 static NSColor* strokeColor = |
| 418 [[NSColor colorWithCalibratedWhite:0 alpha:k25PercentAlpha] retain]; | 427 [[NSColor colorWithCalibratedWhite:0 alpha:alpha] retain]; |
| 419 [strokeColor set]; | 428 [strokeColor set]; |
| 420 [bezierPath stroke]; | 429 [bezierPath stroke]; |
| 421 | 430 |
| 422 // Bottom edge. | 431 // Bottom edge. |
| 423 const CGFloat kBottomEdgeX = 9; | 432 const CGFloat kBottomEdgeX = 9; |
| 424 const CGFloat kBottomEdgeY = 1.2825; | 433 const CGFloat kBottomEdgeY = 1.2825; |
| 425 const CGFloat kBottomEdgeWidth = 22; | 434 const CGFloat kBottomEdgeWidth = 22; |
| 426 NSPoint bottomEdgeStart = NSMakePoint(kBottomEdgeX, kBottomEdgeY); | 435 NSPoint bottomEdgeStart = NSMakePoint(kBottomEdgeX, kBottomEdgeY); |
| 427 NSPoint bottomEdgeEnd = NSMakePoint(kBottomEdgeX + kBottomEdgeWidth, | 436 NSPoint bottomEdgeEnd = NSMakePoint(kBottomEdgeX + kBottomEdgeWidth, |
| 428 kBottomEdgeY); | 437 kBottomEdgeY); |
| 429 NSBezierPath* bottomEdgePath = [NSBezierPath bezierPath]; | 438 NSBezierPath* bottomEdgePath = [NSBezierPath bezierPath]; |
| 430 [bottomEdgePath moveToPoint:bottomEdgeStart]; | 439 [bottomEdgePath moveToPoint:bottomEdgeStart]; |
| 431 [bottomEdgePath lineToPoint:bottomEdgeEnd]; | 440 [bottomEdgePath lineToPoint:bottomEdgeEnd]; |
| 432 static NSColor* bottomEdgeColor = | 441 static NSColor* bottomEdgeColor = |
| 433 [[NSColor colorWithCalibratedWhite:0 alpha:k7PercentAlpha] retain]; | 442 [[NSColor colorWithCalibratedWhite:0 alpha:0.07] retain]; |
| 434 [bottomEdgeColor set]; | 443 [bottomEdgeColor set]; |
| 435 [bottomEdgePath setLineWidth:lineWidth]; | 444 [bottomEdgePath setLineWidth:lineWidth]; |
| 436 [bottomEdgePath setLineCapStyle:NSRoundLineCapStyle]; | 445 [bottomEdgePath setLineCapStyle:NSRoundLineCapStyle]; |
| 437 [bottomEdgePath stroke]; | 446 [bottomEdgePath stroke]; |
| 438 | 447 |
| 439 CGPoint shadowStart = NSZeroPoint; | 448 CGPoint shadowStart = NSZeroPoint; |
| 440 CGPoint shadowEnd = NSZeroPoint; | 449 CGPoint shadowEnd = NSZeroPoint; |
| 441 NSColor* overlayColor = nil; | 450 NSColor* overlayColor = nil; |
| 442 const CGFloat kBottomShadowX = 8; | 451 const CGFloat kBottomShadowX = 8; |
| 443 const CGFloat kBottomShadowY = kBottomEdgeY - lineWidth; | 452 const CGFloat kBottomShadowY = kBottomEdgeY - lineWidth; |
| 444 const CGFloat kTopShadowX = 1; | 453 const CGFloat kTopShadowX = 1; |
| 445 const CGFloat kTopShadowY = kBottomShadowY + 15; | 454 const CGFloat kTopShadowY = kBottomShadowY + 15; |
| 446 const CGFloat kShadowWidth = 24; | 455 const CGFloat kShadowWidth = 24; |
| 447 static NSColor* lightOverlayColor = | 456 static NSColor* lightOverlayColor = |
| 448 [[NSColor colorWithCalibratedWhite:1 alpha:k20PercentAlpha] retain]; | 457 [[NSColor colorWithCalibratedWhite:1 alpha:0.20] retain]; |
| 449 static NSColor* darkOverlayColor = | 458 static NSColor* darkOverlayColor = |
| 450 [[NSColor colorWithCalibratedWhite:0 alpha:k8PercentAlpha] retain]; | 459 [[NSColor colorWithCalibratedWhite:0 alpha:0.08] retain]; |
| 451 | 460 |
| 452 switch ([imageRep overlayOption]) { | 461 switch ([imageRep overlayOption]) { |
| 453 case OverlayOption::LIGHTEN: | 462 case OverlayOption::LIGHTEN: |
| 454 overlayColor = lightOverlayColor; | 463 overlayColor = lightOverlayColor; |
| 455 break; | 464 break; |
| 456 | 465 |
| 457 case OverlayOption::DARKEN: | 466 case OverlayOption::DARKEN: |
| 458 overlayColor = darkOverlayColor; | 467 overlayColor = darkOverlayColor; |
| 459 shadowStart = NSMakePoint(kTopShadowX, kTopShadowY); | 468 shadowStart = NSMakePoint(kTopShadowX, kTopShadowY); |
| 460 shadowEnd = NSMakePoint(kTopShadowX + kShadowWidth, kTopShadowY); | 469 shadowEnd = NSMakePoint(kTopShadowX + kShadowWidth, kTopShadowY); |
| 461 break; | 470 break; |
| 462 | 471 |
| 463 case OverlayOption::NONE: | 472 case OverlayOption::NONE: |
| 464 shadowStart = NSMakePoint(kBottomShadowX, kBottomShadowY); | 473 shadowStart = NSMakePoint(kBottomShadowX, kBottomShadowY); |
| 465 shadowEnd = NSMakePoint(kBottomShadowX + kShadowWidth, kBottomShadowY); | 474 shadowEnd = NSMakePoint(kBottomShadowX + kShadowWidth, kBottomShadowY); |
| 466 break; | 475 break; |
| 467 } | 476 } |
| 468 | 477 |
| 469 // Shadow beneath the bottom or top edge. | 478 // Shadow beneath the bottom or top edge. |
| 470 if (!NSEqualPoints(shadowStart, NSZeroPoint)) { | 479 if (!NSEqualPoints(shadowStart, NSZeroPoint)) { |
| 471 NSBezierPath* shadowPath = [NSBezierPath bezierPath]; | 480 NSBezierPath* shadowPath = [NSBezierPath bezierPath]; |
| 472 [shadowPath moveToPoint:shadowStart]; | 481 [shadowPath moveToPoint:shadowStart]; |
| 473 [shadowPath lineToPoint:shadowEnd]; | 482 [shadowPath lineToPoint:shadowEnd]; |
| 474 [shadowPath setLineWidth:lineWidth]; | 483 [shadowPath setLineWidth:lineWidth]; |
| 475 [shadowPath setLineCapStyle:NSRoundLineCapStyle]; | 484 [shadowPath setLineCapStyle:NSRoundLineCapStyle]; |
| 476 static NSColor* shadowColor = | 485 static NSColor* shadowColor = |
| 477 [[NSColor colorWithCalibratedWhite:0 alpha:k10PercentAlpha] retain]; | 486 [[NSColor colorWithCalibratedWhite:0 alpha:0.10] retain]; |
| 478 [shadowColor set]; | 487 [shadowColor set]; |
| 479 [shadowPath stroke]; | 488 [shadowPath stroke]; |
| 480 } | 489 } |
| 481 | 490 |
| 482 if (overlayColor) { | 491 if (overlayColor) { |
| 483 [overlayColor set]; | 492 [overlayColor set]; |
| 484 [[self newTabButtonBezierPathWithLineWidth:lineWidth] fill]; | 493 [[self newTabButtonBezierPathWithLineWidth:lineWidth] fill]; |
| 485 } | 494 } |
| 486 } | 495 } |
| 487 | 496 |
| 497 + (void)drawNewTabButtonImageWithNormalStroke: |
| 498 (NewTabButtonCustomImageRep*)image { |
| 499 [self drawNewTabButtonImage:image withHeavyStroke:NO]; |
| 500 } |
| 501 |
| 502 + (void)drawNewTabButtonImageWithHeavyStroke: |
| 503 (NewTabButtonCustomImageRep*)image { |
| 504 [self drawNewTabButtonImage:image withHeavyStroke:YES]; |
| 505 } |
| 506 |
| 488 - (NSImage*)imageWithFillColor:(NSColor*)fillColor { | 507 - (NSImage*)imageWithFillColor:(NSColor*)fillColor { |
| 489 NSImage* image = | 508 NSImage* image = |
| 490 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; | 509 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; |
| 491 | 510 |
| 492 [image lockFocus]; | 511 [image lockFocus]; |
| 493 [fillColor set]; | 512 [fillColor set]; |
| 494 CGContextRef context = static_cast<CGContextRef>( | 513 CGContextRef context = static_cast<CGContextRef>( |
| 495 [[NSGraphicsContext currentContext] graphicsPort]); | 514 [[NSGraphicsContext currentContext] graphicsPort]); |
| 496 CGFloat lineWidth = LineWidthFromContext(context); | 515 CGFloat lineWidth = LineWidthFromContext(context); |
| 497 [[NewTabButton newTabButtonBezierPathWithLineWidth:lineWidth] fill]; | 516 [[NewTabButton newTabButtonBezierPathWithLineWidth:lineWidth] fill]; |
| 498 [image unlockFocus]; | 517 [image unlockFocus]; |
| 499 return image; | 518 return image; |
| 500 } | 519 } |
| 501 | 520 |
| 502 @end | 521 @end |
| OLD | NEW |