Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Side by Side Diff: ui/chromeos/network/network_icon.cc

Issue 2186353002: MD/vectorize wifi icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sidestep mysterious compile failure Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 BARS, 171 BARS,
167 NONE 172 NONE
168 }; 173 };
169 174
170 // Amount to fade icons while connecting. 175 // Amount to fade icons while connecting.
171 const double kConnectingImageAlpha = 0.5; 176 const double kConnectingImageAlpha = 0.5;
172 177
173 // Images for strength bars for wired networks. 178 // Images for strength bars for wired networks.
174 const int kNumBarsImages = 5; 179 const int kNumBarsImages = 5;
175 180
176 // Imagaes for strength arcs for wireless networks. 181 // Images for strength arcs for wireless networks.
177 const int kNumArcsImages = 5; 182 const int kNumArcsImages = 5;
178 183
179 // Number of discrete images to use for alpha fade animation 184 // Number of discrete images to use for alpha fade animation
180 const int kNumFadeImages = 10; 185 const int kNumFadeImages = 10;
181 186
187 SkColor GetBaseColorForIconType(IconType icon_type) {
188 // TODO(estade): use kTrayIconColor and kMenuIconColor.
189 return icon_type == ICON_TYPE_TRAY ? SK_ColorWHITE : gfx::kChromeIconGrey;
190 }
191
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
182 //------------------------------------------------------------------------------ 197 //------------------------------------------------------------------------------
183 // Classes for generating scaled images. 198 // Classes for generating scaled images.
184 199
185 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) { 200 const SkBitmap GetEmptyBitmap(const gfx::Size pixel_size) {
186 typedef std::pair<int, int> SizeKey; 201 typedef std::pair<int, int> SizeKey;
187 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap; 202 typedef std::map<SizeKey, SkBitmap> SizeBitmapMap;
188 static SizeBitmapMap* s_empty_bitmaps = new SizeBitmapMap; 203 static SizeBitmapMap* s_empty_bitmaps = new SizeBitmapMap;
189 204
190 SizeKey key(pixel_size.width(), pixel_size.height()); 205 SizeKey key(pixel_size.width(), pixel_size.height());
191 206
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 return canvas.ExtractImageRep(); 265 return canvas.ExtractImageRep();
251 } 266 }
252 267
253 private: 268 private:
254 const gfx::ImageSkia icon_; 269 const gfx::ImageSkia icon_;
255 const Badges badges_; 270 const Badges badges_;
256 271
257 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); 272 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource);
258 }; 273 };
259 274
275 // Depicts a given signal strength using arcs (for WiFi connections).
276 class ArcsImageSource : public gfx::CanvasImageSource {
277 public:
278 ArcsImageSource(IconType icon_type, float signal_strength)
279 : CanvasImageSource(GetSizeForIconType(icon_type), false),
280 icon_type_(icon_type),
281 signal_strength_(signal_strength) {
282 DCHECK_GE(signal_strength, 0);
283 DCHECK_LT(signal_strength, kNumArcsImages);
284 }
285 ~ArcsImageSource() override {}
286
287 // gfx::CanvasImageSource:
288 void Draw(gfx::Canvas* canvas) override {
289 gfx::RectF oval_bounds((gfx::Rect(size())));
290 // Padding between top of arc and top of canvas.
291 const int kIconInset = 2;
292 oval_bounds.Inset(gfx::Insets(kIconInset));
293 // Double the width and height. The new midpoint should be the former
294 // bottom center.
295 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2,
296 -oval_bounds.height());
297
298 const SkScalar kAngleAboveHorizontal = 51.f;
299 const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal;
300 const SkScalar kSweepAngle = 180.f - 2 * kAngleAboveHorizontal;
301
302 SkPaint paint;
303 paint.setAntiAlias(true);
304 paint.setStyle(SkPaint::kFill_Style);
305 const SkColor base_color = GetBaseColorForIconType(icon_type_);
306 // Background. Skip drawing for full signal.
307 if (signal_strength_ != kNumArcsImages - 1) {
308 // TODO(estade): share this alpha with other things in ash (battery,
309 // etc.).
310 paint.setColor(SkColorSetA(base_color, 0x4D));
311 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle,
312 kSweepAngle, true, paint);
313 }
314 // Foreground (signal strength).
315 if (signal_strength_ != 0) {
316 paint.setColor(base_color);
317 // Percent of the height of the background wedge that we draw the
318 // foreground wedge, indexed by signal strength.
319 static const float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f,
320 0.75f, 1.f};
321 const float wedge_percent = kWedgeHeightPercentages[signal_strength_];
322 oval_bounds.Inset(
323 gfx::InsetsF((oval_bounds.height() / 2) * (1.f - wedge_percent)));
324 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle,
325 kSweepAngle, true, paint);
326 }
327 }
328
329 private:
330 IconType icon_type_;
331 // On a scale of 0 to kNumArcsImages - 1, how connected we are.
332 int signal_strength_;
333
334 DISALLOW_COPY_AND_ASSIGN(ArcsImageSource);
335 };
336
260 //------------------------------------------------------------------------------ 337 //------------------------------------------------------------------------------
261 // Utilities for extracting icon images. 338 // Utilities for extracting icon images.
262 339
263 bool IconTypeIsDark(IconType icon_type) { 340 bool IconTypeIsDark(IconType icon_type) {
264 return (icon_type != ICON_TYPE_TRAY); 341 return (icon_type != ICON_TYPE_TRAY);
265 } 342 }
266 343
267 bool IconTypeHasVPNBadge(IconType icon_type) { 344 bool IconTypeHasVPNBadge(IconType icon_type) {
268 return (icon_type != ICON_TYPE_LIST); 345 return (icon_type != ICON_TYPE_LIST);
269 } 346 }
270 347
271 int NumImagesForType(ImageType type) { 348 int NumImagesForType(ImageType type) {
272 return (type == BARS) ? kNumBarsImages : kNumArcsImages; 349 return (type == BARS) ? kNumBarsImages : kNumArcsImages;
273 } 350 }
274 351
275 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) { 352 gfx::ImageSkia* BaseImageForType(ImageType image_type, IconType icon_type) {
276 gfx::ImageSkia* image; 353 gfx::ImageSkia* image;
277 if (image_type == BARS) { 354 if (image_type == BARS) {
278 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 355 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
279 IconTypeIsDark(icon_type) ? 356 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK
280 IDR_AURA_UBER_TRAY_NETWORK_BARS_DARK : 357 : IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT);
281 IDR_AURA_UBER_TRAY_NETWORK_BARS_LIGHT);
282 } else { 358 } else {
283 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 359 image = ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
284 IconTypeIsDark(icon_type) ? 360 IconTypeIsDark(icon_type) ? IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK
285 IDR_AURA_UBER_TRAY_NETWORK_ARCS_DARK : 361 : IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT);
286 IDR_AURA_UBER_TRAY_NETWORK_ARCS_LIGHT);
287 } 362 }
288 return image; 363 return image;
289 } 364 }
290 365
291 ImageType ImageTypeForNetworkType(const std::string& type) { 366 ImageType ImageTypeForNetworkType(const std::string& type) {
292 if (type == shill::kTypeWifi) 367 if (type == shill::kTypeWifi)
293 return ARCS; 368 return ARCS;
294 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) 369 else if (type == shill::kTypeCellular || type == shill::kTypeWimax)
295 return BARS; 370 return BARS;
296 return NONE; 371 return NONE;
297 } 372 }
298 373
299 gfx::ImageSkia GetImageForIndex(ImageType image_type, 374 gfx::ImageSkia GetImageForIndex(ImageType image_type,
300 IconType icon_type, 375 IconType icon_type,
301 int index) { 376 int index) {
377 if (md_icon_controller::UseMaterialDesignNetworkIcons() &&
378 image_type == ARCS) {
379 ArcsImageSource* source = new ArcsImageSource(icon_type, index);
380 return gfx::ImageSkia(source, source->size());
381 }
382
302 int num_images = NumImagesForType(image_type); 383 int num_images = NumImagesForType(image_type);
303 if (index < 0 || index >= num_images) 384 if (index < 0 || index >= num_images)
304 return gfx::ImageSkia(); 385 return gfx::ImageSkia();
305 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); 386 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type);
306 int width = images->width(); 387 int width = images->width();
307 int height = images->height() / num_images; 388 int height = images->height() / num_images;
308 return gfx::ImageSkiaOperations::ExtractSubset(*images, 389 return gfx::ImageSkiaOperations::ExtractSubset(*images,
309 gfx::Rect(0, index * height, width, height)); 390 gfx::Rect(0, index * height, width, height));
310 } 391 }
311 392
312 const gfx::ImageSkia GetConnectedImage(IconType icon_type, 393 gfx::ImageSkia GetConnectedImage(IconType icon_type,
313 const std::string& network_type) { 394 const std::string& network_type) {
314 if (network_type == shill::kTypeVPN) { 395 if (network_type == shill::kTypeVPN) {
315 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 396 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
316 IDR_AURA_UBER_TRAY_NETWORK_VPN); 397 IDR_AURA_UBER_TRAY_NETWORK_VPN);
317 } 398 }
318 ImageType image_type = ImageTypeForNetworkType(network_type); 399 ImageType image_type = ImageTypeForNetworkType(network_type);
319 const int connected_index = NumImagesForType(image_type) - 1; 400 const int connected_index = NumImagesForType(image_type) - 1;
320 return GetImageForIndex(image_type, icon_type, connected_index); 401 return GetImageForIndex(image_type, icon_type, connected_index);
321 } 402 }
322 403
323 const gfx::ImageSkia GetDisconnectedImage(IconType icon_type, 404 const gfx::ImageSkia GetDisconnectedImage(IconType icon_type,
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 } 550 }
470 551
471 gfx::ImageSkia GetIcon(const NetworkState* network, 552 gfx::ImageSkia GetIcon(const NetworkState* network,
472 IconType icon_type, 553 IconType icon_type,
473 int strength_index) { 554 int strength_index) {
474 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 555 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
475 if (network->Matches(NetworkTypePattern::Ethernet())) { 556 if (network->Matches(NetworkTypePattern::Ethernet())) {
476 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); 557 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
477 } else if (network->Matches(NetworkTypePattern::Wireless())) { 558 } else if (network->Matches(NetworkTypePattern::Wireless())) {
478 DCHECK(strength_index > 0); 559 DCHECK(strength_index > 0);
479 return GetImageForIndex( 560 return GetImageForIndex(ImageTypeForNetworkType(network->type()), icon_type,
480 ImageTypeForNetworkType(network->type()), icon_type, strength_index); 561 strength_index);
481 } else if (network->Matches(NetworkTypePattern::VPN())) { 562 } else if (network->Matches(NetworkTypePattern::VPN())) {
482 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); 563 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN);
483 } else { 564 } else {
484 LOG(WARNING) << "Request for icon for unsupported type: " 565 LOG(WARNING) << "Request for icon for unsupported type: "
485 << network->type(); 566 << network->type();
486 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); 567 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
487 } 568 }
488 } 569 }
489 570
490 //------------------------------------------------------------------------------ 571 //------------------------------------------------------------------------------
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 iter != networks.end(); ++iter) { 980 iter != networks.end(); ++iter) {
900 network_paths.insert((*iter)->path()); 981 network_paths.insert((*iter)->path());
901 } 982 }
902 PurgeIconMap(ICON_TYPE_TRAY, network_paths); 983 PurgeIconMap(ICON_TYPE_TRAY, network_paths);
903 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); 984 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths);
904 PurgeIconMap(ICON_TYPE_LIST, network_paths); 985 PurgeIconMap(ICON_TYPE_LIST, network_paths);
905 } 986 }
906 987
907 } // namespace network_icon 988 } // namespace network_icon
908 } // namespace ui 989 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698