bluetooth_device_list_item_battery_view.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/bluetooth/bluetooth_device_list_item_battery_view.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/style/ash_color_id.h"
  8. #include "ash/style/ash_color_provider.h"
  9. #include "ash/system/tray/tray_constants.h"
  10. #include "ash/system/tray/tray_popup_utils.h"
  11. #include "ash/system/tray/unfocusable_label.h"
  12. #include "base/check.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "ui/base/l10n/l10n_util.h"
  15. #include "ui/base/metadata/metadata_impl_macros.h"
  16. #include "ui/color/color_provider.h"
  17. #include "ui/views/controls/image_view.h"
  18. #include "ui/views/controls/label.h"
  19. #include "ui/views/layout/box_layout.h"
  20. namespace ash {
  21. namespace {
  22. // The minimum battery percentage below which the icon and text should be
  23. // shown with the alert text color.
  24. constexpr uint8_t kPositiveBatteryPercentageCutoff = 25;
  25. // The maximum amount of change of battery percentage values before the view
  26. // should be updated.
  27. constexpr uint8_t kBatteryPercentageChangeThreshold = 5;
  28. // The resized battery icon has a total width of |kUnifiedTraySubIconSize|, but
  29. // the battery itself has a width of |9|.
  30. constexpr int kActualBatteryIconWidth = 9;
  31. // The padding between the battery icon and the sub-label, and the sub-label and
  32. // the end of the container view.
  33. constexpr int kSpacingBetweenIconAndLabel = 6;
  34. } // namespace
  35. BluetoothDeviceListItemBatteryView::BluetoothDeviceListItemBatteryView() {
  36. DCHECK(ash::features::IsBluetoothRevampEnabled());
  37. auto box_layout = std::make_unique<views::BoxLayout>(
  38. views::BoxLayout::Orientation::kHorizontal);
  39. box_layout->set_cross_axis_alignment(
  40. views::BoxLayout::CrossAxisAlignment::kCenter);
  41. SetLayoutManager(std::move(box_layout));
  42. }
  43. BluetoothDeviceListItemBatteryView::~BluetoothDeviceListItemBatteryView() =
  44. default;
  45. void BluetoothDeviceListItemBatteryView::UpdateBatteryInfo(
  46. const uint8_t new_battery_percentage,
  47. const int message_id) {
  48. if (!icon_) {
  49. icon_ = AddChildView(std::make_unique<views::ImageView>());
  50. // We set the preferred size to be the size of the battery, effectively
  51. // removing all the extra padding. This allows the battery icon to be
  52. // aligned correctly with the device name label.
  53. icon_->SetPreferredSize(gfx::Size(/*width=*/kActualBatteryIconWidth,
  54. /*height=*/kUnifiedTraySubIconSize));
  55. }
  56. if (!label_) {
  57. label_ = AddChildView(TrayPopupUtils::CreateUnfocusableLabel());
  58. label_->SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(
  59. 0, kSpacingBetweenIconAndLabel, 0, kSpacingBetweenIconAndLabel)));
  60. }
  61. const AshColorProvider::ContentLayerType content_layer_type =
  62. new_battery_percentage >= kPositiveBatteryPercentageCutoff
  63. ? AshColorProvider::ContentLayerType::kTextColorSecondary
  64. : AshColorProvider::ContentLayerType::kTextColorAlert;
  65. label_->SetText(l10n_util::GetStringFUTF16(
  66. message_id, base::NumberToString16(new_battery_percentage)));
  67. label_->SetAutoColorReadabilityEnabled(false);
  68. label_->SetEnabledColor(
  69. AshColorProvider::Get()->GetContentLayerColor(content_layer_type));
  70. if (last_shown_battery_percentage_ &&
  71. ApproximatelyEqual(last_shown_battery_percentage_.value(),
  72. new_battery_percentage)) {
  73. return;
  74. }
  75. last_shown_battery_percentage_ = new_battery_percentage;
  76. PowerStatus::BatteryImageInfo battery_image_info;
  77. battery_image_info.charge_percent = new_battery_percentage;
  78. icon_->SetImage(PowerStatus::GetBatteryImage(
  79. battery_image_info, kUnifiedTraySubIconSize,
  80. GetColorProvider()->GetColor(kColorAshShieldAndBaseOpaque),
  81. AshColorProvider::Get()->GetContentLayerColor(content_layer_type)));
  82. }
  83. bool BluetoothDeviceListItemBatteryView::ApproximatelyEqual(
  84. uint8_t old_charge_percent,
  85. uint8_t new_charge_percent) const {
  86. // Don't update the view when the percentages are identical.
  87. if (old_charge_percent == new_charge_percent)
  88. return true;
  89. // Always update the view if the percentage was or has become 100%. We don't
  90. // do the same if the percentage was or has become 0% since there won't be a
  91. // visual difference between the values.
  92. if (old_charge_percent == 100 || new_charge_percent == 100) {
  93. return false;
  94. }
  95. const uint8_t min = std::min(old_charge_percent, new_charge_percent);
  96. const uint8_t max = std::max(old_charge_percent, new_charge_percent);
  97. // Always update the view if the icon and label would be represented with a
  98. // different color.
  99. if (min < kPositiveBatteryPercentageCutoff &&
  100. max >= kPositiveBatteryPercentageCutoff) {
  101. return false;
  102. }
  103. return max - min < kBatteryPercentageChangeThreshold;
  104. }
  105. BEGIN_METADATA(BluetoothDeviceListItemBatteryView, views::View)
  106. END_METADATA
  107. } // namespace ash