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