diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-17 22:15:13 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-17 22:15:13 +0000 |
commit | a3d3a7cf3b81cc8d00490bb7bfd92b57f0146d9c (patch) | |
tree | 7ae34f2b5e389e3d90bb54a94e04690ca0af416e | |
parent | d32185d54315610c0f3afc5e57edd9c7b7663a95 (diff) | |
download | chromium_src-a3d3a7cf3b81cc8d00490bb7bfd92b57f0146d9c.zip chromium_src-a3d3a7cf3b81cc8d00490bb7bfd92b57f0146d9c.tar.gz chromium_src-a3d3a7cf3b81cc8d00490bb7bfd92b57f0146d9c.tar.bz2 |
Allow the Mac theme provider to give default colors/tints if requested. Switch the download shelf's "open downloads" link to directly use the theme provider.
BUG=http://crbug.com/35554
TEST=no visible change in normal mode; incognito mode still being worked on
Review URL: http://codereview.chromium.org/630002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39279 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | app/theme_provider.h | 15 | ||||
-rw-r--r-- | chrome/browser/browser_theme_provider.cc | 3 | ||||
-rw-r--r-- | chrome/browser/browser_theme_provider.h | 21 | ||||
-rw-r--r-- | chrome/browser/browser_theme_provider_mac.mm | 104 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm | 6 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_theme_provider_init.mm | 31 | ||||
-rw-r--r-- | chrome/browser/cocoa/download_shelf_controller.mm | 36 |
7 files changed, 131 insertions, 85 deletions
diff --git a/app/theme_provider.h b/app/theme_provider.h index cd97fa8..1db73e1 100644 --- a/app/theme_provider.h +++ b/app/theme_provider.h @@ -71,20 +71,23 @@ class ThemeProvider { // Gets the NSImage with the specified |id|. // // The bitmap is not assumed to exist. If a theme does not provide an image, + // if |allow_default| is true, then the default image will be returned, else // this function will return nil. - virtual NSImage* GetNSImageNamed(int id) const = 0; + virtual NSImage* GetNSImageNamed(int id, bool allow_default) const = 0; // Gets the NSColor with the specified |id|. // - // The color is not assumed to exist. If a theme does not provide an color, - // this function will return nil. - virtual NSColor* GetNSColor(int id) const = 0; + // The color is not assumed to exist. If a theme does not provide an color, if + // |allow_default| is true, then the default color will be returned, else this + // function will return nil. + virtual NSColor* GetNSColor(int id, bool allow_default) const = 0; // Gets the NSColor for tinting with the specified |id|. // // The tint is not assumed to exist. If a theme does not provide a tint with - // that id, this function will return nil. - virtual NSColor* GetNSColorTint(int id) const = 0; + // that id, if |allow_default| is true, then the default tint will be + // returned, else this function will return nil. + virtual NSColor* GetNSColorTint(int id, bool allow_default) const = 0; #elif defined(OS_POSIX) && !defined(TOOLKIT_VIEWS) // Gets the GdkPixbuf with the specified |id|. Returns a pointer to a shared // instance of the GdkPixbuf. This shared GdkPixbuf is owned by the theme diff --git a/chrome/browser/browser_theme_provider.cc b/chrome/browser/browser_theme_provider.cc index dd2f301..5655a6d 100644 --- a/chrome/browser/browser_theme_provider.cc +++ b/chrome/browser/browser_theme_provider.cc @@ -541,6 +541,9 @@ void BrowserThemeProvider::NotifyThemeChanged() { service->Notify(NotificationType::BROWSER_THEME_CHANGED, Source<BrowserThemeProvider>(this), NotificationService::NoDetails()); +#if defined(OS_MACOSX) + NotifyPlatformThemeChanged(); +#endif // OS_MACOSX } #if defined(OS_WIN) diff --git a/chrome/browser/browser_theme_provider.h b/chrome/browser/browser_theme_provider.h index 1474c24..73b01ac 100644 --- a/chrome/browser/browser_theme_provider.h +++ b/chrome/browser/browser_theme_provider.h @@ -26,6 +26,13 @@ class PrefService; class Profile; class ResourceBundle; +#ifdef __OBJC__ +@class NSString; +// Sent whenever the browser theme changes. Object => NSValue wrapping the +// BrowserThemeProvider that changed. +extern "C" NSString* const kBrowserThemeDidChangeNotification; +#endif // __OBJC__ + class BrowserThemeProvider : public NonThreadSafe, public ThemeProvider { public: @@ -111,9 +118,9 @@ class BrowserThemeProvider : public NonThreadSafe, virtual GdkPixbuf* GetPixbufNamed(int id) const; virtual GdkPixbuf* GetRTLEnabledPixbufNamed(int id) const; #elif defined(OS_MACOSX) - virtual NSImage* GetNSImageNamed(int id) const; - virtual NSColor* GetNSColor(int id) const; - virtual NSColor* GetNSColorTint(int id) const; + virtual NSImage* GetNSImageNamed(int id, bool allow_default) const; + virtual NSColor* GetNSColor(int id, bool allow_default) const; + virtual NSColor* GetNSColorTint(int id, bool allow_default) const; #endif // Set the current theme to the theme defined in |extension|. @@ -182,6 +189,11 @@ class BrowserThemeProvider : public NonThreadSafe, // Let all the browser views know that themes have changed. virtual void NotifyThemeChanged(); +#if defined(OS_MACOSX) + // Let all the browser views know that themes have changed in a platform way. + virtual void NotifyPlatformThemeChanged(); +#endif // OS_MACOSX + // Clears the platform-specific caches. Do not call directly; it's called // from ClearCaches(). virtual void FreePlatformCaches(); @@ -215,7 +227,8 @@ class BrowserThemeProvider : public NonThreadSafe, #elif defined(OS_MACOSX) typedef std::map<int, NSImage*> NSImageMap; mutable NSImageMap nsimage_cache_; - typedef std::map<int, NSColor*> NSColorMap; + // The bool member of the pair is whether the color is a default color. + typedef std::map<int, std::pair<NSColor*, bool> > NSColorMap; mutable NSColorMap nscolor_cache_; #endif diff --git a/chrome/browser/browser_theme_provider_mac.mm b/chrome/browser/browser_theme_provider_mac.mm index 2f8c749..22de117 100644 --- a/chrome/browser/browser_theme_provider_mac.mm +++ b/chrome/browser/browser_theme_provider_mac.mm @@ -11,6 +11,9 @@ #include "chrome/browser/browser_theme_pack.h" #include "skia/ext/skia_utils_mac.h" +NSString* const kBrowserThemeDidChangeNotification = + @"BrowserThemeDidChangeNotification"; + namespace { void HSLToHSB(const color_utils::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { @@ -25,9 +28,13 @@ void HSLToHSB(const color_utils::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { } -NSImage* BrowserThemeProvider::GetNSImageNamed(int id) const { +NSImage* BrowserThemeProvider::GetNSImageNamed(int id, + bool allow_default) const { DCHECK(CalledOnValidThread()); + if (!allow_default && !HasCustomImage(id)) + return nil; + // Check to see if we already have the image in the cache. NSImageMap::const_iterator nsimage_iter = nsimage_cache_.find(id); if (nsimage_iter != nsimage_cache_.end()) @@ -65,60 +72,93 @@ NSImage* BrowserThemeProvider::GetNSImageNamed(int id) const { return empty_image; } -NSColor* BrowserThemeProvider::GetNSColor(int id) const { +NSColor* BrowserThemeProvider::GetNSColor(int id, + bool allow_default) const { DCHECK(CalledOnValidThread()); // Check to see if we already have the color in the cache. NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); - if (nscolor_iter != nscolor_cache_.end()) - return nscolor_iter->second; + if (nscolor_iter != nscolor_cache_.end()) { + bool cached_is_default = nscolor_iter->second.second; + if (!cached_is_default || allow_default) + return nscolor_iter->second.first; + } + bool is_default = false; SkColor sk_color; if (theme_pack_.get() && theme_pack_->GetColor(id, &sk_color)) { - NSColor* color = [NSColor - colorWithCalibratedRed:SkColorGetR(sk_color)/255.0 - green:SkColorGetG(sk_color)/255.0 - blue:SkColorGetB(sk_color)/255.0 - alpha:SkColorGetA(sk_color)/255.0]; - - // We loaded successfully. Cache the color. - if (color) { - nscolor_cache_[id] = [color retain]; - return color; - } + is_default = false; + } else { + is_default = true; + sk_color = GetDefaultColor(id); + } + + if (is_default && !allow_default) + return nil; + + NSColor* color = [NSColor + colorWithCalibratedRed:SkColorGetR(sk_color)/255.0 + green:SkColorGetG(sk_color)/255.0 + blue:SkColorGetB(sk_color)/255.0 + alpha:SkColorGetA(sk_color)/255.0]; + + // We loaded successfully. Cache the color. + if (color) { + nscolor_cache_[id] = std::make_pair([color retain], is_default); + return color; } return nil; } -NSColor* BrowserThemeProvider::GetNSColorTint(int id) const { +NSColor* BrowserThemeProvider::GetNSColorTint(int id, + bool allow_default) const { DCHECK(CalledOnValidThread()); // Check to see if we already have the color in the cache. NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); - if (nscolor_iter != nscolor_cache_.end()) - return nscolor_iter->second; + if (nscolor_iter != nscolor_cache_.end()) { + bool cached_is_default = nscolor_iter->second.second; + if (!cached_is_default || allow_default) + return nscolor_iter->second.first; + } + bool is_default = false; color_utils::HSL tint; if (theme_pack_.get() && theme_pack_->GetTint(id, &tint)) { - CGFloat hue, saturation, brightness; - HSLToHSB(tint, &hue, &saturation, &brightness); - - NSColor* tint_color = [NSColor colorWithCalibratedHue:hue - saturation:saturation - brightness:brightness - alpha:1.0]; - - // We loaded successfully. Cache the color. - if (tint_color) { - nscolor_cache_[id] = [tint_color retain]; - return tint_color; - } + is_default = false; + } else { + is_default = true; + tint = GetDefaultTint(id); + } + + if (is_default && !allow_default) + return nil; + + CGFloat hue, saturation, brightness; + HSLToHSB(tint, &hue, &saturation, &brightness); + + NSColor* tint_color = [NSColor colorWithCalibratedHue:hue + saturation:saturation + brightness:brightness + alpha:1.0]; + + // We loaded successfully. Cache the color. + if (tint_color) { + nscolor_cache_[id] = std::make_pair([tint_color retain], is_default); + return tint_color; } return nil; } +// Let all the browser views know that themes have changed in a platform way. +void BrowserThemeProvider::NotifyPlatformThemeChanged() { + NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter postNotificationName:kBrowserThemeDidChangeNotification + object:[NSValue valueWithPointer:this]]; +} + void BrowserThemeProvider::FreePlatformCaches() { DCHECK(CalledOnValidThread()); @@ -132,7 +172,7 @@ void BrowserThemeProvider::FreePlatformCaches() { // Free colors. for (NSColorMap::iterator i = nscolor_cache_.begin(); i != nscolor_cache_.end(); i++) { - [i->second release]; + [i->second.first release]; } nscolor_cache_.clear(); } diff --git a/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm index 6c71275..a030b3b 100644 --- a/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm +++ b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm @@ -35,9 +35,9 @@ class MockThemeProvider : public ThemeProvider { MOCK_CONST_METHOD1(GetRawData, RefCountedMemory*(int)); // OSX stuff - MOCK_CONST_METHOD1(GetNSImageNamed, NSImage*(int)); - MOCK_CONST_METHOD1(GetNSColor, NSColor*(int)); - MOCK_CONST_METHOD1(GetNSColorTint, NSColor*(int)); + MOCK_CONST_METHOD2(GetNSImageNamed, NSImage*(int, bool)); + MOCK_CONST_METHOD2(GetNSColor, NSColor*(int, bool)); + MOCK_CONST_METHOD2(GetNSColorTint, NSColor*(int, bool)); }; // Allows us to inject our fake controller below. diff --git a/chrome/browser/cocoa/browser_theme_provider_init.mm b/chrome/browser/cocoa/browser_theme_provider_init.mm index 391d378..69f6c79 100644 --- a/chrome/browser/cocoa/browser_theme_provider_init.mm +++ b/chrome/browser/cocoa/browser_theme_provider_init.mm @@ -61,11 +61,10 @@ return theme; } - NSImage* frameImage = provider->HasCustomImage(IDR_THEME_FRAME) ? - provider->GetNSImageNamed(IDR_THEME_FRAME) : nil; + NSImage* frameImage = provider->GetNSImageNamed(IDR_THEME_FRAME, false); if (frameImage) { NSImage* frameInactiveImage = - provider->GetNSImageNamed(IDR_THEME_FRAME_INACTIVE); + provider->GetNSImageNamed(IDR_THEME_FRAME_INACTIVE, true); [theme setValue:frameImage forAttribute:@"backgroundImage" style:GTMThemeStyleWindow @@ -77,44 +76,42 @@ } NSColor* tabTextColor = - provider->GetNSColor(BrowserThemeProvider::COLOR_TAB_TEXT); + provider->GetNSColor(BrowserThemeProvider::COLOR_TAB_TEXT, false); [theme setValue:tabTextColor forAttribute:@"textColor" style:GTMThemeStyleTabBarSelected state:GTMThemeStateActiveWindow]; NSColor* tabInactiveTextColor = - provider->GetNSColor(BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT); + provider->GetNSColor(BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT, + false); [theme setValue:tabInactiveTextColor forAttribute:@"textColor" style:GTMThemeStyleTabBarDeselected state:GTMThemeStateActiveWindow]; NSColor* bookmarkBarTextColor = - provider->GetNSColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT); + provider->GetNSColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT, false); [theme setValue:bookmarkBarTextColor forAttribute:@"textColor" style:GTMThemeStyleBookmarksBarButton state:GTMThemeStateActiveWindow]; - NSImage* toolbarImage = provider->HasCustomImage(IDR_THEME_TOOLBAR) ? - provider->GetNSImageNamed(IDR_THEME_TOOLBAR) : nil; + NSImage* toolbarImage = provider->GetNSImageNamed(IDR_THEME_TOOLBAR, false); [theme setValue:toolbarImage forAttribute:@"backgroundImage" style:GTMThemeStyleToolBar state:GTMThemeStateActiveWindow]; NSImage* toolbarBackgroundImage = - provider->HasCustomImage(IDR_THEME_TAB_BACKGROUND) ? - provider->GetNSImageNamed(IDR_THEME_TAB_BACKGROUND) : nil; + provider->GetNSImageNamed(IDR_THEME_TAB_BACKGROUND, false); [theme setValue:toolbarBackgroundImage forAttribute:@"backgroundImage" style:GTMThemeStyleTabBarDeselected state:GTMThemeStateActiveWindow]; NSImage* toolbarButtonImage = - provider->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND) ? - provider->GetNSImageNamed(IDR_THEME_BUTTON_BACKGROUND) : nil; + provider->GetNSImageNamed(IDR_THEME_BUTTON_BACKGROUND, false); if (toolbarButtonImage) { [theme setValue:toolbarButtonImage forAttribute:@"backgroundImage" @@ -139,7 +136,7 @@ } NSColor* toolbarButtonIconColor = - provider->GetNSColorTint(BrowserThemeProvider::TINT_BUTTONS); + provider->GetNSColorTint(BrowserThemeProvider::TINT_BUTTONS, false); [theme setValue:toolbarButtonIconColor forAttribute:@"iconColor" style:GTMThemeStyleToolBarButton @@ -152,22 +149,21 @@ state:GTMThemeStateActiveWindow]; NSColor* toolbarBackgroundColor = - provider->GetNSColor(BrowserThemeProvider::COLOR_TOOLBAR); + provider->GetNSColor(BrowserThemeProvider::COLOR_TOOLBAR, false); [theme setValue:toolbarBackgroundColor forAttribute:@"backgroundColor" style:GTMThemeStyleToolBar state:GTMThemeStateActiveWindow]; NSImage* frameOverlayImage = - provider->HasCustomImage(IDR_THEME_FRAME_OVERLAY) ? - provider->GetNSImageNamed(IDR_THEME_FRAME_OVERLAY) : nil; + provider->GetNSImageNamed(IDR_THEME_FRAME_OVERLAY, false); if (frameOverlayImage) { [theme setValue:frameOverlayImage forAttribute:@"overlay" style:GTMThemeStyleWindow state:GTMThemeStateActiveWindow]; NSImage* frameOverlayInactiveImage = - provider->GetNSImageNamed(IDR_THEME_FRAME_OVERLAY_INACTIVE); + provider->GetNSImageNamed(IDR_THEME_FRAME_OVERLAY_INACTIVE, true); if (frameOverlayInactiveImage) { [theme setValue:frameOverlayInactiveImage forAttribute:@"overlay" @@ -179,5 +175,4 @@ return theme; } - @end // @implementation GTMTheme(BrowserThemeProviderInitialization) diff --git a/chrome/browser/cocoa/download_shelf_controller.mm b/chrome/browser/cocoa/download_shelf_controller.mm index ae9395b..259385e 100644 --- a/chrome/browser/cocoa/download_shelf_controller.mm +++ b/chrome/browser/cocoa/download_shelf_controller.mm @@ -45,7 +45,7 @@ const NSTimeInterval kDownloadShelfCloseDuration = 0.12; - (void)layoutItems:(BOOL)skipFirst; - (void)closed; -- (void)updateTheme:(GTMTheme*)theme; +- (void)updateTheme; - (void)themeDidChangeNotification:(NSNotification*)aNotification; @end @@ -79,7 +79,7 @@ const NSTimeInterval kDownloadShelfCloseDuration = 0.12; NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(themeDidChangeNotification:) - name:kGTMThemeDidChangeNotification + name:kBrowserThemeDidChangeNotification object:nil]; [[self animatableView] setResizeDelegate:resizeDelegate_]; @@ -99,33 +99,25 @@ const NSTimeInterval kDownloadShelfCloseDuration = 0.12; } // Called after the current theme has changed. -- (void)themeDidChangeNotification:(NSNotification*)aNotification { - GTMTheme* theme = [aNotification object]; - [self updateTheme:theme]; +- (void)themeDidChangeNotification:(NSNotification*)notification { + [self updateTheme]; } // Adapt appearance to the current theme. Called after theme changes and before // this is shown for the first time. -- (void)updateTheme:(GTMTheme*)theme { - // For the default theme, use a blue color for the link. Ideally, we'd want to - // compare the current theme id with kDefaultThemeID, but the classic theme - // from the gallery does have a different id. Hence, we use the blue color if - // the current theme does not change the bookmark text color. - BOOL useDefaultColor = YES; +- (void)updateTheme { + NSColor* color = nil; + if (bridge_.get() && bridge_->browser() && bridge_->browser()->profile()) { ThemeProvider* provider = bridge_->browser()->profile()->GetThemeProvider(); - if (provider) { - useDefaultColor = provider->GetColor( - BrowserThemeProvider::COLOR_BOOKMARK_TEXT) == - BrowserThemeProvider::GetDefaultColor( - BrowserThemeProvider::COLOR_BOOKMARK_TEXT); - } + + color = + provider->GetNSColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT, false); } - NSColor* color = useDefaultColor ? - [HyperlinkButtonCell defaultTextColor] : - [theme textColorForStyle:GTMThemeStyleBookmarksBarButton - state:GTMThemeStateActiveWindow]; + if (!color) + color = [HyperlinkButtonCell defaultTextColor]; + [showAllDownloadsCell_ setTextColor:color]; } @@ -170,7 +162,7 @@ const NSTimeInterval kDownloadShelfCloseDuration = 0.12; return; if ([[self view] window]) - [self updateTheme:[[self view] gtm_theme]]; + [self updateTheme]; // Animate the shelf out, but not in. // TODO(rohitrao): We do not animate on the way in because Cocoa is already |