bluetooth_device_list_item_multiple_battery_view.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_multiple_battery_view.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/system/tray/tray_constants.h"
  9. #include "ash/system/tray/tray_popup_utils.h"
  10. #include "ash/system/tray/unfocusable_label.h"
  11. #include "base/check.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "ui/base/l10n/l10n_util.h"
  14. #include "ui/base/metadata/metadata_impl_macros.h"
  15. #include "ui/views/controls/image_view.h"
  16. #include "ui/views/controls/label.h"
  17. #include "ui/views/layout/box_layout.h"
  18. #include "ui/views/view_utils.h"
  19. namespace ash {
  20. BluetoothDeviceListItemMultipleBatteryView::
  21. BluetoothDeviceListItemMultipleBatteryView() {
  22. DCHECK(ash::features::IsBluetoothRevampEnabled());
  23. auto box_layout = std::make_unique<views::BoxLayout>(
  24. views::BoxLayout::Orientation::kHorizontal);
  25. box_layout->set_cross_axis_alignment(
  26. views::BoxLayout::CrossAxisAlignment::kCenter);
  27. SetLayoutManager(std::move(box_layout));
  28. }
  29. BluetoothDeviceListItemMultipleBatteryView::
  30. ~BluetoothDeviceListItemMultipleBatteryView() = default;
  31. void BluetoothDeviceListItemMultipleBatteryView::UpdateBatteryInfo(
  32. const chromeos::bluetooth_config::mojom::DeviceBatteryInfoPtr&
  33. battery_info) {
  34. int index = 0;
  35. if (battery_info->left_bud_info) {
  36. if (!left_bud_battery_view_) {
  37. left_bud_battery_view_ = AddChildViewAt(
  38. std::make_unique<BluetoothDeviceListItemBatteryView>(), index);
  39. index++;
  40. }
  41. left_bud_battery_view_->UpdateBatteryInfo(
  42. battery_info->left_bud_info->battery_percentage,
  43. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_LEFT_BUD_LABEL);
  44. } else if (left_bud_battery_view_) {
  45. RemoveChildViewT(left_bud_battery_view_);
  46. left_bud_battery_view_ = nullptr;
  47. }
  48. if (battery_info->case_info) {
  49. if (!case_battery_view_) {
  50. case_battery_view_ = AddChildViewAt(
  51. std::make_unique<BluetoothDeviceListItemBatteryView>(), index);
  52. index++;
  53. }
  54. case_battery_view_->UpdateBatteryInfo(
  55. battery_info->case_info->battery_percentage,
  56. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_CASE_LABEL);
  57. } else if (case_battery_view_) {
  58. RemoveChildViewT(case_battery_view_);
  59. case_battery_view_ = nullptr;
  60. }
  61. if (battery_info->right_bud_info) {
  62. if (!right_bud_battery_view_) {
  63. right_bud_battery_view_ = AddChildViewAt(
  64. std::make_unique<BluetoothDeviceListItemBatteryView>(), index);
  65. index++;
  66. }
  67. right_bud_battery_view_->UpdateBatteryInfo(
  68. battery_info->right_bud_info->battery_percentage,
  69. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_BATTERY_PERCENTAGE_RIGHT_BUD_LABEL);
  70. } else if (right_bud_battery_view_) {
  71. RemoveChildViewT(right_bud_battery_view_);
  72. right_bud_battery_view_ = nullptr;
  73. }
  74. }
  75. BEGIN_METADATA(BluetoothDeviceListItemMultipleBatteryView, views::View)
  76. END_METADATA
  77. } // namespace ash