summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/browser_instant_controller.cc
diff options
context:
space:
mode:
authorkuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-20 23:41:08 +0000
committerkuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-20 23:41:08 +0000
commita6827653d8da67355a7635a218a6078f0348c59c (patch)
treee9e8f3b3c57787aca0a277d6ba69ff4e987b741e /chrome/browser/ui/browser_instant_controller.cc
parent0bb840a863480bf30eaaa44492bd515b3fd8d50f (diff)
downloadchromium_src-a6827653d8da67355a7635a218a6078f0348c59c.zip
chromium_src-a6827653d8da67355a7635a218a6078f0348c59c.tar.gz
chromium_src-a6827653d8da67355a7635a218a6078f0348c59c.tar.bz2
alternate ntp: implement searchbox api for instant overlay to adopt themes
- add SearchBoxExtensionWrapper::GetThemeBackgroundInfo * returns theme background info: color, image url, image horizontal alignment, image vertical alignment, image tiling and image height * color is always filled, the rest are only filled if the theme has a custom background image that overlay needs to draw * should only be called when overlay is in ntp mode * when theme changes in ntp mode, BrowserInstantController caches the theme info; if mode is ntp, event is dispatched to onthemechanged callback, which should call GetThemeBackgroundInfo - add SearchBoxExtensionWrapper::GetThemeAreaHeight * returns the height of the area that the entire theme background image should fill up * should only be called when overlay is in ntp mode and theme background image is not top-aligned; top-aligned images are always painted from overlay's top-left corner, so this value is irrelevant and will be invalid * change of this height is monitored when BrowserView lays out its children, then updated and cached in BrowserInstantController; if above conditions are met, event is dispatched to onthemeareaheightchanged callback, which should call GetThemeAreaHeight - theme area height is also sent to preview when theme changes, 'cos the height may have changed with the previous theme but wasn't updated to preview - theme info and area height are also sent to preview when search mode changes to ntp, 'cos they may have changed during the previous mode but not updated to preview BUG=160957 TEST=the web page needs to be implemented before this can be manually tested Review URL: https://chromiumcodereview.appspot.com/11413018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168908 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/browser_instant_controller.cc')
-rw-r--r--chrome/browser/ui/browser_instant_controller.cc142
1 files changed, 141 insertions, 1 deletions
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc
index f8fd4f5..96c2755 100644
--- a/chrome/browser/ui/browser_instant_controller.cc
+++ b/chrome/browser/ui/browser_instant_controller.cc
@@ -8,6 +8,8 @@
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/themes/theme_service.h"
+#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
@@ -22,6 +24,10 @@
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
+#include "grit/theme_resources.h"
+#include "ui/gfx/color_utils.h"
+#include "ui/gfx/sys_color_change_listener.h"
+
namespace chrome {
@@ -32,11 +38,20 @@ BrowserInstantController::BrowserInstantController(Browser* browser)
: browser_(browser),
instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
chrome::search::IsInstantExtendedAPIEnabled(browser->profile())),
- instant_unload_handler_(browser) {
+ instant_unload_handler_(browser),
+ initialized_theme_info_(false),
+ theme_area_height_(0) {
profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile()));
browser_->search_model()->AddObserver(this);
+
+#if defined(ENABLE_THEMES)
+ // Listen for theme installation.
+ registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
+ content::Source<ThemeService>(
+ ThemeServiceFactory::GetForProfile(browser_->profile())));
+#endif // defined(ENABLE_THEMES)
}
BrowserInstantController::~BrowserInstantController() {
@@ -119,6 +134,20 @@ void BrowserInstantController::ActiveTabChanged() {
instant_.ActiveTabChanged();
}
+void BrowserInstantController::SetContentHeight(int height) {
+ OnThemeAreaHeightChanged(height);
+}
+
+void BrowserInstantController::UpdateThemeInfoForPreview() {
+ // Update theme background info and theme area height.
+ // Initialize |theme_info| if necessary.
+ // |OnThemeChanged| also updates theme area height if necessary.
+ if (!initialized_theme_info_)
+ OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile()));
+ else
+ OnThemeChanged(NULL);
+}
+
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, PrefObserver implementation:
@@ -133,7 +162,118 @@ void BrowserInstantController::OnPreferenceChanged(
void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
const search::Mode& new_mode) {
+ // If mode is now |NTP|, send theme-related information to instant.
+ if (new_mode.is_ntp())
+ UpdateThemeInfoForPreview();
+
instant_.SearchModeChanged(old_mode, new_mode);
}
+////////////////////////////////////////////////////////////////////////////////
+// BrowserInstantController, content::NotificationObserver implementation:
+
+void BrowserInstantController::Observe(
+ int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+#if defined(ENABLE_THEMES)
+ DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
+ OnThemeChanged(content::Source<ThemeService>(source).ptr());
+#endif // defined(ENABLE_THEMES)
+}
+
+void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) {
+ if (theme_service) { // Get theme information from theme service.
+ theme_info_ = ThemeBackgroundInfo();
+
+ // Set theme background color.
+ SkColor background_color =
+ theme_service->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
+ if (gfx::IsInvertedColorScheme())
+ background_color = color_utils::InvertColor(background_color);
+ theme_info_.color_r = SkColorGetR(background_color);
+ theme_info_.color_g = SkColorGetG(background_color);
+ theme_info_.color_b = SkColorGetB(background_color);
+ theme_info_.color_a = SkColorGetA(background_color);
+
+ if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
+ // Set theme id for theme background image url.
+ theme_info_.theme_id = theme_service->GetThemeID();
+
+ // Set theme background image horizontal alignment.
+ int alignment = 0;
+ theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_ALIGNMENT,
+ &alignment);
+ if (alignment & ThemeService::ALIGN_LEFT) {
+ theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT;
+ } else if (alignment & ThemeService::ALIGN_RIGHT) {
+ theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT;
+ } else { // ALIGN_CENTER
+ theme_info_.image_horizontal_alignment =
+ THEME_BKGRND_IMAGE_ALIGN_CENTER;
+ }
+
+ // Set theme background image vertical alignment.
+ if (alignment & ThemeService::ALIGN_TOP)
+ theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP;
+ else if (alignment & ThemeService::ALIGN_BOTTOM)
+ theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM;
+ else // ALIGN_CENTER
+ theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
+
+ // Set theme background image tiling.
+ int tiling = 0;
+ theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_TILING,
+ &tiling);
+ switch (tiling) {
+ case ThemeService::NO_REPEAT:
+ theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT;
+ break;
+ case ThemeService::REPEAT_X:
+ theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X;
+ break;
+ case ThemeService::REPEAT_Y:
+ theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y;
+ break;
+ case ThemeService::REPEAT:
+ theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT;
+ break;
+ }
+
+ // Set theme background image height.
+ gfx::ImageSkia* image = theme_service->GetImageSkiaNamed(
+ IDR_THEME_NTP_BACKGROUND);
+ DCHECK(image);
+ theme_info_.image_height = image->height();
+ }
+
+ initialized_theme_info_ = true;
+ }
+
+ DCHECK(initialized_theme_info_);
+
+ if (browser_->search_model()->mode().is_ntp()) {
+ instant_.ThemeChanged(theme_info_);
+
+ // Theme area height is only sent to preview for non-top-aligned images;
+ // new theme may have a different alignment that requires preview to know
+ // theme area height.
+ OnThemeAreaHeightChanged(theme_area_height_);
+ }
+}
+
+void BrowserInstantController::OnThemeAreaHeightChanged(int height) {
+ theme_area_height_ = height;
+
+ // Notify preview only if mode is |NTP| and theme background image is not top-
+ // aligned; top-aligned images don't need theme area height to determine which
+ // part of the image overlay should draw, 'cos the origin is top-left.
+ if (!browser_->search_model()->mode().is_ntp() ||
+ theme_info_.theme_id.empty() ||
+ theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) {
+ return;
+ }
+ instant_.ThemeAreaHeightChanged(theme_area_height_);
+}
+
} // namespace chrome