| 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 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "app/gfx/color_utils.h" | 9 #include "app/gfx/color_utils.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "chrome/browser/browser_theme_pack.h" | 11 #include "chrome/browser/browser_theme_pack.h" |
| 12 #include "skia/ext/skia_utils_mac.h" | 12 #include "skia/ext/skia_utils_mac.h" |
| 13 | 13 |
| 14 NSString* const kBrowserThemeDidChangeNotification = |
| 15 @"BrowserThemeDidChangeNotification"; |
| 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 void HSLToHSB(const color_utils::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { | 19 void HSLToHSB(const color_utils::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { |
| 17 SkColor color = color_utils::HSLToSkColor(hsl, 255); // alpha doesn't matter | 20 SkColor color = color_utils::HSLToSkColor(hsl, 255); // alpha doesn't matter |
| 18 SkScalar hsv[3]; | 21 SkScalar hsv[3]; |
| 19 SkColorToHSV(color, hsv); | 22 SkColorToHSV(color, hsv); |
| 20 | 23 |
| 21 *h = SkScalarToDouble(hsv[0]) / 360.0; | 24 *h = SkScalarToDouble(hsv[0]) / 360.0; |
| 22 *s = SkScalarToDouble(hsv[1]); | 25 *s = SkScalarToDouble(hsv[1]); |
| 23 *b = SkScalarToDouble(hsv[2]); | 26 *b = SkScalarToDouble(hsv[2]); |
| 24 } | 27 } |
| 25 | 28 |
| 26 } | 29 } |
| 27 | 30 |
| 28 NSImage* BrowserThemeProvider::GetNSImageNamed(int id) const { | 31 NSImage* BrowserThemeProvider::GetNSImageNamed(int id, |
| 32 bool allow_default) const { |
| 29 DCHECK(CalledOnValidThread()); | 33 DCHECK(CalledOnValidThread()); |
| 30 | 34 |
| 35 if (!allow_default && !HasCustomImage(id)) |
| 36 return nil; |
| 37 |
| 31 // Check to see if we already have the image in the cache. | 38 // Check to see if we already have the image in the cache. |
| 32 NSImageMap::const_iterator nsimage_iter = nsimage_cache_.find(id); | 39 NSImageMap::const_iterator nsimage_iter = nsimage_cache_.find(id); |
| 33 if (nsimage_iter != nsimage_cache_.end()) | 40 if (nsimage_iter != nsimage_cache_.end()) |
| 34 return nsimage_iter->second; | 41 return nsimage_iter->second; |
| 35 | 42 |
| 36 // Why don't we load the file directly into the image instead of the whole | 43 // Why don't we load the file directly into the image instead of the whole |
| 37 // SkBitmap > native conversion? | 44 // SkBitmap > native conversion? |
| 38 // - For consistency with other platforms. | 45 // - For consistency with other platforms. |
| 39 // - To get the generated tinted images. | 46 // - To get the generated tinted images. |
| 40 SkBitmap* bitmap = GetBitmapNamed(id); | 47 SkBitmap* bitmap = GetBitmapNamed(id); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 58 empty_image = [[NSImage alloc] initWithSize:image_rect.size]; | 65 empty_image = [[NSImage alloc] initWithSize:image_rect.size]; |
| 59 [empty_image lockFocus]; | 66 [empty_image lockFocus]; |
| 60 [[NSColor redColor] set]; | 67 [[NSColor redColor] set]; |
| 61 NSRectFill(image_rect); | 68 NSRectFill(image_rect); |
| 62 [empty_image unlockFocus]; | 69 [empty_image unlockFocus]; |
| 63 } | 70 } |
| 64 | 71 |
| 65 return empty_image; | 72 return empty_image; |
| 66 } | 73 } |
| 67 | 74 |
| 68 NSColor* BrowserThemeProvider::GetNSColor(int id) const { | 75 NSColor* BrowserThemeProvider::GetNSColor(int id, |
| 76 bool allow_default) const { |
| 69 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
| 70 | 78 |
| 71 // Check to see if we already have the color in the cache. | 79 // Check to see if we already have the color in the cache. |
| 72 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); | 80 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); |
| 73 if (nscolor_iter != nscolor_cache_.end()) | 81 if (nscolor_iter != nscolor_cache_.end()) { |
| 74 return nscolor_iter->second; | 82 bool cached_is_default = nscolor_iter->second.second; |
| 83 if (!cached_is_default || allow_default) |
| 84 return nscolor_iter->second.first; |
| 85 } |
| 75 | 86 |
| 87 bool is_default = false; |
| 76 SkColor sk_color; | 88 SkColor sk_color; |
| 77 if (theme_pack_.get() && theme_pack_->GetColor(id, &sk_color)) { | 89 if (theme_pack_.get() && theme_pack_->GetColor(id, &sk_color)) { |
| 78 NSColor* color = [NSColor | 90 is_default = false; |
| 79 colorWithCalibratedRed:SkColorGetR(sk_color)/255.0 | 91 } else { |
| 80 green:SkColorGetG(sk_color)/255.0 | 92 is_default = true; |
| 81 blue:SkColorGetB(sk_color)/255.0 | 93 sk_color = GetDefaultColor(id); |
| 82 alpha:SkColorGetA(sk_color)/255.0]; | 94 } |
| 83 | 95 |
| 84 // We loaded successfully. Cache the color. | 96 if (is_default && !allow_default) |
| 85 if (color) { | 97 return nil; |
| 86 nscolor_cache_[id] = [color retain]; | 98 |
| 87 return color; | 99 NSColor* color = [NSColor |
| 88 } | 100 colorWithCalibratedRed:SkColorGetR(sk_color)/255.0 |
| 101 green:SkColorGetG(sk_color)/255.0 |
| 102 blue:SkColorGetB(sk_color)/255.0 |
| 103 alpha:SkColorGetA(sk_color)/255.0]; |
| 104 |
| 105 // We loaded successfully. Cache the color. |
| 106 if (color) { |
| 107 nscolor_cache_[id] = std::make_pair([color retain], is_default); |
| 108 return color; |
| 89 } | 109 } |
| 90 | 110 |
| 91 return nil; | 111 return nil; |
| 92 } | 112 } |
| 93 | 113 |
| 94 NSColor* BrowserThemeProvider::GetNSColorTint(int id) const { | 114 NSColor* BrowserThemeProvider::GetNSColorTint(int id, |
| 115 bool allow_default) const { |
| 95 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 96 | 117 |
| 97 // Check to see if we already have the color in the cache. | 118 // Check to see if we already have the color in the cache. |
| 98 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); | 119 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); |
| 99 if (nscolor_iter != nscolor_cache_.end()) | 120 if (nscolor_iter != nscolor_cache_.end()) { |
| 100 return nscolor_iter->second; | 121 bool cached_is_default = nscolor_iter->second.second; |
| 122 if (!cached_is_default || allow_default) |
| 123 return nscolor_iter->second.first; |
| 124 } |
| 101 | 125 |
| 126 bool is_default = false; |
| 102 color_utils::HSL tint; | 127 color_utils::HSL tint; |
| 103 if (theme_pack_.get() && theme_pack_->GetTint(id, &tint)) { | 128 if (theme_pack_.get() && theme_pack_->GetTint(id, &tint)) { |
| 104 CGFloat hue, saturation, brightness; | 129 is_default = false; |
| 105 HSLToHSB(tint, &hue, &saturation, &brightness); | 130 } else { |
| 131 is_default = true; |
| 132 tint = GetDefaultTint(id); |
| 133 } |
| 106 | 134 |
| 107 NSColor* tint_color = [NSColor colorWithCalibratedHue:hue | 135 if (is_default && !allow_default) |
| 108 saturation:saturation | 136 return nil; |
| 109 brightness:brightness | |
| 110 alpha:1.0]; | |
| 111 | 137 |
| 112 // We loaded successfully. Cache the color. | 138 CGFloat hue, saturation, brightness; |
| 113 if (tint_color) { | 139 HSLToHSB(tint, &hue, &saturation, &brightness); |
| 114 nscolor_cache_[id] = [tint_color retain]; | 140 |
| 115 return tint_color; | 141 NSColor* tint_color = [NSColor colorWithCalibratedHue:hue |
| 116 } | 142 saturation:saturation |
| 143 brightness:brightness |
| 144 alpha:1.0]; |
| 145 |
| 146 // We loaded successfully. Cache the color. |
| 147 if (tint_color) { |
| 148 nscolor_cache_[id] = std::make_pair([tint_color retain], is_default); |
| 149 return tint_color; |
| 117 } | 150 } |
| 118 | 151 |
| 119 return nil; | 152 return nil; |
| 120 } | 153 } |
| 121 | 154 |
| 155 // Let all the browser views know that themes have changed in a platform way. |
| 156 void BrowserThemeProvider::NotifyPlatformThemeChanged() { |
| 157 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; |
| 158 [defaultCenter postNotificationName:kBrowserThemeDidChangeNotification |
| 159 object:[NSValue valueWithPointer:this]]; |
| 160 } |
| 161 |
| 122 void BrowserThemeProvider::FreePlatformCaches() { | 162 void BrowserThemeProvider::FreePlatformCaches() { |
| 123 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
| 124 | 164 |
| 125 // Free images. | 165 // Free images. |
| 126 for (NSImageMap::iterator i = nsimage_cache_.begin(); | 166 for (NSImageMap::iterator i = nsimage_cache_.begin(); |
| 127 i != nsimage_cache_.end(); i++) { | 167 i != nsimage_cache_.end(); i++) { |
| 128 [i->second release]; | 168 [i->second release]; |
| 129 } | 169 } |
| 130 nsimage_cache_.clear(); | 170 nsimage_cache_.clear(); |
| 131 | 171 |
| 132 // Free colors. | 172 // Free colors. |
| 133 for (NSColorMap::iterator i = nscolor_cache_.begin(); | 173 for (NSColorMap::iterator i = nscolor_cache_.begin(); |
| 134 i != nscolor_cache_.end(); i++) { | 174 i != nscolor_cache_.end(); i++) { |
| 135 [i->second release]; | 175 [i->second.first release]; |
| 136 } | 176 } |
| 137 nscolor_cache_.clear(); | 177 nscolor_cache_.clear(); |
| 138 } | 178 } |
| OLD | NEW |