Chromium Code Reviews| 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" |
| 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 "chromeos/network/portal_detector/network_portal_detector.h" | 13 #include "chromeos/network/portal_detector/network_portal_detector.h" |
| 14 #include "grit/ui_chromeos_resources.h" | 14 #include "grit/ui_chromeos_resources.h" |
| 15 #include "grit/ui_chromeos_strings.h" | 15 #include "grit/ui_chromeos_strings.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.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/base/webui/web_ui_util.h" | 19 #include "ui/base/webui/web_ui_util.h" |
| 20 #include "ui/chromeos/material_design_icon_controller.h" | |
| 20 #include "ui/chromeos/network/network_icon_animation.h" | 21 #include "ui/chromeos/network/network_icon_animation.h" |
| 21 #include "ui/chromeos/network/network_icon_animation_observer.h" | 22 #include "ui/chromeos/network/network_icon_animation_observer.h" |
| 22 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
| 24 #include "ui/gfx/color_palette.h" | |
| 25 #include "ui/gfx/geometry/insets.h" | |
| 23 #include "ui/gfx/geometry/rect.h" | 26 #include "ui/gfx/geometry/rect.h" |
| 24 #include "ui/gfx/geometry/size_conversions.h" | 27 #include "ui/gfx/geometry/size_conversions.h" |
| 28 #include "ui/gfx/image/canvas_image_source.h" | |
| 25 #include "ui/gfx/image/image_skia_operations.h" | 29 #include "ui/gfx/image/image_skia_operations.h" |
| 26 #include "ui/gfx/image/image_skia_source.h" | 30 #include "ui/gfx/image/image_skia_source.h" |
| 31 #include "ui/gfx/skia_util.h" | |
| 27 | 32 |
| 28 using chromeos::DeviceState; | 33 using chromeos::DeviceState; |
| 29 using chromeos::NetworkConnectionHandler; | 34 using chromeos::NetworkConnectionHandler; |
| 30 using chromeos::NetworkHandler; | 35 using chromeos::NetworkHandler; |
| 31 using chromeos::NetworkPortalDetector; | 36 using chromeos::NetworkPortalDetector; |
| 32 using chromeos::NetworkState; | 37 using chromeos::NetworkState; |
| 33 using chromeos::NetworkStateHandler; | 38 using chromeos::NetworkStateHandler; |
| 34 using chromeos::NetworkTypePattern; | 39 using chromeos::NetworkTypePattern; |
| 35 | 40 |
| 36 namespace ui { | 41 namespace ui { |
| 37 namespace network_icon { | 42 namespace network_icon { |
| 38 | 43 |
| 39 namespace { | 44 namespace { |
| 40 | 45 |
| 46 // Size of the network connectivity icons in dip. | |
| 47 const int kNetworkIconSize = 16; | |
|
tdanderson
2016/07/28 22:00:49
Note the icon should be 16dp when displayed in the
| |
| 48 | |
| 41 //------------------------------------------------------------------------------ | 49 //------------------------------------------------------------------------------ |
| 42 // Struct to pass icon badges to NetworkIconImageSource. | 50 // Struct to pass icon badges to NetworkIconImageSource. |
| 43 struct Badges { | 51 struct Badges { |
| 44 Badges() | 52 Badges() |
| 45 : top_left(NULL), | 53 : top_left(NULL), |
| 46 top_right(NULL), | 54 top_right(NULL), |
| 47 bottom_left(NULL), | 55 bottom_left(NULL), |
| 48 bottom_right(NULL) { | 56 bottom_right(NULL) { |
| 49 } | 57 } |
| 50 const gfx::ImageSkia* top_left; | 58 const gfx::ImageSkia* top_left; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 BARS, | 174 BARS, |
| 167 NONE | 175 NONE |
| 168 }; | 176 }; |
| 169 | 177 |
| 170 // Amount to fade icons while connecting. | 178 // Amount to fade icons while connecting. |
| 171 const double kConnectingImageAlpha = 0.5; | 179 const double kConnectingImageAlpha = 0.5; |
| 172 | 180 |
| 173 // Images for strength bars for wired networks. | 181 // Images for strength bars for wired networks. |
| 174 const int kNumBarsImages = 5; | 182 const int kNumBarsImages = 5; |
| 175 | 183 |
| 176 // Imagaes for strength arcs for wireless networks. | 184 // Images for strength arcs for wireless networks. |
| 177 const int kNumArcsImages = 5; | 185 const int kNumArcsImages = 5; |
| 178 | 186 |
| 179 // Number of discrete images to use for alpha fade animation | 187 // Number of discrete images to use for alpha fade animation |
| 180 const int kNumFadeImages = 10; | 188 const int kNumFadeImages = 10; |
| 181 | 189 |
| 190 bool IconTypeIsDark(IconType icon_type) { | |
| 191 return (icon_type != ICON_TYPE_TRAY); | |
| 192 } | |
| 193 | |
| 194 bool IconTypeHasVPNBadge(IconType icon_type) { | |
| 195 return (icon_type != ICON_TYPE_LIST); | |
| 196 } | |
| 197 | |
| 198 int NumImagesForType(ImageType type) { | |
| 199 return (type == BARS) ? kNumBarsImages : kNumArcsImages; | |
| 200 } | |
| 201 | |
| 182 //------------------------------------------------------------------------------ | 202 //------------------------------------------------------------------------------ |
| 183 // Classes for generating scaled images. | 203 // Classes for generating scaled images. |
| 184 | 204 |
| 185 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) { | 205 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) { |
| 186 typedef std::pair<int, int> SizeKey; | 206 typedef std::pair<int, int> SizeKey; |
| 187 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; | 207 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; |
| 188 static SizeBitmapMap* s_empty_bitmaps = new SizeBitmapMap; | 208 static SizeBitmapMap* s_empty_bitmaps = new SizeBitmapMap; |
| 189 | 209 |
| 190 SizeKey key(pixel_size.width(), pixel_size.height()); | 210 SizeKey key(pixel_size.width(), pixel_size.height()); |
| 191 | 211 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 return canvas.ExtractImageRep(); | 270 return canvas.ExtractImageRep(); |
| 251 } | 271 } |
| 252 | 272 |
| 253 private: | 273 private: |
| 254 const gfx::ImageSkia icon_; | 274 const gfx::ImageSkia icon_; |
| 255 const Badges badges_; | 275 const Badges badges_; |
| 256 | 276 |
| 257 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); | 277 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); |
| 258 }; | 278 }; |
| 259 | 279 |
| 280 // Depicts a given signal strength using arcs (for WiFi connections). | |
| 281 class ArcsImageSource : public gfx::CanvasImageSource { | |
| 282 public: | |
| 283 explicit ArcsImageSource(IconType icon_type, float signal_strength) | |
|
oshima
2016/07/28 21:02:16
nit: no explicit
Evan Stade
2016/07/29 16:59:13
Done.
| |
| 284 : CanvasImageSource(gfx::Size(kNetworkIconSize, kNetworkIconSize), false), | |
| 285 icon_type_(icon_type), | |
| 286 signal_strength_(signal_strength) { | |
| 287 DCHECK_GE(signal_strength, 0); | |
| 288 DCHECK_LE(signal_strength, kMaxSignalStrength); | |
| 289 } | |
| 290 ~ArcsImageSource() override {} | |
| 291 | |
|
oshima
2016/07/28 21:02:15
nit: // gfx::CanvasImageSource:
Evan Stade
2016/07/29 16:59:13
Done.
| |
| 292 void Draw(gfx::Canvas* canvas) override { | |
| 293 gfx::RectF oval_bounds((gfx::Rect(size()))); | |
|
tdanderson
2016/07/28 22:00:49
nit: one layer of () not needed.
Evan Stade
2016/07/29 16:59:13
no, it's needed. Something about disambiguating fu
| |
| 294 // Padding between top of arc and top of canvas. | |
| 295 const int kIconInset = 2; | |
| 296 oval_bounds.Inset(gfx::Insets(kIconInset)); | |
| 297 // Double the width and height. The new midpoint should be the former | |
| 298 // bottom center. | |
| 299 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2, | |
| 300 -oval_bounds.height()); | |
| 301 | |
| 302 const SkScalar angle_above_horizontal = 51.f; | |
|
oshima
2016/07/28 21:02:16
nit:
kAngleAbove..
same for below.
Evan Stade
2016/07/29 16:59:13
Done.
| |
| 303 const SkScalar start_angle = 180.f + angle_above_horizontal; | |
| 304 const SkScalar sweep_angle = 180.f - 2 * angle_above_horizontal; | |
| 305 | |
| 306 SkPaint paint; | |
| 307 paint.setAntiAlias(true); | |
| 308 paint.setStyle(SkPaint::kFill_Style); | |
| 309 const SkColor base_color = | |
| 310 IconTypeIsDark(icon_type_) ? gfx::kChromeIconGrey : SK_ColorWHITE; | |
| 311 // Background. Skip drawing for full signal. | |
| 312 if (signal_strength_ != kMaxSignalStrength) { | |
| 313 paint.setColor(SkColorSetA(base_color, 0x4D)); | |
|
tdanderson
2016/07/28 22:00:49
Can we move the 0x4D somewhere common so it can be
Evan Stade
2016/07/29 16:59:13
yes, and I see that you rounded 0.3*255 down inste
stevenjb
2016/07/29 17:09:19
This code is used in src/chrome by network_menu.cc
| |
| 314 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), start_angle, | |
| 315 sweep_angle, true, paint); | |
| 316 } | |
| 317 // Foreground (signal strength). | |
| 318 if (signal_strength_ != 0) { | |
| 319 paint.setColor(base_color); | |
| 320 // Percent of the height of the background wedge that we draw the | |
| 321 // foreground wedge, indexed by signal strength. | |
| 322 static const float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f, | |
| 323 0.75f, 1.f}; | |
| 324 const float wedge_percent = kWedgeHeightPercentages[signal_strength_]; | |
| 325 float size_reduction = (oval_bounds.height() / 2) * (1.f - wedge_percent); | |
|
tdanderson
2016/07/28 22:00:49
nit: const
Evan Stade
2016/07/29 16:59:13
inlined this
| |
| 326 oval_bounds.Inset(size_reduction, size_reduction); | |
| 327 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), start_angle, | |
| 328 sweep_angle, true, paint); | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 private: | |
| 333 IconType icon_type_; | |
| 334 // On a scale of 0 to kMaxSignalStrength, how connected we are. | |
| 335 int signal_strength_; | |
| 336 static const int kMaxSignalStrength = kNumArcsImages - 1; | |
| 337 | |
| 338 DISALLOW_COPY_AND_ASSIGN(ArcsImageSource); | |
| 339 }; | |
| 340 | |
| 260 //------------------------------------------------------------------------------ | 341 //------------------------------------------------------------------------------ |
| 261 // Utilities for extracting icon images. | 342 // Utilities for extracting icon images. |
| 262 | 343 |
| 263 bool IconTypeIsDark(IconType icon_type) { | |
| 264 return (icon_type != ICON_TYPE_TRAY); | |
| 265 } | |
| 266 | |
| 267 bool IconTypeHasVPNBadge(IconType icon_type) { | |
| 268 return (icon_type != ICON_TYPE_LIST); | |
| 269 } | |
| 270 | |
| 271 int NumImagesForType(ImageType type) { | |
| 272 return (type == BARS) ? kNumBarsImages : kNumArcsImages; | |
| 273 } | |
| 274 | |
| 275 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { | 344 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { |
| 276 gfx::ImageSkia* image; | 345 gfx::ImageSkia* image; |
| 277 if (image_type == BARS) { | 346 if (image_type == BARS) { |
| 278 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 347 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 279 IconTypeIsDark(icon_type) ? | 348 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK |
| 280 IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK : | 349 : IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT); |
| 281 IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT); | |
| 282 } else { | 350 } else { |
| 283 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 351 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 284 IconTypeIsDark(icon_type) ? | 352 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK |
| 285 IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK : | 353 : IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT); |
| 286 IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT); | |
| 287 } | 354 } |
| 288 return image; | 355 return image; |
| 289 } | 356 } |
| 290 | 357 |
| 291 ImageType ImageTypeForNetworkType(const std::string& type) { | 358 ImageType ImageTypeForNetworkType(const std::string& type) { |
| 292 if (type == shill::kTypeWifi) | 359 if (type == shill::kTypeWifi) |
| 293 return ARCS; | 360 return ARCS; |
| 294 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) | 361 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) |
| 295 return BARS; | 362 return BARS; |
| 296 return NONE; | 363 return NONE; |
| 297 } | 364 } |
| 298 | 365 |
| 299 gfx::ImageSkia GetImageForIndex(ImageType image_type, | 366 gfx::ImageSkia GetImageForIndex(ImageType image_type, |
| 300 IconType icon_type, | 367 IconType icon_type, |
| 301 int index) { | 368 int index) { |
| 369 if (md_icon_controller::UseMaterialDesignNetworkIcons()) { | |
| 370 if (image_type == ARCS) | |
|
oshima
2016/07/28 21:02:16
consolidate into single if?
Evan Stade
2016/07/29 16:59:13
well I anticipate very soon adding vectorized vers
oshima
2016/07/29 18:17:38
Ah, i see. You may keep it as is then. sorry for n
| |
| 371 return gfx::ImageSkia(new ArcsImageSource(icon_type, index), | |
| 372 gfx::Size(kNetworkIconSize, kNetworkIconSize)); | |
| 373 } | |
| 374 | |
| 302 int num_images = NumImagesForType(image_type); | 375 int num_images = NumImagesForType(image_type); |
| 303 if (index < 0 || index >= num_images) | 376 if (index < 0 || index >= num_images) |
| 304 return gfx::ImageSkia(); | 377 return gfx::ImageSkia(); |
| 305 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); | 378 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); |
| 306 int width = images->width(); | 379 int width = images->width(); |
| 307 int height = images->height() / num_images; | 380 int height = images->height() / num_images; |
| 308 return gfx::ImageSkiaOperations::ExtractSubset(*images, | 381 return gfx::ImageSkiaOperations::ExtractSubset(*images, |
| 309 gfx::Rect(0, index * height, width, height)); | 382 gfx::Rect(0, index * height, width, height)); |
| 310 } | 383 } |
| 311 | 384 |
| 312 const gfx::ImageSkia GetConnectedImage(IconType icon_type, | 385 gfx::ImageSkia GetConnectedImage(IconType icon_type, |
| 313 const std::string& network_type) { | 386 const std::string& network_type) { |
| 314 if (network_type == shill::kTypeVPN) { | 387 if (network_type == shill::kTypeVPN) { |
| 315 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 388 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 316 IDR_AURA_UBER_TRAY_NETWORK_VPN); | 389 IDR_AURA_UBER_TRAY_NETWORK_VPN); |
| 317 } | 390 } |
| 318 ImageType image_type = ImageTypeForNetworkType(network_type); | 391 ImageType image_type = ImageTypeForNetworkType(network_type); |
| 319 const int connected_index = NumImagesForType(image_type) - 1; | 392 const int connected_index = NumImagesForType(image_type) - 1; |
| 320 return GetImageForIndex(image_type, icon_type, connected_index); | 393 return GetImageForIndex(image_type, icon_type, connected_index); |
| 321 } | 394 } |
| 322 | 395 |
| 323 const gfx::ImageSkia GetDisconnectedImage(IconType icon_type, | 396 const gfx::ImageSkia GetDisconnectedImage(IconType icon_type, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 } | 542 } |
| 470 | 543 |
| 471 gfx::ImageSkia GetIcon(const NetworkState* network, | 544 gfx::ImageSkia GetIcon(const NetworkState* network, |
| 472 IconType icon_type, | 545 IconType icon_type, |
| 473 int strength_index) { | 546 int strength_index) { |
| 474 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 547 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 475 if (network->Matches(NetworkTypePattern::Ethernet())) { | 548 if (network->Matches(NetworkTypePattern::Ethernet())) { |
| 476 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 549 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
| 477 } else if (network->Matches(NetworkTypePattern::Wireless())) { | 550 } else if (network->Matches(NetworkTypePattern::Wireless())) { |
| 478 DCHECK(strength_index > 0); | 551 DCHECK(strength_index > 0); |
| 479 return GetImageForIndex( | 552 return GetImageForIndex(ImageTypeForNetworkType(network->type()), icon_type, |
| 480 ImageTypeForNetworkType(network->type()), icon_type, strength_index); | 553 strength_index); |
| 481 } else if (network->Matches(NetworkTypePattern::VPN())) { | 554 } else if (network->Matches(NetworkTypePattern::VPN())) { |
| 482 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); | 555 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); |
| 483 } else { | 556 } else { |
| 484 LOG(WARNING) << "Request for icon for unsupported type: " | 557 LOG(WARNING) << "Request for icon for unsupported type: " |
| 485 << network->type(); | 558 << network->type(); |
| 486 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); | 559 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); |
| 487 } | 560 } |
| 488 } | 561 } |
| 489 | 562 |
| 490 //------------------------------------------------------------------------------ | 563 //------------------------------------------------------------------------------ |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 899 iter != networks.end(); ++iter) { | 972 iter != networks.end(); ++iter) { |
| 900 network_paths.insert((*iter)->path()); | 973 network_paths.insert((*iter)->path()); |
| 901 } | 974 } |
| 902 PurgeIconMap(ICON_TYPE_TRAY, network_paths); | 975 PurgeIconMap(ICON_TYPE_TRAY, network_paths); |
| 903 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); | 976 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); |
| 904 PurgeIconMap(ICON_TYPE_LIST, network_paths); | 977 PurgeIconMap(ICON_TYPE_LIST, network_paths); |
| 905 } | 978 } |
| 906 | 979 |
| 907 } // namespace network_icon | 980 } // namespace network_icon |
| 908 } // namespace ui | 981 } // namespace ui |
| OLD | NEW |