OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/status/network_menu_icon.h" | 5 #include "chrome/browser/chromeos/status/network_menu_icon.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/chromeos/accessibility/accessibility_util.h" | 13 #include "chrome/browser/chromeos/accessibility/accessibility_util.h" |
14 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/image/image_skia_operations.h" |
20 #include "ui/gfx/image/image_skia_source.h" | 21 #include "ui/gfx/image/image_skia_source.h" |
21 #include "ui/gfx/skbitmap_operations.h" | |
22 | 22 |
23 using std::max; | 23 using std::max; |
24 using std::min; | 24 using std::min; |
25 | 25 |
26 namespace chromeos { | 26 namespace chromeos { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // Amount to fade icons while connecting. | 30 // Amount to fade icons while connecting. |
31 const double kConnectingImageAlpha = 0.5; | 31 const double kConnectingImageAlpha = 0.5; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 break; | 151 break; |
152 case NETWORK_TECHNOLOGY_UNKNOWN: | 152 case NETWORK_TECHNOLOGY_UNKNOWN: |
153 break; | 153 break; |
154 } | 154 } |
155 if (id == kUnknownBadgeType) | 155 if (id == kUnknownBadgeType) |
156 return NULL; | 156 return NULL; |
157 else | 157 else |
158 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); | 158 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); |
159 } | 159 } |
160 | 160 |
161 const SkBitmap GetEmptyBitmapOfSameSize(const gfx::ImageSkiaRep& reference) { | 161 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) { |
162 typedef std::pair<int, int> SizeKey; | 162 typedef std::pair<int, int> SizeKey; |
163 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; | 163 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; |
164 static SizeBitmapMap* empty_bitmaps_ = new SizeBitmapMap; | 164 static SizeBitmapMap* empty_bitmaps_ = new SizeBitmapMap; |
165 | 165 |
166 SizeKey key(reference.pixel_width(), reference.pixel_height()); | 166 SizeKey key(pixel_size.width(), pixel_size.height()); |
167 | 167 |
168 SizeBitmapMap::iterator iter = empty_bitmaps_->find(key); | 168 SizeBitmapMap::iterator iter = empty_bitmaps_->find(key); |
169 if (iter != empty_bitmaps_->end()) | 169 if (iter != empty_bitmaps_->end()) |
170 return iter->second; | 170 return iter->second; |
171 | 171 |
172 SkBitmap empty; | 172 SkBitmap empty; |
173 empty.setConfig(SkBitmap::kARGB_8888_Config, key.first, key.second); | 173 empty.setConfig(SkBitmap::kARGB_8888_Config, key.first, key.second); |
174 empty.allocPixels(); | 174 empty.allocPixels(); |
175 empty.eraseARGB(0, 0, 0, 0); | 175 empty.eraseARGB(0, 0, 0, 0); |
176 (*empty_bitmaps_)[key] = empty; | 176 (*empty_bitmaps_)[key] = empty; |
177 return empty; | 177 return empty; |
178 } | 178 } |
179 | 179 |
180 class FadedImageSource : public gfx::ImageSkiaSource { | 180 class EmptyImageSource: public gfx::ImageSkiaSource { |
181 public: | 181 public: |
182 FadedImageSource(const gfx::ImageSkia& source, double alpha) | 182 EmptyImageSource(const gfx::Size& size) |
183 : source_(source), | 183 : size_(size) { |
184 alpha_(alpha) { | |
185 } | 184 } |
186 virtual ~FadedImageSource() {} | |
187 | 185 |
188 virtual gfx::ImageSkiaRep GetImageForScale( | 186 virtual gfx::ImageSkiaRep GetImageForScale( |
189 ui::ScaleFactor scale_factor) OVERRIDE { | 187 ui::ScaleFactor scale_factor) OVERRIDE { |
190 gfx::ImageSkiaRep image_rep = source_.GetRepresentation(scale_factor); | 188 gfx::Size pixel_size = size_.Scale(ui::GetScaleFactorScale(scale_factor)); |
191 const SkBitmap empty_bitmap = GetEmptyBitmapOfSameSize(image_rep); | 189 SkBitmap empty_bitmap = GetEmptyBitmap(pixel_size); |
192 SkBitmap faded_bitmap = SkBitmapOperations::CreateBlendedBitmap( | 190 return gfx::ImageSkiaRep(empty_bitmap, scale_factor); |
193 empty_bitmap, image_rep.sk_bitmap(), alpha_); | |
194 return gfx::ImageSkiaRep(faded_bitmap, image_rep.scale_factor()); | |
195 } | 191 } |
| 192 private: |
| 193 const gfx::Size size_; |
196 | 194 |
197 private: | 195 DISALLOW_COPY_AND_ASSIGN(EmptyImageSource); |
198 const gfx::ImageSkia source_; | |
199 const float alpha_; | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(FadedImageSource); | |
202 }; | 196 }; |
203 | 197 |
204 // This defines how we assemble a network icon. | 198 // This defines how we assemble a network icon. |
205 class NetworkIconImageSource : public gfx::ImageSkiaSource { | 199 class NetworkIconImageSource : public gfx::ImageSkiaSource { |
206 public: | 200 public: |
207 NetworkIconImageSource(const gfx::ImageSkia& icon, | 201 NetworkIconImageSource(const gfx::ImageSkia& icon, |
208 const gfx::ImageSkia* top_left_badge, | 202 const gfx::ImageSkia* top_left_badge, |
209 const gfx::ImageSkia* top_right_badge, | 203 const gfx::ImageSkia* top_right_badge, |
210 const gfx::ImageSkia* bottom_left_badge, | 204 const gfx::ImageSkia* bottom_left_badge, |
211 const gfx::ImageSkia* bottom_right_badge) | 205 const gfx::ImageSkia* bottom_right_badge) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 private: | 239 private: |
246 const gfx::ImageSkia icon_; | 240 const gfx::ImageSkia icon_; |
247 const gfx::ImageSkia *top_left_badge_; | 241 const gfx::ImageSkia *top_left_badge_; |
248 const gfx::ImageSkia *top_right_badge_; | 242 const gfx::ImageSkia *top_right_badge_; |
249 const gfx::ImageSkia *bottom_left_badge_; | 243 const gfx::ImageSkia *bottom_left_badge_; |
250 const gfx::ImageSkia *bottom_right_badge_; | 244 const gfx::ImageSkia *bottom_right_badge_; |
251 | 245 |
252 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); | 246 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); |
253 }; | 247 }; |
254 | 248 |
255 // This defines how we assemble a network menu icon. | |
256 class NetworkMenuIconSource : public gfx::ImageSkiaSource { | |
257 public: | |
258 NetworkMenuIconSource(NetworkMenuIcon::ImageType type, | |
259 int index, | |
260 NetworkMenuIcon::ResourceColorTheme color) | |
261 : type_(type), | |
262 index_(index), | |
263 color_(color) { | |
264 } | |
265 virtual ~NetworkMenuIconSource() {} | |
266 | |
267 virtual gfx::ImageSkiaRep GetImageForScale( | |
268 ui::ScaleFactor scale_factor) OVERRIDE { | |
269 int width, height; | |
270 gfx::ImageSkia* images; | |
271 if (type_ == NetworkMenuIcon::ARCS) { | |
272 if (index_ >= kNumArcsImages) | |
273 return gfx::ImageSkiaRep(); | |
274 images = ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
275 color_ == NetworkMenuIcon::COLOR_DARK ? | |
276 IDR_STATUSBAR_NETWORK_ARCS_DARK : IDR_STATUSBAR_NETWORK_ARCS_LIGHT); | |
277 width = images->width(); | |
278 height = images->height() / kNumArcsImages; | |
279 } else { | |
280 if (index_ >= kNumBarsImages) | |
281 return gfx::ImageSkiaRep(); | |
282 | |
283 images = ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
284 color_ == NetworkMenuIcon::COLOR_DARK ? | |
285 IDR_STATUSBAR_NETWORK_BARS_DARK : IDR_STATUSBAR_NETWORK_BARS_LIGHT); | |
286 width = images->width(); | |
287 height = images->height() / kNumBarsImages; | |
288 } | |
289 gfx::ImageSkiaRep image_rep = images->GetRepresentation(scale_factor); | |
290 | |
291 float scale = ui::GetScaleFactorScale(image_rep.scale_factor()); | |
292 height *= scale; | |
293 width *= scale; | |
294 | |
295 SkIRect subset = SkIRect::MakeXYWH(0, index_ * height, width, height); | |
296 | |
297 SkBitmap dst_bitmap; | |
298 image_rep.sk_bitmap().extractSubset(&dst_bitmap, subset); | |
299 return gfx::ImageSkiaRep(dst_bitmap, image_rep.scale_factor()); | |
300 } | |
301 | |
302 gfx::Size size() const { | |
303 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
304 // NeworkMenuIcons all have the same size in DIP for arc/bars. | |
305 if (type_ == NetworkMenuIcon::ARCS) { | |
306 gfx::Size size = rb.GetImageSkiaNamed( | |
307 IDR_STATUSBAR_NETWORK_ARCS_DARK)->size(); | |
308 return gfx::Size(size.width(), size.height() / kNumArcsImages); | |
309 } else { | |
310 gfx::Size size = rb.GetImageSkiaNamed( | |
311 IDR_STATUSBAR_NETWORK_BARS_DARK)->size(); | |
312 return gfx::Size(size.width(), size.height() / kNumBarsImages); | |
313 } | |
314 } | |
315 | |
316 private: | |
317 const NetworkMenuIcon::ImageType type_; | |
318 const int index_; | |
319 const NetworkMenuIcon::ResourceColorTheme color_; | |
320 | |
321 DISALLOW_COPY_AND_ASSIGN(NetworkMenuIconSource); | |
322 }; | |
323 | |
324 gfx::ImageSkia CreateVpnImage() { | 249 gfx::ImageSkia CreateVpnImage() { |
325 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 250 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
326 const gfx::ImageSkia* ethernet_icon = rb.GetImageSkiaNamed(IDR_STATUSBAR_VPN); | 251 const gfx::ImageSkia* ethernet_icon = rb.GetImageSkiaNamed(IDR_STATUSBAR_VPN); |
327 const gfx::ImageSkia* vpn_badge = rb.GetImageSkiaNamed(kVpnBadgeId); | 252 const gfx::ImageSkia* vpn_badge = rb.GetImageSkiaNamed(kVpnBadgeId); |
328 return NetworkMenuIcon::GenerateImageFromComponents( | 253 return NetworkMenuIcon::GenerateImageFromComponents( |
329 *ethernet_icon, NULL, NULL, vpn_badge, NULL); | 254 *ethernet_icon, NULL, NULL, vpn_badge, NULL); |
330 } | 255 } |
331 | 256 |
| 257 gfx::ImageSkia GetEmptyImage(const gfx::Size& size) { |
| 258 return gfx::ImageSkia(new EmptyImageSource(size), size); |
| 259 } |
| 260 |
332 } // namespace | 261 } // namespace |
333 | 262 |
334 //////////////////////////////////////////////////////////////////////////////// | 263 //////////////////////////////////////////////////////////////////////////////// |
335 // NetworkIcon | 264 // NetworkIcon |
336 | 265 |
337 // Sets up and generates an ImageSkia for a Network icon. | 266 // Sets up and generates an ImageSkia for a Network icon. |
338 class NetworkIcon { | 267 class NetworkIcon { |
339 public: | 268 public: |
340 // Default constructor is used by the status bar icon (NetworkMenuIcon). | 269 // Default constructor is used by the status bar icon (NetworkMenuIcon). |
341 explicit NetworkIcon(NetworkMenuIcon::ResourceColorTheme color); | 270 explicit NetworkIcon(NetworkMenuIcon::ResourceColorTheme color); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 icon_->UpdateIcon(network); | 775 icon_->UpdateIcon(network); |
847 // Overlay the VPN badge if connecting to a VPN. | 776 // Overlay the VPN badge if connecting to a VPN. |
848 if (network->type() != TYPE_VPN && cros->virtual_network()) { | 777 if (network->type() != TYPE_VPN && cros->virtual_network()) { |
849 if (cros->virtual_network()->connecting()) { | 778 if (cros->virtual_network()->connecting()) { |
850 const gfx::ImageSkia* vpn_badge = rb.GetImageSkiaNamed(kVpnBadgeId); | 779 const gfx::ImageSkia* vpn_badge = rb.GetImageSkiaNamed(kVpnBadgeId); |
851 const double animation = GetAnimation(); | 780 const double animation = GetAnimation(); |
852 animating = true; | 781 animating = true; |
853 // Even though this is the only place we use vpn_connecting_badge_, | 782 // Even though this is the only place we use vpn_connecting_badge_, |
854 // it is important that this is a member variable since we set a | 783 // it is important that this is a member variable since we set a |
855 // pointer to it and access that pointer in icon_->GenerateImage(). | 784 // pointer to it and access that pointer in icon_->GenerateImage(). |
856 vpn_connecting_badge_ = gfx::ImageSkia( | 785 vpn_connecting_badge_ = gfx::ImageSkiaOperations::CreateBlendedImage( |
857 new FadedImageSource(*vpn_badge, animation), vpn_badge->size()); | 786 GetEmptyImage(vpn_badge->size()), *vpn_badge, animation); |
858 icon_->set_bottom_left_badge(&vpn_connecting_badge_); | 787 icon_->set_bottom_left_badge(&vpn_connecting_badge_); |
859 } | 788 } |
860 } | 789 } |
861 if (!animating) | 790 if (!animating) |
862 animation_connecting_.Stop(); | 791 animation_connecting_.Stop(); |
863 | 792 |
864 // Set the text to display. | 793 // Set the text to display. |
865 if (network->type() == TYPE_ETHERNET) { | 794 if (network->type() == TYPE_ETHERNET) { |
866 if (mode_ == MENU_MODE) { | 795 if (mode_ == MENU_MODE) { |
867 text_ = l10n_util::GetStringFUTF16( | 796 text_ = l10n_util::GetStringFUTF16( |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 top_left_badge, | 852 top_left_badge, |
924 top_right_badge, | 853 top_right_badge, |
925 bottom_left_badge, | 854 bottom_left_badge, |
926 bottom_right_badge), | 855 bottom_right_badge), |
927 icon.size()); | 856 icon.size()); |
928 } | 857 } |
929 | 858 |
930 // We blend connecting icons with a black image to generate a faded icon. | 859 // We blend connecting icons with a black image to generate a faded icon. |
931 const gfx::ImageSkia NetworkMenuIcon::GenerateConnectingImage( | 860 const gfx::ImageSkia NetworkMenuIcon::GenerateConnectingImage( |
932 const gfx::ImageSkia& source) { | 861 const gfx::ImageSkia& source) { |
933 return gfx::ImageSkia(new FadedImageSource(source, kConnectingImageAlpha), | 862 return gfx::ImageSkiaOperations::CreateBlendedImage( |
934 source.size()); | 863 GetEmptyImage(source.size()), source, kConnectingImageAlpha); |
935 } | 864 } |
936 | 865 |
937 // Generates and caches an icon image for a network's current state. | 866 // Generates and caches an icon image for a network's current state. |
938 const gfx::ImageSkia NetworkMenuIcon::GetImage(const Network* network, | 867 const gfx::ImageSkia NetworkMenuIcon::GetImage(const Network* network, |
939 ResourceColorTheme color) { | 868 ResourceColorTheme color) { |
940 DCHECK(network); | 869 DCHECK(network); |
941 // Maintain a static (global) icon map. Note: Icons are never destroyed; | 870 // Maintain a static (global) icon map. Note: Icons are never destroyed; |
942 // it is assumed that a finite and reasonable number of network icons will be | 871 // it is assumed that a finite and reasonable number of network icons will be |
943 // created during a session. | 872 // created during a session. |
944 | 873 |
(...skipping 23 matching lines...) Expand all Loading... |
968 | 897 |
969 // Returns an icon for a disconnected VPN. | 898 // Returns an icon for a disconnected VPN. |
970 const gfx::ImageSkia NetworkMenuIcon::GetVpnImage() { | 899 const gfx::ImageSkia NetworkMenuIcon::GetVpnImage() { |
971 static const gfx::ImageSkia *vpn_image = new gfx::ImageSkia(CreateVpnImage()); | 900 static const gfx::ImageSkia *vpn_image = new gfx::ImageSkia(CreateVpnImage()); |
972 return *vpn_image; | 901 return *vpn_image; |
973 } | 902 } |
974 | 903 |
975 const gfx::ImageSkia NetworkMenuIcon::GetImage(ImageType type, | 904 const gfx::ImageSkia NetworkMenuIcon::GetImage(ImageType type, |
976 int index, | 905 int index, |
977 ResourceColorTheme color) { | 906 ResourceColorTheme color) { |
978 NetworkMenuIconSource* source = new NetworkMenuIconSource(type, index, color); | 907 int width, height = 0; |
979 return gfx::ImageSkia(source, source->size()); | 908 gfx::ImageSkia* images = NULL; |
| 909 if (type == NetworkMenuIcon::ARCS) { |
| 910 if (index >= kNumArcsImages) |
| 911 return gfx::ImageSkia(); |
| 912 images = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 913 color == NetworkMenuIcon::COLOR_DARK ? |
| 914 IDR_STATUSBAR_NETWORK_ARCS_DARK : IDR_STATUSBAR_NETWORK_ARCS_LIGHT); |
| 915 width = images->width(); |
| 916 height = images->height() / kNumArcsImages; |
| 917 } else { |
| 918 if (index >= kNumBarsImages) |
| 919 return gfx::ImageSkia(); |
| 920 |
| 921 images = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 922 color == NetworkMenuIcon::COLOR_DARK ? |
| 923 IDR_STATUSBAR_NETWORK_BARS_DARK : IDR_STATUSBAR_NETWORK_BARS_LIGHT); |
| 924 width = images->width(); |
| 925 height = images->height() / kNumBarsImages; |
| 926 } |
| 927 return gfx::ImageSkiaOperations::ExtractSubset(*images, |
| 928 gfx::Rect(0, index * height, width, height)); |
980 } | 929 } |
981 | 930 |
982 const gfx::ImageSkia NetworkMenuIcon::GetDisconnectedImage( | 931 const gfx::ImageSkia NetworkMenuIcon::GetDisconnectedImage( |
983 ImageType type, | 932 ImageType type, |
984 ResourceColorTheme color) { | 933 ResourceColorTheme color) { |
985 return GetImage(type, 0, color); | 934 return GetImage(type, 0, color); |
986 } | 935 } |
987 | 936 |
988 const gfx::ImageSkia NetworkMenuIcon::GetConnectedImage(ImageType type, | 937 const gfx::ImageSkia NetworkMenuIcon::GetConnectedImage(ImageType type, |
989 ResourceColorTheme color) { | 938 ResourceColorTheme color) { |
990 return GetImage(type, NumImages(type) - 1, color); | 939 return GetImage(type, NumImages(type) - 1, color); |
991 } | 940 } |
992 | 941 |
993 int NetworkMenuIcon::NumImages(ImageType type) { | 942 int NetworkMenuIcon::NumImages(ImageType type) { |
994 return (type == ARCS) ? kNumArcsImages : kNumBarsImages; | 943 return (type == ARCS) ? kNumArcsImages : kNumBarsImages; |
995 } | 944 } |
996 | 945 |
997 } // chromeos | 946 } // chromeos |
OLD | NEW |