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);
48 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id
, SkColor
* color
) {
51 case NativeTheme::kColorId_MenuBorderColor
:
52 *color
= kMenuBorderColor
;
54 case NativeTheme::kColorId_EnabledMenuButtonBorderColor
:
55 *color
= kEnabledMenuButtonBorderColor
;
57 case NativeTheme::kColorId_FocusedMenuButtonBorderColor
:
58 *color
= kFocusedMenuButtonBorderColor
;
60 case NativeTheme::kColorId_HoverMenuButtonBorderColor
:
61 *color
= kHoverMenuButtonBorderColor
;
63 case NativeTheme::kColorId_MenuSeparatorColor
:
64 *color
= kMenuSeparatorColor
;
66 case NativeTheme::kColorId_MenuBackgroundColor
:
67 *color
= kMenuBackgroundColor
;
69 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
70 *color
= kMenuHighlightBackgroundColor
;
72 case NativeTheme::kColorId_HoverMenuItemBackgroundColor
:
73 *color
= kHoverMenuItemBackgroundColor
;
75 case NativeTheme::kColorId_EnabledMenuItemForegroundColor
:
76 *color
= kEnabledMenuItemForegroundColor
;
78 case NativeTheme::kColorId_DisabledMenuItemForegroundColor
:
79 *color
= kDisabledMenuItemForegroundColor
;
81 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
82 *color
= SK_ColorBLACK
;
84 case NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
85 *color
= SK_ColorWHITE
;
87 case NativeTheme::kColorId_ButtonDisabledColor
:
88 *color
= kDisabledMenuItemForegroundColor
;
91 case NativeTheme::kColorId_ButtonHoverBackgroundColor
:
92 *color
= kButtonHoverBackgroundColor
;
94 case NativeTheme::kColorId_BlueButtonEnabledColor
:
95 *color
= kBlueButtonEnabledColor
;
97 case NativeTheme::kColorId_BlueButtonDisabledColor
:
98 *color
= kBlueButtonDisabledColor
;
100 case NativeTheme::kColorId_BlueButtonPressedColor
:
101 *color
= kBlueButtonPressedColor
;
103 case NativeTheme::kColorId_BlueButtonHoverColor
:
104 *color
= kBlueButtonHoverColor
;
106 case NativeTheme::kColorId_BlueButtonShadowColor
:
107 *color
= kBlueButtonShadowColor
;
113 if (gfx::IsInvertedColorScheme()) {
115 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor
:
116 *color
= kMenuInvertedSchemeHighlightBackgroundColor
;
119 *color
= color_utils::InvertColor(*color
);
125 gfx::Size
CommonThemeGetPartSize(NativeTheme::Part part
,
126 NativeTheme::State state
,
127 const NativeTheme::ExtraParams
& extra
) {
130 case NativeTheme::kComboboxArrow
:
131 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
132 IDR_MENU_DROPARROW
).Size();
134 case NativeTheme::kMenuCheck
: {
135 const gfx::ImageSkia
* check
=
136 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
137 IDR_MENU_CHECK_CHECKED
).ToImageSkia();
138 size
.SetSize(check
->width(), check
->height());
148 void CommonThemePaintComboboxArrow(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
149 gfx::ImageSkia
* arrow
= ui::ResourceBundle::GetSharedInstance().
150 GetImageSkiaNamed(IDR_MENU_DROPARROW
);
151 CommonThemeCreateCanvas(canvas
)->DrawImageInt(*arrow
, rect
.x(), rect
.y());
154 void CommonThemePaintMenuSeparator(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
156 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
158 paint
.setColor(kMenuSeparatorColor
);
159 int position_y
= rect
.y() + rect
.height() / 2;
160 canvas
->drawLine(rect
.x(), position_y
, rect
.right(), position_y
, paint
);
163 void CommonThemePaintMenuGutter(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
165 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor
, &color
);
167 paint
.setColor(kMenuSeparatorColor
);
168 int position_x
= rect
.x() + rect
.width() / 2;
169 canvas
->drawLine(position_x
, rect
.y(), position_x
, rect
.bottom(), paint
);
172 void CommonThemePaintMenuBackground(SkCanvas
* canvas
, const gfx::Rect
& rect
) {
174 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
, &color
);
176 paint
.setColor(color
);
177 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
180 void CommonThemePaintMenuItemBackground(SkCanvas
* canvas
,
181 NativeTheme::State state
,
182 const gfx::Rect
& rect
) {
186 case NativeTheme::kNormal
:
187 case NativeTheme::kDisabled
:
188 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
,
190 paint
.setColor(color
);
192 case NativeTheme::kHovered
:
193 CommonThemeGetSystemColor(
194 NativeTheme::kColorId_FocusedMenuItemBackgroundColor
, &color
);
195 paint
.setColor(color
);
198 NOTREACHED() << "Invalid state " << state
;
201 canvas
->drawRect(gfx::RectToSkRect(rect
), paint
);
205 scoped_ptr
<gfx::Canvas
> CommonThemeCreateCanvas(SkCanvas
* sk_canvas
) {
206 // TODO(pkotwicz): Do something better and don't infer device
207 // scale factor from canvas scale.
208 SkMatrix m
= sk_canvas
->getTotalMatrix();
209 float device_scale
= static_cast<float>(SkScalarAbs(m
.getScaleX()));
210 return scoped_ptr
<gfx::Canvas
>(
211 gfx::Canvas::CreateCanvasWithoutScaling(sk_canvas
, device_scale
));