| 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 "ash/system/chromeos/network/network_icon.h" | 5 #include "ash/system/chromeos/network/network_icon.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/system/chromeos/network/network_icon_animation.h" |
| 8 #include "ash/system/chromeos/network/network_icon_animation_observer.h" | 9 #include "ash/system/chromeos/network/network_icon_animation_observer.h" |
| 9 #include "base/observer_list.h" | |
| 10 #include "chromeos/network/device_state.h" | 10 #include "chromeos/network/device_state.h" |
| 11 #include "chromeos/network/network_state.h" | 11 #include "chromeos/network/network_state.h" |
| 12 #include "chromeos/network/network_state_handler.h" | 12 #include "chromeos/network/network_state_handler.h" |
| 13 #include "grit/ash_resources.h" | 13 #include "grit/ash_resources.h" |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 15 #include "ui/base/animation/animation_delegate.h" | |
| 16 #include "ui/base/animation/throb_animation.h" | |
| 17 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/image/image_skia_operations.h" | 17 #include "ui/gfx/image/image_skia_operations.h" |
| 20 #include "ui/gfx/image/image_skia_source.h" | 18 #include "ui/gfx/image/image_skia_source.h" |
| 21 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 22 #include "ui/gfx/size_conversions.h" | 20 #include "ui/gfx/size_conversions.h" |
| 23 | 21 |
| 24 using chromeos::DeviceState; | 22 using chromeos::DeviceState; |
| 25 using chromeos::NetworkStateHandler; | 23 using chromeos::NetworkStateHandler; |
| 26 using chromeos::NetworkState; | 24 using chromeos::NetworkState; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 top_right(NULL), | 110 top_right(NULL), |
| 113 bottom_left(NULL), | 111 bottom_left(NULL), |
| 114 bottom_right(NULL) { | 112 bottom_right(NULL) { |
| 115 } | 113 } |
| 116 const gfx::ImageSkia* top_left; | 114 const gfx::ImageSkia* top_left; |
| 117 const gfx::ImageSkia* top_right; | 115 const gfx::ImageSkia* top_right; |
| 118 const gfx::ImageSkia* bottom_left; | 116 const gfx::ImageSkia* bottom_left; |
| 119 const gfx::ImageSkia* bottom_right; | 117 const gfx::ImageSkia* bottom_right; |
| 120 }; | 118 }; |
| 121 | 119 |
| 122 // Animation cycle length. | |
| 123 const int kThrobDurationMs = 750; | |
| 124 | |
| 125 // Amount to fade icons while connecting. | 120 // Amount to fade icons while connecting. |
| 126 const double kConnectingImageAlpha = 0.5; | 121 const double kConnectingImageAlpha = 0.5; |
| 127 | 122 |
| 128 // Images for strength bars for wired networks. | 123 // Images for strength bars for wired networks. |
| 129 const int kNumBarsImages = 5; | 124 const int kNumBarsImages = 5; |
| 130 gfx::ImageSkia* kBarsImagesAnimatingDark[kNumBarsImages - 1]; | 125 gfx::ImageSkia* kBarsImagesAnimatingDark[kNumBarsImages - 1]; |
| 131 gfx::ImageSkia* kBarsImagesAnimatingLight[kNumBarsImages - 1]; | 126 gfx::ImageSkia* kBarsImagesAnimatingLight[kNumBarsImages - 1]; |
| 132 | 127 |
| 133 // Imagaes for strength arcs for wireless networks. | 128 // Imagaes for strength arcs for wireless networks. |
| 134 const int kNumArcsImages = 5; | 129 const int kNumArcsImages = 5; |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_LIGHT); | 389 IDR_AURA_UBER_TRAY_NETWORK_ROAMING_LIGHT); |
| 395 } | 390 } |
| 396 } | 391 } |
| 397 if (!network->IsConnectingState()) { | 392 if (!network->IsConnectingState()) { |
| 398 badges->top_left = BadgeForNetworkTechnology(network, color); | 393 badges->top_left = BadgeForNetworkTechnology(network, color); |
| 399 } | 394 } |
| 400 } | 395 } |
| 401 } | 396 } |
| 402 | 397 |
| 403 //------------------------------------------------------------------------------ | 398 //------------------------------------------------------------------------------ |
| 404 // Handle connecting images | 399 // Get connecting images |
| 405 | |
| 406 class ConnectingAnimation : public ui::AnimationDelegate { | |
| 407 public: | |
| 408 ConnectingAnimation() | |
| 409 : ALLOW_THIS_IN_INITIALIZER_LIST(animation_(this)) { | |
| 410 // Set up the animation throbber. | |
| 411 animation_.SetThrobDuration(kThrobDurationMs); | |
| 412 animation_.SetTweenType(ui::Tween::LINEAR); | |
| 413 } | |
| 414 | |
| 415 virtual ~ConnectingAnimation() {} | |
| 416 | |
| 417 // ui::AnimationDelegate implementation. | |
| 418 virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE { | |
| 419 if (animation == &animation_) { | |
| 420 FOR_EACH_OBSERVER(AnimationObserver, observers_, NetworkIconChanged()); | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 double GetAnimation() { | |
| 425 if (!animation_.is_animating()) { | |
| 426 animation_.Reset(); | |
| 427 animation_.StartThrobbing(-1 /*throb indefinitely*/); | |
| 428 return 0; | |
| 429 } | |
| 430 return animation_.GetCurrentValue(); | |
| 431 } | |
| 432 | |
| 433 void AddObserver(AnimationObserver* observer) { | |
| 434 observers_.AddObserver(observer); | |
| 435 } | |
| 436 | |
| 437 void RemoveObserver(AnimationObserver* observer) { | |
| 438 observers_.RemoveObserver(observer); | |
| 439 if (observers_.size() == 0) | |
| 440 animation_.Stop(); | |
| 441 } | |
| 442 | |
| 443 private: | |
| 444 ui::ThrobAnimation animation_; | |
| 445 ObserverList<AnimationObserver> observers_; | |
| 446 }; | |
| 447 | |
| 448 ConnectingAnimation* GetConnectingAnimation() { | |
| 449 static ConnectingAnimation* s_connecting_animation = | |
| 450 new ConnectingAnimation(); | |
| 451 return s_connecting_animation; | |
| 452 } | |
| 453 | 400 |
| 454 gfx::ImageSkia GetConnectingImage(const std::string& type, | 401 gfx::ImageSkia GetConnectingImage(const std::string& type, |
| 455 ResourceColorTheme color) { | 402 ResourceColorTheme color) { |
| 456 ImageType image_type = (type == flimflam::kTypeWifi) ? ARCS : BARS; | 403 ImageType image_type = (type == flimflam::kTypeWifi) ? ARCS : BARS; |
| 457 int image_count = NumImagesForType(image_type) - 1; | 404 int image_count = NumImagesForType(image_type) - 1; |
| 458 gfx::ImageSkia** images = ImageListForType(image_type, color); | 405 gfx::ImageSkia** images = ImageListForType(image_type, color); |
| 459 double animation = GetConnectingAnimation()->GetAnimation(); | 406 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); |
| 460 int index = animation * nextafter(static_cast<float>(image_count), 0); | 407 int index = animation * nextafter(static_cast<float>(image_count), 0); |
| 461 index = std::max(std::min(index, image_count - 1), 0); | 408 index = std::max(std::min(index, image_count - 1), 0); |
| 462 | 409 |
| 463 // Lazily cache images. | 410 // Lazily cache images. |
| 464 if (!images[index]) { | 411 if (!images[index]) { |
| 465 gfx::ImageSkia source = GetImageForIndex(image_type, color, index + 1); | 412 gfx::ImageSkia source = GetImageForIndex(image_type, color, index + 1); |
| 466 images[index] = new gfx::ImageSkia( | 413 images[index] = new gfx::ImageSkia( |
| 467 gfx::ImageSkiaOperations::CreateBlendedImage( | 414 gfx::ImageSkiaOperations::CreateBlendedImage( |
| 468 gfx::ImageSkia(new EmptyImageSource(source.size()), source.size()), | 415 gfx::ImageSkia(new EmptyImageSource(source.size()), source.size()), |
| 469 source, | 416 source, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 } | 493 } |
| 547 | 494 |
| 548 //------------------------------------------------------------------------------ | 495 //------------------------------------------------------------------------------ |
| 549 // Public interface | 496 // Public interface |
| 550 | 497 |
| 551 gfx::ImageSkia GetImageForNetwork(const NetworkState* network, | 498 gfx::ImageSkia GetImageForNetwork(const NetworkState* network, |
| 552 ResourceColorTheme color, | 499 ResourceColorTheme color, |
| 553 AnimationObserver* observer) { | 500 AnimationObserver* observer) { |
| 554 if (network->IsConnectingState()) { | 501 if (network->IsConnectingState()) { |
| 555 if (observer) | 502 if (observer) |
| 556 GetConnectingAnimation()->AddObserver(observer); | 503 NetworkIconAnimation::GetInstance()->AddObserver(observer); |
| 557 return GetConnectingImage(network->type(), color); | 504 return GetConnectingImage(network->type(), color); |
| 558 } | 505 } |
| 559 // Not connecting, remove observer. | 506 // Not connecting, remove observer. |
| 560 if (observer) | 507 if (observer) |
| 561 GetConnectingAnimation()->RemoveObserver(observer); | 508 NetworkIconAnimation::GetInstance()->RemoveObserver(observer); |
| 562 | 509 |
| 563 NetworkIconMap* icon_map = GetIconMap(color); | 510 NetworkIconMap* icon_map = GetIconMap(color); |
| 564 | 511 |
| 565 // Find or add the icon. | 512 // Find or add the icon. |
| 566 NetworkIconImpl* icon; | 513 NetworkIconImpl* icon; |
| 567 NetworkIconMap::iterator iter = icon_map->find(network->path()); | 514 NetworkIconMap::iterator iter = icon_map->find(network->path()); |
| 568 if (iter == icon_map->end()) { | 515 if (iter == icon_map->end()) { |
| 569 icon = new NetworkIconImpl(network->path(), color); | 516 icon = new NetworkIconImpl(network->path(), color); |
| 570 icon_map->insert(std::make_pair(network->path(), icon)); | 517 icon_map->insert(std::make_pair(network->path(), icon)); |
| 571 } else { | 518 } else { |
| 572 icon = iter->second; | 519 icon = iter->second; |
| 573 } | 520 } |
| 574 | 521 |
| 575 // Update and return the icon's image. | 522 // Update and return the icon's image. |
| 576 icon->Update(network); | 523 icon->Update(network); |
| 577 return icon->image(); | 524 return icon->image(); |
| 578 } | 525 } |
| 579 | 526 |
| 580 } // namespace network_icon | 527 } // namespace network_icon |
| 581 } // namespace ash | 528 } // namespace ash |
| OLD | NEW |