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"
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);
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);
52 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
55 case NativeTheme::kColorId_MenuBorderColor
:
56 *color
= kMenuBorderColor
;
58 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
59 *color
= kEnabledMenuButtonBorderColor
;
61 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
62 *color
= kFocusedMenuButtonBorderColor
;
64 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
65 *color
= kHoverMenuButtonBorderColor
;
67 case NativeTheme::kColorId_MenuSeparatorColor
:
68 *color
= kMenuSeparatorColor
;
70 case NativeTheme::kColorId_MenuBackgroundColor
:
71 *color
= kMenuBackgroundColor
;
73 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
74 *color
= kMenuHighlightBackgroundColor
;
76 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
77 *color
= kHoverMenuItemBackgroundColor
;
79 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
80 *color
= kEnabledMenuItemForegroundColor
;
82 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
83 *color
= kDisabledMenuItemForegroundColor
;
85 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
86 *color
= SK_ColorBLACK
;
88 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
89 *color
= SK_ColorWHITE
;
91 case NativeTheme::kColorId_ButtonDisabledColor
:
92 *color
= kDisabledMenuItemForegroundColor
;
95 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
96 *color
= kButtonHoverBackgroundColor
;
98 case NativeTheme::kColorId_BlueButtonEnabledColor
:
99 *color
= kBlueButtonEnabledColor
;
101 case NativeTheme::kColorId_BlueButtonDisabledColor
:
102 *color
= kBlueButtonDisabledColor
;
104 case NativeTheme::kColorId_BlueButtonPressedColor
:
105 *color
= kBlueButtonPressedColor
;
107 case NativeTheme::kColorId_BlueButtonHoverColor
:
108 *color
= kBlueButtonHoverColor
;
110 case NativeTheme::kColorId_BlueButtonShadowColor
:
111 *color
= kBlueButtonShadowColor
;
113 // Material spinner/throbber
114 case NativeTheme::kColorId_ThrobberSpinningColor
:
115 *color
= kThrobberSpinningColor
;
117 case NativeTheme::kColorId_ThrobberWaitingColor
:
118 *color
= kThrobberWaitingColor
;
120 case NativeTheme::kColorId_ThrobberLightColor
:
121 *color
= kThrobberLightColor
;
127 if (gfx::IsInvertedColorScheme()) {
129 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
130 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
133 *color
= color_utils::InvertColor(*color
);
139 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
140 NativeTheme::State state
,
141 const NativeTheme::ExtraParams
& extra
) {
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());
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
) {
170 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
179 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
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
) {
188 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
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
) {
200 case NativeTheme::kNormal
:
201 case NativeTheme::kDisabled
:
202 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
204 paint
.setColor(color
);
206 case NativeTheme::kHovered
:
207 CommonThemeGetSystemColor(
208 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
209 paint
.setColor(color
);
212 NOTREACHED() << "Invalid state " << state
;
215 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
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
));