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 "ui/chromeos/network/network_icon.h" | 5 #include "ui/chromeos/network/network_icon.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chromeos/network/device_state.h" | 9 #include "chromeos/network/device_state.h" |
10 #include "chromeos/network/network_connection_handler.h" | 10 #include "chromeos/network/network_connection_handler.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 using chromeos::NetworkPortalDetector; | 38 using chromeos::NetworkPortalDetector; |
39 using chromeos::NetworkState; | 39 using chromeos::NetworkState; |
40 using chromeos::NetworkStateHandler; | 40 using chromeos::NetworkStateHandler; |
41 using chromeos::NetworkTypePattern; | 41 using chromeos::NetworkTypePattern; |
42 | 42 |
43 namespace ui { | 43 namespace ui { |
44 namespace network_icon { | 44 namespace network_icon { |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
| 48 // Constants for offseting the badge displayed on top of the signal strength |
| 49 // icon. The badge will extend outside of the base icon bounds by these amounts. |
| 50 // Only used for MD. All values are in dp. |
| 51 |
| 52 // The badge offsets are different depending on whether the icon is in the tray |
| 53 // or menu. |
| 54 const int kTrayIconBadgeOffset = 3; |
| 55 const int kMenuIconBadgeOffset = 2; |
| 56 |
| 57 // TODO(estade): use kTrayIconSize. See crbug.com/623987 |
| 58 const int kTrayIconSide = 16; |
| 59 |
| 60 bool UseMd() { |
| 61 return md_icon_controller::UseMaterialDesignNetworkIcons(); |
| 62 } |
| 63 |
48 //------------------------------------------------------------------------------ | 64 //------------------------------------------------------------------------------ |
49 // Struct to pass icon badges to NetworkIconImageSource. | 65 // Struct to pass icon badges to NetworkIconImageSource. |
50 struct Badges { | 66 struct Badges { |
51 Badges() | 67 Badges() |
52 : top_left(NULL), | 68 : top_left(nullptr), |
53 top_right(NULL), | 69 top_right(nullptr), |
54 bottom_left(NULL), | 70 bottom_left(nullptr), |
55 bottom_right(NULL) { | 71 bottom_right(nullptr) {} |
56 } | |
57 const gfx::ImageSkia* top_left; | 72 const gfx::ImageSkia* top_left; |
58 const gfx::ImageSkia* top_right; | 73 const gfx::ImageSkia* top_right; |
59 const gfx::ImageSkia* bottom_left; | 74 const gfx::ImageSkia* bottom_left; |
60 const gfx::ImageSkia* bottom_right; | 75 const gfx::ImageSkia* bottom_right; |
61 }; | 76 }; |
62 | 77 |
63 //------------------------------------------------------------------------------ | 78 //------------------------------------------------------------------------------ |
64 // class used for maintaining a map of network state and images. | 79 // class used for maintaining a map of network state and images. |
65 class NetworkIconImpl { | 80 class NetworkIconImpl { |
66 public: | 81 public: |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 138 |
124 //------------------------------------------------------------------------------ | 139 //------------------------------------------------------------------------------ |
125 // Maintain a static (global) icon map. Note: Icons are never destroyed; | 140 // Maintain a static (global) icon map. Note: Icons are never destroyed; |
126 // it is assumed that a finite and reasonable number of network icons will be | 141 // it is assumed that a finite and reasonable number of network icons will be |
127 // created during a session. | 142 // created during a session. |
128 | 143 |
129 typedef std::map<std::string, NetworkIconImpl*> NetworkIconMap; | 144 typedef std::map<std::string, NetworkIconImpl*> NetworkIconMap; |
130 | 145 |
131 NetworkIconMap* GetIconMapInstance(IconType icon_type, bool create) { | 146 NetworkIconMap* GetIconMapInstance(IconType icon_type, bool create) { |
132 typedef std::map<IconType, NetworkIconMap*> IconTypeMap; | 147 typedef std::map<IconType, NetworkIconMap*> IconTypeMap; |
133 static IconTypeMap* s_icon_map = NULL; | 148 static IconTypeMap* s_icon_map = nullptr; |
134 if (s_icon_map == NULL) { | 149 if (s_icon_map == nullptr) { |
135 if (!create) | 150 if (!create) |
136 return NULL; | 151 return nullptr; |
137 s_icon_map = new IconTypeMap; | 152 s_icon_map = new IconTypeMap; |
138 } | 153 } |
139 if (s_icon_map->count(icon_type) == 0) { | 154 if (s_icon_map->count(icon_type) == 0) { |
140 if (!create) | 155 if (!create) |
141 return NULL; | 156 return nullptr; |
142 (*s_icon_map)[icon_type] = new NetworkIconMap; | 157 (*s_icon_map)[icon_type] = new NetworkIconMap; |
143 } | 158 } |
144 return (*s_icon_map)[icon_type]; | 159 return (*s_icon_map)[icon_type]; |
145 } | 160 } |
146 | 161 |
147 NetworkIconMap* GetIconMap(IconType icon_type) { | 162 NetworkIconMap* GetIconMap(IconType icon_type) { |
148 return GetIconMapInstance(icon_type, true); | 163 return GetIconMapInstance(icon_type, true); |
149 } | 164 } |
150 | 165 |
151 void PurgeIconMap(IconType icon_type, | 166 void PurgeIconMap(IconType icon_type, |
(...skipping 30 matching lines...) Expand all Loading... |
182 const int kNumNetworkImages = 5; | 197 const int kNumNetworkImages = 5; |
183 | 198 |
184 // Number of discrete images to use for alpha fade animation | 199 // Number of discrete images to use for alpha fade animation |
185 const int kNumFadeImages = 10; | 200 const int kNumFadeImages = 10; |
186 | 201 |
187 SkColor GetBaseColorForIconType(IconType icon_type) { | 202 SkColor GetBaseColorForIconType(IconType icon_type) { |
188 // TODO(estade): use kTrayIconColor and kMenuIconColor. | 203 // TODO(estade): use kTrayIconColor and kMenuIconColor. |
189 return icon_type == ICON_TYPE_TRAY ? SK_ColorWHITE : gfx::kChromeIconGrey; | 204 return icon_type == ICON_TYPE_TRAY ? SK_ColorWHITE : gfx::kChromeIconGrey; |
190 } | 205 } |
191 | 206 |
192 gfx::Size GetSizeForIconType(IconType icon_type) { | |
193 // TODO(estade): use kTrayIconSize and kMenuIconSize. | |
194 return icon_type == ICON_TYPE_TRAY ? gfx::Size(16, 16) : gfx::Size(20, 20); | |
195 } | |
196 | |
197 bool IconTypeIsDark(IconType icon_type) { | 207 bool IconTypeIsDark(IconType icon_type) { |
198 return (icon_type != ICON_TYPE_TRAY); | 208 return (icon_type != ICON_TYPE_TRAY); |
199 } | 209 } |
200 | 210 |
201 bool IconTypeHasVPNBadge(IconType icon_type) { | 211 bool IconTypeHasVPNBadge(IconType icon_type) { |
202 return (icon_type != ICON_TYPE_LIST); | 212 return (icon_type != ICON_TYPE_LIST); |
203 } | 213 } |
204 | 214 |
205 //------------------------------------------------------------------------------ | |
206 // Classes for generating scaled images. | |
207 | |
208 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) { | |
209 typedef std::pair<int, int> SizeKey; | |
210 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; | |
211 static SizeBitmapMap* s_empty_bitmaps = new SizeBitmapMap; | |
212 | |
213 SizeKey key(pixel_size.width(), pixel_size.height()); | |
214 | |
215 SizeBitmapMap::iterator iter = s_empty_bitmaps->find(key); | |
216 if (iter != s_empty_bitmaps->end()) | |
217 return iter->second; | |
218 | |
219 SkBitmap empty; | |
220 empty.allocN32Pixels(key.first, key.second); | |
221 empty.eraseARGB(0, 0, 0, 0); | |
222 (*s_empty_bitmaps)[key] = empty; | |
223 return empty; | |
224 } | |
225 | |
226 class EmptyImageSource: public gfx::ImageSkiaSource { | |
227 public: | |
228 explicit EmptyImageSource(const gfx::Size& size) | |
229 : size_(size) { | |
230 } | |
231 | |
232 gfx::ImageSkiaRep GetImageForScale(float scale) override { | |
233 gfx::Size pixel_size = gfx::ScaleToFlooredSize(size_, scale); | |
234 SkBitmap empty_bitmap = GetEmptyBitmap(pixel_size); | |
235 return gfx::ImageSkiaRep(empty_bitmap, scale); | |
236 } | |
237 | |
238 private: | |
239 const gfx::Size size_; | |
240 | |
241 DISALLOW_COPY_AND_ASSIGN(EmptyImageSource); | |
242 }; | |
243 | |
244 // This defines how we assemble a network icon. | 215 // This defines how we assemble a network icon. |
245 class NetworkIconImageSource : public gfx::ImageSkiaSource { | 216 class NetworkIconImageSource : public gfx::CanvasImageSource { |
246 public: | 217 public: |
247 NetworkIconImageSource(const gfx::ImageSkia& icon, const Badges& badges) | 218 NetworkIconImageSource(const gfx::ImageSkia& icon, const Badges& badges) |
248 : icon_(icon), | 219 : CanvasImageSource(icon.size(), false), icon_(icon), badges_(badges) {} |
249 badges_(badges) { | |
250 } | |
251 ~NetworkIconImageSource() override {} | 220 ~NetworkIconImageSource() override {} |
252 | 221 |
253 // TODO(pkotwicz): Figure out what to do when a new image resolution becomes | 222 // TODO(pkotwicz): Figure out what to do when a new image resolution becomes |
254 // available. | 223 // available. |
255 gfx::ImageSkiaRep GetImageForScale(float scale) override { | 224 void Draw(gfx::Canvas* canvas) override { |
256 gfx::Canvas canvas(icon_.size(), scale, false); | 225 canvas->DrawImageInt(icon_, 0, 0); |
257 canvas.DrawImageInt(icon_, 0, 0); | |
258 | 226 |
259 if (badges_.top_left) | 227 if (badges_.top_left) |
260 canvas.DrawImageInt(*badges_.top_left, 0, 0); | 228 canvas->DrawImageInt(*badges_.top_left, 0, 0); |
261 if (badges_.top_right) | 229 if (badges_.top_right) |
262 canvas.DrawImageInt(*badges_.top_right, | 230 canvas->DrawImageInt(*badges_.top_right, |
263 icon_.width() - badges_.top_right->width(), 0); | 231 icon_.width() - badges_.top_right->width(), 0); |
264 if (badges_.bottom_left) { | 232 if (badges_.bottom_left) { |
265 canvas.DrawImageInt(*badges_.bottom_left, | 233 canvas->DrawImageInt(*badges_.bottom_left, 0, |
266 0, icon_.height() - badges_.bottom_left->height()); | 234 icon_.height() - badges_.bottom_left->height()); |
267 } | 235 } |
268 if (badges_.bottom_right) { | 236 if (badges_.bottom_right) { |
269 canvas.DrawImageInt(*badges_.bottom_right, | 237 canvas->DrawImageInt(*badges_.bottom_right, |
270 icon_.width() - badges_.bottom_right->width(), | 238 icon_.width() - badges_.bottom_right->width(), |
271 icon_.height() - badges_.bottom_right->height()); | 239 icon_.height() - badges_.bottom_right->height()); |
272 } | 240 } |
273 return canvas.ExtractImageRep(); | |
274 } | 241 } |
275 | 242 |
276 private: | 243 private: |
277 const gfx::ImageSkia icon_; | 244 const gfx::ImageSkia icon_; |
278 const Badges badges_; | 245 const Badges badges_; |
279 | 246 |
280 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); | 247 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); |
281 }; | 248 }; |
282 | 249 |
| 250 // This defines how we assemble a network icon. |
| 251 class NetworkIconImageSourceMd : public gfx::CanvasImageSource { |
| 252 public: |
| 253 static gfx::ImageSkia CreateImage(const gfx::ImageSkia& icon, |
| 254 const Badges& badges) { |
| 255 auto source = new NetworkIconImageSourceMd(icon, badges); |
| 256 return gfx::ImageSkia(source, source->size()); |
| 257 } |
| 258 |
| 259 // gfx::CanvasImageSource: |
| 260 void Draw(gfx::Canvas* canvas) override { |
| 261 const int width = size().width(); |
| 262 const int height = size().height(); |
| 263 |
| 264 // The base icon is centered in both dimensions. |
| 265 const int icon_y = (height - icon_.height()) / 2; |
| 266 canvas->DrawImageInt(icon_, (width - icon_.width()) / 2, icon_y); |
| 267 |
| 268 // The badges are flush against the edges of the canvas, except at the top, |
| 269 // where the badge is only 1dp higher than the base image. |
| 270 const int top_badge_y = icon_y - 1; |
| 271 if (badges_.top_left) |
| 272 canvas->DrawImageInt(*badges_.top_left, 0, top_badge_y); |
| 273 if (badges_.top_right) { |
| 274 canvas->DrawImageInt(*badges_.top_right, |
| 275 width - badges_.top_right->width(), top_badge_y); |
| 276 } |
| 277 if (badges_.bottom_left) { |
| 278 canvas->DrawImageInt(*badges_.bottom_left, 0, |
| 279 height - badges_.bottom_left->height()); |
| 280 } |
| 281 if (badges_.bottom_right) { |
| 282 canvas->DrawImageInt(*badges_.bottom_right, |
| 283 width - badges_.bottom_right->width(), |
| 284 height - badges_.bottom_right->height()); |
| 285 } |
| 286 } |
| 287 |
| 288 bool HasRepresentationAtAllScales() const override { return true; } |
| 289 |
| 290 private: |
| 291 NetworkIconImageSourceMd(const gfx::ImageSkia& icon, const Badges& badges) |
| 292 : CanvasImageSource(GetSizeForBaseIconSize(icon.size()), false), |
| 293 icon_(icon), |
| 294 badges_(badges) {} |
| 295 ~NetworkIconImageSourceMd() override {} |
| 296 |
| 297 static gfx::Size GetSizeForBaseIconSize(const gfx::Size& base_icon_size) { |
| 298 gfx::Size size = base_icon_size; |
| 299 const int badge_offset = base_icon_size.width() == kTrayIconSide |
| 300 ? kTrayIconBadgeOffset |
| 301 : kMenuIconBadgeOffset; |
| 302 size.Enlarge(badge_offset * 2, badge_offset * 2); |
| 303 return size; |
| 304 } |
| 305 |
| 306 const gfx::ImageSkia icon_; |
| 307 const Badges badges_; |
| 308 |
| 309 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSourceMd); |
| 310 }; |
| 311 |
283 // Depicts a given signal strength using arcs (e.g. for WiFi connections) or | 312 // Depicts a given signal strength using arcs (e.g. for WiFi connections) or |
284 // bars (e.g. for cell connections). | 313 // bars (e.g. for cell connections). |
285 class NetworkIconImageSourceMd : public gfx::CanvasImageSource { | 314 class SignalStrengthImageSource : public gfx::CanvasImageSource { |
286 public: | 315 public: |
287 NetworkIconImageSourceMd(ImageType image_type, | 316 SignalStrengthImageSource(ImageType image_type, |
288 IconType icon_type, | 317 IconType icon_type, |
289 int signal_strength) | 318 int signal_strength) |
290 : CanvasImageSource(GetSizeForIconType(icon_type), false), | 319 : CanvasImageSource(GetSizeForIconType(icon_type), false), |
291 image_type_(image_type), | 320 image_type_(image_type), |
292 icon_type_(icon_type), | 321 icon_type_(icon_type), |
293 signal_strength_(signal_strength) { | 322 signal_strength_(signal_strength) { |
294 if (image_type_ == NONE) | 323 if (image_type_ == NONE) |
295 image_type_ = ARCS; | 324 image_type_ = ARCS; |
296 | 325 |
297 DCHECK_GE(signal_strength, 0); | 326 DCHECK_GE(signal_strength, 0); |
298 DCHECK_LT(signal_strength, kNumNetworkImages); | 327 DCHECK_LT(signal_strength, kNumNetworkImages); |
299 } | 328 } |
300 ~NetworkIconImageSourceMd() override {} | 329 ~SignalStrengthImageSource() override {} |
301 | 330 |
302 // gfx::CanvasImageSource: | 331 // gfx::CanvasImageSource: |
303 void Draw(gfx::Canvas* canvas) override { | 332 void Draw(gfx::Canvas* canvas) override { |
304 if (image_type_ == ARCS) | 333 if (image_type_ == ARCS) |
305 DrawArcs(canvas); | 334 DrawArcs(canvas); |
306 else | 335 else |
307 DrawBars(canvas); | 336 DrawBars(canvas); |
308 } | 337 } |
| 338 |
309 bool HasRepresentationAtAllScales() const override { | 339 bool HasRepresentationAtAllScales() const override { |
310 // This image source can handle any scale factor. TODO(estade): investigate | |
311 // why this doesn't seem to be respected. (Perhaps because we compose | |
312 // multiple images that don't have representations at all scales?) | |
313 return true; | 340 return true; |
314 } | 341 } |
315 | 342 |
316 private: | 343 private: |
| 344 static gfx::Size GetSizeForIconType(IconType icon_type) { |
| 345 return icon_type == ICON_TYPE_TRAY |
| 346 ? gfx::Size(kTrayIconSide, kTrayIconSide) |
| 347 // TODO(estade): use kMenuIconSize instead of 20. |
| 348 : gfx::Size(20, 20); |
| 349 } |
| 350 |
317 void DrawArcs(gfx::Canvas* canvas) { | 351 void DrawArcs(gfx::Canvas* canvas) { |
318 gfx::RectF oval_bounds((gfx::Rect(size()))); | 352 gfx::RectF oval_bounds((gfx::Rect(size()))); |
319 oval_bounds.Inset(gfx::Insets(kIconInset)); | 353 oval_bounds.Inset(gfx::Insets(kIconInset)); |
320 // Double the width and height. The new midpoint should be the former | 354 // Double the width and height. The new midpoint should be the former |
321 // bottom center. | 355 // bottom center. |
322 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2, | 356 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2, |
323 -oval_bounds.height()); | 357 -oval_bounds.height()); |
324 | 358 |
325 const SkScalar kAngleAboveHorizontal = 51.f; | 359 const SkScalar kAngleAboveHorizontal = 51.f; |
326 const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal; | 360 const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 436 |
403 // Padding between outside of icon and edge of the canvas, in dp. This value | 437 // Padding between outside of icon and edge of the canvas, in dp. This value |
404 // stays the same regardless of the canvas size (which depends on | 438 // stays the same regardless of the canvas size (which depends on |
405 // |icon_type_|). | 439 // |icon_type_|). |
406 static constexpr int kIconInset = 2; | 440 static constexpr int kIconInset = 2; |
407 | 441 |
408 // TODO(estade): share this alpha with other things in ash (battery, etc.). | 442 // TODO(estade): share this alpha with other things in ash (battery, etc.). |
409 // See crbug.com/623987 and crbug.com/632827 | 443 // See crbug.com/623987 and crbug.com/632827 |
410 static constexpr int kBgAlpha = 0x4D; | 444 static constexpr int kBgAlpha = 0x4D; |
411 | 445 |
412 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSourceMd); | 446 DISALLOW_COPY_AND_ASSIGN(SignalStrengthImageSource); |
413 }; | 447 }; |
414 | 448 |
415 //------------------------------------------------------------------------------ | 449 //------------------------------------------------------------------------------ |
416 // Utilities for extracting icon images. | 450 // Utilities for extracting icon images. |
417 | 451 |
418 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { | 452 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { |
419 gfx::ImageSkia* image; | 453 gfx::ImageSkia* image; |
420 if (image_type == BARS) { | 454 if (image_type == BARS) { |
421 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 455 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
422 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK | 456 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK |
(...skipping 10 matching lines...) Expand all Loading... |
433 if (type == shill::kTypeWifi) | 467 if (type == shill::kTypeWifi) |
434 return ARCS; | 468 return ARCS; |
435 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) | 469 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) |
436 return BARS; | 470 return BARS; |
437 return NONE; | 471 return NONE; |
438 } | 472 } |
439 | 473 |
440 gfx::ImageSkia GetImageForIndex(ImageType image_type, | 474 gfx::ImageSkia GetImageForIndex(ImageType image_type, |
441 IconType icon_type, | 475 IconType icon_type, |
442 int index) { | 476 int index) { |
443 if (md_icon_controller::UseMaterialDesignNetworkIcons()) { | 477 if (UseMd()) { |
444 gfx::CanvasImageSource* source = | 478 gfx::CanvasImageSource* source = |
445 new NetworkIconImageSourceMd(image_type, icon_type, index); | 479 new SignalStrengthImageSource(image_type, icon_type, index); |
446 return gfx::ImageSkia(source, source->size()); | 480 return gfx::ImageSkia(source, source->size()); |
447 } | 481 } |
448 | 482 |
449 if (index < 0 || index >= kNumNetworkImages) | 483 if (index < 0 || index >= kNumNetworkImages) |
450 return gfx::ImageSkia(); | 484 return gfx::ImageSkia(); |
451 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); | 485 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); |
452 int width = images->width(); | 486 int width = images->width(); |
453 int height = images->height() / kNumNetworkImages; | 487 int height = images->height() / kNumNetworkImages; |
454 return gfx::ImageSkiaOperations::ExtractSubset(*images, | 488 return gfx::ImageSkiaOperations::ExtractSubset(*images, |
455 gfx::Rect(0, index * height, width, height)); | 489 gfx::Rect(0, index * height, width, height)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 int index = animation * nextafter(static_cast<float>(kImageCount), 0); | 523 int index = animation * nextafter(static_cast<float>(kImageCount), 0); |
490 index = std::max(std::min(index, kImageCount - 1), 0); | 524 index = std::max(std::min(index, kImageCount - 1), 0); |
491 gfx::ImageSkia** images; | 525 gfx::ImageSkia** images; |
492 bool dark = IconTypeIsDark(icon_type); | 526 bool dark = IconTypeIsDark(icon_type); |
493 if (image_type == BARS) | 527 if (image_type == BARS) |
494 images = dark ? s_bars_images_dark : s_bars_images_light; | 528 images = dark ? s_bars_images_dark : s_bars_images_light; |
495 else | 529 else |
496 images = dark ? s_arcs_images_dark : s_arcs_images_light; | 530 images = dark ? s_arcs_images_dark : s_arcs_images_light; |
497 if (!images[index]) { | 531 if (!images[index]) { |
498 // Lazily cache images. | 532 // Lazily cache images. |
| 533 // TODO(estade): should the alpha be applied in SignalStrengthImageSource? |
499 gfx::ImageSkia source = GetImageForIndex(image_type, icon_type, index + 1); | 534 gfx::ImageSkia source = GetImageForIndex(image_type, icon_type, index + 1); |
500 images[index] = new gfx::ImageSkia( | 535 images[index] = |
501 gfx::ImageSkiaOperations::CreateBlendedImage( | 536 new gfx::ImageSkia(gfx::ImageSkiaOperations::CreateTransparentImage( |
502 gfx::ImageSkia(new EmptyImageSource(source.size()), source.size()), | 537 source, kConnectingImageAlpha)); |
503 source, | |
504 kConnectingImageAlpha)); | |
505 } | 538 } |
506 return images[index]; | 539 return images[index]; |
507 } | 540 } |
508 | 541 |
509 gfx::ImageSkia* ConnectingVpnImage(double animation) { | 542 gfx::ImageSkia* ConnectingVpnImage(double animation) { |
510 int index = animation * nextafter(static_cast<float>(kNumFadeImages), 0); | 543 int index = animation * nextafter(static_cast<float>(kNumFadeImages), 0); |
511 static gfx::ImageSkia* s_vpn_images[kNumFadeImages]; | 544 static gfx::ImageSkia* s_vpn_images[kNumFadeImages]; |
512 if (!s_vpn_images[index]) { | 545 if (!s_vpn_images[index]) { |
513 // Lazily cache images. | 546 // Lazily cache images. |
514 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 547 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
515 gfx::ImageSkia* icon = rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); | 548 gfx::ImageSkia* icon = rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); |
516 s_vpn_images[index] = new gfx::ImageSkia( | 549 s_vpn_images[index] = new gfx::ImageSkia( |
517 gfx::ImageSkiaOperations::CreateBlendedImage( | 550 gfx::ImageSkiaOperations::CreateTransparentImage(*icon, animation)); |
518 gfx::ImageSkia(new EmptyImageSource(icon->size()), icon->size()), | |
519 *icon, | |
520 animation)); | |
521 } | 551 } |
522 return s_vpn_images[index]; | 552 return s_vpn_images[index]; |
523 } | 553 } |
524 | 554 |
525 gfx::ImageSkia* ConnectingVpnBadge(double animation) { | 555 gfx::ImageSkia* ConnectingVpnBadge(double animation) { |
526 int index = animation * nextafter(static_cast<float>(kNumFadeImages), 0); | 556 int index = animation * nextafter(static_cast<float>(kNumFadeImages), 0); |
527 static gfx::ImageSkia* s_vpn_badges[kNumFadeImages]; | 557 static gfx::ImageSkia* s_vpn_badges[kNumFadeImages]; |
528 if (!s_vpn_badges[index]) { | 558 if (!s_vpn_badges[index]) { |
529 // Lazily cache images. | 559 // Lazily cache images. |
530 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 560 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
531 gfx::ImageSkia* icon = | |
532 rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); // For size | |
533 gfx::ImageSkia* badge = | 561 gfx::ImageSkia* badge = |
534 rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); | 562 rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); |
535 s_vpn_badges[index] = new gfx::ImageSkia( | 563 s_vpn_badges[index] = new gfx::ImageSkia( |
536 gfx::ImageSkiaOperations::CreateBlendedImage( | 564 gfx::ImageSkiaOperations::CreateTransparentImage(*badge, animation)); |
537 gfx::ImageSkia(new EmptyImageSource(icon->size()), icon->size()), | |
538 *badge, | |
539 animation)); | |
540 } | 565 } |
541 return s_vpn_badges[index]; | 566 return s_vpn_badges[index]; |
542 } | 567 } |
543 | 568 |
544 int StrengthIndex(int strength) { | 569 int StrengthIndex(int strength) { |
545 // Return an index in the range [1, kNumNetworkImages - 1]. | 570 // Return an index in the range [1, kNumNetworkImages - 1]. |
546 const float findex = (static_cast<float>(strength) / 100.0f) * | 571 const float findex = (static_cast<float>(strength) / 100.0f) * |
547 nextafter(static_cast<float>(kNumNetworkImages - 1), 0); | 572 nextafter(static_cast<float>(kNumNetworkImages - 1), 0); |
548 int index = 1 + static_cast<int>(findex); | 573 int index = 1 + static_cast<int>(findex); |
549 index = std::max(std::min(index, kNumNetworkImages - 1), 1); | 574 index = std::max(std::min(index, kNumNetworkImages - 1), 1); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 } else if (technology == shill::kNetworkTechnologyLteAdvanced) { | 613 } else if (technology == shill::kNetworkTechnologyLteAdvanced) { |
589 id = IconTypeIsDark(icon_type) ? | 614 id = IconTypeIsDark(icon_type) ? |
590 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_DARK : | 615 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_DARK : |
591 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_LIGHT; | 616 IDR_AURA_UBER_TRAY_NETWORK_LTE_ADVANCED_LIGHT; |
592 } else if (technology == shill::kNetworkTechnologyGsm) { | 617 } else if (technology == shill::kNetworkTechnologyGsm) { |
593 id = IconTypeIsDark(icon_type) ? | 618 id = IconTypeIsDark(icon_type) ? |
594 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : | 619 IDR_AURA_UBER_TRAY_NETWORK_GPRS_DARK : |
595 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; | 620 IDR_AURA_UBER_TRAY_NETWORK_GPRS_LIGHT; |
596 } | 621 } |
597 if (id == kUnknownBadgeType) | 622 if (id == kUnknownBadgeType) |
598 return NULL; | 623 return nullptr; |
599 else | 624 else |
600 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); | 625 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id); |
601 } | 626 } |
602 | 627 |
603 const gfx::ImageSkia* BadgeForVPN(IconType icon_type) { | |
604 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
605 IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); | |
606 } | |
607 | |
608 gfx::ImageSkia GetIcon(const NetworkState* network, | 628 gfx::ImageSkia GetIcon(const NetworkState* network, |
609 IconType icon_type, | 629 IconType icon_type, |
610 int strength_index) { | 630 int strength_index) { |
611 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 631 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
612 if (network->Matches(NetworkTypePattern::Ethernet())) { | 632 if (network->Matches(NetworkTypePattern::Ethernet())) { |
613 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 633 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
614 } else if (network->Matches(NetworkTypePattern::Wireless())) { | 634 } else if (network->Matches(NetworkTypePattern::Wireless())) { |
615 DCHECK(strength_index > 0); | 635 DCHECK(strength_index > 0); |
616 return GetImageForIndex(ImageTypeForNetworkType(network->type()), icon_type, | 636 return GetImageForIndex(ImageTypeForNetworkType(network->type()), icon_type, |
617 strength_index); | 637 strength_index); |
618 } else if (network->Matches(NetworkTypePattern::VPN())) { | 638 } else if (network->Matches(NetworkTypePattern::VPN())) { |
619 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); | 639 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); |
620 } else { | 640 } else { |
621 LOG(WARNING) << "Request for icon for unsupported type: " | 641 LOG(WARNING) << "Request for icon for unsupported type: " |
622 << network->type(); | 642 << network->type(); |
623 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 643 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
624 } | 644 } |
625 } | 645 } |
626 | 646 |
627 //------------------------------------------------------------------------------ | 647 //------------------------------------------------------------------------------ |
628 // Get connecting images | 648 // Get connecting images |
629 | 649 |
630 gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) { | 650 gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) { |
631 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 651 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
632 const NetworkState* connected_network = NULL; | 652 const NetworkState* connected_network = nullptr; |
633 if (icon_type == ICON_TYPE_TRAY) { | 653 if (icon_type == ICON_TYPE_TRAY) { |
634 connected_network = | 654 connected_network = |
635 handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual()); | 655 handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual()); |
636 } | 656 } |
637 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); | 657 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); |
638 | 658 |
| 659 gfx::ImageSkia icon; |
| 660 Badges badges; |
639 if (connected_network) { | 661 if (connected_network) { |
640 gfx::ImageSkia icon = GetImageForNetwork(connected_network, icon_type); | 662 icon = GetImageForNetwork(connected_network, icon_type); |
641 Badges badges; | |
642 badges.bottom_left = ConnectingVpnBadge(animation); | 663 badges.bottom_left = ConnectingVpnBadge(animation); |
643 return gfx::ImageSkia( | |
644 new NetworkIconImageSource(icon, badges), icon.size()); | |
645 } else { | 664 } else { |
646 gfx::ImageSkia* icon = ConnectingVpnImage(animation); | 665 icon = *ConnectingVpnImage(animation); |
647 return gfx::ImageSkia( | |
648 new NetworkIconImageSource(*icon, Badges()), icon->size()); | |
649 } | 666 } |
| 667 return UseMd() ? NetworkIconImageSourceMd::CreateImage(icon, badges) |
| 668 : gfx::ImageSkia(new NetworkIconImageSource(icon, badges), |
| 669 icon.size()); |
650 } | 670 } |
651 | 671 |
652 gfx::ImageSkia GetConnectingImage(IconType icon_type, | 672 gfx::ImageSkia GetConnectingImage(IconType icon_type, |
653 const std::string& network_type) { | 673 const std::string& network_type) { |
654 if (network_type == shill::kTypeVPN) | 674 if (network_type == shill::kTypeVPN) |
655 return GetConnectingVpnImage(icon_type); | 675 return GetConnectingVpnImage(icon_type); |
656 | 676 |
657 ImageType image_type = ImageTypeForNetworkType(network_type); | 677 ImageType image_type = ImageTypeForNetworkType(network_type); |
658 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); | 678 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); |
659 | 679 |
660 gfx::ImageSkia* icon = ConnectingWirelessImage( | 680 gfx::ImageSkia* icon = ConnectingWirelessImage( |
661 image_type, icon_type, animation); | 681 image_type, icon_type, animation); |
662 return gfx::ImageSkia( | 682 return UseMd() ? NetworkIconImageSourceMd::CreateImage(*icon, Badges()) |
663 new NetworkIconImageSource(*icon, Badges()), icon->size()); | 683 : gfx::ImageSkia(new NetworkIconImageSource(*icon, Badges()), |
| 684 icon->size()); |
664 } | 685 } |
665 | 686 |
666 } // namespace | 687 } // namespace |
667 | 688 |
668 //------------------------------------------------------------------------------ | 689 //------------------------------------------------------------------------------ |
669 // NetworkIconImpl | 690 // NetworkIconImpl |
670 | 691 |
671 NetworkIconImpl::NetworkIconImpl(const std::string& path, IconType icon_type) | 692 NetworkIconImpl::NetworkIconImpl(const std::string& path, IconType icon_type) |
672 : network_path_(path), | 693 : network_path_(path), |
673 icon_type_(icon_type), | 694 icon_type_(icon_type), |
674 strength_index_(-1), | 695 strength_index_(-1), |
675 technology_badge_(NULL), | 696 technology_badge_(nullptr), |
676 vpn_badge_(NULL), | 697 vpn_badge_(nullptr), |
677 behind_captive_portal_(false) { | 698 behind_captive_portal_(false) { |
678 // Default image | 699 // Default image |
679 image_ = GetDisconnectedImage(icon_type, shill::kTypeWifi); | 700 image_ = GetDisconnectedImage(icon_type, shill::kTypeWifi); |
680 } | 701 } |
681 | 702 |
682 void NetworkIconImpl::Update(const NetworkState* network) { | 703 void NetworkIconImpl::Update(const NetworkState* network) { |
683 DCHECK(network); | 704 DCHECK(network); |
684 // Determine whether or not we need to update the icon. | 705 // Determine whether or not we need to update the icon. |
685 bool dirty = image_.isNull(); | 706 bool dirty = image_.isNull(); |
686 | 707 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 | 768 |
748 if (behind_captive_portal == behind_captive_portal_) | 769 if (behind_captive_portal == behind_captive_portal_) |
749 return false; | 770 return false; |
750 behind_captive_portal_ = behind_captive_portal; | 771 behind_captive_portal_ = behind_captive_portal; |
751 return true; | 772 return true; |
752 } | 773 } |
753 | 774 |
754 bool NetworkIconImpl::UpdateVPNBadge() { | 775 bool NetworkIconImpl::UpdateVPNBadge() { |
755 const NetworkState* vpn = NetworkHandler::Get()->network_state_handler()-> | 776 const NetworkState* vpn = NetworkHandler::Get()->network_state_handler()-> |
756 ConnectedNetworkByType(NetworkTypePattern::VPN()); | 777 ConnectedNetworkByType(NetworkTypePattern::VPN()); |
757 if (vpn && vpn_badge_ == NULL) { | 778 if (vpn && vpn_badge_ == nullptr) { |
758 vpn_badge_ = BadgeForVPN(icon_type_); | 779 vpn_badge_ = ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 780 IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); |
759 return true; | 781 return true; |
760 } else if (!vpn && vpn_badge_ != NULL) { | 782 } else if (!vpn && vpn_badge_ != nullptr) { |
761 vpn_badge_ = NULL; | 783 vpn_badge_ = nullptr; |
762 return true; | 784 return true; |
763 } | 785 } |
764 return false; | 786 return false; |
765 } | 787 } |
766 | 788 |
767 void NetworkIconImpl::GetBadges(const NetworkState* network, Badges* badges) { | 789 void NetworkIconImpl::GetBadges(const NetworkState* network, Badges* badges) { |
768 DCHECK(network); | 790 DCHECK(network); |
769 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 791 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
770 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 792 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
771 | 793 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 IDR_AURA_UBER_TRAY_NETWORK_PORTAL_LIGHT); | 830 IDR_AURA_UBER_TRAY_NETWORK_PORTAL_LIGHT); |
809 badges->bottom_right = badge; | 831 badges->bottom_right = badge; |
810 } | 832 } |
811 } | 833 } |
812 | 834 |
813 void NetworkIconImpl::GenerateImage(const NetworkState* network) { | 835 void NetworkIconImpl::GenerateImage(const NetworkState* network) { |
814 DCHECK(network); | 836 DCHECK(network); |
815 gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_); | 837 gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_); |
816 Badges badges; | 838 Badges badges; |
817 GetBadges(network, &badges); | 839 GetBadges(network, &badges); |
818 image_ = gfx::ImageSkia( | 840 image_ = UseMd() ? NetworkIconImageSourceMd::CreateImage(icon, badges) |
819 new NetworkIconImageSource(icon, badges), icon.size()); | 841 : gfx::ImageSkia(new NetworkIconImageSource(icon, badges), |
| 842 icon.size()); |
820 } | 843 } |
821 | 844 |
822 namespace { | 845 namespace { |
823 | 846 |
824 NetworkIconImpl* FindAndUpdateImageImpl(const NetworkState* network, | 847 NetworkIconImpl* FindAndUpdateImageImpl(const NetworkState* network, |
825 IconType icon_type) { | 848 IconType icon_type) { |
826 // Find or add the icon. | 849 // Find or add the icon. |
827 NetworkIconMap* icon_map = GetIconMap(icon_type); | 850 NetworkIconMap* icon_map = GetIconMap(icon_type); |
828 NetworkIconImpl* icon; | 851 NetworkIconImpl* icon; |
829 NetworkIconMap::iterator iter = icon_map->find(network->path()); | 852 NetworkIconMap::iterator iter = icon_map->find(network->path()); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 iter != networks.end(); ++iter) { | 1059 iter != networks.end(); ++iter) { |
1037 network_paths.insert((*iter)->path()); | 1060 network_paths.insert((*iter)->path()); |
1038 } | 1061 } |
1039 PurgeIconMap(ICON_TYPE_TRAY, network_paths); | 1062 PurgeIconMap(ICON_TYPE_TRAY, network_paths); |
1040 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); | 1063 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); |
1041 PurgeIconMap(ICON_TYPE_LIST, network_paths); | 1064 PurgeIconMap(ICON_TYPE_LIST, network_paths); |
1042 } | 1065 } |
1043 | 1066 |
1044 } // namespace network_icon | 1067 } // namespace network_icon |
1045 } // namespace ui | 1068 } // namespace ui |
OLD | NEW |