| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_button.h" | 5 #include "chrome/browser/chromeos/status/network_menu_button.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 canvas->DrawBitmapInt(IconForDisplay( | 215 canvas->DrawBitmapInt(IconForDisplay( |
| 216 *ResourceBundle::GetSharedInstance(). | 216 *ResourceBundle::GetSharedInstance(). |
| 217 GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS_PRESSED), SkBitmap()), | 217 GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS_PRESSED), SkBitmap()), |
| 218 0, 0); | 218 0, 0); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { | 221 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { |
| 222 canvas->DrawBitmapInt(IconForDisplay(icon(), badge()), 0, 0); | 222 canvas->DrawBitmapInt(IconForDisplay(icon(), badge()), 0, 0); |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Override the DrawIcon method to draw the wifi icon. | |
| 226 // The wifi icon is composed of 1 or more alpha-blended icons to show the | |
| 227 // network strength. We also draw an animation for when there's upload/download | |
| 228 // traffic. | |
| 229 /* TODO(chocobo): Add this code back in when UI is finalized. | |
| 230 void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { | |
| 231 | |
| 232 // First draw the base icon. | |
| 233 canvas->DrawBitmapInt(icon(), 0, 0); | |
| 234 | |
| 235 // If wifi, we draw the wifi signal bars. | |
| 236 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | |
| 237 if (cros->wifi_connecting() || | |
| 238 (!cros->ethernet_connected() && cros->wifi_connected())) { | |
| 239 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 240 // We want a value between 0-1. | |
| 241 // 0 reperesents no signal and 1 represents full signal strength. | |
| 242 double value = cros->wifi_connecting() ? | |
| 243 animation_connecting_.GetCurrentValue() : | |
| 244 cros->wifi_strength() / 100.0; | |
| 245 if (value < 0) | |
| 246 value = 0; | |
| 247 else if (value > 1) | |
| 248 value = 1; | |
| 249 | |
| 250 // If we are animating network traffic and not connecting, then we need to | |
| 251 // figure out if we are to also draw the extra image. | |
| 252 int downloading_index = -1; | |
| 253 int uploading_index = -1; | |
| 254 if (!animation_connecting_.is_animating()) { | |
| 255 // For network animation, we only show animation in one direction. | |
| 256 // So when we are hiding, we just use 1 minus the value. | |
| 257 // We have kNumWifiImages + 1 number of states. For the first state, where | |
| 258 // we are not adding any images, we set the index to -1. | |
| 259 if (animation_downloading_.is_animating()) { | |
| 260 double value_downloading = animation_downloading_.IsShowing() ? | |
| 261 animation_downloading_.GetCurrentValue() : | |
| 262 1.0 - animation_downloading_.GetCurrentValue(); | |
| 263 downloading_index = static_cast<int>(value_downloading * | |
| 264 nextafter(static_cast<float>(kNumWifiImages + 1), 0)) - 1; | |
| 265 } | |
| 266 if (animation_uploading_.is_animating()) { | |
| 267 double value_uploading = animation_uploading_.IsShowing() ? | |
| 268 animation_uploading_.GetCurrentValue() : | |
| 269 1.0 - animation_uploading_.GetCurrentValue(); | |
| 270 uploading_index = static_cast<int>(value_uploading * | |
| 271 nextafter(static_cast<float>(kNumWifiImages + 1), 0)) - 1; | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 // We need to determine opacity for each of the kNumWifiImages images. | |
| 276 // We split the range (0-1) into equal ranges per kNumWifiImages images. | |
| 277 // For example if kNumWifiImages is 3, then [0-0.33) is the first image and | |
| 278 // [0.33-0.66) is the second image and [0.66-1] is the last image. | |
| 279 // For each of the image: | |
| 280 // If value < the range of this image, draw at kMinOpacity opacity. | |
| 281 // If value > the range of this image, draw at kMaxOpacity-1 opacity. | |
| 282 // If value within the range of this image, draw at an opacity value | |
| 283 // between kMinOpacity and kMaxOpacity-1 relative to where in the range | |
| 284 // value is at. | |
| 285 // NOTE: Use an array rather than just calculating a resource number to | |
| 286 // avoid creating implicit ordering dependencies on the resource values. | |
| 287 static const int kWifiUpImages[kNumWifiImages] = { | |
| 288 IDR_STATUSBAR_WIFI_UP1, | |
| 289 IDR_STATUSBAR_WIFI_UP2, | |
| 290 IDR_STATUSBAR_WIFI_UP3, | |
| 291 IDR_STATUSBAR_WIFI_UP4, | |
| 292 IDR_STATUSBAR_WIFI_UP5, | |
| 293 IDR_STATUSBAR_WIFI_UP6, | |
| 294 IDR_STATUSBAR_WIFI_UP7, | |
| 295 IDR_STATUSBAR_WIFI_UP8, | |
| 296 IDR_STATUSBAR_WIFI_UP9, | |
| 297 }; | |
| 298 static const int kWifiUpPImages[kNumWifiImages] = { | |
| 299 IDR_STATUSBAR_WIFI_UP1P, | |
| 300 IDR_STATUSBAR_WIFI_UP2P, | |
| 301 IDR_STATUSBAR_WIFI_UP3P, | |
| 302 IDR_STATUSBAR_WIFI_UP4P, | |
| 303 IDR_STATUSBAR_WIFI_UP5P, | |
| 304 IDR_STATUSBAR_WIFI_UP6P, | |
| 305 IDR_STATUSBAR_WIFI_UP7P, | |
| 306 IDR_STATUSBAR_WIFI_UP8P, | |
| 307 IDR_STATUSBAR_WIFI_UP9P, | |
| 308 }; | |
| 309 static const int kWifiDownImages[kNumWifiImages] = { | |
| 310 IDR_STATUSBAR_WIFI_DOWN1, | |
| 311 IDR_STATUSBAR_WIFI_DOWN2, | |
| 312 IDR_STATUSBAR_WIFI_DOWN3, | |
| 313 IDR_STATUSBAR_WIFI_DOWN4, | |
| 314 IDR_STATUSBAR_WIFI_DOWN5, | |
| 315 IDR_STATUSBAR_WIFI_DOWN6, | |
| 316 IDR_STATUSBAR_WIFI_DOWN7, | |
| 317 IDR_STATUSBAR_WIFI_DOWN8, | |
| 318 IDR_STATUSBAR_WIFI_DOWN9, | |
| 319 }; | |
| 320 static const int kWifiDownPImages[kNumWifiImages] = { | |
| 321 IDR_STATUSBAR_WIFI_DOWN1P, | |
| 322 IDR_STATUSBAR_WIFI_DOWN2P, | |
| 323 IDR_STATUSBAR_WIFI_DOWN3P, | |
| 324 IDR_STATUSBAR_WIFI_DOWN4P, | |
| 325 IDR_STATUSBAR_WIFI_DOWN5P, | |
| 326 IDR_STATUSBAR_WIFI_DOWN6P, | |
| 327 IDR_STATUSBAR_WIFI_DOWN7P, | |
| 328 IDR_STATUSBAR_WIFI_DOWN8P, | |
| 329 IDR_STATUSBAR_WIFI_DOWN9P, | |
| 330 }; | |
| 331 | |
| 332 double value_per_image = 1.0 / kNumWifiImages; | |
| 333 SkPaint paint; | |
| 334 for (int i = 0; i < kNumWifiImages; i++) { | |
| 335 if (value > value_per_image) { | |
| 336 paint.setAlpha(kMaxOpacity - 1); | |
| 337 value -= value_per_image; | |
| 338 } else { | |
| 339 // Map value between 0 and value_per_image to [kMinOpacity,kMaxOpacity). | |
| 340 paint.setAlpha(kMinOpacity + static_cast<int>(value / value_per_image * | |
| 341 nextafter(static_cast<float>(kMaxOpacity - kMinOpacity), 0))); | |
| 342 // For following iterations, we want to draw at kMinOpacity. | |
| 343 // So we set value to 0 here. | |
| 344 value = 0; | |
| 345 } | |
| 346 canvas->DrawBitmapInt(*rb.GetBitmapNamed(kWifiUpImages[i]), 0, 0, paint); | |
| 347 canvas->DrawBitmapInt(*rb.GetBitmapNamed(kWifiDownImages[i]), 0, 0, | |
| 348 paint); | |
| 349 | |
| 350 // Draw network traffic downloading/uploading image if necessary. | |
| 351 if (i == downloading_index) { | |
| 352 canvas->DrawBitmapInt(*rb.GetBitmapNamed(kWifiDownPImages[i]), 0, 0, | |
| 353 paint); | |
| 354 } | |
| 355 if (i == uploading_index) { | |
| 356 canvas->DrawBitmapInt(*rb.GetBitmapNamed(kWifiUpPImages[i]), 0, 0, | |
| 357 paint); | |
| 358 } | |
| 359 } | |
| 360 } | |
| 361 } | |
| 362 */ | |
| 363 //////////////////////////////////////////////////////////////////////////////// | 225 //////////////////////////////////////////////////////////////////////////////// |
| 364 // NetworkMenuButton, NetworkLibrary::Observer implementation: | 226 // NetworkMenuButton, NetworkLibrary::Observer implementation: |
| 365 | 227 |
| 366 void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { | 228 void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { |
| 367 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 229 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 368 if (CrosLibrary::Get()->EnsureLoaded()) { | 230 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 369 if (cros->wifi_connecting() || cros->cellular_connecting()) { | 231 if (cros->wifi_connecting() || cros->cellular_connecting()) { |
| 370 // Start the connecting animation if not running. | 232 // Start the connecting animation if not running. |
| 371 if (!animation_connecting_.is_animating()) { | 233 if (!animation_connecting_.is_animating()) { |
| 372 animation_connecting_.Reset(); | 234 animation_connecting_.Reset(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 399 SetBadge(SkBitmap()); | 261 SetBadge(SkBitmap()); |
| 400 } | 262 } |
| 401 } else { | 263 } else { |
| 402 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); | 264 SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); |
| 403 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); | 265 SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); |
| 404 } | 266 } |
| 405 | 267 |
| 406 SchedulePaint(); | 268 SchedulePaint(); |
| 407 } | 269 } |
| 408 | 270 |
| 409 void NetworkMenuButton::NetworkTraffic(NetworkLibrary* cros, int traffic_type) { | |
| 410 /* TODO(chocobo): Add this code back in when network traffic UI is finalized. | |
| 411 if (!cros->ethernet_connected() && cros->wifi_connected() && | |
| 412 !cros->wifi_connecting()) { | |
| 413 // For downloading/uploading animation, we want to force at least one cycle | |
| 414 // so that it looks smooth. And if we keep downloading/uploading, we will | |
| 415 // keep calling StartThrobbing which will update the cycle count back to 2. | |
| 416 if (traffic_type & TRAFFIC_DOWNLOAD) | |
| 417 animation_downloading_.StartThrobbing(2); | |
| 418 if (traffic_type & TRAFFIC_UPLOAD) | |
| 419 animation_uploading_.StartThrobbing(2); | |
| 420 } | |
| 421 */ | |
| 422 } | |
| 423 | |
| 424 void NetworkMenuButton::SetBadge(const SkBitmap& badge) { | 271 void NetworkMenuButton::SetBadge(const SkBitmap& badge) { |
| 425 badge_ = badge; | 272 badge_ = badge; |
| 426 } | 273 } |
| 427 | 274 |
| 428 // static | 275 // static |
| 429 SkBitmap NetworkMenuButton::IconForNetworkStrength(int strength, bool black) { | 276 SkBitmap NetworkMenuButton::IconForNetworkStrength(int strength, bool black) { |
| 430 // Compose wifi icon by superimposing various icons. | 277 // Compose wifi icon by superimposing various icons. |
| 431 // NOTE: Use an array rather than just calculating a resource number to avoid | 278 // NOTE: Use an array rather than just calculating a resource number to avoid |
| 432 // creating implicit ordering dependencies on the resource values. | 279 // creating implicit ordering dependencies on the resource values. |
| 433 static const int kBarsImages[kNumWifiImages] = { | 280 static const int kBarsImages[kNumWifiImages] = { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 if (host_->ShouldOpenButtonOptions(this)) { | 449 if (host_->ShouldOpenButtonOptions(this)) { |
| 603 label = | 450 label = |
| 604 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG); | 451 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG); |
| 605 menu_items_.push_back(MenuItem(menus::MenuModel::TYPE_COMMAND, label, | 452 menu_items_.push_back(MenuItem(menus::MenuModel::TYPE_COMMAND, label, |
| 606 SkBitmap(), std::string(), FLAG_OPTIONS)); | 453 SkBitmap(), std::string(), FLAG_OPTIONS)); |
| 607 } | 454 } |
| 608 } | 455 } |
| 609 } | 456 } |
| 610 | 457 |
| 611 } // namespace chromeos | 458 } // namespace chromeos |
| OLD | NEW |