Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / native_theme / common_theme.cc
blob7d64560681410cf207ca0e3184ba05b1d284325c
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/resource_bundle.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/color_utils.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/skia_util.h"
15 #include "ui/resources/grit/ui_resources.h"
17 namespace {
19 // Theme colors returned by GetSystemColor().
21 // MenuItem:
22 const SkColor kMenuBackgroundColor = SK_ColorWHITE;
23 const SkColor kMenuHighlightBackgroundColor = SkColorSetRGB(0x42, 0x81, 0xF4);
24 const SkColor kMenuInvertedSchemeHighlightBackgroundColor =
25 SkColorSetRGB(48, 48, 48);
26 const SkColor kMenuBorderColor = SkColorSetRGB(0xBA, 0xBA, 0xBA);
27 const SkColor kEnabledMenuButtonBorderColor = SkColorSetARGB(36, 0, 0, 0);
28 const SkColor kFocusedMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
29 const SkColor kHoverMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
30 const SkColor kMenuSeparatorColor = SkColorSetRGB(0xE9, 0xE9, 0xE9);
31 const SkColor kEnabledMenuItemForegroundColor = SK_ColorBLACK;
32 const SkColor kDisabledMenuItemForegroundColor = SkColorSetRGB(161, 161, 146);
33 const SkColor kHoverMenuItemBackgroundColor =
34 SkColorSetARGB(204, 255, 255, 255);
35 // Button:
36 const SkColor kButtonHoverBackgroundColor = SkColorSetRGB(0xEA, 0xEA, 0xEA);
37 const SkColor kBlueButtonEnabledColor = SK_ColorWHITE;
38 const SkColor kBlueButtonDisabledColor = SK_ColorWHITE;
39 const SkColor kBlueButtonPressedColor = SK_ColorWHITE;
40 const SkColor kBlueButtonHoverColor = SK_ColorWHITE;
41 const SkColor kBlueButtonShadowColor = SkColorSetRGB(0x53, 0x8C, 0xEA);
42 // Material colors:
43 const SkColor kAmber = SkColorSetRGB(0xFF, 0xC1, 0x07);
44 const SkColor kGoogleBlue = SkColorSetRGB(0x42, 0x85, 0xF4);
45 const SkColor kChromeIconGrey = SkColorSetRGB(0x5A, 0x5A, 0x5A);
46 // Material spinner/throbber:
47 const SkColor kThrobberWaitingColor = SkColorSetRGB(0xA6, 0xA6, 0xA6);
48 const SkColor kThrobberLightColor = SkColorSetRGB(0xF4, 0xF8, 0xFD);
50 } // namespace
52 namespace ui {
54 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id, SkColor* color) {
55 switch (color_id) {
56 // MenuItem
57 case NativeTheme::kColorId_MenuBorderColor:
58 *color = kMenuBorderColor;
59 break;
60 case NativeTheme::kColorId_EnabledMenuButtonBorderColor:
61 *color = kEnabledMenuButtonBorderColor;
62 break;
63 case NativeTheme::kColorId_FocusedMenuButtonBorderColor:
64 *color = kFocusedMenuButtonBorderColor;
65 break;
66 case NativeTheme::kColorId_HoverMenuButtonBorderColor:
67 *color = kHoverMenuButtonBorderColor;
68 break;
69 case NativeTheme::kColorId_MenuSeparatorColor:
70 *color = kMenuSeparatorColor;
71 break;
72 case NativeTheme::kColorId_MenuBackgroundColor:
73 *color = kMenuBackgroundColor;
74 break;
75 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
76 *color = kMenuHighlightBackgroundColor;
77 break;
78 case NativeTheme::kColorId_HoverMenuItemBackgroundColor:
79 *color = kHoverMenuItemBackgroundColor;
80 break;
81 case NativeTheme::kColorId_EnabledMenuItemForegroundColor:
82 *color = kEnabledMenuItemForegroundColor;
83 break;
84 case NativeTheme::kColorId_DisabledMenuItemForegroundColor:
85 *color = kDisabledMenuItemForegroundColor;
86 break;
87 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor:
88 *color = SK_ColorBLACK;
89 break;
90 case NativeTheme::kColorId_SelectedMenuItemForegroundColor:
91 *color = SK_ColorWHITE;
92 break;
93 case NativeTheme::kColorId_ButtonDisabledColor:
94 *color = kDisabledMenuItemForegroundColor;
95 break;
96 // Button
97 case NativeTheme::kColorId_ButtonHoverBackgroundColor:
98 *color = kButtonHoverBackgroundColor;
99 break;
100 case NativeTheme::kColorId_BlueButtonEnabledColor:
101 *color = kBlueButtonEnabledColor;
102 break;
103 case NativeTheme::kColorId_BlueButtonDisabledColor:
104 *color = kBlueButtonDisabledColor;
105 break;
106 case NativeTheme::kColorId_BlueButtonPressedColor:
107 *color = kBlueButtonPressedColor;
108 break;
109 case NativeTheme::kColorId_BlueButtonHoverColor:
110 *color = kBlueButtonHoverColor;
111 break;
112 case NativeTheme::kColorId_BlueButtonShadowColor:
113 *color = kBlueButtonShadowColor;
114 break;
115 // Material icons
116 case NativeTheme::kColorId_Amber:
117 *color = kAmber;
118 break;
119 case NativeTheme::kColorId_ChromeIconGrey:
120 *color = kChromeIconGrey;
121 break;
122 case NativeTheme::kColorId_GoogleBlue:
123 *color = kGoogleBlue;
124 break;
125 // Material spinner/throbber
126 case NativeTheme::kColorId_ThrobberSpinningColor:
127 *color = kGoogleBlue;
128 break;
129 case NativeTheme::kColorId_ThrobberWaitingColor:
130 *color = kThrobberWaitingColor;
131 break;
132 case NativeTheme::kColorId_ThrobberLightColor:
133 *color = kThrobberLightColor;
134 break;
135 default:
136 return false;
139 if (color_utils::IsInvertedColorScheme()) {
140 switch (color_id) {
141 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
142 *color = kMenuInvertedSchemeHighlightBackgroundColor;
143 break;
144 default:
145 *color = color_utils::InvertColor(*color);
148 return true;
151 gfx::Size CommonThemeGetPartSize(NativeTheme::Part part,
152 NativeTheme::State state,
153 const NativeTheme::ExtraParams& extra) {
154 gfx::Size size;
155 switch (part) {
156 case NativeTheme::kComboboxArrow:
157 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
158 IDR_MENU_DROPARROW).Size();
160 case NativeTheme::kMenuCheck: {
161 const gfx::ImageSkia* check =
162 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
163 IDR_MENU_CHECK_CHECKED).ToImageSkia();
164 size.SetSize(check->width(), check->height());
165 break;
167 default:
168 break;
171 return size;
174 void CommonThemePaintComboboxArrow(SkCanvas* canvas, const gfx::Rect& rect) {
175 gfx::ImageSkia* arrow = ui::ResourceBundle::GetSharedInstance().
176 GetImageSkiaNamed(IDR_MENU_DROPARROW);
177 CommonThemeCreateCanvas(canvas)->DrawImageInt(*arrow, rect.x(), rect.y());
180 void CommonThemePaintMenuSeparator(SkCanvas* canvas, const gfx::Rect& rect) {
181 SkColor color;
182 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
183 SkPaint paint;
184 paint.setColor(kMenuSeparatorColor);
185 int position_y = rect.y() + rect.height() / 2;
186 canvas->drawLine(rect.x(), position_y, rect.right(), position_y, paint);
189 void CommonThemePaintMenuGutter(SkCanvas* canvas, const gfx::Rect& rect) {
190 SkColor color;
191 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
192 SkPaint paint;
193 paint.setColor(kMenuSeparatorColor);
194 int position_x = rect.x() + rect.width() / 2;
195 canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), paint);
198 void CommonThemePaintMenuBackground(SkCanvas* canvas, const gfx::Rect& rect) {
199 SkColor color;
200 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor, &color);
201 SkPaint paint;
202 paint.setColor(color);
203 canvas->drawRect(gfx::RectToSkRect(rect), paint);
206 void CommonThemePaintMenuItemBackground(SkCanvas* canvas,
207 NativeTheme::State state,
208 const gfx::Rect& rect) {
209 SkColor color;
210 SkPaint paint;
211 switch (state) {
212 case NativeTheme::kNormal:
213 case NativeTheme::kDisabled:
214 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor,
215 &color);
216 paint.setColor(color);
217 break;
218 case NativeTheme::kHovered:
219 CommonThemeGetSystemColor(
220 NativeTheme::kColorId_FocusedMenuItemBackgroundColor, &color);
221 paint.setColor(color);
222 break;
223 default:
224 NOTREACHED() << "Invalid state " << state;
225 break;
227 canvas->drawRect(gfx::RectToSkRect(rect), paint);
230 // static
231 scoped_ptr<gfx::Canvas> CommonThemeCreateCanvas(SkCanvas* sk_canvas) {
232 // TODO(pkotwicz): Do something better and don't infer device
233 // scale factor from canvas scale.
234 SkMatrix m = sk_canvas->getTotalMatrix();
235 float device_scale = static_cast<float>(SkScalarAbs(m.getScaleX()));
236 return make_scoped_ptr(new gfx::Canvas(sk_canvas, device_scale));
239 } // namespace ui