| 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 |
| 31 if (!HasCustomImage(id)) | 35 if (!HasCustomImage(id) && !allow_default) |
| 32 return nil; | 36 return nil; |
| 33 | 37 |
| 34 // 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. |
| 35 NSImageMap::const_iterator nsimage_iter = nsimage_cache_.find(id); | 39 NSImageMap::const_iterator nsimage_iter = nsimage_cache_.find(id); |
| 36 if (nsimage_iter != nsimage_cache_.end()) | 40 if (nsimage_iter != nsimage_cache_.end()) |
| 37 return nsimage_iter->second; | 41 return nsimage_iter->second; |
| 38 | 42 |
| 39 // 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 |
| 40 // SkBitmap > native conversion? | 44 // SkBitmap > native conversion? |
| 41 // - For consistency with other platforms. | 45 // - For consistency with other platforms. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 61 empty_image = [[NSImage alloc] initWithSize:image_rect.size]; | 65 empty_image = [[NSImage alloc] initWithSize:image_rect.size]; |
| 62 [empty_image lockFocus]; | 66 [empty_image lockFocus]; |
| 63 [[NSColor redColor] set]; | 67 [[NSColor redColor] set]; |
| 64 NSRectFill(image_rect); | 68 NSRectFill(image_rect); |
| 65 [empty_image unlockFocus]; | 69 [empty_image unlockFocus]; |
| 66 } | 70 } |
| 67 | 71 |
| 68 return empty_image; | 72 return empty_image; |
| 69 } | 73 } |
| 70 | 74 |
| 71 NSColor* BrowserThemeProvider::GetNSColor(int id) const { | 75 NSColor* BrowserThemeProvider::GetNSColor(int id, |
| 76 bool allow_default) const { |
| 72 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
| 73 | 78 |
| 74 // 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. |
| 75 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); | 80 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); |
| 76 if (nscolor_iter != nscolor_cache_.end()) | 81 if (nscolor_iter != nscolor_cache_.end()) { |
| 77 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 } |
| 78 | 86 |
| 87 bool is_default = false; |
| 79 SkColor sk_color; | 88 SkColor sk_color; |
| 80 if (theme_pack_.get() && theme_pack_->GetColor(id, &sk_color)) { | 89 if (theme_pack_.get() && theme_pack_->GetColor(id, &sk_color)) { |
| 81 NSColor* color = [NSColor | 90 is_default = false; |
| 82 colorWithCalibratedRed:SkColorGetR(sk_color)/255.0 | 91 } else { |
| 83 green:SkColorGetG(sk_color)/255.0 | 92 is_default = true; |
| 84 blue:SkColorGetB(sk_color)/255.0 | 93 sk_color = GetDefaultColor(id); |
| 85 alpha:SkColorGetA(sk_color)/255.0]; | 94 } |
| 86 | 95 |
| 87 // We loaded successfully. Cache the color. | 96 if (is_default && !allow_default) |
| 88 if (color) { | 97 return nil; |
| 89 nscolor_cache_[id] = [color retain]; | 98 |
| 90 return color; | 99 NSColor* color = [NSColor |
| 91 } | 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; |
| 92 } | 109 } |
| 93 | 110 |
| 94 return nil; | 111 return nil; |
| 95 } | 112 } |
| 96 | 113 |
| 97 NSColor* BrowserThemeProvider::GetNSColorTint(int id) const { | 114 NSColor* BrowserThemeProvider::GetNSColorTint(int id, |
| 115 bool allow_default) const { |
| 98 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 99 | 117 |
| 100 // 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. |
| 101 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); | 119 NSColorMap::const_iterator nscolor_iter = nscolor_cache_.find(id); |
| 102 if (nscolor_iter != nscolor_cache_.end()) | 120 if (nscolor_iter != nscolor_cache_.end()) { |
| 103 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 } |
| 104 | 125 |
| 126 bool is_default = false; |
| 105 color_utils::HSL tint; | 127 color_utils::HSL tint; |
| 106 if (theme_pack_.get() && theme_pack_->GetTint(id, &tint)) { | 128 if (theme_pack_.get() && theme_pack_->GetTint(id, &tint)) { |
| 107 CGFloat hue, saturation, brightness; | 129 is_default = false; |
| 108 HSLToHSB(tint, &hue, &saturation, &brightness); | 130 } else { |
| 131 is_default = true; |
| 132 tint = GetDefaultTint(id); |
| 133 } |
| 109 | 134 |
| 110 NSColor* tint_color = [NSColor colorWithCalibratedHue:hue | 135 if (is_default && !allow_default) |
| 111 saturation:saturation | 136 return nil; |
| 112 brightness:brightness | |
| 113 alpha:1.0]; | |
| 114 | 137 |
| 115 // We loaded successfully. Cache the color. | 138 CGFloat hue, saturation, brightness; |
| 116 if (tint_color) { | 139 HSLToHSB(tint, &hue, &saturation, &brightness); |
| 117 nscolor_cache_[id] = [tint_color retain]; | 140 |
| 118 return tint_color; | 141 NSColor* tint_color = [NSColor colorWithCalibratedHue:hue |
| 119 } | 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; |
| 120 } | 150 } |
| 121 | 151 |
| 122 return nil; | 152 return nil; |
| 123 } | 153 } |
| 124 | 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 |
| 125 void BrowserThemeProvider::FreePlatformCaches() { | 162 void BrowserThemeProvider::FreePlatformCaches() { |
| 126 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
| 127 | 164 |
| 128 // Free images. | 165 // Free images. |
| 129 for (NSImageMap::iterator i = nsimage_cache_.begin(); | 166 for (NSImageMap::iterator i = nsimage_cache_.begin(); |
| 130 i != nsimage_cache_.end(); i++) { | 167 i != nsimage_cache_.end(); i++) { |
| 131 [i->second release]; | 168 [i->second release]; |
| 132 } | 169 } |
| 133 nsimage_cache_.clear(); | 170 nsimage_cache_.clear(); |
| 134 | 171 |
| 135 // Free colors. | 172 // Free colors. |
| 136 for (NSColorMap::iterator i = nscolor_cache_.begin(); | 173 for (NSColorMap::iterator i = nscolor_cache_.begin(); |
| 137 i != nscolor_cache_.end(); i++) { | 174 i != nscolor_cache_.end(); i++) { |
| 138 [i->second release]; | 175 [i->second.first release]; |
| 139 } | 176 } |
| 140 nscolor_cache_.clear(); | 177 nscolor_cache_.clear(); |
| 141 } | 178 } |
| OLD | NEW |