1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
ash / game_dashboard / game_dashboard_battery_view.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/game_dashboard/game_dashboard_battery_view.h"
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/style/ash_color_id.h"
#include "ash/system/tray/tray_constants.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_styles.h"
#include "ui/gfx/color_palette.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view_class_properties.h"
namespace ash {
GameDashboardBatteryView::GameDashboardBatteryView() {
// Initialize the battery view.
SetSize(gfx::Size(20, 20));
SetProperty(views::kMarginsKey, gfx::Insets());
// Add an observer to the device's Power Status service and
// connect it to the battery icon.
PowerStatus::Get()->AddObserver(this);
}
GameDashboardBatteryView::~GameDashboardBatteryView() {
PowerStatus::Get()->RemoveObserver(this);
}
void GameDashboardBatteryView::OnThemeChanged() {
views::View::OnThemeChanged();
// Assume the icon color has changed, so as to trigger an icon color update.
UpdateStatus(/*theme_changed=*/true);
}
void GameDashboardBatteryView::OnPowerStatusChanged() {
UpdateStatus();
}
void GameDashboardBatteryView::UpdateStatus(bool theme_changed) {
MaybeUpdateImage(theme_changed);
SetVisible(PowerStatus::Get()->IsBatteryPresent());
GetViewAccessibility().SetName(
PowerStatus::Get()->GetAccessibleNameString(/*full_description=*/true));
}
void GameDashboardBatteryView::MaybeUpdateImage(bool theme_changed) {
auto* color_provider = GetColorProvider();
SkColor icon_fg_color = color_provider->GetColor(kColorAshIconColorPrimary);
std::optional<SkColor> badge_color =
color_provider->GetColor(kColorAshIconColorPrimary);
if (features::IsBatterySaverAvailable() &&
PowerStatus::Get()->IsBatterySaverActive() &&
cros_styles::DarkModeEnabled()) {
icon_fg_color = gfx::kGoogleYellow700;
badge_color = gfx::kGoogleYellow700;
}
auto image_info =
PowerStatus::Get()->GenerateBatteryImageInfo(icon_fg_color, badge_color);
if (!theme_changed && battery_image_info_ &&
battery_image_info_->ApproximatelyEqual(image_info)) {
return;
}
battery_image_info_ = image_info;
SetImage(PowerStatus::GetBatteryImage(battery_image_info_.value(),
kUnifiedTrayBatteryIconSize,
color_provider));
}
BEGIN_METADATA(GameDashboardBatteryView)
END_METADATA
} // namespace ash