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"
20 // Theme colors returned by GetSystemColor().
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);
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);
44 const SkColor kLinkDisabledColorMd
= SK_ColorBLACK
;
45 const SkColor kLinkEnabledColorMd
= SkColorSetRGB(0x33, 0x67, 0xD6);
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);
58 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
59 if (ui::MaterialDesignController::IsModeMaterial()) {
62 case NativeTheme::kColorId_LinkDisabled
:
63 *color
= kLinkDisabledColorMd
;
65 case NativeTheme::kColorId_LinkEnabled
:
66 case NativeTheme::kColorId_LinkPressed
:
67 // Normal and pressed share a color.
68 *color
= kLinkEnabledColorMd
;
77 case NativeTheme::kColorId_MenuBorderColor
:
78 *color
= kMenuBorderColor
;
80 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
81 *color
= kEnabledMenuButtonBorderColor
;
83 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
84 *color
= kFocusedMenuButtonBorderColor
;
86 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
87 *color
= kHoverMenuButtonBorderColor
;
89 case NativeTheme::kColorId_MenuSeparatorColor
:
90 *color
= kMenuSeparatorColor
;
92 case NativeTheme::kColorId_MenuBackgroundColor
:
93 *color
= kMenuBackgroundColor
;
95 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
96 *color
= kMenuHighlightBackgroundColor
;
98 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
99 *color
= kHoverMenuItemBackgroundColor
;
101 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
102 *color
= kEnabledMenuItemForegroundColor
;
104 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
105 *color
= kDisabledMenuItemForegroundColor
;
107 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
108 *color
= SK_ColorBLACK
;
110 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
111 *color
= SK_ColorWHITE
;
113 case NativeTheme::kColorId_ButtonDisabledColor
:
114 *color
= kDisabledMenuItemForegroundColor
;
117 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
118 *color
= kButtonHoverBackgroundColor
;
120 case NativeTheme::kColorId_BlueButtonEnabledColor
:
121 *color
= kBlueButtonEnabledColor
;
123 case NativeTheme::kColorId_BlueButtonDisabledColor
:
124 *color
= kBlueButtonDisabledColor
;
126 case NativeTheme::kColorId_BlueButtonPressedColor
:
127 *color
= kBlueButtonPressedColor
;
129 case NativeTheme::kColorId_BlueButtonHoverColor
:
130 *color
= kBlueButtonHoverColor
;
132 case NativeTheme::kColorId_BlueButtonShadowColor
:
133 *color
= kBlueButtonShadowColor
;
136 case NativeTheme::kColorId_Amber
:
139 case NativeTheme::kColorId_ChromeIconGrey
:
140 *color
= kChromeIconGrey
;
142 case NativeTheme::kColorId_GoogleBlue
:
143 *color
= kGoogleBlue
;
145 // Material spinner/throbber
146 case NativeTheme::kColorId_ThrobberSpinningColor
:
147 *color
= kGoogleBlue
;
149 case NativeTheme::kColorId_ThrobberWaitingColor
:
150 *color
= kThrobberWaitingColor
;
152 case NativeTheme::kColorId_ThrobberLightColor
:
153 *color
= kThrobberLightColor
;
159 if (color_utils::IsInvertedColorScheme()) {
161 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
162 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
165 *color
= color_utils::InvertColor(*color
);
171 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
172 NativeTheme::State state
,
173 const NativeTheme::ExtraParams
& extra
) {
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());
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
) {
202 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
211 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
220 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
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
) {
232 case NativeTheme::kNormal
:
233 case NativeTheme::kDisabled
:
234 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
236 paint
.setColor(color
);
238 case NativeTheme::kHovered
:
239 CommonThemeGetSystemColor(
240 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
241 paint
.setColor(color
);
244 NOTREACHED() << "Invalid state " << state
;
247 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
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
));