ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / themes / theme_properties.cc
blobde0efe1ef6c17956280c81be0ee8f7b09b037165
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"
15 namespace {
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
22 // Default colors.
23 #if defined(OS_CHROMEOS)
24 // Used for theme fallback colors.
25 const SkColor kDefaultColorFrame[] = {
26 SkColorSetRGB(109, 109, 109), SkColorSetRGB(217, 217, 217)};
27 const SkColor kDefaultColorFrameInactive[] = {
28 SkColorSetRGB(176, 176, 176), SkColorSetRGB(230, 230, 230)};
29 #elif defined(OS_MACOSX)
30 const SkColor kDefaultColorFrame = SkColorSetRGB(224, 224, 224);
31 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(246, 246, 246);
32 #else
33 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
34 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
35 #endif // OS_CHROMEOS
37 const SkColor kDefaultColorFrameIncognito = SkColorSetRGB(83, 106, 139);
38 const SkColor kDefaultColorFrameIncognitoInactive =
39 SkColorSetRGB(126, 139, 156);
41 #if defined(OS_MACOSX)
42 const SkColor kDefaultColorToolbar = SkColorSetRGB(230, 230, 230);
43 #else
44 const SkColor kDefaultColorToolbar[] = {
45 SkColorSetRGB(223, 223, 223), SkColorSetRGB(242, 242, 242)};
46 #endif // OS_MACOSX
48 const SkColor kDefaultColorTabText = SK_ColorBLACK;
50 #if defined(OS_MACOSX)
51 const SkColor kDefaultColorBackgroundTabText = SK_ColorBLACK;
52 #else
53 const SkColor kDefaultColorBackgroundTabText = SkColorSetRGB(64, 64, 64);
54 #endif // OS_MACOSX
56 const SkColor kDefaultColorBookmarkText = SK_ColorBLACK;
58 #if defined(OS_WIN)
59 const SkColor kDefaultColorNTPBackground =
60 color_utils::GetSysSkColor(COLOR_WINDOW);
61 const SkColor kDefaultColorNTPText =
62 color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
63 const SkColor kDefaultColorNTPLink =
64 color_utils::GetSysSkColor(COLOR_HOTLIGHT);
65 #else
66 // TODO(beng): source from theme provider.
67 const SkColor kDefaultColorNTPBackground = SK_ColorWHITE;
68 const SkColor kDefaultColorNTPText = SK_ColorBLACK;
69 const SkColor kDefaultColorNTPLink = SkColorSetRGB(6, 55, 116);
70 #endif // OS_WIN
72 const SkColor kDefaultColorNTPHeader = SkColorSetRGB(150, 150, 150);
73 const SkColor kDefaultColorNTPSection = SkColorSetRGB(229, 229, 229);
74 const SkColor kDefaultColorNTPSectionText = SK_ColorBLACK;
75 const SkColor kDefaultColorNTPSectionLink = SkColorSetRGB(6, 55, 116);
76 const SkColor kDefaultColorButtonBackground = SkColorSetARGB(0, 0, 0, 0);
78 // Default tints.
79 const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 };
80 const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 };
81 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f };
82 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f };
83 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f };
84 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 };
86 // Default display properties.
87 const int kDefaultDisplayPropertyNTPAlignment =
88 ThemeProperties::ALIGN_CENTER;
89 const int kDefaultDisplayPropertyNTPTiling =
90 ThemeProperties::NO_REPEAT;
91 // By default, we do not use the ntp alternate logo.
92 const int kDefaultDisplayPropertyNTPAlternateLogo = 0;
94 // ----------------------------------------------------------------------------
95 // Defaults for properties which are not stored in the browser theme pack.
97 const SkColor kDefaultColorControlBackground = SK_ColorWHITE;
98 const SkColor kDefaultColorToolbarSeparator[] = {
99 SkColorSetRGB(170, 170, 171), SkColorSetRGB(182, 180, 182)};
101 #if defined(OS_MACOSX)
102 const SkColor kDefaultColorToolbarButtonStroke = SkColorSetARGB(75, 81, 81, 81);
103 const SkColor kDefaultColorToolbarButtonStrokeInactive =
104 SkColorSetARGB(75, 99, 99, 99);
105 const SkColor kDefaultColorToolbarBezel = SkColorSetRGB(204, 204, 204);
106 const SkColor kDefaultColorToolbarStroke = SkColorSetRGB(103, 103, 103);
107 const SkColor kDefaultColorToolbarStrokeInactive = SkColorSetRGB(163, 163, 163);
108 #endif // OS_MACOSX
110 // ----------------------------------------------------------------------------
111 // Defaults for layout properties which are not stored in the browser theme
112 // pack. The array indices here are the values of
113 // ui::MaterialDesignController::Mode, see
114 // ui/base/resource/material_design/material_design_controller.h
116 // Additional horizontal padding applied on the trailing edge of icon-label
117 // views.
118 const int kIconLabelViewTrailingPadding[] = {2, 8, 8};
120 // The horizontal space between the edge and a bubble
121 const int kLocationBarBubbleHorizontalPadding[] = {1, 5, 5};
123 // The additional vertical padding of a bubble.
124 const int kLocationBarBubbleVerticalPadding[] = {1, 3, 3};
126 // The height to be occupied by the LocationBar. For
127 // MaterialDesignController::NON_MATERIAL the height is determined from image
128 // assets.
129 const int kLocationBarHeight[] = {0, 28, 32};
131 // Space between items in the location bar, as well as between items and the
132 // edges.
133 const int kLocationBarHorizontalPadding[] = {3, 6, 6};
135 // The Vertical padding of items in the location bar.
136 const int kLocationBarVerticalPadding[] = {2, 6, 6};
138 // The number of pixels in the omnibox dropdown border image interior to
139 // the actual border.
140 const int kOmniboxDropdownBorderInterior[] = {6, 0, 0};
142 // In an omnibox dropdown row, the minimum distance between the top and
143 // bottom of the row's icon and the top or bottom of the row edge.
144 const int kOmniboxDropdownMinIconVerticalPadding[] = {2, 4, 8};
146 // In an omnibox dropdown row, the minimum distance between the top and
147 // bottom of the row's text and the top or bottom of the row edge.
148 const int kOmniboxDropdownMinTextVerticalPadding[] = {3, 4, 8};
150 // The spacing between a ToolbarButton's image and its border.
151 const int kToolbarButtonBorderInset[] = {2, 6, 6};
153 // Non-ash uses a rounded content area with no shadow in the assets.
154 const int kToolbarViewContentShadowHeight[] = {0, 0, 0};
156 // Ash doesn't use a rounded content area and its top edge has an extra shadow.
157 const int kToolbarViewContentShadowHeightAsh[] = {2, 0, 0};
159 // Additional horizontal padding between the elements in the toolbar.
160 const int kToolbarViewElementPadding[] = {0, 0, 8};
162 // Padding between the right-edge of the location bar and browser actions.
163 const int kToolbarViewLocationBarRightPadding[] = {0, 4, 8};
165 // The edge graphics have some built-in spacing/shadowing, so we have to adjust
166 // our spacing to make it match.
167 const int kToolbarViewLeftEdgeSpacing[] = {3, 4, 8};
168 const int kToolbarViewRightEdgeSpacing[] = {2, 4, 8};
170 // The horizontal space between most items.
171 const int kToolbarViewStandardSpacing[] = {3, 4, 8};
173 // The minimal vertical padding of the toolbar.
174 const int kToolbarViewVerticalPadding[] = {5, 4, 4};
176 // ----------------------------------------------------------------------------
178 // Strings used in alignment properties.
179 const char kAlignmentCenter[] = "center";
180 const char kAlignmentTop[] = "top";
181 const char kAlignmentBottom[] = "bottom";
182 const char kAlignmentLeft[] = "left";
183 const char kAlignmentRight[] = "right";
185 // Strings used in background tiling repetition properties.
186 const char kTilingNoRepeat[] = "no-repeat";
187 const char kTilingRepeatX[] = "repeat-x";
188 const char kTilingRepeatY[] = "repeat-y";
189 const char kTilingRepeat[] = "repeat";
191 // The image resources that will be tinted by the 'button' tint value.
192 // If you change this list, you must increment the version number in
193 // browser_theme_pack.cc, and you should assign persistent IDs to the
194 // data table at the start of said file or else tinted versions of
195 // these resources will not be created.
197 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
198 // should be removed once the cocoa port no longer uses them.
199 const int kToolbarButtonIDs[] = {
200 IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P,
201 IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P,
202 IDR_HOME, IDR_HOME_H, IDR_HOME_P,
203 IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P,
204 IDR_STOP, IDR_STOP_D, IDR_STOP_H, IDR_STOP_P,
205 IDR_BROWSER_ACTIONS_OVERFLOW, IDR_BROWSER_ACTIONS_OVERFLOW_H,
206 IDR_BROWSER_ACTIONS_OVERFLOW_P,
207 IDR_TOOLS, IDR_TOOLS_H, IDR_TOOLS_P,
208 IDR_MENU_DROPARROW,
209 IDR_TOOLBAR_BEZEL_HOVER, IDR_TOOLBAR_BEZEL_PRESSED, IDR_TOOLS_BAR,
212 SkColor TintForUnderline(SkColor input) {
213 return SkColorSetA(input, SkColorGetA(input) / 3);
216 } // namespace
218 // static
219 int ThemeProperties::StringToAlignment(const std::string& alignment) {
220 int alignment_mask = 0;
221 for (const std::string& component : base::SplitString(
222 alignment, base::kWhitespaceASCII,
223 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
224 if (base::LowerCaseEqualsASCII(component, kAlignmentTop))
225 alignment_mask |= ALIGN_TOP;
226 else if (base::LowerCaseEqualsASCII(component, kAlignmentBottom))
227 alignment_mask |= ALIGN_BOTTOM;
228 else if (base::LowerCaseEqualsASCII(component, kAlignmentLeft))
229 alignment_mask |= ALIGN_LEFT;
230 else if (base::LowerCaseEqualsASCII(component, kAlignmentRight))
231 alignment_mask |= ALIGN_RIGHT;
233 return alignment_mask;
236 // static
237 int ThemeProperties::StringToTiling(const std::string& tiling) {
238 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeatX))
239 return REPEAT_X;
240 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeatY))
241 return REPEAT_Y;
242 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeat))
243 return REPEAT;
244 // NO_REPEAT is the default choice.
245 return NO_REPEAT;
248 // static
249 std::string ThemeProperties::AlignmentToString(int alignment) {
250 // Convert from an AlignmentProperty back into a string.
251 std::string vertical_string(kAlignmentCenter);
252 std::string horizontal_string(kAlignmentCenter);
254 if (alignment & ALIGN_TOP)
255 vertical_string = kAlignmentTop;
256 else if (alignment & ALIGN_BOTTOM)
257 vertical_string = kAlignmentBottom;
259 if (alignment & ALIGN_LEFT)
260 horizontal_string = kAlignmentLeft;
261 else if (alignment & ALIGN_RIGHT)
262 horizontal_string = kAlignmentRight;
264 return horizontal_string + " " + vertical_string;
267 // static
268 std::string ThemeProperties::TilingToString(int tiling) {
269 // Convert from a TilingProperty back into a string.
270 if (tiling == REPEAT_X)
271 return kTilingRepeatX;
272 if (tiling == REPEAT_Y)
273 return kTilingRepeatY;
274 if (tiling == REPEAT)
275 return kTilingRepeat;
276 return kTilingNoRepeat;
279 // static
280 const std::set<int>& ThemeProperties::GetTintableToolbarButtons() {
281 CR_DEFINE_STATIC_LOCAL(std::set<int>, button_set, ());
282 if (button_set.empty()) {
283 button_set = std::set<int>(
284 kToolbarButtonIDs,
285 kToolbarButtonIDs + arraysize(kToolbarButtonIDs));
288 return button_set;
291 // static
292 color_utils::HSL ThemeProperties::GetDefaultTint(int id) {
293 switch (id) {
294 case TINT_FRAME:
295 return kDefaultTintFrame;
296 case TINT_FRAME_INACTIVE:
297 return kDefaultTintFrameInactive;
298 case TINT_FRAME_INCOGNITO:
299 return kDefaultTintFrameIncognito;
300 case TINT_FRAME_INCOGNITO_INACTIVE:
301 return kDefaultTintFrameIncognitoInactive;
302 case TINT_BUTTONS:
303 return kDefaultTintButtons;
304 case TINT_BACKGROUND_TAB:
305 return kDefaultTintBackgroundTab;
306 default:
307 color_utils::HSL result = {-1, -1, -1};
308 return result;
312 // static
313 SkColor ThemeProperties::GetDefaultColor(int id) {
314 int mode = ui::MaterialDesignController::IsModeMaterial();
315 switch (id) {
316 // Properties stored in theme pack.
317 case COLOR_FRAME:
318 #if defined(OS_CHROMEOS)
319 return kDefaultColorFrame[mode];
320 #else
321 return kDefaultColorFrame;
322 #endif // OS_CHROMEOS
323 case COLOR_FRAME_INACTIVE:
324 #if defined(OS_CHROMEOS)
325 return kDefaultColorFrameInactive[mode];
326 #else
327 return kDefaultColorFrameInactive;
328 #endif // OS_CHROMEOS
329 case COLOR_FRAME_INCOGNITO:
330 return kDefaultColorFrameIncognito;
331 case COLOR_FRAME_INCOGNITO_INACTIVE:
332 return kDefaultColorFrameIncognitoInactive;
333 case COLOR_TOOLBAR:
334 #if defined(OS_MACOSX)
335 return kDefaultColorToolbar;
336 #else
337 return kDefaultColorToolbar[mode];
338 #endif // OS_MACOSX
339 case COLOR_TAB_TEXT:
340 return kDefaultColorTabText;
341 case COLOR_BACKGROUND_TAB_TEXT:
342 return kDefaultColorBackgroundTabText;
343 case COLOR_BOOKMARK_TEXT:
344 return kDefaultColorBookmarkText;
345 case COLOR_NTP_BACKGROUND:
346 return kDefaultColorNTPBackground;
347 case COLOR_NTP_TEXT:
348 return kDefaultColorNTPText;
349 case COLOR_NTP_LINK:
350 return kDefaultColorNTPLink;
351 case COLOR_NTP_LINK_UNDERLINE:
352 return TintForUnderline(kDefaultColorNTPLink);
353 case COLOR_NTP_HEADER:
354 return kDefaultColorNTPHeader;
355 case COLOR_NTP_SECTION:
356 return kDefaultColorNTPSection;
357 case COLOR_NTP_SECTION_TEXT:
358 return kDefaultColorNTPSectionText;
359 case COLOR_NTP_SECTION_LINK:
360 return kDefaultColorNTPSectionLink;
361 case COLOR_NTP_SECTION_LINK_UNDERLINE:
362 return TintForUnderline(kDefaultColorNTPSectionLink);
363 case COLOR_BUTTON_BACKGROUND:
364 return kDefaultColorButtonBackground;
366 // Properties not stored in theme pack.
367 case COLOR_CONTROL_BACKGROUND:
368 return kDefaultColorControlBackground;
369 case COLOR_TOOLBAR_SEPARATOR:
370 return kDefaultColorToolbarSeparator[mode];
371 #if defined(OS_MACOSX)
372 case COLOR_TOOLBAR_BUTTON_STROKE:
373 return kDefaultColorToolbarButtonStroke;
374 case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE:
375 return kDefaultColorToolbarButtonStrokeInactive;
376 case COLOR_TOOLBAR_BEZEL:
377 return kDefaultColorToolbarBezel;
378 case COLOR_TOOLBAR_STROKE:
379 return kDefaultColorToolbarStroke;
380 case COLOR_TOOLBAR_STROKE_INACTIVE:
381 return kDefaultColorToolbarStrokeInactive;
382 #endif
383 default:
384 // Return a debugging red color.
385 return SK_ColorRED;
389 // static
390 int ThemeProperties::GetDefaultDisplayProperty(int id) {
391 int mode = ui::MaterialDesignController::GetMode();
392 switch (id) {
393 case ThemeProperties::NTP_BACKGROUND_ALIGNMENT:
394 return kDefaultDisplayPropertyNTPAlignment;
395 case ThemeProperties::NTP_BACKGROUND_TILING:
396 return kDefaultDisplayPropertyNTPTiling;
397 case ThemeProperties::NTP_LOGO_ALTERNATE:
398 return kDefaultDisplayPropertyNTPAlternateLogo;
399 case ThemeProperties::PROPERTY_ICON_LABEL_VIEW_TRAILING_PADDING:
400 return kIconLabelViewTrailingPadding[mode];
401 case ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING:
402 return kLocationBarBubbleHorizontalPadding[mode];
403 case ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_VERTICAL_PADDING:
404 return kLocationBarBubbleVerticalPadding[mode];
405 case ThemeProperties::PROPERTY_LOCATION_BAR_HEIGHT:
406 return kLocationBarHeight[mode];
407 case ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING:
408 return kLocationBarHorizontalPadding[mode];
409 case ThemeProperties::PROPERTY_LOCATION_BAR_VERTICAL_PADDING:
410 return kLocationBarVerticalPadding[mode];
411 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR:
412 return kOmniboxDropdownBorderInterior[mode];
413 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_ICON_VERTICAL_PADDING:
414 return kOmniboxDropdownMinIconVerticalPadding[mode];
415 case ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING:
416 return kOmniboxDropdownMinTextVerticalPadding[mode];
417 case ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET:
418 return kToolbarButtonBorderInset[mode];
419 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT:
420 return kToolbarViewContentShadowHeight[mode];
421 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_CONTENT_SHADOW_HEIGHT_ASH:
422 return kToolbarViewContentShadowHeightAsh[mode];
423 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_ELEMENT_PADDING:
424 return kToolbarViewElementPadding[mode];
425 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_LEFT_EDGE_SPACING:
426 return kToolbarViewLeftEdgeSpacing[mode];
427 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_LOCATION_BAR_RIGHT_PADDING:
428 return kToolbarViewLocationBarRightPadding[mode];
429 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_RIGHT_EDGE_SPACING:
430 return kToolbarViewRightEdgeSpacing[mode];
431 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_STANDARD_SPACING:
432 return kToolbarViewStandardSpacing[mode];
433 case ThemeProperties::PROPERTY_TOOLBAR_VIEW_VERTICAL_PADDING:
434 return kToolbarViewVerticalPadding[mode];
435 default:
436 return -1;