Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / ui / native_theme / common_theme.cc
blob48e5531de7b502ec2ea136e8f2891cffd156e316
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
3 // found in the LICENSE file.
5 #include "ui/native_theme/common_theme.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "ui/base/resource/material_design/material_design_controller.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/color_utils.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/image/image_skia.h"
15 #include "ui/gfx/skia_util.h"
16 #include "ui/resources/grit/ui_resources.h"
18 namespace {
20 // Theme colors returned by GetSystemColor().
22 // MenuItem:
23 const SkColor kMenuBackgroundColor = SK_ColorWHITE;
24 const SkColor kMenuHighlightBackgroundColor = SkColorSetRGB(0x42, 0x81, 0xF4);
25 const SkColor kMenuInvertedSchemeHighlightBackgroundColor =
26 SkColorSetRGB(48, 48, 48);
27 const SkColor kMenuBorderColor = SkColorSetRGB(0xBA, 0xBA, 0xBA);
28 const SkColor kEnabledMenuButtonBorderColor = SkColorSetARGB(36, 0, 0, 0);
29 const SkColor kFocusedMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
30 const SkColor kHoverMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
31 const SkColor kMenuSeparatorColor = SkColorSetRGB(0xE9, 0xE9, 0xE9);
32 const SkColor kEnabledMenuItemForegroundColor = SK_ColorBLACK;
33 const SkColor kDisabledMenuItemForegroundColor = SkColorSetRGB(161, 161, 146);
34 const SkColor kHoverMenuItemBackgroundColor =
35 SkColorSetARGB(204, 255, 255, 255);
36 // Button:
37 const SkColor kButtonHoverBackgroundColor = SkColorSetRGB(0xEA, 0xEA, 0xEA);
38 const SkColor kBlueButtonEnabledColor = SK_ColorWHITE;
39 const SkColor kBlueButtonDisabledColor = SK_ColorWHITE;
40 const SkColor kBlueButtonPressedColor = SK_ColorWHITE;
41 const SkColor kBlueButtonHoverColor = SK_ColorWHITE;
42 const SkColor kBlueButtonShadowColor = SkColorSetRGB(0x53, 0x8C, 0xEA);
43 // Link:
44 const SkColor kLinkDisabledColorMd = SK_ColorBLACK;
45 const SkColor kLinkEnabledColorMd = SkColorSetRGB(0x33, 0x67, 0xD6);
46 // Material colors:
47 const SkColor kAmber = SkColorSetRGB(0xFF, 0xC1, 0x07);
48 const SkColor kGoogleBlue = SkColorSetRGB(0x42, 0x85, 0xF4);
49 const SkColor kChromeIconGrey = SkColorSetRGB(0x5A, 0x5A, 0x5A);
50 // Material spinner/throbber:
51 const SkColor kThrobberWaitingColor = SkColorSetRGB(0xA6, 0xA6, 0xA6);
52 const SkColor kThrobberLightColor = SkColorSetRGB(0xF4, 0xF8, 0xFD);
54 } // namespace
56 namespace ui {
58 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id, SkColor* color) {
59 if (ui::MaterialDesignController::IsModeMaterial()) {
60 switch (color_id) {
61 // Link
62 case NativeTheme::kColorId_LinkDisabled:
63 *color = kLinkDisabledColorMd;
64 return true;
65 case NativeTheme::kColorId_LinkEnabled:
66 case NativeTheme::kColorId_LinkPressed:
67 // Normal and pressed share a color.
68 *color = kLinkEnabledColorMd;
69 return true;
70 default:
71 break;
75 switch (color_id) {
76 // MenuItem
77 case NativeTheme::kColorId_MenuBorderColor:
78 *color = kMenuBorderColor;
79 break;
80 case NativeTheme::kColorId_EnabledMenuButtonBorderColor:
81 *color = kEnabledMenuButtonBorderColor;
82 break;
83 case NativeTheme::kColorId_FocusedMenuButtonBorderColor:
84 *color = kFocusedMenuButtonBorderColor;
85 break;
86 case NativeTheme::kColorId_HoverMenuButtonBorderColor:
87 *color = kHoverMenuButtonBorderColor;
88 break;
89 case NativeTheme::kColorId_MenuSeparatorColor:
90 *color = kMenuSeparatorColor;
91 break;
92 case NativeTheme::kColorId_MenuBackgroundColor:
93 *color = kMenuBackgroundColor;
94 break;
95 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
96 *color = kMenuHighlightBackgroundColor;
97 break;
98 case NativeTheme::kColorId_HoverMenuItemBackgroundColor:
99 *color = kHoverMenuItemBackgroundColor;
100 break;
101 case NativeTheme::kColorId_EnabledMenuItemForegroundColor:
102 *color = kEnabledMenuItemForegroundColor;
103 break;
104 case NativeTheme::kColorId_DisabledMenuItemForegroundColor:
105 *color = kDisabledMenuItemForegroundColor;
106 break;
107 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor:
108 *color = SK_ColorBLACK;
109 break;
110 case NativeTheme::kColorId_SelectedMenuItemForegroundColor:
111 *color = SK_ColorWHITE;
112 break;
113 case NativeTheme::kColorId_ButtonDisabledColor:
114 *color = kDisabledMenuItemForegroundColor;
115 break;
116 // Button
117 case NativeTheme::kColorId_ButtonHoverBackgroundColor:
118 *color = kButtonHoverBackgroundColor;
119 break;
120 case NativeTheme::kColorId_BlueButtonEnabledColor:
121 *color = kBlueButtonEnabledColor;
122 break;
123 case NativeTheme::kColorId_BlueButtonDisabledColor:
124 *color = kBlueButtonDisabledColor;
125 break;
126 case NativeTheme::kColorId_BlueButtonPressedColor:
127 *color = kBlueButtonPressedColor;
128 break;
129 case NativeTheme::kColorId_BlueButtonHoverColor:
130 *color = kBlueButtonHoverColor;
131 break;
132 case NativeTheme::kColorId_BlueButtonShadowColor:
133 *color = kBlueButtonShadowColor;
134 break;
135 // Material icons
136 case NativeTheme::kColorId_Amber:
137 *color = kAmber;
138 break;
139 case NativeTheme::kColorId_ChromeIconGrey:
140 *color = kChromeIconGrey;
141 break;
142 case NativeTheme::kColorId_GoogleBlue:
143 *color = kGoogleBlue;
144 break;
145 // Material spinner/throbber
146 case NativeTheme::kColorId_ThrobberSpinningColor:
147 *color = kGoogleBlue;
148 break;
149 case NativeTheme::kColorId_ThrobberWaitingColor:
150 *color = kThrobberWaitingColor;
151 break;
152 case NativeTheme::kColorId_ThrobberLightColor:
153 *color = kThrobberLightColor;
154 break;
155 default:
156 return false;
159 if (color_utils::IsInvertedColorScheme()) {
160 switch (color_id) {
161 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
162 *color = kMenuInvertedSchemeHighlightBackgroundColor;
163 break;
164 default:
165 *color = color_utils::InvertColor(*color);
168 return true;
171 gfx::Size CommonThemeGetPartSize(NativeTheme::Part part,
172 NativeTheme::State state,
173 const NativeTheme::ExtraParams& extra) {
174 gfx::Size size;
175 switch (part) {
176 case NativeTheme::kComboboxArrow:
177 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
178 IDR_MENU_DROPARROW).Size();
180 case NativeTheme::kMenuCheck: {
181 const gfx::ImageSkia* check =
182 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
183 IDR_MENU_CHECK_CHECKED).ToImageSkia();
184 size.SetSize(check->width(), check->height());
185 break;
187 default:
188 break;
191 return size;
194 void CommonThemePaintComboboxArrow(SkCanvas* canvas, const gfx::Rect& rect) {
195 gfx::ImageSkia* arrow = ui::ResourceBundle::GetSharedInstance().
196 GetImageSkiaNamed(IDR_MENU_DROPARROW);
197 CommonThemeCreateCanvas(canvas)->DrawImageInt(*arrow, rect.x(), rect.y());
200 void CommonThemePaintMenuSeparator(SkCanvas* canvas, const gfx::Rect& rect) {
201 SkColor color;
202 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
203 SkPaint paint;
204 paint.setColor(kMenuSeparatorColor);
205 int position_y = rect.y() + rect.height() / 2;
206 canvas->drawLine(rect.x(), position_y, rect.right(), position_y, paint);
209 void CommonThemePaintMenuGutter(SkCanvas* canvas, const gfx::Rect& rect) {
210 SkColor color;
211 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
212 SkPaint paint;
213 paint.setColor(kMenuSeparatorColor);
214 int position_x = rect.x() + rect.width() / 2;
215 canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), paint);
218 void CommonThemePaintMenuBackground(SkCanvas* canvas, const gfx::Rect& rect) {
219 SkColor color;
220 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor, &color);
221 SkPaint paint;
222 paint.setColor(color);
223 canvas->drawRect(gfx::RectToSkRect(rect), paint);
226 void CommonThemePaintMenuItemBackground(SkCanvas* canvas,
227 NativeTheme::State state,
228 const gfx::Rect& rect) {
229 SkColor color;
230 SkPaint paint;
231 switch (state) {
232 case NativeTheme::kNormal:
233 case NativeTheme::kDisabled:
234 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor,
235 &color);
236 paint.setColor(color);
237 break;
238 case NativeTheme::kHovered:
239 CommonThemeGetSystemColor(
240 NativeTheme::kColorId_FocusedMenuItemBackgroundColor, &color);
241 paint.setColor(color);
242 break;
243 default:
244 NOTREACHED() << "Invalid state " << state;
245 break;
247 canvas->drawRect(gfx::RectToSkRect(rect), paint);
250 // static
251 scoped_ptr<gfx::Canvas> CommonThemeCreateCanvas(SkCanvas* sk_canvas) {
252 // TODO(pkotwicz): Do something better and don't infer device
253 // scale factor from canvas scale.
254 SkMatrix m = sk_canvas->getTotalMatrix();
255 float device_scale = static_cast<float>(SkScalarAbs(m.getScaleX()));
256 return make_scoped_ptr(new gfx::Canvas(sk_canvas, device_scale));
259 } // namespace ui