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 kGoogleBlue
= SkColorSetRGB(0x42, 0x85, 0xF4);
44 const SkColor kChromeIconGrey
= SkColorSetRGB(0x5A, 0x5A, 0x5A);
45 // Material spinner/throbber:
46 const SkColor kThrobberWaitingColor
= SkColorSetRGB(0xA6, 0xA6, 0xA6);
47 const SkColor kThrobberLightColor
= SkColorSetRGB(0xF4, 0xF8, 0xFD);
53 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
56 case NativeTheme::kColorId_MenuBorderColor
:
57 *color
= kMenuBorderColor
;
59 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
60 *color
= kEnabledMenuButtonBorderColor
;
62 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
63 *color
= kFocusedMenuButtonBorderColor
;
65 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
66 *color
= kHoverMenuButtonBorderColor
;
68 case NativeTheme::kColorId_MenuSeparatorColor
:
69 *color
= kMenuSeparatorColor
;
71 case NativeTheme::kColorId_MenuBackgroundColor
:
72 *color
= kMenuBackgroundColor
;
74 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
75 *color
= kMenuHighlightBackgroundColor
;
77 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
78 *color
= kHoverMenuItemBackgroundColor
;
80 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
81 *color
= kEnabledMenuItemForegroundColor
;
83 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
84 *color
= kDisabledMenuItemForegroundColor
;
86 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
87 *color
= SK_ColorBLACK
;
89 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
90 *color
= SK_ColorWHITE
;
92 case NativeTheme::kColorId_ButtonDisabledColor
:
93 *color
= kDisabledMenuItemForegroundColor
;
96 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
97 *color
= kButtonHoverBackgroundColor
;
99 case NativeTheme::kColorId_BlueButtonEnabledColor
:
100 *color
= kBlueButtonEnabledColor
;
102 case NativeTheme::kColorId_BlueButtonDisabledColor
:
103 *color
= kBlueButtonDisabledColor
;
105 case NativeTheme::kColorId_BlueButtonPressedColor
:
106 *color
= kBlueButtonPressedColor
;
108 case NativeTheme::kColorId_BlueButtonHoverColor
:
109 *color
= kBlueButtonHoverColor
;
111 case NativeTheme::kColorId_BlueButtonShadowColor
:
112 *color
= kBlueButtonShadowColor
;
115 case NativeTheme::kColorId_ChromeIconGrey
:
116 *color
= kChromeIconGrey
;
118 case NativeTheme::kColorId_GoogleBlue
:
119 *color
= kGoogleBlue
;
121 // Material spinner/throbber
122 case NativeTheme::kColorId_ThrobberSpinningColor
:
123 *color
= kGoogleBlue
;
125 case NativeTheme::kColorId_ThrobberWaitingColor
:
126 *color
= kThrobberWaitingColor
;
128 case NativeTheme::kColorId_ThrobberLightColor
:
129 *color
= kThrobberLightColor
;
135 if (color_utils::IsInvertedColorScheme()) {
137 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
138 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
141 *color
= color_utils::InvertColor(*color
);
147 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
148 NativeTheme::State state
,
149 const NativeTheme::ExtraParams
& extra
) {
152 case NativeTheme::kComboboxArrow
:
153 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
154 IDR_MENU_DROPARROW
).Size();
156 case NativeTheme::kMenuCheck
: {
157 const gfx::ImageSkia
* check
=
158 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
159 IDR_MENU_CHECK_CHECKED
).ToImageSkia();
160 size
.SetSize(check
->width(), check
->height());
170 void CommonThemePaintComboboxArrow(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
171 gfx::ImageSkia
* arrow
= ui::ResourceBundle::GetSharedInstance().
172 GetImageSkiaNamed(IDR_MENU_DROPARROW
);
173 CommonThemeCreateCanvas(canvas
)->DrawImageInt(*arrow
, rect
.x(), rect
.y());
176 void CommonThemePaintMenuSeparator(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
178 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
180 paint
.setColor(kMenuSeparatorColor
);
181 int position_y
= rect
.y() + rect
.height() / 2;
182 canvas
->drawLine(rect
.x(), position_y
, rect
.right(), position_y
, paint
);
185 void CommonThemePaintMenuGutter(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
187 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
189 paint
.setColor(kMenuSeparatorColor
);
190 int position_x
= rect
.x() + rect
.width() / 2;
191 canvas
->drawLine(position_x
, rect
.y(), position_x
, rect
.bottom(), paint
);
194 void CommonThemePaintMenuBackground(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
196 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
198 paint
.setColor(color
);
199 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
202 void CommonThemePaintMenuItemBackground(SkCanvas
* canvas
,
203 NativeTheme::State state
,
204 const gfx::Rect
& rect
) {
208 case NativeTheme::kNormal
:
209 case NativeTheme::kDisabled
:
210 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
212 paint
.setColor(color
);
214 case NativeTheme::kHovered
:
215 CommonThemeGetSystemColor(
216 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
217 paint
.setColor(color
);
220 NOTREACHED() << "Invalid state " << state
;
223 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
227 scoped_ptr
<gfx::Canvas
> CommonThemeCreateCanvas(SkCanvas
* sk_canvas
) {
228 // TODO(pkotwicz): Do something better and don't infer device
229 // scale factor from canvas scale.
230 SkMatrix m
= sk_canvas
->getTotalMatrix();
231 float device_scale
= static_cast<float>(SkScalarAbs(m
.getScaleX()));
232 return make_scoped_ptr(new gfx::Canvas(sk_canvas
, device_scale
));