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);
42 // Material spinner/throbber:
43 const SkColor kThrobberSpinningColor
= SkColorSetRGB(0x42, 0x81, 0xF4);
44 const SkColor kThrobberWaitingColor
= SkColorSetRGB(0xA6, 0xA6, 0xA6);
45 const SkColor kThrobberLightColor
= SkColorSetRGB(0xF4, 0xF8, 0xFD);
51 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
54 case NativeTheme::kColorId_MenuBorderColor
:
55 *color
= kMenuBorderColor
;
57 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
58 *color
= kEnabledMenuButtonBorderColor
;
60 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
61 *color
= kFocusedMenuButtonBorderColor
;
63 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
64 *color
= kHoverMenuButtonBorderColor
;
66 case NativeTheme::kColorId_MenuSeparatorColor
:
67 *color
= kMenuSeparatorColor
;
69 case NativeTheme::kColorId_MenuBackgroundColor
:
70 *color
= kMenuBackgroundColor
;
72 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
73 *color
= kMenuHighlightBackgroundColor
;
75 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
76 *color
= kHoverMenuItemBackgroundColor
;
78 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
79 *color
= kEnabledMenuItemForegroundColor
;
81 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
82 *color
= kDisabledMenuItemForegroundColor
;
84 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
85 *color
= SK_ColorBLACK
;
87 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
88 *color
= SK_ColorWHITE
;
90 case NativeTheme::kColorId_ButtonDisabledColor
:
91 *color
= kDisabledMenuItemForegroundColor
;
94 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
95 *color
= kButtonHoverBackgroundColor
;
97 case NativeTheme::kColorId_BlueButtonEnabledColor
:
98 *color
= kBlueButtonEnabledColor
;
100 case NativeTheme::kColorId_BlueButtonDisabledColor
:
101 *color
= kBlueButtonDisabledColor
;
103 case NativeTheme::kColorId_BlueButtonPressedColor
:
104 *color
= kBlueButtonPressedColor
;
106 case NativeTheme::kColorId_BlueButtonHoverColor
:
107 *color
= kBlueButtonHoverColor
;
109 case NativeTheme::kColorId_BlueButtonShadowColor
:
110 *color
= kBlueButtonShadowColor
;
112 // Material spinner/throbber
113 case NativeTheme::kColorId_ThrobberSpinningColor
:
114 *color
= kThrobberSpinningColor
;
116 case NativeTheme::kColorId_ThrobberWaitingColor
:
117 *color
= kThrobberWaitingColor
;
119 case NativeTheme::kColorId_ThrobberLightColor
:
120 *color
= kThrobberLightColor
;
126 if (color_utils::IsInvertedColorScheme()) {
128 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
129 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
132 *color
= color_utils::InvertColor(*color
);
138 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
139 NativeTheme::State state
,
140 const NativeTheme::ExtraParams
& extra
) {
143 case NativeTheme::kComboboxArrow
:
144 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
145 IDR_MENU_DROPARROW
).Size();
147 case NativeTheme::kMenuCheck
: {
148 const gfx::ImageSkia
* check
=
149 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
150 IDR_MENU_CHECK_CHECKED
).ToImageSkia();
151 size
.SetSize(check
->width(), check
->height());
161 void CommonThemePaintComboboxArrow(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
162 gfx::ImageSkia
* arrow
= ui::ResourceBundle::GetSharedInstance().
163 GetImageSkiaNamed(IDR_MENU_DROPARROW
);
164 CommonThemeCreateCanvas(canvas
)->DrawImageInt(*arrow
, rect
.x(), rect
.y());
167 void CommonThemePaintMenuSeparator(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
169 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
171 paint
.setColor(kMenuSeparatorColor
);
172 int position_y
= rect
.y() + rect
.height() / 2;
173 canvas
->drawLine(rect
.x(), position_y
, rect
.right(), position_y
, paint
);
176 void CommonThemePaintMenuGutter(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
178 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
180 paint
.setColor(kMenuSeparatorColor
);
181 int position_x
= rect
.x() + rect
.width() / 2;
182 canvas
->drawLine(position_x
, rect
.y(), position_x
, rect
.bottom(), paint
);
185 void CommonThemePaintMenuBackground(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
187 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
189 paint
.setColor(color
);
190 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
193 void CommonThemePaintMenuItemBackground(SkCanvas
* canvas
,
194 NativeTheme::State state
,
195 const gfx::Rect
& rect
) {
199 case NativeTheme::kNormal
:
200 case NativeTheme::kDisabled
:
201 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
203 paint
.setColor(color
);
205 case NativeTheme::kHovered
:
206 CommonThemeGetSystemColor(
207 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
208 paint
.setColor(color
);
211 NOTREACHED() << "Invalid state " << state
;
214 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
218 scoped_ptr
<gfx::Canvas
> CommonThemeCreateCanvas(SkCanvas
* sk_canvas
) {
219 // TODO(pkotwicz): Do something better and don't infer device
220 // scale factor from canvas scale.
221 SkMatrix m
= sk_canvas
->getTotalMatrix();
222 float device_scale
= static_cast<float>(SkScalarAbs(m
.getScaleX()));
223 return make_scoped_ptr(new gfx::Canvas(sk_canvas
, device_scale
));