| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browser_theme_provider.h" | 5 #include "chrome/browser/browser_theme_provider.h" |
| 6 | 6 |
| 7 #include "base/gfx/png_decoder.h" | 7 #include "base/gfx/png_decoder.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 static const char* kColorFrameIncognito = "frame_incognito"; | 32 static const char* kColorFrameIncognito = "frame_incognito"; |
| 33 static const char* kColorFrameIncognitoInactive = | 33 static const char* kColorFrameIncognitoInactive = |
| 34 "frame_incognito_inactive"; | 34 "frame_incognito_inactive"; |
| 35 static const char* kColorToolbar = "toolbar"; | 35 static const char* kColorToolbar = "toolbar"; |
| 36 static const char* kColorTabText = "tab_text"; | 36 static const char* kColorTabText = "tab_text"; |
| 37 static const char* kColorBackgroundTabText = "background_tab_text"; | 37 static const char* kColorBackgroundTabText = "background_tab_text"; |
| 38 static const char* kColorBookmarkText = "bookmark_text"; | 38 static const char* kColorBookmarkText = "bookmark_text"; |
| 39 static const char* kColorNTPText = "ntp_text"; | 39 static const char* kColorNTPText = "ntp_text"; |
| 40 static const char* kColorNTPLink = "ntp_link"; | 40 static const char* kColorNTPLink = "ntp_link"; |
| 41 static const char* kColorNTPSection = "ntp_section"; | 41 static const char* kColorNTPSection = "ntp_section"; |
| 42 static const char* kColorControlBackground = "control_background"; |
| 43 static const char* kColorButtonBackground = "button_background"; |
| 42 | 44 |
| 43 // Strings used by themes to identify tints to apply to different parts of | 45 // Strings used by themes to identify tints to apply to different parts of |
| 44 // our UI. The frame tints apply to the frame color and produce the | 46 // our UI. The frame tints apply to the frame color and produce the |
| 45 // COLOR_FRAME* colors. | 47 // COLOR_FRAME* colors. |
| 46 static const char* kTintButtons = "buttons"; | 48 static const char* kTintButtons = "buttons"; |
| 47 static const char* kTintFrame = "frame"; | 49 static const char* kTintFrame = "frame"; |
| 48 static const char* kTintFrameInactive = "frame_inactive"; | 50 static const char* kTintFrameInactive = "frame_inactive"; |
| 49 static const char* kTintFrameIncognito = "frame_incognito"; | 51 static const char* kTintFrameIncognito = "frame_incognito"; |
| 50 static const char* kTintFrameIncognitoInactive = | 52 static const char* kTintFrameIncognitoInactive = |
| 51 "frame_incognito_inactive"; | 53 "frame_incognito_inactive"; |
| 52 static const char* kTintBackgroundTab = "background_tab"; | 54 static const char* kTintBackgroundTab = "background_tab"; |
| 53 | 55 |
| 54 // Default colors. | 56 // Default colors. |
| 55 static const SkColor kDefaultColorFrame = SkColorSetRGB(77, 139, 217); | 57 static const SkColor kDefaultColorFrame = SkColorSetRGB(77, 139, 217); |
| 56 static const SkColor kDefaultColorFrameInactive = SkColorSetRGB(152, 188, 233); | 58 static const SkColor kDefaultColorFrameInactive = SkColorSetRGB(152, 188, 233); |
| 57 static const SkColor kDefaultColorFrameIncognito = SkColorSetRGB(83, 106, 139); | 59 static const SkColor kDefaultColorFrameIncognito = SkColorSetRGB(83, 106, 139); |
| 58 static const SkColor kDefaultColorFrameIncognitoInactive = | 60 static const SkColor kDefaultColorFrameIncognitoInactive = |
| 59 SkColorSetRGB(126, 139, 156); | 61 SkColorSetRGB(126, 139, 156); |
| 60 static const SkColor kDefaultColorToolbar = SkColorSetRGB(210, 225, 246); | 62 static const SkColor kDefaultColorToolbar = SkColorSetRGB(210, 225, 246); |
| 61 static const SkColor kDefaultColorTabText = SkColorSetRGB(0, 0, 0); | 63 static const SkColor kDefaultColorTabText = SkColorSetRGB(0, 0, 0); |
| 62 static const SkColor kDefaultColorBackgroundTabText = SkColorSetRGB(64, 64, 64); | 64 static const SkColor kDefaultColorBackgroundTabText = SkColorSetRGB(64, 64, 64); |
| 63 static const SkColor kDefaultColorBookmarkText = SkColorSetRGB(64, 64, 64); | 65 static const SkColor kDefaultColorBookmarkText = SkColorSetRGB(64, 64, 64); |
| 64 static const SkColor kDefaultColorNTPText = SkColorSetRGB(0, 0, 0); | 66 static const SkColor kDefaultColorNTPText = SkColorSetRGB(0, 0, 0); |
| 65 static const SkColor kDefaultColorNTPLink = SkColorSetRGB(0, 0, 204); | 67 static const SkColor kDefaultColorNTPLink = SkColorSetRGB(0, 0, 204); |
| 66 static const SkColor kDefaultColorNTPSection = SkColorSetRGB(225, 236, 254); | 68 static const SkColor kDefaultColorNTPSection = SkColorSetRGB(225, 236, 254); |
| 69 static const SkColor kDefaultColorControlBackground = NULL; |
| 70 static const SkColor kDefaultColorButtonBackground = NULL; |
| 67 | 71 |
| 68 // Default tints. | 72 // Default tints. |
| 69 static const skia::HSL kDefaultTintButtons = { -1, -1, -1 }; | 73 static const skia::HSL kDefaultTintButtons = { -1, -1, -1 }; |
| 70 static const skia::HSL kDefaultTintFrame = { -1, -1, -1 }; | 74 static const skia::HSL kDefaultTintFrame = { -1, -1, -1 }; |
| 71 static const skia::HSL kDefaultTintFrameInactive = { -1, 0.5f, 0.72f }; | 75 static const skia::HSL kDefaultTintFrameInactive = { -1, 0.5f, 0.72f }; |
| 72 static const skia::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; | 76 static const skia::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; |
| 73 static const skia::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; | 77 static const skia::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; |
| 74 static const skia::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 }; | 78 static const skia::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 }; |
| 75 | 79 |
| 76 // The image resources that will be tinted by the 'button' tint value. | 80 // The image resources that will be tinted by the 'button' tint value. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 return NULL; | 171 return NULL; |
| 168 } | 172 } |
| 169 } | 173 } |
| 170 | 174 |
| 171 SkColor BrowserThemeProvider::GetColor(int id) { | 175 SkColor BrowserThemeProvider::GetColor(int id) { |
| 172 DCHECK(CalledOnValidThread()); | 176 DCHECK(CalledOnValidThread()); |
| 173 | 177 |
| 174 // TODO(glen): Figure out if we need to tint these. http://crbug.com/11578 | 178 // TODO(glen): Figure out if we need to tint these. http://crbug.com/11578 |
| 175 switch (id) { | 179 switch (id) { |
| 176 case COLOR_FRAME: | 180 case COLOR_FRAME: |
| 177 return (colors_.find(kColorFrame) != colors_.end()) ? | 181 return FindColor(kColorFrame, kDefaultColorFrame); |
| 178 colors_[kColorFrame] : kDefaultColorFrame; | |
| 179 case COLOR_FRAME_INACTIVE: | 182 case COLOR_FRAME_INACTIVE: |
| 180 return (colors_.find(kColorFrameInactive) != colors_.end()) ? | 183 return FindColor(kColorFrameInactive, kDefaultColorFrameInactive); |
| 181 colors_[kColorFrameInactive] : kDefaultColorFrameInactive; | |
| 182 case COLOR_FRAME_INCOGNITO: | 184 case COLOR_FRAME_INCOGNITO: |
| 183 return (colors_.find(kColorFrameIncognito) != colors_.end()) ? | 185 return FindColor(kColorFrameIncognito, kDefaultColorFrameIncognito); |
| 184 colors_[kColorFrameIncognito] : kDefaultColorFrameIncognito; | |
| 185 case COLOR_FRAME_INCOGNITO_INACTIVE: | 186 case COLOR_FRAME_INCOGNITO_INACTIVE: |
| 186 return (colors_.find(kColorFrameIncognitoInactive) != colors_.end()) ? | 187 return FindColor(kColorFrameIncognitoInactive, |
| 187 colors_[kColorFrameIncognitoInactive] : | 188 kDefaultColorFrameIncognitoInactive); |
| 188 kDefaultColorFrameIncognitoInactive; | |
| 189 case COLOR_TOOLBAR: | 189 case COLOR_TOOLBAR: |
| 190 return (colors_.find(kColorToolbar) != colors_.end()) ? | 190 return FindColor(kColorToolbar, kDefaultColorToolbar); |
| 191 colors_[kColorToolbar] : kDefaultColorToolbar; | |
| 192 case COLOR_TAB_TEXT: | 191 case COLOR_TAB_TEXT: |
| 193 return (colors_.find(kColorTabText) != colors_.end()) ? | 192 return FindColor(kColorTabText, kDefaultColorTabText); |
| 194 colors_[kColorTabText] : kDefaultColorTabText; | |
| 195 case COLOR_BACKGROUND_TAB_TEXT: | 193 case COLOR_BACKGROUND_TAB_TEXT: |
| 196 return (colors_.find(kColorBackgroundTabText) != colors_.end()) ? | 194 return FindColor(kColorBackgroundTabText, kDefaultColorBackgroundTabText); |
| 197 colors_[kColorBackgroundTabText] : | |
| 198 kDefaultColorBackgroundTabText; | |
| 199 case COLOR_BOOKMARK_TEXT: | 195 case COLOR_BOOKMARK_TEXT: |
| 200 return (colors_.find(kColorBookmarkText) != colors_.end()) ? | 196 return FindColor(kColorBookmarkText, kDefaultColorBookmarkText); |
| 201 colors_[kColorBookmarkText] : | |
| 202 kDefaultColorBookmarkText; | |
| 203 case COLOR_NTP_TEXT: | 197 case COLOR_NTP_TEXT: |
| 204 return (colors_.find(kColorNTPText) != colors_.end()) ? | 198 return FindColor(kColorNTPText, kDefaultColorNTPText); |
| 205 colors_[kColorNTPText] : | |
| 206 kDefaultColorNTPText; | |
| 207 case COLOR_NTP_LINK: | 199 case COLOR_NTP_LINK: |
| 208 return (colors_.find(kColorNTPLink) != colors_.end()) ? | 200 return FindColor(kColorNTPLink, kDefaultColorNTPLink); |
| 209 colors_[kColorNTPLink] : | |
| 210 kDefaultColorNTPLink; | |
| 211 case COLOR_NTP_SECTION: | 201 case COLOR_NTP_SECTION: |
| 212 return (colors_.find(kColorNTPSection) != colors_.end()) ? | 202 return FindColor(kColorNTPSection, kDefaultColorNTPSection); |
| 213 colors_[kColorNTPSection] : | 203 case COLOR_CONTROL_BACKGROUND: |
| 214 kDefaultColorNTPSection; | 204 return FindColor(kColorControlBackground, kDefaultColorControlBackground); |
| 205 case COLOR_BUTTON_BACKGROUND: |
| 206 return FindColor(kColorButtonBackground, kDefaultColorButtonBackground); |
| 215 default: | 207 default: |
| 216 NOTREACHED() << "Unknown color requested"; | 208 NOTREACHED() << "Unknown color requested"; |
| 217 } | 209 } |
| 218 | 210 |
| 219 // Return a debugging red color. | 211 // Return a debugging red color. |
| 220 return 0xffff0000; | 212 return 0xffff0000; |
| 221 } | 213 } |
| 222 | 214 |
| 223 bool BrowserThemeProvider::ShouldUseNativeFrame() { | 215 bool BrowserThemeProvider::ShouldUseNativeFrame() { |
| 224 if (images_.find(IDR_THEME_FRAME) != images_.end()) | 216 if (images_.find(IDR_THEME_FRAME) != images_.end()) |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 368 } |
| 377 | 369 |
| 378 void BrowserThemeProvider::SetColorData(DictionaryValue* colors_value) { | 370 void BrowserThemeProvider::SetColorData(DictionaryValue* colors_value) { |
| 379 colors_.clear(); | 371 colors_.clear(); |
| 380 | 372 |
| 381 if (colors_value) { | 373 if (colors_value) { |
| 382 DictionaryValue::key_iterator iter = colors_value->begin_keys(); | 374 DictionaryValue::key_iterator iter = colors_value->begin_keys(); |
| 383 while (iter != colors_value->end_keys()) { | 375 while (iter != colors_value->end_keys()) { |
| 384 ListValue* color_list; | 376 ListValue* color_list; |
| 385 if (colors_value->GetList(*iter, &color_list) && | 377 if (colors_value->GetList(*iter, &color_list) && |
| 386 color_list->GetSize() == 3) { | 378 (color_list->GetSize() == 3 || color_list->GetSize() == 4)) { |
| 387 int r, g, b; | 379 int r, g, b; |
| 388 color_list->GetInteger(0, &r); | 380 color_list->GetInteger(0, &r); |
| 389 color_list->GetInteger(1, &g); | 381 color_list->GetInteger(1, &g); |
| 390 color_list->GetInteger(2, &b); | 382 color_list->GetInteger(2, &b); |
| 391 colors_[WideToUTF8(*iter)] = SkColorSetRGB(r, g, b); | 383 if (color_list->GetSize() == 4) { |
| 384 double alpha; |
| 385 color_list->GetReal(3, &alpha); |
| 386 colors_[WideToUTF8(*iter)] = SkColorSetARGB( |
| 387 static_cast<int>(alpha * 255), r, g, b); |
| 388 } else { |
| 389 colors_[WideToUTF8(*iter)] = SkColorSetRGB(r, g, b); |
| 390 } |
| 392 } | 391 } |
| 393 ++iter; | 392 ++iter; |
| 394 } | 393 } |
| 395 } | 394 } |
| 396 } | 395 } |
| 397 | 396 |
| 398 void BrowserThemeProvider::SetTintData(DictionaryValue* tints_value) { | 397 void BrowserThemeProvider::SetTintData(DictionaryValue* tints_value) { |
| 399 tints_.clear(); | 398 tints_.clear(); |
| 400 | 399 |
| 401 if (tints_value) { | 400 if (tints_value) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 SkBitmap* bg_tab = | 486 SkBitmap* bg_tab = |
| 488 new SkBitmap(TintBitmap(blurred, TINT_BACKGROUND_TAB)); | 487 new SkBitmap(TintBitmap(blurred, TINT_BACKGROUND_TAB)); |
| 489 generated_images_.push_back(bg_tab); | 488 generated_images_.push_back(bg_tab); |
| 490 return bg_tab; | 489 return bg_tab; |
| 491 } | 490 } |
| 492 } | 491 } |
| 493 | 492 |
| 494 return NULL; | 493 return NULL; |
| 495 } | 494 } |
| 496 | 495 |
| 497 void BrowserThemeProvider::NotifyThemeChanged() { | |
| 498 // TODO(glen): If we're in glass and IDR_THEME_FRAME has been provided, | |
| 499 // swap us back to opaque frame. | |
| 500 | |
| 501 // Redraw! | |
| 502 for (BrowserList::const_iterator browser = BrowserList::begin(); | |
| 503 browser != BrowserList::end(); ++browser) { | |
| 504 (*browser)->window()->UserChangedTheme(); | |
| 505 } | |
| 506 } | |
| 507 | |
| 508 void BrowserThemeProvider::LoadThemePrefs() { | |
| 509 PrefService* prefs = profile_->GetPrefs(); | |
| 510 | |
| 511 // TODO(glen): Figure out if any custom prefs were loaded, and if so | |
| 512 // UMA-log the fact that a theme was loaded. | |
| 513 if (prefs->HasPrefPath(prefs::kCurrentThemeImages) || | |
| 514 prefs->HasPrefPath(prefs::kCurrentThemeColors) || | |
| 515 prefs->HasPrefPath(prefs::kCurrentThemeTints)) { | |
| 516 // Our prefs already have the extension path baked in, so we don't need | |
| 517 // to provide it. | |
| 518 SetImageData(prefs->GetMutableDictionary(prefs::kCurrentThemeImages), | |
| 519 FilePath()); | |
| 520 SetColorData(prefs->GetMutableDictionary(prefs::kCurrentThemeColors)); | |
| 521 SetTintData(prefs->GetMutableDictionary(prefs::kCurrentThemeTints)); | |
| 522 GenerateFrameColors(); | |
| 523 GenerateFrameImages(); | |
| 524 UserMetrics::RecordAction(L"Themes_loaded", profile_); | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 void BrowserThemeProvider::SaveImageData(DictionaryValue* images_value) { | 496 void BrowserThemeProvider::SaveImageData(DictionaryValue* images_value) { |
| 529 // Save our images data. | 497 // Save our images data. |
| 530 DictionaryValue* pref_images = | 498 DictionaryValue* pref_images = |
| 531 profile_->GetPrefs()->GetMutableDictionary(prefs::kCurrentThemeImages); | 499 profile_->GetPrefs()->GetMutableDictionary(prefs::kCurrentThemeImages); |
| 532 pref_images->Clear(); | 500 pref_images->Clear(); |
| 533 | 501 |
| 534 if (images_value) { | 502 if (images_value) { |
| 535 DictionaryValue::key_iterator iter = images_value->begin_keys(); | 503 DictionaryValue::key_iterator iter = images_value->begin_keys(); |
| 536 while (iter != images_value->end_keys()) { | 504 while (iter != images_value->end_keys()) { |
| 537 std::string val; | 505 std::string val; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 ListValue* hsl_list = new ListValue(); | 544 ListValue* hsl_list = new ListValue(); |
| 577 hsl_list->Set(0, Value::CreateRealValue(hsl.h)); | 545 hsl_list->Set(0, Value::CreateRealValue(hsl.h)); |
| 578 hsl_list->Set(1, Value::CreateRealValue(hsl.s)); | 546 hsl_list->Set(1, Value::CreateRealValue(hsl.s)); |
| 579 hsl_list->Set(2, Value::CreateRealValue(hsl.l)); | 547 hsl_list->Set(2, Value::CreateRealValue(hsl.l)); |
| 580 pref_tints->Set(UTF8ToWide((*iter).first), hsl_list); | 548 pref_tints->Set(UTF8ToWide((*iter).first), hsl_list); |
| 581 ++iter; | 549 ++iter; |
| 582 } | 550 } |
| 583 } | 551 } |
| 584 } | 552 } |
| 585 | 553 |
| 554 void BrowserThemeProvider::NotifyThemeChanged() { |
| 555 // TODO(glen): If we're in glass and IDR_THEME_FRAME has been provided, |
| 556 // swap us back to opaque frame. |
| 557 |
| 558 // Redraw! |
| 559 for (BrowserList::const_iterator browser = BrowserList::begin(); |
| 560 browser != BrowserList::end(); ++browser) { |
| 561 (*browser)->window()->UserChangedTheme(); |
| 562 } |
| 563 } |
| 564 |
| 565 void BrowserThemeProvider::LoadThemePrefs() { |
| 566 PrefService* prefs = profile_->GetPrefs(); |
| 567 |
| 568 // TODO(glen): Figure out if any custom prefs were loaded, and if so |
| 569 // UMA-log the fact that a theme was loaded. |
| 570 if (prefs->HasPrefPath(prefs::kCurrentThemeImages) || |
| 571 prefs->HasPrefPath(prefs::kCurrentThemeColors) || |
| 572 prefs->HasPrefPath(prefs::kCurrentThemeTints)) { |
| 573 // Our prefs already have the extension path baked in, so we don't need |
| 574 // to provide it. |
| 575 SetImageData(prefs->GetMutableDictionary(prefs::kCurrentThemeImages), |
| 576 FilePath()); |
| 577 SetColorData(prefs->GetMutableDictionary(prefs::kCurrentThemeColors)); |
| 578 SetTintData(prefs->GetMutableDictionary(prefs::kCurrentThemeTints)); |
| 579 GenerateFrameColors(); |
| 580 GenerateFrameImages(); |
| 581 UserMetrics::RecordAction(L"Themes_loaded", profile_); |
| 582 } |
| 583 } |
| 584 |
| 585 SkColor BrowserThemeProvider::FindColor(const char* id, |
| 586 SkColor default_color) { |
| 587 return (colors_.find(id) != colors_.end()) ? colors_[id] : default_color; |
| 588 } |
| 589 |
| 586 void BrowserThemeProvider::FreeImages() { | 590 void BrowserThemeProvider::FreeImages() { |
| 587 for (std::vector<SkBitmap*>::iterator i = generated_images_.begin(); | 591 for (std::vector<SkBitmap*>::iterator i = generated_images_.begin(); |
| 588 i != generated_images_.end(); i++) { | 592 i != generated_images_.end(); i++) { |
| 589 delete *i; | 593 delete *i; |
| 590 } | 594 } |
| 591 generated_images_.clear(); | 595 generated_images_.clear(); |
| 592 image_cache_.clear(); | 596 image_cache_.clear(); |
| 593 } | 597 } |
| OLD | NEW |