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"
19 // Theme colors returned by GetSystemColor().
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);
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);
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);
54 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
57 case NativeTheme::kColorId_MenuBorderColor
:
58 *color
= kMenuBorderColor
;
60 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
61 *color
= kEnabledMenuButtonBorderColor
;
63 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
64 *color
= kFocusedMenuButtonBorderColor
;
66 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
67 *color
= kHoverMenuButtonBorderColor
;
69 case NativeTheme::kColorId_MenuSeparatorColor
:
70 *color
= kMenuSeparatorColor
;
72 case NativeTheme::kColorId_MenuBackgroundColor
:
73 *color
= kMenuBackgroundColor
;
75 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
76 *color
= kMenuHighlightBackgroundColor
;
78 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
79 *color
= kHoverMenuItemBackgroundColor
;
81 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
82 *color
= kEnabledMenuItemForegroundColor
;
84 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
85 *color
= kDisabledMenuItemForegroundColor
;
87 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
88 *color
= SK_ColorBLACK
;
90 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
91 *color
= SK_ColorWHITE
;
93 case NativeTheme::kColorId_ButtonDisabledColor
:
94 *color
= kDisabledMenuItemForegroundColor
;
97 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
98 *color
= kButtonHoverBackgroundColor
;
100 case NativeTheme::kColorId_BlueButtonEnabledColor
:
101 *color
= kBlueButtonEnabledColor
;
103 case NativeTheme::kColorId_BlueButtonDisabledColor
:
104 *color
= kBlueButtonDisabledColor
;
106 case NativeTheme::kColorId_BlueButtonPressedColor
:
107 *color
= kBlueButtonPressedColor
;
109 case NativeTheme::kColorId_BlueButtonHoverColor
:
110 *color
= kBlueButtonHoverColor
;
112 case NativeTheme::kColorId_BlueButtonShadowColor
:
113 *color
= kBlueButtonShadowColor
;
116 case NativeTheme::kColorId_Amber
:
119 case NativeTheme::kColorId_ChromeIconGrey
:
120 *color
= kChromeIconGrey
;
122 case NativeTheme::kColorId_GoogleBlue
:
123 *color
= kGoogleBlue
;
125 // Material spinner/throbber
126 case NativeTheme::kColorId_ThrobberSpinningColor
:
127 *color
= kGoogleBlue
;
129 case NativeTheme::kColorId_ThrobberWaitingColor
:
130 *color
= kThrobberWaitingColor
;
132 case NativeTheme::kColorId_ThrobberLightColor
:
133 *color
= kThrobberLightColor
;
139 if (color_utils::IsInvertedColorScheme()) {
141 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
142 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
145 *color
= color_utils::InvertColor(*color
);
151 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
152 NativeTheme::State state
,
153 const NativeTheme::ExtraParams
& extra
) {
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());
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
) {
182 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
191 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
200 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
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
) {
212 case NativeTheme::kNormal
:
213 case NativeTheme::kDisabled
:
214 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
216 paint
.setColor(color
);
218 case NativeTheme::kHovered
:
219 CommonThemeGetSystemColor(
220 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
221 paint
.setColor(color
);
224 NOTREACHED() << "Invalid state " << state
;
227 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
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
));