[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/themes/theme_properties.h" |
| 6 | |
| 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 8 | #include "base/strings/string_split.h" |
[email protected] | 9f0abdb | 2013-06-10 21:49:34 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 10 | #include "chrome/browser/themes/browser_theme_pack.h" |
| 11 | #include "grit/theme_resources.h" |
| 12 | #include "grit/ui_resources.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // Defaults for properties which are stored in the browser theme pack. If you |
| 18 | // change these defaults, you must increment the version number in |
| 19 | // browser_theme_pack.h |
| 20 | |
| 21 | // Default colors. |
| 22 | #if defined(USE_AURA) |
| 23 | // Used for theme fallback colors. |
| 24 | const SkColor kDefaultColorFrame = SkColorSetRGB(109, 109, 109); |
| 25 | const SkColor kDefaultColorFrameInactive = SkColorSetRGB(176, 176, 176); |
| 26 | #else |
| 27 | const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201); |
| 28 | const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228); |
| 29 | #endif // USE_AURA |
| 30 | const SkColor kDefaultColorFrameIncognito = SkColorSetRGB(83, 106, 139); |
| 31 | const SkColor kDefaultColorFrameIncognitoInactive = |
| 32 | SkColorSetRGB(126, 139, 156); |
[email protected] | 768c0ff | 2013-06-21 02:50:55 | [diff] [blame^] | 33 | const SkColor kDefaultColorFrameManagedUser = SkColorSetRGB(165, 197, 225); |
| 34 | const SkColor kDefaultColorFrameManagedUserInactive = |
| 35 | SkColorSetRGB(180, 225, 247); |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 36 | #if defined(OS_MACOSX) |
| 37 | const SkColor kDefaultColorToolbar = SkColorSetRGB(230, 230, 230); |
| 38 | #else |
| 39 | const SkColor kDefaultColorToolbar = SkColorSetRGB(223, 223, 223); |
| 40 | #endif |
| 41 | const SkColor kDefaultColorTabText = SK_ColorBLACK; |
| 42 | #if defined(OS_MACOSX) |
| 43 | const SkColor kDefaultColorBackgroundTabText = SK_ColorBLACK; |
| 44 | #else |
| 45 | const SkColor kDefaultColorBackgroundTabText = SkColorSetRGB(64, 64, 64); |
| 46 | #endif |
| 47 | const SkColor kDefaultColorBookmarkText = SK_ColorBLACK; |
| 48 | #if defined(OS_WIN) |
| 49 | const SkColor kDefaultColorNTPBackground = |
| 50 | color_utils::GetSysSkColor(COLOR_WINDOW); |
| 51 | const SkColor kDefaultColorNTPText = |
| 52 | color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
| 53 | const SkColor kDefaultColorNTPLink = |
| 54 | color_utils::GetSysSkColor(COLOR_HOTLIGHT); |
| 55 | #else |
| 56 | // TODO(beng): source from theme provider. |
| 57 | const SkColor kDefaultColorNTPBackground = SK_ColorWHITE; |
| 58 | const SkColor kDefaultColorNTPText = SK_ColorBLACK; |
| 59 | const SkColor kDefaultColorNTPLink = SkColorSetRGB(6, 55, 116); |
| 60 | #endif |
| 61 | const SkColor kDefaultColorNTPHeader = SkColorSetRGB(150, 150, 150); |
| 62 | const SkColor kDefaultColorNTPSection = SkColorSetRGB(229, 229, 229); |
| 63 | const SkColor kDefaultColorNTPSectionText = SK_ColorBLACK; |
| 64 | const SkColor kDefaultColorNTPSectionLink = SkColorSetRGB(6, 55, 116); |
| 65 | const SkColor kDefaultColorButtonBackground = SkColorSetARGB(0, 0, 0, 0); |
| 66 | |
| 67 | // Default tints. |
| 68 | const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 }; |
| 69 | const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 }; |
| 70 | const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f }; |
| 71 | const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; |
| 72 | const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; |
| 73 | const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 }; |
| 74 | |
| 75 | // Default display properties. |
| 76 | const int kDefaultDisplayPropertyNTPAlignment = |
| 77 | ThemeProperties::ALIGN_BOTTOM; |
| 78 | const int kDefaultDisplayPropertyNTPTiling = |
| 79 | ThemeProperties::NO_REPEAT; |
| 80 | const int kDefaultDisplayPropertyNTPInverseLogo = 0; |
| 81 | |
| 82 | // ---------------------------------------------------------------------------- |
| 83 | // Defaults for properties which are not stored in the browser theme pack. |
| 84 | |
| 85 | const SkColor kDefaultColorControlBackground = SK_ColorWHITE; |
| 86 | const SkColor kDefaultColorToolbarSeparator = SkColorSetRGB(170, 170, 171); |
| 87 | |
| 88 | #if defined(OS_MACOSX) |
| 89 | const SkColor kDefaultColorToolbarButtonStroke = SkColorSetARGB(75, 81, 81, 81); |
| 90 | const SkColor kDefaultColorToolbarButtonStrokeInactive = |
| 91 | SkColorSetARGB(75, 99, 99, 99); |
| 92 | const SkColor kDefaultColorToolbarBezel = SkColorSetRGB(204, 204, 204); |
| 93 | const SkColor kDefaultColorToolbarStroke = SkColorSetRGB(103, 103, 103); |
| 94 | const SkColor kDefaultColorToolbarStrokeInactive = SkColorSetRGB(163, 163, 163); |
| 95 | #endif |
| 96 | |
| 97 | // ---------------------------------------------------------------------------- |
| 98 | |
| 99 | // Strings used in alignment properties. |
| 100 | const char* kAlignmentCenter = "center"; |
| 101 | const char* kAlignmentTop = "top"; |
| 102 | const char* kAlignmentBottom = "bottom"; |
| 103 | const char* kAlignmentLeft = "left"; |
| 104 | const char* kAlignmentRight = "right"; |
| 105 | |
| 106 | // Strings used in background tiling repetition properties. |
| 107 | const char* kTilingNoRepeat = "no-repeat"; |
| 108 | const char* kTilingRepeatX = "repeat-x"; |
| 109 | const char* kTilingRepeatY = "repeat-y"; |
| 110 | const char* kTilingRepeat = "repeat"; |
| 111 | |
| 112 | // The image resources that will be tinted by the 'button' tint value. |
| 113 | // If you change this list, you must increment the version number in |
| 114 | // browser_theme_pack.cc, and you should assign persistent IDs to the |
| 115 | // data table at the start of said file or else tinted versions of |
| 116 | // these resources will not be created. |
| 117 | const int kToolbarButtonIDs[] = { |
| 118 | IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P, |
| 119 | IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P, |
| 120 | IDR_HOME, IDR_HOME_H, IDR_HOME_P, |
| 121 | IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P, |
| 122 | IDR_STOP, IDR_STOP_D, IDR_STOP_H, IDR_STOP_P, |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 123 | IDR_BROWSER_ACTIONS_OVERFLOW, IDR_BROWSER_ACTIONS_OVERFLOW_H, |
| 124 | IDR_BROWSER_ACTIONS_OVERFLOW_P, |
| 125 | IDR_TOOLS, IDR_TOOLS_H, IDR_TOOLS_P, |
| 126 | IDR_MENU_DROPARROW, |
| 127 | IDR_THROBBER, IDR_THROBBER_WAITING, IDR_THROBBER_LIGHT, |
[email protected] | 19205e9 | 2013-04-02 17:12:24 | [diff] [blame] | 128 | IDR_TOOLBAR_BEZEL_HOVER, IDR_TOOLBAR_BEZEL_PRESSED, IDR_TOOLS_BAR, |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | SkColor TintForUnderline(SkColor input) { |
| 132 | return SkColorSetA(input, SkColorGetA(input) / 3); |
| 133 | } |
| 134 | |
| 135 | } // namespace |
| 136 | |
| 137 | // static |
| 138 | int ThemeProperties::StringToAlignment(const std::string& alignment) { |
| 139 | std::vector<std::string> split; |
| 140 | base::SplitStringAlongWhitespace(alignment, &split); |
| 141 | |
| 142 | int alignment_mask = 0; |
| 143 | for (std::vector<std::string>::iterator component(split.begin()); |
| 144 | component != split.end(); ++component) { |
| 145 | if (LowerCaseEqualsASCII(*component, kAlignmentTop)) |
| 146 | alignment_mask |= ALIGN_TOP; |
| 147 | else if (LowerCaseEqualsASCII(*component, kAlignmentBottom)) |
| 148 | alignment_mask |= ALIGN_BOTTOM; |
| 149 | else if (LowerCaseEqualsASCII(*component, kAlignmentLeft)) |
| 150 | alignment_mask |= ALIGN_LEFT; |
| 151 | else if (LowerCaseEqualsASCII(*component, kAlignmentRight)) |
| 152 | alignment_mask |= ALIGN_RIGHT; |
| 153 | } |
| 154 | return alignment_mask; |
| 155 | } |
| 156 | |
| 157 | // static |
| 158 | int ThemeProperties::StringToTiling(const std::string& tiling) { |
| 159 | const char* component = tiling.c_str(); |
| 160 | |
| 161 | if (base::strcasecmp(component, kTilingRepeatX) == 0) |
| 162 | return REPEAT_X; |
| 163 | if (base::strcasecmp(component, kTilingRepeatY) == 0) |
| 164 | return REPEAT_Y; |
| 165 | if (base::strcasecmp(component, kTilingRepeat) == 0) |
| 166 | return REPEAT; |
| 167 | // NO_REPEAT is the default choice. |
| 168 | return NO_REPEAT; |
| 169 | } |
| 170 | |
| 171 | // static |
| 172 | std::string ThemeProperties::AlignmentToString(int alignment) { |
| 173 | // Convert from an AlignmentProperty back into a string. |
| 174 | std::string vertical_string(kAlignmentCenter); |
| 175 | std::string horizontal_string(kAlignmentCenter); |
| 176 | |
| 177 | if (alignment & ALIGN_TOP) |
| 178 | vertical_string = kAlignmentTop; |
| 179 | else if (alignment & ALIGN_BOTTOM) |
| 180 | vertical_string = kAlignmentBottom; |
| 181 | |
| 182 | if (alignment & ALIGN_LEFT) |
| 183 | horizontal_string = kAlignmentLeft; |
| 184 | else if (alignment & ALIGN_RIGHT) |
| 185 | horizontal_string = kAlignmentRight; |
| 186 | |
| 187 | return horizontal_string + " " + vertical_string; |
| 188 | } |
| 189 | |
| 190 | // static |
| 191 | std::string ThemeProperties::TilingToString(int tiling) { |
| 192 | // Convert from a TilingProperty back into a string. |
| 193 | if (tiling == REPEAT_X) |
| 194 | return kTilingRepeatX; |
| 195 | if (tiling == REPEAT_Y) |
| 196 | return kTilingRepeatY; |
| 197 | if (tiling == REPEAT) |
| 198 | return kTilingRepeat; |
| 199 | return kTilingNoRepeat; |
| 200 | } |
| 201 | |
| 202 | // static |
| 203 | bool ThemeProperties::IsThemeableImage(int id) { |
| 204 | // TODO(pkotwicz): Cache results to improve lookup speed. |
| 205 | std::set<int> themeable_idrs; |
| 206 | BrowserThemePack::GetThemeableImageIDRs(&themeable_idrs); |
| 207 | return themeable_idrs.find(id) != themeable_idrs.end(); |
| 208 | } |
| 209 | |
| 210 | // static |
| 211 | const std::set<int>& ThemeProperties::GetTintableToolbarButtons() { |
| 212 | CR_DEFINE_STATIC_LOCAL(std::set<int>, button_set, ()); |
| 213 | if (button_set.empty()) { |
| 214 | button_set = std::set<int>( |
| 215 | kToolbarButtonIDs, |
| 216 | kToolbarButtonIDs + arraysize(kToolbarButtonIDs)); |
| 217 | } |
| 218 | |
| 219 | return button_set; |
| 220 | } |
| 221 | |
| 222 | // static |
| 223 | color_utils::HSL ThemeProperties::GetDefaultTint(int id) { |
| 224 | switch (id) { |
| 225 | case TINT_FRAME: |
| 226 | return kDefaultTintFrame; |
| 227 | case TINT_FRAME_INACTIVE: |
| 228 | return kDefaultTintFrameInactive; |
| 229 | case TINT_FRAME_INCOGNITO: |
| 230 | return kDefaultTintFrameIncognito; |
| 231 | case TINT_FRAME_INCOGNITO_INACTIVE: |
| 232 | return kDefaultTintFrameIncognitoInactive; |
| 233 | case TINT_BUTTONS: |
| 234 | return kDefaultTintButtons; |
| 235 | case TINT_BACKGROUND_TAB: |
| 236 | return kDefaultTintBackgroundTab; |
| 237 | default: |
| 238 | color_utils::HSL result = {-1, -1, -1}; |
| 239 | return result; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // static |
| 244 | SkColor ThemeProperties::GetDefaultColor(int id) { |
| 245 | switch (id) { |
| 246 | // Properties stored in theme pack. |
| 247 | case COLOR_FRAME: |
| 248 | return kDefaultColorFrame; |
| 249 | case COLOR_FRAME_INACTIVE: |
| 250 | return kDefaultColorFrameInactive; |
| 251 | case COLOR_FRAME_INCOGNITO: |
| 252 | return kDefaultColorFrameIncognito; |
| 253 | case COLOR_FRAME_INCOGNITO_INACTIVE: |
| 254 | return kDefaultColorFrameIncognitoInactive; |
[email protected] | 768c0ff | 2013-06-21 02:50:55 | [diff] [blame^] | 255 | case COLOR_FRAME_MANAGED_USER: |
| 256 | return kDefaultColorFrameManagedUser; |
| 257 | case COLOR_FRAME_MANAGED_USER_INACTIVE: |
| 258 | return kDefaultColorFrameManagedUserInactive; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 259 | case COLOR_TOOLBAR: |
| 260 | return kDefaultColorToolbar; |
| 261 | case COLOR_TAB_TEXT: |
| 262 | return kDefaultColorTabText; |
| 263 | case COLOR_BACKGROUND_TAB_TEXT: |
| 264 | return kDefaultColorBackgroundTabText; |
| 265 | case COLOR_BOOKMARK_TEXT: |
| 266 | return kDefaultColorBookmarkText; |
| 267 | case COLOR_NTP_BACKGROUND: |
| 268 | return kDefaultColorNTPBackground; |
| 269 | case COLOR_NTP_TEXT: |
| 270 | return kDefaultColorNTPText; |
| 271 | case COLOR_NTP_LINK: |
| 272 | return kDefaultColorNTPLink; |
| 273 | case COLOR_NTP_LINK_UNDERLINE: |
| 274 | return TintForUnderline(kDefaultColorNTPLink); |
| 275 | case COLOR_NTP_HEADER: |
| 276 | return kDefaultColorNTPHeader; |
| 277 | case COLOR_NTP_SECTION: |
| 278 | return kDefaultColorNTPSection; |
| 279 | case COLOR_NTP_SECTION_TEXT: |
| 280 | return kDefaultColorNTPSectionText; |
| 281 | case COLOR_NTP_SECTION_LINK: |
| 282 | return kDefaultColorNTPSectionLink; |
| 283 | case COLOR_NTP_SECTION_LINK_UNDERLINE: |
| 284 | return TintForUnderline(kDefaultColorNTPSectionLink); |
| 285 | case COLOR_BUTTON_BACKGROUND: |
| 286 | return kDefaultColorButtonBackground; |
| 287 | |
| 288 | // Properties not stored in theme pack. |
| 289 | case COLOR_CONTROL_BACKGROUND: |
| 290 | return kDefaultColorControlBackground; |
| 291 | case COLOR_TOOLBAR_SEPARATOR: |
| 292 | return kDefaultColorToolbarSeparator; |
| 293 | #if defined(OS_MACOSX) |
| 294 | case COLOR_TOOLBAR_BUTTON_STROKE: |
| 295 | return kDefaultColorToolbarButtonStroke; |
| 296 | case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE: |
| 297 | return kDefaultColorToolbarButtonStrokeInactive; |
| 298 | case COLOR_TOOLBAR_BEZEL: |
| 299 | return kDefaultColorToolbarBezel; |
| 300 | case COLOR_TOOLBAR_STROKE: |
| 301 | return kDefaultColorToolbarStroke; |
| 302 | case COLOR_TOOLBAR_STROKE_INACTIVE: |
| 303 | return kDefaultColorToolbarStrokeInactive; |
| 304 | #endif |
| 305 | default: |
| 306 | // Return a debugging red color. |
| 307 | return SK_ColorRED; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // static |
| 312 | bool ThemeProperties::GetDefaultDisplayProperty(int id, int* result) { |
| 313 | switch (id) { |
| 314 | case NTP_BACKGROUND_ALIGNMENT: |
| 315 | *result = kDefaultDisplayPropertyNTPAlignment; |
| 316 | return true; |
| 317 | case NTP_BACKGROUND_TILING: |
| 318 | *result = kDefaultDisplayPropertyNTPTiling; |
| 319 | return true; |
| 320 | case NTP_LOGO_ALTERNATE: |
| 321 | *result = kDefaultDisplayPropertyNTPInverseLogo; |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | return false; |
| 326 | } |