Chromium Code Reviews| 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 }; |
|
Mark Mentovai
2017/03/22 17:02:55
Fix the messed-up indentation on this while you’re
Elly Fong-Jones
2017/03/23 16:22:38
Done.
| |
| 29 | 29 |
| 30 const CGFloat k7PercentAlpha = 0.07; | 30 const CGFloat k7PercentAlpha = 0.07; |
|
Mark Mentovai
2017/03/22 17:02:56
Umm…do we really need these constants?
If they ha
Elly Fong-Jones
2017/03/23 16:22:38
It is done.
| |
| 31 const CGFloat k8PercentAlpha = 0.08; | 31 const CGFloat k8PercentAlpha = 0.08; |
| 32 const CGFloat k10PercentAlpha = 0.1; | 32 const CGFloat k10PercentAlpha = 0.1; |
| 33 const CGFloat k20PercentAlpha = 0.2; | 33 const CGFloat k20PercentAlpha = 0.2; |
| 34 const CGFloat k25PercentAlpha = 0.25; | 34 const CGFloat k25PercentAlpha = 0.25; |
| 35 const CGFloat k100PercentAlpha = 1.0; | |
| 35 | 36 |
| 36 NSImage* GetMaskImageFromCell(NewTabButtonCell* aCell) { | 37 NSImage* GetMaskImageFromCell(NewTabButtonCell* aCell) { |
| 37 return [aCell imageForState:image_button_cell::kDefaultState view:nil]; | 38 return [aCell imageForState:image_button_cell::kDefaultState view:nil]; |
| 38 } | 39 } |
| 39 | 40 |
| 40 // Creates an NSImage with size |size| and bitmap image representations for both | 41 // 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 | 42 // 1x and 2x scale factors. |drawingHandler| is called once for every scale |
| 42 // factor. This is similar to -[NSImage imageWithSize:flipped:drawingHandler:], | 43 // factor. This is similar to -[NSImage imageWithSize:flipped:drawingHandler:], |
| 43 // but this function always evaluates drawingHandler eagerly, and it works on | 44 // but this function always evaluates drawingHandler eagerly, and it works on |
| 44 // 10.6 and 10.7. | 45 // 10.6 and 10.7. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 | 179 |
| 179 // Returns a new tab button image appropriate for the specified button state | 180 // 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 | 181 // (e.g. hover) and theme. In Material Design, the theme color affects the |
| 181 // button color. | 182 // button color. |
| 182 - (NSImage*)imageForState:(image_button_cell::ButtonState)state | 183 - (NSImage*)imageForState:(image_button_cell::ButtonState)state |
| 183 theme:(const ui::ThemeProvider*)theme; | 184 theme:(const ui::ThemeProvider*)theme; |
| 184 | 185 |
| 185 // Returns a new tab button image bezier path with the specified line width. | 186 // Returns a new tab button image bezier path with the specified line width. |
| 186 + (NSBezierPath*)newTabButtonBezierPathWithLineWidth:(CGFloat)lineWidth; | 187 + (NSBezierPath*)newTabButtonBezierPathWithLineWidth:(CGFloat)lineWidth; |
| 187 | 188 |
| 188 // NSCustomImageRep custom drawing method that renders the new tab button image. | 189 // Draws the new tab button image to |imageRep|, with either a normal stroke or |
| 189 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep; | 190 // a heavy stroke for increased visibility. |
| 191 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep | |
| 192 withHeavyStroke:(BOOL)heavyStroke; | |
| 193 | |
| 194 // NSCustomImageRep custom drawing method shims for normal and heavy strokes | |
| 195 // respectively. | |
| 196 + (void)drawNewTabButtonImageWithNormalStroke: | |
| 197 (NewTabButtonCustomImageRep*)imageRep; | |
| 198 + (void)drawNewTabButtonImageWithHeavyStroke: | |
| 199 (NewTabButtonCustomImageRep*)imageRep; | |
| 190 | 200 |
| 191 // Returns a new tab button image filled with |fillColor|. | 201 // Returns a new tab button image filled with |fillColor|. |
| 192 - (NSImage*)imageWithFillColor:(NSColor*)fillColor; | 202 - (NSImage*)imageWithFillColor:(NSColor*)fillColor; |
| 193 | 203 |
| 194 @end | 204 @end |
| 195 | 205 |
| 196 @implementation NewTabButton | 206 @implementation NewTabButton |
| 197 | 207 |
| 198 + (Class)cellClass { | 208 + (Class)cellClass { |
| 199 return [NewTabButtonCell class]; | 209 return [NewTabButtonCell class]; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 fillColor = | 321 fillColor = |
| 312 theme->GetNSImageColorNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE); | 322 theme->GetNSImageColorNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE); |
| 313 break; | 323 break; |
| 314 | 324 |
| 315 default: | 325 default: |
| 316 fillColor = [NSColor redColor]; | 326 fillColor = [NSColor redColor]; |
| 317 // All states should be accounted for above. | 327 // All states should be accounted for above. |
| 318 NOTREACHED(); | 328 NOTREACHED(); |
| 319 } | 329 } |
| 320 | 330 |
| 331 SEL drawSelector = @selector(drawNewTabButtonImageWithNormalStroke:); | |
| 332 if (theme && theme->ShouldIncreaseContrast()) | |
| 333 drawSelector = @selector(drawNewTabButtonImageWithHeavyStroke:); | |
| 334 | |
| 321 base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep( | 335 base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep( |
| 322 [[NewTabButtonCustomImageRep alloc] | 336 [[NewTabButtonCustomImageRep alloc] |
| 323 initWithDrawSelector:@selector(drawNewTabButtonImage:) | 337 initWithDrawSelector:drawSelector |
| 324 delegate:[NewTabButton class]]); | 338 delegate:[NewTabButton class]]); |
| 325 [imageRep setDestView:self]; | 339 [imageRep setDestView:self]; |
| 326 [imageRep setFillColor:fillColor]; | 340 [imageRep setFillColor:fillColor]; |
| 327 [imageRep setPatternPhasePosition: | 341 [imageRep setPatternPhasePosition: |
| 328 [[self window] | 342 [[self window] |
| 329 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]]; | 343 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]]; |
| 330 [imageRep setOverlayOption:overlayOption]; | 344 [imageRep setOverlayOption:overlayOption]; |
| 331 | 345 |
| 332 NSImage* newTabButtonImage = | 346 NSImage* newTabButtonImage = |
| 333 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; | 347 [[[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. | 405 // Adjust by half the line width to get crisp lines. |
| 392 NSAffineTransform* transform = [NSAffineTransform transform]; | 406 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 393 [transform translateXBy:lineWidth / 2 yBy:lineWidth / 2]; | 407 [transform translateXBy:lineWidth / 2 yBy:lineWidth / 2]; |
| 394 [bezierPath transformUsingAffineTransform:transform]; | 408 [bezierPath transformUsingAffineTransform:transform]; |
| 395 | 409 |
| 396 [bezierPath setLineWidth:lineWidth]; | 410 [bezierPath setLineWidth:lineWidth]; |
| 397 | 411 |
| 398 return bezierPath; | 412 return bezierPath; |
| 399 } | 413 } |
| 400 | 414 |
| 401 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep { | 415 + (void)drawNewTabButtonImage:(NewTabButtonCustomImageRep*)imageRep |
| 416 withHeavyStroke:(BOOL)heavyStroke { | |
| 402 [[NSGraphicsContext currentContext] | 417 [[NSGraphicsContext currentContext] |
| 403 cr_setPatternPhase:[imageRep patternPhasePosition] | 418 cr_setPatternPhase:[imageRep patternPhasePosition] |
| 404 forView:[imageRep destView]]; | 419 forView:[imageRep destView]]; |
| 405 | 420 |
| 406 CGContextRef context = static_cast<CGContextRef>( | 421 CGContextRef context = static_cast<CGContextRef>( |
| 407 [[NSGraphicsContext currentContext] graphicsPort]); | 422 [[NSGraphicsContext currentContext] graphicsPort]); |
| 408 CGFloat lineWidth = LineWidthFromContext(context); | 423 CGFloat lineWidth = LineWidthFromContext(context); |
| 409 NSBezierPath* bezierPath = | 424 NSBezierPath* bezierPath = |
| 410 [self newTabButtonBezierPathWithLineWidth:lineWidth]; | 425 [self newTabButtonBezierPathWithLineWidth:lineWidth]; |
| 411 | 426 |
| 412 if ([imageRep fillColor]) { | 427 if ([imageRep fillColor]) { |
| 413 [[imageRep fillColor] set]; | 428 [[imageRep fillColor] set]; |
| 414 [bezierPath fill]; | 429 [bezierPath fill]; |
| 415 } | 430 } |
| 416 | 431 |
| 432 CGFloat alpha = heavyStroke ? k100PercentAlpha : k25PercentAlpha; | |
| 417 static NSColor* strokeColor = | 433 static NSColor* strokeColor = |
| 418 [[NSColor colorWithCalibratedWhite:0 alpha:k25PercentAlpha] retain]; | 434 [[NSColor colorWithCalibratedWhite:0 alpha:alpha] retain]; |
| 419 [strokeColor set]; | 435 [strokeColor set]; |
| 420 [bezierPath stroke]; | 436 [bezierPath stroke]; |
| 421 | 437 |
| 422 // Bottom edge. | 438 // Bottom edge. |
| 423 const CGFloat kBottomEdgeX = 9; | 439 const CGFloat kBottomEdgeX = 9; |
| 424 const CGFloat kBottomEdgeY = 1.2825; | 440 const CGFloat kBottomEdgeY = 1.2825; |
| 425 const CGFloat kBottomEdgeWidth = 22; | 441 const CGFloat kBottomEdgeWidth = 22; |
| 426 NSPoint bottomEdgeStart = NSMakePoint(kBottomEdgeX, kBottomEdgeY); | 442 NSPoint bottomEdgeStart = NSMakePoint(kBottomEdgeX, kBottomEdgeY); |
| 427 NSPoint bottomEdgeEnd = NSMakePoint(kBottomEdgeX + kBottomEdgeWidth, | 443 NSPoint bottomEdgeEnd = NSMakePoint(kBottomEdgeX + kBottomEdgeWidth, |
| 428 kBottomEdgeY); | 444 kBottomEdgeY); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 [shadowColor set]; | 494 [shadowColor set]; |
| 479 [shadowPath stroke]; | 495 [shadowPath stroke]; |
| 480 } | 496 } |
| 481 | 497 |
| 482 if (overlayColor) { | 498 if (overlayColor) { |
| 483 [overlayColor set]; | 499 [overlayColor set]; |
| 484 [[self newTabButtonBezierPathWithLineWidth:lineWidth] fill]; | 500 [[self newTabButtonBezierPathWithLineWidth:lineWidth] fill]; |
| 485 } | 501 } |
| 486 } | 502 } |
| 487 | 503 |
| 504 + (void)drawNewTabButtonImageWithNormalStroke: | |
| 505 (NewTabButtonCustomImageRep*)image { | |
| 506 [self drawNewTabButtonImage:image withHeavyStroke:NO]; | |
| 507 } | |
| 508 | |
| 509 + (void)drawNewTabButtonImageWithHeavyStroke: | |
| 510 (NewTabButtonCustomImageRep*)image { | |
| 511 [self drawNewTabButtonImage:image withHeavyStroke:YES]; | |
| 512 } | |
| 513 | |
| 488 - (NSImage*)imageWithFillColor:(NSColor*)fillColor { | 514 - (NSImage*)imageWithFillColor:(NSColor*)fillColor { |
| 489 NSImage* image = | 515 NSImage* image = |
| 490 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; | 516 [[[NSImage alloc] initWithSize:newTabButtonImageSize] autorelease]; |
| 491 | 517 |
| 492 [image lockFocus]; | 518 [image lockFocus]; |
| 493 [fillColor set]; | 519 [fillColor set]; |
| 494 CGContextRef context = static_cast<CGContextRef>( | 520 CGContextRef context = static_cast<CGContextRef>( |
| 495 [[NSGraphicsContext currentContext] graphicsPort]); | 521 [[NSGraphicsContext currentContext] graphicsPort]); |
| 496 CGFloat lineWidth = LineWidthFromContext(context); | 522 CGFloat lineWidth = LineWidthFromContext(context); |
| 497 [[NewTabButton newTabButtonBezierPathWithLineWidth:lineWidth] fill]; | 523 [[NewTabButton newTabButtonBezierPathWithLineWidth:lineWidth] fill]; |
| 498 [image unlockFocus]; | 524 [image unlockFocus]; |
| 499 return image; | 525 return image; |
| 500 } | 526 } |
| 501 | 527 |
| 502 @end | 528 @end |
| OLD | NEW |