| 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/toolbar/toolbar_button_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/toolbar/toolbar_button_cocoa.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 #import "chrome/browser/ui/cocoa/view_id_util.h" | 10 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 @synthesize icon = icon_; | 72 @synthesize icon = icon_; |
| 73 @synthesize style = style_; | 73 @synthesize style = style_; |
| 74 | 74 |
| 75 - (void)dealloc { | 75 - (void)dealloc { |
| 76 [icon_ release]; | 76 [icon_ release]; |
| 77 [super dealloc]; | 77 [super dealloc]; |
| 78 } | 78 } |
| 79 | 79 |
| 80 + (void)drawImage:(ToolbarButtonImageRep*)imageRep { | 80 + (void)drawImage:(ToolbarButtonImageRep*)imageRep { |
| 81 // Create the path used for the background fill. | 81 ToolbarButtonImageBackgroundStyle displayStyle = [imageRep style]; |
| 82 NSBezierPath* roundedRectPath = | |
| 83 [NSBezierPath bezierPathWithRoundedRect:kMDButtonBounds | |
| 84 xRadius:2 | |
| 85 yRadius:2]; | |
| 86 | 82 |
| 87 // Determine the fill color. | 83 // Non-default styles draw a background. |
| 88 NSColor* fillColor = nil; | 84 if (displayStyle != ToolbarButtonImageBackgroundStyle::DEFAULT) { |
| 89 switch (imageRep.style) { | 85 // Create the path used for the background fill. |
| 90 case ToolbarButtonImageBackgroundStyle::HOVER: | 86 const int kCornerRadius = 2; |
| 91 fillColor = [NSColor colorWithCalibratedWhite:0 alpha:0.08]; | 87 NSBezierPath* roundedRectPath = |
| 92 break; | 88 [NSBezierPath bezierPathWithRoundedRect:kMDButtonBounds |
| 93 case ToolbarButtonImageBackgroundStyle::HOVER_THEMED: | 89 xRadius:kCornerRadius |
| 94 fillColor = [NSColor colorWithCalibratedWhite:1 alpha:0.12]; | 90 yRadius:kCornerRadius]; |
| 95 break; | 91 |
| 96 case ToolbarButtonImageBackgroundStyle::PRESSED: | 92 // Determine the fill color. |
| 97 fillColor = [NSColor colorWithCalibratedWhite:0 alpha:0.12]; | 93 NSColor* fillColor = nil; |
| 98 break; | 94 const CGFloat kEightPercentAlpha = 0.08; |
| 99 case ToolbarButtonImageBackgroundStyle::PRESSED_THEMED: | 95 const CGFloat kTwelvePercentAlpha = 0.12; |
| 100 fillColor = [NSColor colorWithCalibratedWhite:1 alpha:0.16]; | 96 const CGFloat kSixteenPercentAlpha = 0.16; |
| 101 break; | 97 switch (displayStyle) { |
| 98 case ToolbarButtonImageBackgroundStyle::HOVER: |
| 99 fillColor = [NSColor colorWithCalibratedWhite:0 |
| 100 alpha:kEightPercentAlpha]; |
| 101 break; |
| 102 case ToolbarButtonImageBackgroundStyle::HOVER_THEMED: |
| 103 fillColor = [NSColor colorWithCalibratedWhite:1 |
| 104 alpha:kTwelvePercentAlpha]; |
| 105 break; |
| 106 case ToolbarButtonImageBackgroundStyle::PRESSED: |
| 107 fillColor = [NSColor colorWithCalibratedWhite:0 |
| 108 alpha:kTwelvePercentAlpha]; |
| 109 break; |
| 110 case ToolbarButtonImageBackgroundStyle::PRESSED_THEMED: |
| 111 fillColor = [NSColor colorWithCalibratedWhite:1 |
| 112 alpha:kSixteenPercentAlpha]; |
| 113 break; |
| 114 case ToolbarButtonImageBackgroundStyle::DEFAULT: |
| 115 NOTREACHED(); |
| 116 } |
| 117 |
| 118 // Fill the path. |
| 119 [fillColor set]; |
| 120 [roundedRectPath fill]; |
| 102 } | 121 } |
| 103 | 122 |
| 104 // Fill the path. | |
| 105 [fillColor set]; | |
| 106 [roundedRectPath fill]; | |
| 107 | |
| 108 // Center the icon within the button. | 123 // Center the icon within the button. |
| 109 NSSize iconSize = [imageRep.icon size]; | 124 NSSize iconSize = [imageRep.icon size]; |
| 110 CGFloat iconInset = (kMDButtonBounds.size.width - iconSize.width) / 2; | 125 CGFloat iconInset = (kMDButtonBounds.size.width - iconSize.width) / 2; |
| 111 NSRect iconDestRect = NSInsetRect(kMDButtonBounds, iconInset, iconInset); | 126 NSRect iconDestRect = NSInsetRect(kMDButtonBounds, iconInset, iconInset); |
| 112 [imageRep.icon drawInRect:iconDestRect | 127 [imageRep.icon drawInRect:iconDestRect |
| 113 fromRect:NSZeroRect | 128 fromRect:NSZeroRect |
| 114 operation:NSCompositeSourceOver | 129 operation:NSCompositeSourceOver |
| 115 fraction:1]; | 130 fraction:1]; |
| 116 } | 131 } |
| 117 | 132 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 259 |
| 245 // Create the image from the image rep. | 260 // Create the image from the image rep. |
| 246 NSImage* image = | 261 NSImage* image = |
| 247 [[[NSImage alloc] initWithSize:kMDButtonBounds.size] autorelease]; | 262 [[[NSImage alloc] initWithSize:kMDButtonBounds.size] autorelease]; |
| 248 [image setCacheMode:NSImageCacheAlways]; | 263 [image setCacheMode:NSImageCacheAlways]; |
| 249 [image addRepresentation:imageRep]; | 264 [image addRepresentation:imageRep]; |
| 250 | 265 |
| 251 return image; | 266 return image; |
| 252 } | 267 } |
| 253 | 268 |
| 269 - (NSImage*)image { |
| 270 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 271 return [super image]; |
| 272 } |
| 273 // setImage: stores the image in an ivar. |
| 274 return image_.get(); |
| 275 } |
| 276 |
| 254 - (void)setImage:(NSImage*)anImage { | 277 - (void)setImage:(NSImage*)anImage { |
| 255 [super setImage:anImage]; | 278 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 256 if (ui::MaterialDesignController::IsModeMaterial()) { | 279 [super setImage:anImage]; |
| 257 [self resetButtonStateImages]; | 280 return; |
| 258 } | 281 } |
| 282 |
| 283 // We want to set the default image as the image for kDefaultState. Setting it |
| 284 // as the default image (via setImage:) can cause ghosting from the two |
| 285 // default images being drawn over each other. However we also need to keep |
| 286 // the default image around for resetButtonStateImages, so stick it in an |
| 287 // ivar. |
| 288 image_.reset([anImage retain]); |
| 289 [self resetButtonStateImages]; |
| 259 } | 290 } |
| 260 | 291 |
| 261 - (void)resetButtonStateImages { | 292 - (void)resetButtonStateImages { |
| 262 DCHECK(ui::MaterialDesignController::IsModeMaterial()); | 293 DCHECK(ui::MaterialDesignController::IsModeMaterial()); |
| 263 | 294 |
| 264 NSImage* normalIcon = nil; | 295 NSImage* normalIcon = nil; |
| 265 NSImage* disabledIcon = nil; | 296 NSImage* disabledIcon = nil; |
| 266 | 297 |
| 267 gfx::VectorIconId iconId = [self vectorIconId]; | 298 gfx::VectorIconId iconId = [self vectorIconId]; |
| 268 if (iconId == gfx::VectorIconId::VECTOR_ICON_NONE) { | 299 if (iconId == gfx::VectorIconId::VECTOR_ICON_NONE) { |
| 269 // If the button does not have a vector icon id (e.g. it's an extension | 300 // If the button does not have a vector icon id (e.g. it's an extension |
| 270 // button), use its image. | 301 // button), use its image. The hover, etc. images will be created using |
| 271 normalIcon = disabledIcon = [self image]; | 302 // imageForIcon:withBackgroundStyle: so do the same for the default image. |
| 303 // If we don't do this, the icon may not appear in the same place as in the |
| 304 // other states, causing the icon to appear to shift as you mouse over the |
| 305 // button. |
| 306 NSImage* defaultImage = |
| 307 [self imageForIcon:[self image] |
| 308 withBackgroundStyle:ToolbarButtonImageBackgroundStyle::DEFAULT]; |
| 309 normalIcon = disabledIcon = defaultImage; |
| 272 } else { | 310 } else { |
| 273 // Compute the normal and disabled vector icon colors. | 311 // Compute the normal and disabled vector icon colors. |
| 274 BOOL isDarkTheme = [[self window] hasDarkTheme]; | 312 BOOL isDarkTheme = [[self window] hasDarkTheme]; |
| 275 const SkColor vectorIconColor = [self vectorIconColor:isDarkTheme]; | 313 const SkColor vectorIconColor = [self vectorIconColor:isDarkTheme]; |
| 276 CGFloat normalAlpha = isDarkTheme ? 0xCC : 0xFF; | 314 CGFloat normalAlpha = isDarkTheme ? 0xCC : 0xFF; |
| 277 const SkColor normalColor = SkColorSetA(vectorIconColor, normalAlpha); | 315 const SkColor normalColor = SkColorSetA(vectorIconColor, normalAlpha); |
| 278 const SkColor disabledColor = SkColorSetA(vectorIconColor, 0x33); | 316 const SkColor disabledColor = SkColorSetA(vectorIconColor, 0x33); |
| 279 | 317 |
| 280 // Create the normal and disabled state icons. These icons are always the | 318 // Create the normal and disabled state icons. These icons are always the |
| 281 // same shape but use a different color. | 319 // same shape but use a different color. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 // Update the hover and pressed image backgrounds to match the current theme. | 390 // Update the hover and pressed image backgrounds to match the current theme. |
| 353 if (ui::MaterialDesignController::IsModeMaterial()) { | 391 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 354 [self resetButtonStateImages]; | 392 [self resetButtonStateImages]; |
| 355 } | 393 } |
| 356 } | 394 } |
| 357 | 395 |
| 358 - (void)windowDidChangeActive { | 396 - (void)windowDidChangeActive { |
| 359 } | 397 } |
| 360 | 398 |
| 361 @end | 399 @end |
| OLD | NEW |