Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ui / native_theme / common_theme.cc
blob80f3452d76d822e45f668cc4e78bb3297c71ad49
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/gfx/sys_color_change_listener.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 // Material spinner/throbber:
44 const SkColor kThrobberSpinningColor = SkColorSetRGB(0x42, 0x81, 0xF4);
45 const SkColor kThrobberWaitingColor = SkColorSetRGB(0xA6, 0xA6, 0xA6);
46 const SkColor kThrobberLightColor = SkColorSetRGB(0xF4, 0xF8, 0xFD);
48 } // namespace
50 namespace ui {
52 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id, SkColor* color) {
53 switch (color_id) {
54 // MenuItem
55 case NativeTheme::kColorId_MenuBorderColor:
56 *color = kMenuBorderColor;
57 break;
58 case NativeTheme::kColorId_EnabledMenuButtonBorderColor:
59 *color = kEnabledMenuButtonBorderColor;
60 break;
61 case NativeTheme::kColorId_FocusedMenuButtonBorderColor:
62 *color = kFocusedMenuButtonBorderColor;
63 break;
64 case NativeTheme::kColorId_HoverMenuButtonBorderColor:
65 *color = kHoverMenuButtonBorderColor;
66 break;
67 case NativeTheme::kColorId_MenuSeparatorColor:
68 *color = kMenuSeparatorColor;
69 break;
70 case NativeTheme::kColorId_MenuBackgroundColor:
71 *color = kMenuBackgroundColor;
72 break;
73 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
74 *color = kMenuHighlightBackgroundColor;
75 break;
76 case NativeTheme::kColorId_HoverMenuItemBackgroundColor:
77 *color = kHoverMenuItemBackgroundColor;
78 break;
79 case NativeTheme::kColorId_EnabledMenuItemForegroundColor:
80 *color = kEnabledMenuItemForegroundColor;
81 break;
82 case NativeTheme::kColorId_DisabledMenuItemForegroundColor:
83 *color = kDisabledMenuItemForegroundColor;
84 break;
85 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor:
86 *color = SK_ColorBLACK;
87 break;
88 case NativeTheme::kColorId_SelectedMenuItemForegroundColor:
89 *color = SK_ColorWHITE;
90 break;
91 case NativeTheme::kColorId_ButtonDisabledColor:
92 *color = kDisabledMenuItemForegroundColor;
93 break;
94 // Button
95 case NativeTheme::kColorId_ButtonHoverBackgroundColor:
96 *color = kButtonHoverBackgroundColor;
97 break;
98 case NativeTheme::kColorId_BlueButtonEnabledColor:
99 *color = kBlueButtonEnabledColor;
100 break;
101 case NativeTheme::kColorId_BlueButtonDisabledColor:
102 *color = kBlueButtonDisabledColor;
103 break;
104 case NativeTheme::kColorId_BlueButtonPressedColor:
105 *color = kBlueButtonPressedColor;
106 break;
107 case NativeTheme::kColorId_BlueButtonHoverColor:
108 *color = kBlueButtonHoverColor;
109 break;
110 case NativeTheme::kColorId_BlueButtonShadowColor:
111 *color = kBlueButtonShadowColor;
112 break;
113 // Material spinner/throbber
114 case NativeTheme::kColorId_ThrobberSpinningColor:
115 *color = kThrobberSpinningColor;
116 break;
117 case NativeTheme::kColorId_ThrobberWaitingColor:
118 *color = kThrobberWaitingColor;
119 break;
120 case NativeTheme::kColorId_ThrobberLightColor:
121 *color = kThrobberLightColor;
122 break;
123 default:
124 return false;
127 if (gfx::IsInvertedColorScheme()) {
128 switch (color_id) {
129 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
130 *color = kMenuInvertedSchemeHighlightBackgroundColor;
131 break;
132 default:
133 *color = color_utils::InvertColor(*color);
136 return true;
139 gfx::Size CommonThemeGetPartSize(NativeTheme::Part part,
140 NativeTheme::State state,
141 const NativeTheme::ExtraParams& extra) {
142 gfx::Size size;
143 switch (part) {
144 case NativeTheme::kComboboxArrow:
145 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
146 IDR_MENU_DROPARROW).Size();
148 case NativeTheme::kMenuCheck: {
149 const gfx::ImageSkia* check =
150 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
151 IDR_MENU_CHECK_CHECKED).ToImageSkia();
152 size.SetSize(check->width(), check->height());
153 break;
155 default:
156 break;
159 return size;
162 void CommonThemePaintComboboxArrow(SkCanvas* canvas, const gfx::Rect& rect) {
163 gfx::ImageSkia* arrow = ui::ResourceBundle::GetSharedInstance().
164 GetImageSkiaNamed(IDR_MENU_DROPARROW);
165 CommonThemeCreateCanvas(canvas)->DrawImageInt(*arrow, rect.x(), rect.y());
168 void CommonThemePaintMenuSeparator(SkCanvas* canvas, const gfx::Rect& rect) {
169 SkColor color;
170 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
171 SkPaint paint;
172 paint.setColor(kMenuSeparatorColor);
173 int position_y = rect.y() + rect.height() / 2;
174 canvas->drawLine(rect.x(), position_y, rect.right(), position_y, paint);
177 void CommonThemePaintMenuGutter(SkCanvas* canvas, const gfx::Rect& rect) {
178 SkColor color;
179 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
180 SkPaint paint;
181 paint.setColor(kMenuSeparatorColor);
182 int position_x = rect.x() + rect.width() / 2;
183 canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), paint);
186 void CommonThemePaintMenuBackground(SkCanvas* canvas, const gfx::Rect& rect) {
187 SkColor color;
188 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor, &color);
189 SkPaint paint;
190 paint.setColor(color);
191 canvas->drawRect(gfx::RectToSkRect(rect), paint);
194 void CommonThemePaintMenuItemBackground(SkCanvas* canvas,
195 NativeTheme::State state,
196 const gfx::Rect& rect) {
197 SkColor color;
198 SkPaint paint;
199 switch (state) {
200 case NativeTheme::kNormal:
201 case NativeTheme::kDisabled:
202 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor,
203 &color);
204 paint.setColor(color);
205 break;
206 case NativeTheme::kHovered:
207 CommonThemeGetSystemColor(
208 NativeTheme::kColorId_FocusedMenuItemBackgroundColor, &color);
209 paint.setColor(color);
210 break;
211 default:
212 NOTREACHED() << "Invalid state " << state;
213 break;
215 canvas->drawRect(gfx::RectToSkRect(rect), paint);
218 // static
219 scoped_ptr<gfx::Canvas> CommonThemeCreateCanvas(SkCanvas* sk_canvas) {
220 // TODO(pkotwicz): Do something better and don't infer device
221 // scale factor from canvas scale.
222 SkMatrix m = sk_canvas->getTotalMatrix();
223 float device_scale = static_cast<float>(SkScalarAbs(m.getScaleX()));
224 return make_scoped_ptr(new gfx::Canvas(sk_canvas, device_scale));
227 } // namespace ui