Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ui/native_theme/native_theme_mac.h" | 5 #include "ui/native_theme/native_theme_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 // CGColorRefToSkColor will not like. But RGB is additive, so the conversion | 84 // CGColorRefToSkColor will not like. But RGB is additive, so the conversion |
| 85 // is easy (RGB to grayscale is less easy). | 85 // is easy (RGB to grayscale is less easy). |
| 86 const CGFloat* components = CGColorGetComponents(cg_color); | 86 const CGFloat* components = CGColorGetComponents(cg_color); |
| 87 CGFloat alpha = component_count == 2 ? components[1] : 1.0; | 87 CGFloat alpha = component_count == 2 ? components[1] : 1.0; |
| 88 return SkColorSetARGB(SkScalarRoundToInt(255.0 * alpha), | 88 return SkColorSetARGB(SkScalarRoundToInt(255.0 * alpha), |
| 89 SkScalarRoundToInt(255.0 * components[0]), | 89 SkScalarRoundToInt(255.0 * components[0]), |
| 90 SkScalarRoundToInt(255.0 * components[0]), | 90 SkScalarRoundToInt(255.0 * components[0]), |
| 91 SkScalarRoundToInt(255.0 * components[0])); | 91 SkScalarRoundToInt(255.0 * components[0])); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Converts an SkColor to grayscale by flat averaging of components, preserving | |
| 95 // the alpha channel. | |
| 96 SkColor ColorToGrayscale(SkColor color) { | |
| 97 uint8_t component = (SkColorGetR(color) + | |
| 98 SkColorGetG(color) + | |
| 99 SkColorGetB(color)) / 3; | |
|
Avi (use Gerrit)
2016/08/19 00:21:48
Do we want to do a luminosity average here? http:/
Elly Fong-Jones
2016/08/22 18:17:57
Hm! Originally when I tried out luminance it looke
| |
| 100 return SkColorSetARGB(SkColorGetA(color), component, component, component); | |
| 101 } | |
| 102 | |
| 94 } // namespace | 103 } // namespace |
| 95 | 104 |
| 96 namespace ui { | 105 namespace ui { |
| 97 | 106 |
| 98 // static | 107 // static |
| 99 NativeTheme* NativeTheme::GetInstanceForWeb() { | 108 NativeTheme* NativeTheme::GetInstanceForWeb() { |
| 100 return NativeThemeMac::instance(); | 109 return NativeThemeMac::instance(); |
| 101 } | 110 } |
| 102 | 111 |
| 103 // static | 112 // static |
| 104 NativeThemeMac* NativeThemeMac::instance() { | 113 NativeThemeMac* NativeThemeMac::instance() { |
| 105 CR_DEFINE_STATIC_LOCAL(NativeThemeMac, s_native_theme, ()); | 114 CR_DEFINE_STATIC_LOCAL(NativeThemeMac, s_native_theme, ()); |
| 106 return &s_native_theme; | 115 return &s_native_theme; |
| 107 } | 116 } |
| 108 | 117 |
| 118 // static | |
| 119 SkColor NativeThemeMac::ApplySystemControlTint(SkColor color) { | |
| 120 if ([NSColor currentControlTint] == NSGraphiteControlTint) | |
| 121 return ColorToGrayscale(color); | |
| 122 return color; | |
| 123 } | |
| 124 | |
| 109 SkColor NativeThemeMac::GetSystemColor(ColorId color_id) const { | 125 SkColor NativeThemeMac::GetSystemColor(ColorId color_id) const { |
| 110 // TODO(tapted): Add caching for these, and listen for | 126 // TODO(tapted): Add caching for these, and listen for |
| 111 // NSSystemColorsDidChangeNotification. | 127 // NSSystemColorsDidChangeNotification. |
| 112 switch (color_id) { | 128 switch (color_id) { |
| 113 case kColorId_WindowBackground: | 129 case kColorId_WindowBackground: |
| 114 return NSSystemColorToSkColor([NSColor windowBackgroundColor]); | 130 return NSSystemColorToSkColor([NSColor windowBackgroundColor]); |
| 115 case kColorId_DialogBackground: | 131 case kColorId_DialogBackground: |
| 116 return kDialogBackgroundColor; | 132 return kDialogBackgroundColor; |
| 117 case kColorId_BubbleBackground: | 133 case kColorId_BubbleBackground: |
| 118 return SK_ColorWHITE; | 134 return SK_ColorWHITE; |
| 119 | 135 |
| 120 case kColorId_FocusedBorderColor: | 136 case kColorId_FocusedBorderColor: |
| 121 case kColorId_FocusedMenuButtonBorderColor: | 137 case kColorId_FocusedMenuButtonBorderColor: |
| 122 return NSSystemColorToSkColor([NSColor keyboardFocusIndicatorColor]); | 138 return NSSystemColorToSkColor([NSColor keyboardFocusIndicatorColor]); |
| 123 case kColorId_UnfocusedBorderColor: | 139 case kColorId_UnfocusedBorderColor: |
| 124 return NSSystemColorToSkColor([NSColor controlColor]); | 140 return NSSystemColorToSkColor([NSColor controlColor]); |
| 125 | 141 |
| 126 // Buttons and labels. | 142 // Buttons and labels. |
| 127 case kColorId_ButtonBackgroundColor: | 143 case kColorId_ButtonBackgroundColor: |
| 128 case kColorId_ButtonHoverBackgroundColor: | 144 case kColorId_ButtonHoverBackgroundColor: |
| 129 case kColorId_HoverMenuButtonBorderColor: | 145 case kColorId_HoverMenuButtonBorderColor: |
| 130 case kColorId_LabelBackgroundColor: | 146 case kColorId_LabelBackgroundColor: |
| 131 return NSSystemColorToSkColor([NSColor controlBackgroundColor]); | 147 return NSSystemColorToSkColor([NSColor controlBackgroundColor]); |
| 132 case kColorId_ButtonEnabledColor: | 148 case kColorId_ButtonEnabledColor: |
| 133 case kColorId_EnabledMenuButtonBorderColor: | 149 case kColorId_EnabledMenuButtonBorderColor: |
| 134 case kColorId_LabelEnabledColor: | 150 case kColorId_LabelEnabledColor: |
| 135 return NSSystemColorToSkColor([NSColor controlTextColor]); | 151 return NSSystemColorToSkColor([NSColor controlTextColor]); |
| 152 case kColorId_CallToActionColor: | |
| 153 return NSSystemColorToSkColor([NSColor controlTextColor]); | |
| 136 case kColorId_ButtonDisabledColor: | 154 case kColorId_ButtonDisabledColor: |
| 137 case kColorId_LabelDisabledColor: | 155 case kColorId_LabelDisabledColor: |
| 138 return NSSystemColorToSkColor([NSColor disabledControlTextColor]); | 156 return NSSystemColorToSkColor([NSColor disabledControlTextColor]); |
| 139 case kColorId_ButtonHighlightColor: | 157 case kColorId_ButtonHighlightColor: |
| 140 // Although the NSColor documentation names "selectedControlTextColor" as | 158 // Although the NSColor documentation names "selectedControlTextColor" as |
| 141 // the color for a "text in a .. control being clicked or dragged", it | 159 // the color for a "text in a .. control being clicked or dragged", it |
| 142 // remains black, and text on Yosemite-style pressed buttons is white. | 160 // remains black, and text on Yosemite-style pressed buttons is white. |
| 143 return SK_ColorWHITE; | 161 return SK_ColorWHITE; |
| 144 case kColorId_ButtonHoverColor: | 162 case kColorId_ButtonHoverColor: |
| 145 return NSSystemColorToSkColor([NSColor selectedControlTextColor]); | 163 return NSSystemColorToSkColor([NSColor selectedControlTextColor]); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 start_colors[ButtonBackgroundType::PRESSED] = kPressedBlueStart; | 298 start_colors[ButtonBackgroundType::PRESSED] = kPressedBlueStart; |
| 281 | 299 |
| 282 ColorByState end_colors; | 300 ColorByState end_colors; |
| 283 end_colors[ButtonBackgroundType::DISABLED] = kGrey; | 301 end_colors[ButtonBackgroundType::DISABLED] = kGrey; |
| 284 end_colors[ButtonBackgroundType::HIGHLIGHTED] = kBlueEnd; | 302 end_colors[ButtonBackgroundType::HIGHLIGHTED] = kBlueEnd; |
| 285 end_colors[ButtonBackgroundType::NORMAL] = SK_ColorWHITE; | 303 end_colors[ButtonBackgroundType::NORMAL] = SK_ColorWHITE; |
| 286 end_colors[ButtonBackgroundType::PRESSED] = kPressedBlueEnd; | 304 end_colors[ButtonBackgroundType::PRESSED] = kPressedBlueEnd; |
| 287 | 305 |
| 288 SkColor gradient_colors[] = {start_colors[type], end_colors[type]}; | 306 SkColor gradient_colors[] = {start_colors[type], end_colors[type]}; |
| 289 | 307 |
| 308 for (size_t i = 0; i < arraysize(gradient_colors); ++i) | |
| 309 gradient_colors[i] = ApplySystemControlTint(gradient_colors[i]); | |
| 310 | |
| 290 return SkGradientShader::MakeLinear( | 311 return SkGradientShader::MakeLinear( |
| 291 gradient_points, gradient_colors, gradient_positions, | 312 gradient_points, gradient_colors, gradient_positions, |
| 292 arraysize(gradient_positions), | 313 arraysize(gradient_positions), |
| 293 SkShader::kClamp_TileMode); | 314 SkShader::kClamp_TileMode); |
| 294 } | 315 } |
| 295 | 316 |
| 296 // static | 317 // static |
| 297 sk_sp<SkShader> NativeThemeMac::GetButtonBorderShader(ButtonBackgroundType type, | 318 sk_sp<SkShader> NativeThemeMac::GetButtonBorderShader(ButtonBackgroundType type, |
| 298 int height) { | 319 int height) { |
| 299 using ColorByState = EnumArray<ButtonBackgroundType, SkColor>; | 320 using ColorByState = EnumArray<ButtonBackgroundType, SkColor>; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 313 top_edge[ButtonBackgroundType::PRESSED] = SkColorSetRGB(0x4f, 0x72, 0xfb); | 334 top_edge[ButtonBackgroundType::PRESSED] = SkColorSetRGB(0x4f, 0x72, 0xfb); |
| 314 ColorByState bottom_edge; | 335 ColorByState bottom_edge; |
| 315 bottom_edge[ButtonBackgroundType::DISABLED] = SkColorSetRGB(0xbe, 0xbe, 0xbe); | 336 bottom_edge[ButtonBackgroundType::DISABLED] = SkColorSetRGB(0xbe, 0xbe, 0xbe); |
| 316 bottom_edge[ButtonBackgroundType::HIGHLIGHTED] = | 337 bottom_edge[ButtonBackgroundType::HIGHLIGHTED] = |
| 317 SkColorSetRGB(0x43, 0x52, 0xff); | 338 SkColorSetRGB(0x43, 0x52, 0xff); |
| 318 bottom_edge[ButtonBackgroundType::NORMAL] = SkColorSetRGB(0x9d, 0x9d, 0x9d); | 339 bottom_edge[ButtonBackgroundType::NORMAL] = SkColorSetRGB(0x9d, 0x9d, 0x9d); |
| 319 bottom_edge[ButtonBackgroundType::PRESSED] = SkColorSetRGB(0x3e, 0x12, 0xff); | 340 bottom_edge[ButtonBackgroundType::PRESSED] = SkColorSetRGB(0x3e, 0x12, 0xff); |
| 320 | 341 |
| 321 SkColor gradient_colors[] = {top_edge[type], bottom_edge[type]}; | 342 SkColor gradient_colors[] = {top_edge[type], bottom_edge[type]}; |
| 322 | 343 |
| 344 for (size_t i = 0; i < arraysize(gradient_colors); ++i) | |
| 345 gradient_colors[i] = ApplySystemControlTint(gradient_colors[i]); | |
| 346 | |
| 323 return SkGradientShader::MakeLinear( | 347 return SkGradientShader::MakeLinear( |
| 324 gradient_points, gradient_colors, gradient_positions, | 348 gradient_points, gradient_colors, gradient_positions, |
| 325 arraysize(gradient_positions), SkShader::kClamp_TileMode); | 349 arraysize(gradient_positions), SkShader::kClamp_TileMode); |
| 326 } | 350 } |
| 327 | 351 |
| 328 // static | 352 // static |
| 329 void NativeThemeMac::PaintStyledGradientButton(SkCanvas* canvas, | 353 void NativeThemeMac::PaintStyledGradientButton(SkCanvas* canvas, |
| 330 const gfx::Rect& integer_bounds, | 354 const gfx::Rect& integer_bounds, |
| 331 ButtonBackgroundType type, | 355 ButtonBackgroundType type, |
| 332 bool round_left, | 356 bool round_left, |
| 333 bool round_right, | 357 bool round_right, |
| 334 bool focus) { | 358 bool focus) { |
| 335 const SkScalar kBorderThickness = 1; | 359 const SkScalar kBorderThickness = 1; |
| 336 const SkScalar kFocusRingThickness = 4; | 360 const SkScalar kFocusRingThickness = 4; |
| 337 const SkColor kFocusRingColor = SkColorSetARGB(0x94, 0x79, 0xa7, 0xe9); | 361 const SkColor kFocusRingColor = ApplySystemControlTint( |
| 362 SkColorSetARGB(0x94, 0x79, 0xa7, 0xe9)); | |
| 338 | 363 |
| 339 const SkVector kNoCurve = {0, 0}; | 364 const SkVector kNoCurve = {0, 0}; |
| 340 const SkVector kCurve = {kButtonCornerRadius, kButtonCornerRadius}; | 365 const SkVector kCurve = {kButtonCornerRadius, kButtonCornerRadius}; |
| 341 const SkVector kLeftCurves[4] = {kCurve, kNoCurve, kNoCurve, kCurve}; | 366 const SkVector kLeftCurves[4] = {kCurve, kNoCurve, kNoCurve, kCurve}; |
| 342 const SkVector kRightCurves[4] = {kNoCurve, kCurve, kCurve, kNoCurve}; | 367 const SkVector kRightCurves[4] = {kNoCurve, kCurve, kCurve, kNoCurve}; |
| 343 | 368 |
| 344 const SkScalar kShadowOffsetY = 1; | 369 const SkScalar kShadowOffsetY = 1; |
| 345 const SkColor kShadowColor = SkColorSetA(SK_ColorBLACK, 0x05); | 370 const SkColor kShadowColor = SkColorSetA(SK_ColorBLACK, 0x05); |
| 346 const double kShadowBlur = 0.0; | 371 const double kShadowBlur = 0.0; |
| 347 const std::vector<gfx::ShadowValue> shadows( | 372 const std::vector<gfx::ShadowValue> shadows( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 canvas->drawDRRect(outer_shape, shape, paint); | 415 canvas->drawDRRect(outer_shape, shape, paint); |
| 391 } | 416 } |
| 392 | 417 |
| 393 NativeThemeMac::NativeThemeMac() { | 418 NativeThemeMac::NativeThemeMac() { |
| 394 } | 419 } |
| 395 | 420 |
| 396 NativeThemeMac::~NativeThemeMac() { | 421 NativeThemeMac::~NativeThemeMac() { |
| 397 } | 422 } |
| 398 | 423 |
| 399 } // namespace ui | 424 } // namespace ui |
| OLD | NEW |