1 // Copyright (c) 2013 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 "chrome/browser/themes/theme_properties.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/themes/browser_theme_pack.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/resource/material_design/material_design_controller.h"
13 #include "ui/resources/grit/ui_resources.h"
17 // ----------------------------------------------------------------------------
18 // Defaults for properties which are stored in the browser theme pack. If you
19 // change these defaults, you must increment the version number in
20 // browser_theme_pack.h
23 #if defined(OS_CHROMEOS)
24 // Used for theme fallback colors.
25 const SkColor kDefaultColorFrame
[] = {
26 SkColorSetRGB(109, 109, 109), SkColorSetRGB(204, 204, 204)};
27 const SkColor kDefaultColorFrameInactive
[] = {
28 SkColorSetRGB(176, 176, 176), SkColorSetRGB(220, 220, 220)};
29 #elif defined(OS_MACOSX)
30 const SkColor kDefaultColorFrame
= SkColorSetRGB(224, 224, 224);
31 const SkColor kDefaultColorFrameInactive
= SkColorSetRGB(246, 246, 246);
33 const SkColor kDefaultColorFrame
= SkColorSetRGB(66, 116, 201);
34 const SkColor kDefaultColorFrameInactive
= SkColorSetRGB(161, 182, 228);
37 const SkColor kDefaultColorFrameIncognito
[] = {
38 SkColorSetRGB(83, 106, 139), SkColorSetRGB(160, 160, 162)};
39 const SkColor kDefaultColorFrameIncognitoInactive
[] = {
40 SkColorSetRGB(126, 139, 156), SkColorSetRGB(120, 120, 122)};
42 #if defined(OS_MACOSX)
43 const SkColor kDefaultColorToolbar
= SkColorSetRGB(230, 230, 230);
45 const SkColor kDefaultColorToolbar
[] = {
46 SkColorSetRGB(223, 223, 223), SkColorSetRGB(242, 242, 242)};
49 const SkColor kDefaultColorTabText
= SK_ColorBLACK
;
51 #if defined(OS_MACOSX)
52 const SkColor kDefaultColorBackgroundTabText
= SK_ColorBLACK
;
54 const SkColor kDefaultColorBackgroundTabText
[] = {
55 SkColorSetRGB(64, 64, 64), SK_ColorBLACK
};
58 const SkColor kDefaultColorBookmarkText
= SK_ColorBLACK
;
61 const SkColor kDefaultColorNTPBackground
=
62 color_utils::GetSysSkColor(COLOR_WINDOW
);
63 const SkColor kDefaultColorNTPText
=
64 color_utils::GetSysSkColor(COLOR_WINDOWTEXT
);
65 const SkColor kDefaultColorNTPLink
=
66 color_utils::GetSysSkColor(COLOR_HOTLIGHT
);
68 // TODO(beng): source from theme provider.
69 const SkColor kDefaultColorNTPBackground
= SK_ColorWHITE
;
70 const SkColor kDefaultColorNTPText
= SK_ColorBLACK
;
71 const SkColor kDefaultColorNTPLink
= SkColorSetRGB(6, 55, 116);
74 const SkColor kDefaultColorNTPHeader
= SkColorSetRGB(150, 150, 150);
75 const SkColor kDefaultColorNTPSection
= SkColorSetRGB(229, 229, 229);
76 const SkColor kDefaultColorNTPSectionText
= SK_ColorBLACK
;
77 const SkColor kDefaultColorNTPSectionLink
= SkColorSetRGB(6, 55, 116);
78 const SkColor kDefaultColorButtonBackground
= SkColorSetARGB(0, 0, 0, 0);
81 const color_utils::HSL kDefaultTintButtons
= { -1, -1, -1 };
82 const color_utils::HSL kDefaultTintFrame
= { -1, -1, -1 };
83 const color_utils::HSL kDefaultTintFrameInactive
= { -1, -1, 0.75f
};
84 const color_utils::HSL kDefaultTintFrameIncognito
= { -1, 0.2f
, 0.35f
};
85 const color_utils::HSL kDefaultTintFrameIncognitoInactive
= { -1, 0.3f
, 0.6f
};
86 const color_utils::HSL kDefaultTintBackgroundTab
= { -1, 0.5, 0.75 };
88 // Default display properties.
89 const int kDefaultDisplayPropertyNTPAlignment
=
90 ThemeProperties::ALIGN_CENTER
;
91 const int kDefaultDisplayPropertyNTPTiling
=
92 ThemeProperties::NO_REPEAT
;
93 // By default, we do not use the ntp alternate logo.
94 const int kDefaultDisplayPropertyNTPAlternateLogo
= 0;
96 // ----------------------------------------------------------------------------
97 // Defaults for properties which are not stored in the browser theme pack.
99 const SkColor kDefaultColorControlBackground
= SK_ColorWHITE
;
100 const SkColor kDefaultColorToolbarSeparator
[] = {
101 SkColorSetRGB(170, 170, 171), SkColorSetRGB(182, 180, 182)};
103 #if defined(OS_MACOSX)
104 const SkColor kDefaultColorToolbarButtonStroke
= SkColorSetARGB(75, 81, 81, 81);
105 const SkColor kDefaultColorToolbarButtonStrokeInactive
=
106 SkColorSetARGB(75, 99, 99, 99);
107 const SkColor kDefaultColorToolbarBezel
= SkColorSetRGB(204, 204, 204);
108 const SkColor kDefaultColorToolbarStroke
= SkColorSetRGB(103, 103, 103);
109 const SkColor kDefaultColorToolbarStrokeInactive
= SkColorSetRGB(163, 163, 163);
112 // ----------------------------------------------------------------------------
113 // Defaults for layout properties which are not stored in the browser theme
114 // pack. The array indices here are the values of
115 // ui::MaterialDesignController::Mode, see
116 // ui/base/resource/material_design/material_design_controller.h
118 // Additional horizontal padding applied on the trailing edge of icon-label
120 const int kIconLabelViewTrailingPadding
[] = {2, 8, 8};
122 // The horizontal space between the edge and a bubble
123 const int kLocationBarBubbleHorizontalPadding
[] = {1, 5, 5};
125 // The additional vertical padding of a bubble.
126 const int kLocationBarBubbleVerticalPadding
[] = {1, 3, 3};
128 // The height to be occupied by the LocationBar. For
129 // MaterialDesignController::NON_MATERIAL the height is determined from image
131 const int kLocationBarHeight
[] = {0, 28, 32};
133 // Space between items in the location bar, as well as between items and the
135 const int kLocationBarHorizontalPadding
[] = {3, 6, 6};
137 // The Vertical padding of items in the location bar.
138 const int kLocationBarVerticalPadding
[] = {2, 6, 6};
140 // The number of pixels in the omnibox dropdown border image interior to
141 // the actual border.
142 const int kOmniboxDropdownBorderInterior
[] = {6, 0, 0};
144 // In an omnibox dropdown row, the minimum distance between the top and
145 // bottom of the row's icon and the top or bottom of the row edge.
146 const int kOmniboxDropdownMinIconVerticalPadding
[] = {2, 4, 8};
148 // In an omnibox dropdown row, the minimum distance between the top and
149 // bottom of the row's text and the top or bottom of the row edge.
150 const int kOmniboxDropdownMinTextVerticalPadding
[] = {3, 4, 8};
152 // The spacing between a ToolbarButton's image and its border.
153 const int kToolbarButtonBorderInset
[] = {2, 6, 6};
155 // The minimum bottom vertical padding of the toolbar.
156 const int kToolbarViewBottomVerticalPadding
[] = {5, 5, 5};
158 // Non-ash uses a rounded content area with no shadow in the assets.
159 const int kToolbarViewContentShadowHeight
[] = {0, 0, 0};
161 // Ash doesn't use a rounded content area and its top edge has an extra shadow.
162 const int kToolbarViewContentShadowHeightAsh
[] = {2, 0, 0};
164 // Additional horizontal padding between the elements in the toolbar.
165 const int kToolbarViewElementPadding
[] = {0, 0, 8};
167 // Padding between the right-edge of the location bar and browser actions.
168 const int kToolbarViewLocationBarRightPadding
[] = {0, 4, 8};
170 // The edge graphics have some built-in spacing/shadowing, so we have to adjust
171 // our spacing to make it match.
172 const int kToolbarViewLeftEdgeSpacing
[] = {3, 4, 8};
173 const int kToolbarViewRightEdgeSpacing
[] = {2, 4, 8};
175 // The horizontal space between most items.
176 const int kToolbarViewStandardSpacing
[] = {3, 4, 8};
178 // The minimum top vertical padding of the toolbar.
179 const int kToolbarViewTopVerticalPadding
[] = {5, 4, 4};
181 // ----------------------------------------------------------------------------
183 // Strings used in alignment properties.
184 const char kAlignmentCenter
[] = "center";
185 const char kAlignmentTop
[] = "top";
186 const char kAlignmentBottom
[] = "bottom";
187 const char kAlignmentLeft
[] = "left";
188 const char kAlignmentRight
[] = "right";
190 // Strings used in background tiling repetition properties.
191 const char kTilingNoRepeat
[] = "no-repeat";
192 const char kTilingRepeatX
[] = "repeat-x";
193 const char kTilingRepeatY
[] = "repeat-y";
194 const char kTilingRepeat
[] = "repeat";
196 // The image resources that will be tinted by the 'button' tint value.
197 // If you change this list, you must increment the version number in
198 // browser_theme_pack.cc, and you should assign persistent IDs to the
199 // data table at the start of said file or else tinted versions of
200 // these resources will not be created.
202 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
203 // should be removed once the cocoa port no longer uses them.
204 const int kToolbarButtonIDs
[] = {
205 IDR_BACK
, IDR_BACK_D
, IDR_BACK_H
, IDR_BACK_P
,
206 IDR_FORWARD
, IDR_FORWARD_D
, IDR_FORWARD_H
, IDR_FORWARD_P
,
207 IDR_HOME
, IDR_HOME_H
, IDR_HOME_P
,
208 IDR_RELOAD
, IDR_RELOAD_H
, IDR_RELOAD_P
,
209 IDR_STOP
, IDR_STOP_D
, IDR_STOP_H
, IDR_STOP_P
,
210 IDR_BROWSER_ACTIONS_OVERFLOW
, IDR_BROWSER_ACTIONS_OVERFLOW_H
,
211 IDR_BROWSER_ACTIONS_OVERFLOW_P
,
212 IDR_TOOLS
, IDR_TOOLS_H
, IDR_TOOLS_P
,
214 IDR_TOOLBAR_BEZEL_HOVER
, IDR_TOOLBAR_BEZEL_PRESSED
, IDR_TOOLS_BAR
,
217 SkColor
TintForUnderline(SkColor input
) {
218 return SkColorSetA(input
, SkColorGetA(input
) / 3);
224 int ThemeProperties::StringToAlignment(const std::string
& alignment
) {
225 int alignment_mask
= 0;
226 for (const std::string
& component
: base::SplitString(
227 alignment
, base::kWhitespaceASCII
,
228 base::TRIM_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
)) {
229 if (base::LowerCaseEqualsASCII(component
, kAlignmentTop
))
230 alignment_mask
|= ALIGN_TOP
;
231 else if (base::LowerCaseEqualsASCII(component
, kAlignmentBottom
))
232 alignment_mask
|= ALIGN_BOTTOM
;
233 else if (base::LowerCaseEqualsASCII(component
, kAlignmentLeft
))
234 alignment_mask
|= ALIGN_LEFT
;
235 else if (base::LowerCaseEqualsASCII(component
, kAlignmentRight
))
236 alignment_mask
|= ALIGN_RIGHT
;
238 return alignment_mask
;
242 int ThemeProperties::StringToTiling(const std::string
& tiling
) {
243 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeatX
))
245 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeatY
))
247 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeat
))
249 // NO_REPEAT is the default choice.
254 std::string
ThemeProperties::AlignmentToString(int alignment
) {
255 // Convert from an AlignmentProperty back into a string.
256 std::string
vertical_string(kAlignmentCenter
);
257 std::string
horizontal_string(kAlignmentCenter
);
259 if (alignment
& ALIGN_TOP
)
260 vertical_string
= kAlignmentTop
;
261 else if (alignment
& ALIGN_BOTTOM
)
262 vertical_string
= kAlignmentBottom
;
264 if (alignment
& ALIGN_LEFT
)
265 horizontal_string
= kAlignmentLeft
;
266 else if (alignment
& ALIGN_RIGHT
)
267 horizontal_string
= kAlignmentRight
;
269 return horizontal_string
+ " " + vertical_string
;
273 std::string
ThemeProperties::TilingToString(int tiling
) {
274 // Convert from a TilingProperty back into a string.
275 if (tiling
== REPEAT_X
)
276 return kTilingRepeatX
;
277 if (tiling
== REPEAT_Y
)
278 return kTilingRepeatY
;
279 if (tiling
== REPEAT
)
280 return kTilingRepeat
;
281 return kTilingNoRepeat
;
285 const std::set
<int>& ThemeProperties::GetTintableToolbarButtons() {
286 CR_DEFINE_STATIC_LOCAL(std::set
<int>, button_set
, ());
287 if (button_set
.empty()) {
288 button_set
= std::set
<int>(
290 kToolbarButtonIDs
+ arraysize(kToolbarButtonIDs
));
297 color_utils::HSL
ThemeProperties::GetDefaultTint(int id
) {
300 return kDefaultTintFrame
;
301 case TINT_FRAME_INACTIVE
:
302 return kDefaultTintFrameInactive
;
303 case TINT_FRAME_INCOGNITO
:
304 return kDefaultTintFrameIncognito
;
305 case TINT_FRAME_INCOGNITO_INACTIVE
:
306 return kDefaultTintFrameIncognitoInactive
;
308 return kDefaultTintButtons
;
309 case TINT_BACKGROUND_TAB
:
310 return kDefaultTintBackgroundTab
;
312 color_utils::HSL result
= {-1, -1, -1};
318 SkColor
ThemeProperties::GetDefaultColor(int id
) {
319 int mode
= ui::MaterialDesignController::IsModeMaterial();
321 // Properties stored in theme pack.
323 #if defined(OS_CHROMEOS)
324 return kDefaultColorFrame
[mode
];
326 return kDefaultColorFrame
;
327 #endif // OS_CHROMEOS
328 case COLOR_FRAME_INACTIVE
:
329 #if defined(OS_CHROMEOS)
330 return kDefaultColorFrameInactive
[mode
];
332 return kDefaultColorFrameInactive
;
333 #endif // OS_CHROMEOS
334 case COLOR_FRAME_INCOGNITO
:
335 return kDefaultColorFrameIncognito
[mode
];
336 case COLOR_FRAME_INCOGNITO_INACTIVE
:
337 return kDefaultColorFrameIncognitoInactive
[mode
];
339 #if defined(OS_MACOSX)
340 return kDefaultColorToolbar
;
342 return kDefaultColorToolbar
[mode
];
345 return kDefaultColorTabText
;
346 case COLOR_BACKGROUND_TAB_TEXT
:
347 #if defined(OS_MACOSX)
348 return kDefaultColorBackgroundTabText
;
350 return kDefaultColorBackgroundTabText
[mode
];
352 case COLOR_BOOKMARK_TEXT
:
353 return kDefaultColorBookmarkText
;
354 case COLOR_NTP_BACKGROUND
:
355 return kDefaultColorNTPBackground
;
357 return kDefaultColorNTPText
;
359 return kDefaultColorNTPLink
;
360 case COLOR_NTP_LINK_UNDERLINE
:
361 return TintForUnderline(kDefaultColorNTPLink
);
362 case COLOR_NTP_HEADER
:
363 return kDefaultColorNTPHeader
;
364 case COLOR_NTP_SECTION
:
365 return kDefaultColorNTPSection
;
366 case COLOR_NTP_SECTION_TEXT
:
367 return kDefaultColorNTPSectionText
;
368 case COLOR_NTP_SECTION_LINK
:
369 return kDefaultColorNTPSectionLink
;
370 case COLOR_NTP_SECTION_LINK_UNDERLINE
:
371 return TintForUnderline(kDefaultColorNTPSectionLink
);
372 case COLOR_BUTTON_BACKGROUND
:
373 return kDefaultColorButtonBackground
;
375 // Properties not stored in theme pack.
376 case COLOR_CONTROL_BACKGROUND
:
377 return kDefaultColorControlBackground
;
378 case COLOR_TOOLBAR_SEPARATOR
:
379 return kDefaultColorToolbarSeparator
[mode
];
380 #if defined(OS_MACOSX)
381 case COLOR_TOOLBAR_BUTTON_STROKE
:
382 return kDefaultColorToolbarButtonStroke
;
383 case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE
:
384 return kDefaultColorToolbarButtonStrokeInactive
;
385 case COLOR_TOOLBAR_BEZEL
:
386 return kDefaultColorToolbarBezel
;
387 case COLOR_TOOLBAR_STROKE
:
388 return kDefaultColorToolbarStroke
;
389 case COLOR_TOOLBAR_STROKE_INACTIVE
:
390 return kDefaultColorToolbarStrokeInactive
;
393 // Return a debugging red color.
399 int ThemeProperties::GetDefaultDisplayProperty(int id
) {
400 int mode
= ui::MaterialDesignController::GetMode();
402 case NTP_BACKGROUND_ALIGNMENT
:
403 return kDefaultDisplayPropertyNTPAlignment
;
404 case NTP_BACKGROUND_TILING
:
405 return kDefaultDisplayPropertyNTPTiling
;
406 case NTP_LOGO_ALTERNATE
:
407 return kDefaultDisplayPropertyNTPAlternateLogo
;
408 case PROPERTY_ICON_LABEL_VIEW_TRAILING_PADDING
:
409 return kIconLabelViewTrailingPadding
[mode
];
410 case PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING
:
411 return kLocationBarBubbleHorizontalPadding
[mode
];
412 case PROPERTY_LOCATION_BAR_BUBBLE_VERTICAL_PADDING
:
413 return kLocationBarBubbleVerticalPadding
[mode
];
414 case PROPERTY_LOCATION_BAR_HEIGHT
:
415 return kLocationBarHeight
[mode
];
416 case PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING
:
417 return kLocationBarHorizontalPadding
[mode
];
418 case PROPERTY_LOCATION_BAR_VERTICAL_PADDING
:
419 return kLocationBarVerticalPadding
[mode
];
420 case PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
:
421 return kOmniboxDropdownBorderInterior
[mode
];
422 case PROPERTY_OMNIBOX_DROPDOWN_MIN_ICON_VERTICAL_PADDING
:
423 return kOmniboxDropdownMinIconVerticalPadding
[mode
];
424 case PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
:
425 return kOmniboxDropdownMinTextVerticalPadding
[mode
];
426 case PROPERTY_TOOLBAR_BUTTON_BORDER_INSET
:
427 return kToolbarButtonBorderInset
[mode
];
428 case PROPERTY_TOOLBAR_VIEW_BOTTOM_VERTICAL_PADDING
:
429 return kToolbarViewBottomVerticalPadding
[mode
];
430 case PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT
:
431 return kToolbarViewContentShadowHeight
[mode
];
432 case PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT_ASH
:
433 return kToolbarViewContentShadowHeightAsh
[mode
];
434 case PROPERTY_TOOLBAR_VIEW_ELEMENT_PADDING
:
435 return kToolbarViewElementPadding
[mode
];
436 case PROPERTY_TOOLBAR_VIEW_LEFT_EDGE_SPACING
:
437 return kToolbarViewLeftEdgeSpacing
[mode
];
438 case PROPERTY_TOOLBAR_VIEW_LOCATION_BAR_RIGHT_PADDING
:
439 return kToolbarViewLocationBarRightPadding
[mode
];
440 case PROPERTY_TOOLBAR_VIEW_RIGHT_EDGE_SPACING
:
441 return kToolbarViewRightEdgeSpacing
[mode
];
442 case PROPERTY_TOOLBAR_VIEW_STANDARD_SPACING
:
443 return kToolbarViewStandardSpacing
[mode
];
444 case PROPERTY_TOOLBAR_VIEW_TOP_VERTICAL_PADDING
:
445 return kToolbarViewTopVerticalPadding
[mode
];