Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/common/system/chromeos/power/power_status.h" | 5 #include "ash/common/system/chromeos/power/power_status.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "ash/common/material_design/material_design_controller.h" | 10 #include "ash/common/material_design/material_design_controller.h" |
| 11 #include "ash/resources/grit/ash_resources.h" | 11 #include "ash/resources/grit/ash_resources.h" |
| 12 #include "ash/resources/vector_icons/vector_icons.h" | 12 #include "ash/resources/vector_icons/vector_icons.h" |
| 13 #include "ash/strings/grit/ash_strings.h" | 13 #include "ash/strings/grit/ash_strings.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/power_manager_client.h" | 18 #include "chromeos/dbus/power_manager_client.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/l10n/time_format.h" | 20 #include "ui/base/l10n/time_format.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/display/display.h" | 22 #include "ui/display/display.h" |
| 23 #include "ui/display/screen.h" | 23 #include "ui/display/screen.h" |
| 24 #include "ui/gfx/canvas.h" | 24 #include "ui/gfx/canvas.h" |
| 25 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
| 26 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 27 #include "ui/gfx/image/image_skia_operations.h" | 27 #include "ui/gfx/image/image_skia_rep.h" |
| 28 #include "ui/gfx/paint_vector_icon.h" | 28 #include "ui/gfx/paint_vector_icon.h" |
| 29 | 29 |
| 30 namespace ash { | 30 namespace ash { |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 // Updates |proto| to ensure that its fields are consistent. | 33 // Updates |proto| to ensure that its fields are consistent. |
| 34 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { | 34 void SanitizeProto(power_manager::PowerSupplyProperties* proto) { |
| 35 DCHECK(proto); | 35 DCHECK(proto); |
| 36 | 36 |
| 37 if (proto->battery_state() == | 37 if (proto->battery_state() == |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 } | 116 } |
| 117 NOTREACHED(); | 117 NOTREACHED(); |
| 118 return gfx::kNoneIcon; | 118 return gfx::kNoneIcon; |
| 119 } | 119 } |
| 120 | 120 |
| 121 static PowerStatus* g_power_status = NULL; | 121 static PowerStatus* g_power_status = NULL; |
| 122 | 122 |
| 123 // Minimum battery percentage rendered in UI. | 123 // Minimum battery percentage rendered in UI. |
| 124 const int kMinBatteryPercent = 1; | 124 const int kMinBatteryPercent = 1; |
| 125 | 125 |
| 126 // Width and height of battery images. | |
| 127 const int kBatteryImageHeight = 25; | |
| 128 const int kBatteryImageWidth = 25; | |
| 129 | |
| 130 // Number of different power states. | 126 // Number of different power states. |
| 131 const int kNumPowerImages = 15; | 127 const int kNumPowerImages = 15; |
| 132 | 128 |
| 133 // The height of the battery icon in material design (as measured from the | 129 // The height of the battery icon in material design (as measured from the |
| 134 // user-visible bottom of the icon to the user-visible top of the icon). | 130 // user-visible bottom of the icon to the user-visible top of the icon). |
| 135 const int kBatteryImageHeightMd = 12; | 131 const int kBatteryImageHeightMd = 12; |
| 136 | 132 |
| 137 // The dimensions of the canvas containing the material design battery icon. | 133 // The dimensions of the canvas containing the material design battery icon. |
| 138 const int kBatteryCanvasSizeMd = 16; | 134 const int kBatteryCanvasSizeMd = 16; |
| 139 | 135 |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 info->index = kNumPowerImages - 1; | 381 info->index = kNumPowerImages - 1; |
| 386 } else if (!IsBatteryPresent()) { | 382 } else if (!IsBatteryPresent()) { |
| 387 info->index = kNumPowerImages; | 383 info->index = kNumPowerImages; |
| 388 } else { | 384 } else { |
| 389 info->index = | 385 info->index = |
| 390 static_cast<int>(GetBatteryPercent() / 100.0 * (kNumPowerImages - 1)); | 386 static_cast<int>(GetBatteryPercent() / 100.0 * (kNumPowerImages - 1)); |
| 391 info->index = std::max(std::min(info->index, kNumPowerImages - 2), 0); | 387 info->index = std::max(std::min(info->index, kNumPowerImages - 2), 0); |
| 392 } | 388 } |
| 393 } | 389 } |
| 394 | 390 |
| 395 gfx::ImageSkia PowerStatus::GetBatteryImage( | 391 // static |
| 396 const BatteryImageInfo& info) const { | 392 gfx::Size PowerStatus::GetBatteryImageSizeInDip() { |
| 397 if (!MaterialDesignController::UseMaterialDesignSystemIcons()) | 393 return gfx::Size(kBatteryCanvasSizeMd, kBatteryCanvasSizeMd); |
|
Peter Kasting
2017/03/25 00:22:30
Nit: Now that there is no non-MD path, remove "Md"
| |
| 398 return GetBatteryImageNonMd(info); | 394 } |
| 399 | 395 |
| 396 gfx::ImageSkiaRep PowerStatus::GetBatteryImage(const BatteryImageInfo& info, | |
| 397 float scale) const { | |
| 400 const bool use_alert_color = | 398 const bool use_alert_color = |
| 401 (info.charge_level == kMinVisualChargeLevelMd && !IsLinePowerConnected()); | 399 (info.charge_level == kMinVisualChargeLevelMd && !IsLinePowerConnected()); |
| 402 const SkColor badge_color = | 400 const SkColor badge_color = |
| 403 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor; | 401 use_alert_color ? kBatteryAlertColor : kBatteryBadgeColor; |
| 404 const SkColor charge_color = | 402 const SkColor charge_color = |
| 405 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor; | 403 use_alert_color ? kBatteryAlertColor : kBatteryChargeColor; |
| 406 gfx::Canvas canvas( | 404 |
| 407 gfx::Size(kBatteryCanvasSizeMd, kBatteryCanvasSizeMd), | 405 gfx::Canvas canvas(GetBatteryImageSizeInDip(), scale, false /* opaque */); |
| 408 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(), | |
| 409 false); | |
| 410 | 406 |
| 411 // Paint the battery's base (background) color. | 407 // Paint the battery's base (background) color. |
| 412 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, | 408 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, |
| 413 kBatteryBaseColor); | 409 kBatteryBaseColor); |
| 414 | 410 |
| 415 // Paint the charged portion of the battery. Note that |charge_height| adjusts | 411 // Paint the charged portion of the battery. Note that |charge_height| adjusts |
| 416 // for the 2dp of padding between the bottom of the battery icon and the | 412 // for the 2dp of padding between the bottom of the battery icon and the |
| 417 // bottom edge of |canvas|. | 413 // bottom edge of |canvas|. |
| 418 const int charge_height = info.charge_level + 2; | 414 const int charge_height = info.charge_level + 2; |
| 419 gfx::Rect clip_rect(0, kBatteryCanvasSizeMd - charge_height, | 415 gfx::Rect clip_rect(0, kBatteryCanvasSizeMd - charge_height, |
| 420 kBatteryCanvasSizeMd, charge_height); | 416 kBatteryCanvasSizeMd, charge_height); |
| 421 canvas.Save(); | 417 canvas.Save(); |
| 422 canvas.ClipRect(clip_rect); | 418 canvas.ClipRect(clip_rect); |
| 423 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, | 419 PaintVectorIcon(&canvas, kSystemTrayBatteryIcon, kBatteryCanvasSizeMd, |
| 424 charge_color); | 420 charge_color); |
| 425 canvas.Restore(); | 421 canvas.Restore(); |
| 426 | 422 |
| 427 // Paint the badge over top of the battery, if applicable. | 423 // Paint the badge over top of the battery, if applicable. |
| 428 if (info.icon_badge != ICON_BADGE_NONE) { | 424 if (info.icon_badge != ICON_BADGE_NONE) { |
| 429 PaintVectorIcon(&canvas, VectorIconForIconBadge(info.icon_badge), | 425 PaintVectorIcon(&canvas, VectorIconForIconBadge(info.icon_badge), |
| 430 kBatteryCanvasSizeMd, badge_color); | 426 kBatteryCanvasSizeMd, badge_color); |
| 431 } | 427 } |
| 432 | 428 |
| 433 return gfx::ImageSkia(canvas.ExtractImageRep()); | 429 return gfx::ImageSkiaRep(canvas.GetBitmap(), scale); |
| 434 } | |
| 435 | |
| 436 gfx::ImageSkia PowerStatus::GetBatteryImageNonMd( | |
| 437 const BatteryImageInfo& info) const { | |
| 438 gfx::Image all; | |
| 439 all = ui::ResourceBundle::GetSharedInstance().GetImageNamed(info.resource_id); | |
| 440 gfx::Rect region(info.offset * kBatteryImageWidth, | |
| 441 info.index * kBatteryImageHeight, kBatteryImageWidth, | |
|
Peter Kasting
2017/03/25 00:22:30
With this removal, |resource_id|, |offset|, and |i
Evan Stade
2017/03/27 15:11:23
that's all done here: https://codereview.chromium.
| |
| 442 kBatteryImageHeight); | |
| 443 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); | |
| 444 } | 430 } |
| 445 | 431 |
| 446 base::string16 PowerStatus::GetAccessibleNameString( | 432 base::string16 PowerStatus::GetAccessibleNameString( |
| 447 bool full_description) const { | 433 bool full_description) const { |
| 448 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 434 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 449 if (IsBatteryFull()) { | 435 if (IsBatteryFull()) { |
| 450 return rb.GetLocalizedString( | 436 return rb.GetLocalizedString( |
| 451 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); | 437 IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); |
| 452 } | 438 } |
| 453 | 439 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 | 495 |
| 510 void PowerStatus::PowerChanged( | 496 void PowerStatus::PowerChanged( |
| 511 const power_manager::PowerSupplyProperties& proto) { | 497 const power_manager::PowerSupplyProperties& proto) { |
| 512 proto_ = proto; | 498 proto_ = proto; |
| 513 SanitizeProto(&proto_); | 499 SanitizeProto(&proto_); |
| 514 for (auto& observer : observers_) | 500 for (auto& observer : observers_) |
| 515 observer.OnPowerStatusChanged(); | 501 observer.OnPowerStatusChanged(); |
| 516 } | 502 } |
| 517 | 503 |
| 518 } // namespace ash | 504 } // namespace ash |
| OLD | NEW |