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 // Non-ash uses a rounded content area with no shadow in the assets.
156 const int kToolbarViewContentShadowHeight
[] = {0, 0, 0};
158 // Ash doesn't use a rounded content area and its top edge has an extra shadow.
159 const int kToolbarViewContentShadowHeightAsh
[] = {2, 0, 0};
161 // Additional horizontal padding between the elements in the toolbar.
162 const int kToolbarViewElementPadding
[] = {0, 0, 8};
164 // Padding between the right-edge of the location bar and browser actions.
165 const int kToolbarViewLocationBarRightPadding
[] = {0, 4, 8};
167 // The edge graphics have some built-in spacing/shadowing, so we have to adjust
168 // our spacing to make it match.
169 const int kToolbarViewLeftEdgeSpacing
[] = {3, 4, 8};
170 const int kToolbarViewRightEdgeSpacing
[] = {2, 4, 8};
172 // The horizontal space between most items.
173 const int kToolbarViewStandardSpacing
[] = {3, 4, 8};
175 // The minimal vertical padding of the toolbar.
176 const int kToolbarViewVerticalPadding
[] = {5, 4, 4};
178 // ----------------------------------------------------------------------------
180 // Strings used in alignment properties.
181 const char kAlignmentCenter
[] = "center";
182 const char kAlignmentTop
[] = "top";
183 const char kAlignmentBottom
[] = "bottom";
184 const char kAlignmentLeft
[] = "left";
185 const char kAlignmentRight
[] = "right";
187 // Strings used in background tiling repetition properties.
188 const char kTilingNoRepeat
[] = "no-repeat";
189 const char kTilingRepeatX
[] = "repeat-x";
190 const char kTilingRepeatY
[] = "repeat-y";
191 const char kTilingRepeat
[] = "repeat";
193 // The image resources that will be tinted by the 'button' tint value.
194 // If you change this list, you must increment the version number in
195 // browser_theme_pack.cc, and you should assign persistent IDs to the
196 // data table at the start of said file or else tinted versions of
197 // these resources will not be created.
199 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
200 // should be removed once the cocoa port no longer uses them.
201 const int kToolbarButtonIDs
[] = {
202 IDR_BACK
, IDR_BACK_D
, IDR_BACK_H
, IDR_BACK_P
,
203 IDR_FORWARD
, IDR_FORWARD_D
, IDR_FORWARD_H
, IDR_FORWARD_P
,
204 IDR_HOME
, IDR_HOME_H
, IDR_HOME_P
,
205 IDR_RELOAD
, IDR_RELOAD_H
, IDR_RELOAD_P
,
206 IDR_STOP
, IDR_STOP_D
, IDR_STOP_H
, IDR_STOP_P
,
207 IDR_BROWSER_ACTIONS_OVERFLOW
, IDR_BROWSER_ACTIONS_OVERFLOW_H
,
208 IDR_BROWSER_ACTIONS_OVERFLOW_P
,
209 IDR_TOOLS
, IDR_TOOLS_H
, IDR_TOOLS_P
,
211 IDR_TOOLBAR_BEZEL_HOVER
, IDR_TOOLBAR_BEZEL_PRESSED
, IDR_TOOLS_BAR
,
214 SkColor
TintForUnderline(SkColor input
) {
215 return SkColorSetA(input
, SkColorGetA(input
) / 3);
221 int ThemeProperties::StringToAlignment(const std::string
& alignment
) {
222 int alignment_mask
= 0;
223 for (const std::string
& component
: base::SplitString(
224 alignment
, base::kWhitespaceASCII
,
225 base::TRIM_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
)) {
226 if (base::LowerCaseEqualsASCII(component
, kAlignmentTop
))
227 alignment_mask
|= ALIGN_TOP
;
228 else if (base::LowerCaseEqualsASCII(component
, kAlignmentBottom
))
229 alignment_mask
|= ALIGN_BOTTOM
;
230 else if (base::LowerCaseEqualsASCII(component
, kAlignmentLeft
))
231 alignment_mask
|= ALIGN_LEFT
;
232 else if (base::LowerCaseEqualsASCII(component
, kAlignmentRight
))
233 alignment_mask
|= ALIGN_RIGHT
;
235 return alignment_mask
;
239 int ThemeProperties::StringToTiling(const std::string
& tiling
) {
240 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeatX
))
242 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeatY
))
244 if (base::LowerCaseEqualsASCII(tiling
, kTilingRepeat
))
246 // NO_REPEAT is the default choice.
251 std::string
ThemeProperties::AlignmentToString(int alignment
) {
252 // Convert from an AlignmentProperty back into a string.
253 std::string
vertical_string(kAlignmentCenter
);
254 std::string
horizontal_string(kAlignmentCenter
);
256 if (alignment
& ALIGN_TOP
)
257 vertical_string
= kAlignmentTop
;
258 else if (alignment
& ALIGN_BOTTOM
)
259 vertical_string
= kAlignmentBottom
;
261 if (alignment
& ALIGN_LEFT
)
262 horizontal_string
= kAlignmentLeft
;
263 else if (alignment
& ALIGN_RIGHT
)
264 horizontal_string
= kAlignmentRight
;
266 return horizontal_string
+ " " + vertical_string
;
270 std::string
ThemeProperties::TilingToString(int tiling
) {
271 // Convert from a TilingProperty back into a string.
272 if (tiling
== REPEAT_X
)
273 return kTilingRepeatX
;
274 if (tiling
== REPEAT_Y
)
275 return kTilingRepeatY
;
276 if (tiling
== REPEAT
)
277 return kTilingRepeat
;
278 return kTilingNoRepeat
;
282 const std::set
<int>& ThemeProperties::GetTintableToolbarButtons() {
283 CR_DEFINE_STATIC_LOCAL(std::set
<int>, button_set
, ());
284 if (button_set
.empty()) {
285 button_set
= std::set
<int>(
287 kToolbarButtonIDs
+ arraysize(kToolbarButtonIDs
));
294 color_utils::HSL
ThemeProperties::GetDefaultTint(int id
) {
297 return kDefaultTintFrame
;
298 case TINT_FRAME_INACTIVE
:
299 return kDefaultTintFrameInactive
;
300 case TINT_FRAME_INCOGNITO
:
301 return kDefaultTintFrameIncognito
;
302 case TINT_FRAME_INCOGNITO_INACTIVE
:
303 return kDefaultTintFrameIncognitoInactive
;
305 return kDefaultTintButtons
;
306 case TINT_BACKGROUND_TAB
:
307 return kDefaultTintBackgroundTab
;
309 color_utils::HSL result
= {-1, -1, -1};
315 SkColor
ThemeProperties::GetDefaultColor(int id
) {
316 int mode
= ui::MaterialDesignController::IsModeMaterial();
318 // Properties stored in theme pack.
320 #if defined(OS_CHROMEOS)
321 return kDefaultColorFrame
[mode
];
323 return kDefaultColorFrame
;
324 #endif // OS_CHROMEOS
325 case COLOR_FRAME_INACTIVE
:
326 #if defined(OS_CHROMEOS)
327 return kDefaultColorFrameInactive
[mode
];
329 return kDefaultColorFrameInactive
;
330 #endif // OS_CHROMEOS
331 case COLOR_FRAME_INCOGNITO
:
332 return kDefaultColorFrameIncognito
[mode
];
333 case COLOR_FRAME_INCOGNITO_INACTIVE
:
334 return kDefaultColorFrameIncognitoInactive
[mode
];
336 #if defined(OS_MACOSX)
337 return kDefaultColorToolbar
;
339 return kDefaultColorToolbar
[mode
];
342 return kDefaultColorTabText
;
343 case COLOR_BACKGROUND_TAB_TEXT
:
344 #if defined(OS_MACOSX)
345 return kDefaultColorBackgroundTabText
;
347 return kDefaultColorBackgroundTabText
[mode
];
349 case COLOR_BOOKMARK_TEXT
:
350 return kDefaultColorBookmarkText
;
351 case COLOR_NTP_BACKGROUND
:
352 return kDefaultColorNTPBackground
;
354 return kDefaultColorNTPText
;
356 return kDefaultColorNTPLink
;
357 case COLOR_NTP_LINK_UNDERLINE
:
358 return TintForUnderline(kDefaultColorNTPLink
);
359 case COLOR_NTP_HEADER
:
360 return kDefaultColorNTPHeader
;
361 case COLOR_NTP_SECTION
:
362 return kDefaultColorNTPSection
;
363 case COLOR_NTP_SECTION_TEXT
:
364 return kDefaultColorNTPSectionText
;
365 case COLOR_NTP_SECTION_LINK
:
366 return kDefaultColorNTPSectionLink
;
367 case COLOR_NTP_SECTION_LINK_UNDERLINE
:
368 return TintForUnderline(kDefaultColorNTPSectionLink
);
369 case COLOR_BUTTON_BACKGROUND
:
370 return kDefaultColorButtonBackground
;
372 // Properties not stored in theme pack.
373 case COLOR_CONTROL_BACKGROUND
:
374 return kDefaultColorControlBackground
;
375 case COLOR_TOOLBAR_SEPARATOR
:
376 return kDefaultColorToolbarSeparator
[mode
];
377 #if defined(OS_MACOSX)
378 case COLOR_TOOLBAR_BUTTON_STROKE
:
379 return kDefaultColorToolbarButtonStroke
;
380 case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE
:
381 return kDefaultColorToolbarButtonStrokeInactive
;
382 case COLOR_TOOLBAR_BEZEL
:
383 return kDefaultColorToolbarBezel
;
384 case COLOR_TOOLBAR_STROKE
:
385 return kDefaultColorToolbarStroke
;
386 case COLOR_TOOLBAR_STROKE_INACTIVE
:
387 return kDefaultColorToolbarStrokeInactive
;
390 // Return a debugging red color.
396 int ThemeProperties::GetDefaultDisplayProperty(int id
) {
397 int mode
= ui::MaterialDesignController::GetMode();
399 case ThemeProperties::NTP_BACKGROUND_ALIGNMENT
:
400 return kDefaultDisplayPropertyNTPAlignment
;
401 case ThemeProperties::NTP_BACKGROUND_TILING
:
402 return kDefaultDisplayPropertyNTPTiling
;
403 case ThemeProperties::NTP_LOGO_ALTERNATE
:
404 return kDefaultDisplayPropertyNTPAlternateLogo
;
405 case ThemeProperties::PROPERTY_ICON_LABEL_VIEW_TRAILING_PADDING
:
406 return kIconLabelViewTrailingPadding
[mode
];
407 case ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING
:
408 return kLocationBarBubbleHorizontalPadding
[mode
];
409 case ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_VERTICAL_PADDING
:
410 return kLocationBarBubbleVerticalPadding
[mode
];
411 case ThemeProperties::PROPERTY_LOCATION_BAR_HEIGHT
:
412 return kLocationBarHeight
[mode
];
413 case ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING
:
414 return kLocationBarHorizontalPadding
[mode
];
415 case ThemeProperties::PROPERTY_LOCATION_BAR_VERTICAL_PADDING
:
416 return kLocationBarVerticalPadding
[mode
];
417 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
:
418 return kOmniboxDropdownBorderInterior
[mode
];
419 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_ICON_VERTICAL_PADDING
:
420 return kOmniboxDropdownMinIconVerticalPadding
[mode
];
421 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
:
422 return kOmniboxDropdownMinTextVerticalPadding
[mode
];
423 case ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET
:
424 return kToolbarButtonBorderInset
[mode
];
425 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT
:
426 return kToolbarViewContentShadowHeight
[mode
];
427 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT_ASH
:
428 return kToolbarViewContentShadowHeightAsh
[mode
];
429 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_ELEMENT_PADDING
:
430 return kToolbarViewElementPadding
[mode
];
431 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_LEFT_EDGE_SPACING
:
432 return kToolbarViewLeftEdgeSpacing
[mode
];
433 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_LOCATION_BAR_RIGHT_PADDING
:
434 return kToolbarViewLocationBarRightPadding
[mode
];
435 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_RIGHT_EDGE_SPACING
:
436 return kToolbarViewRightEdgeSpacing
[mode
];
437 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_STANDARD_SPACING
:
438 return kToolbarViewStandardSpacing
[mode
];
439 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_VERTICAL_PADDING
:
440 return kToolbarViewVerticalPadding
[mode
];