File: tray_power.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 5,755 bytes parent folder | download | duplicates (6)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2012 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/system/power/tray_power.h"

#include <utility>

#include "ash/accessibility/accessibility_delegate.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/system/power/battery_notification.h"
#include "ash/system/power/dual_role_notification.h"
#include "ash/system/power/power_status.h"
#include "ash/system/time/time_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_item_view.h"
#include "ash/system/tray/tray_utils.h"
#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "base/time/time.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/chromeos/styles/cros_styles.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/image/image_skia_source.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/view.h"

using message_center::MessageCenter;
using message_center::Notification;

namespace ash {

PowerTrayView::PowerTrayView(Shelf* shelf) : TrayItemView(shelf) {
  CreateImageView();

  previous_battery_saver_state_ = PowerStatus::Get()->IsBatterySaverActive();
  PowerStatus::Get()->AddObserver(this);

  GetViewAccessibility().SetRole(ax::mojom::Role::kImage);
  UpdateAccessibleName();
}

PowerTrayView::~PowerTrayView() {
  PowerStatus::Get()->RemoveObserver(this);
}

gfx::Size PowerTrayView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  // The battery icon is a lot thinner than other icons, hence the special
  // logic.
  gfx::Size standard_size =
      TrayItemView::CalculatePreferredSize(available_size);
  if (IsHorizontalAlignment())
    return gfx::Size(kUnifiedTrayBatteryWidth, standard_size.height());

  // Ensure battery isn't too tall in side shelf.
  return gfx::Size(standard_size.width(), kUnifiedTrayIconSize);
}

views::View* PowerTrayView::GetTooltipHandlerForPoint(const gfx::Point& point) {
  return GetLocalBounds().Contains(point) ? this : nullptr;
}

void PowerTrayView::OnThemeChanged() {
  TrayItemView::OnThemeChanged();
  UpdateStatus(false);
  UpdateImage(/*icon_color_changed=*/true);
}

void PowerTrayView::HandleLocaleChange() {
  UpdateStatus(false);
}

void PowerTrayView::UpdateLabelOrImageViewColor(bool active) {
  TrayItemView::UpdateLabelOrImageViewColor(active);

  cros_tokens::CrosSysColorIds icon_fg_token = cros_tokens::kCrosSysOnSurface;
  if (active) {
    icon_fg_token = cros_tokens::kCrosSysSystemOnPrimaryContainer;
  } else if (features::IsBatterySaverAvailable() &&
             PowerStatus::Get()->IsBatterySaverActive()) {
    icon_fg_token = cros_tokens::kCrosSysOnWarningContainer;
  }
  const SkColor icon_fg_color = GetColorProvider()->GetColor(icon_fg_token);

  PowerStatus::BatteryImageInfo info =
      PowerStatus::Get()->GenerateBatteryImageInfo(icon_fg_color);

  image_view()->SetImage(
      ui::ImageModel::FromImageSkia(PowerStatus::GetBatteryImage(
          info, kUnifiedTrayBatteryIconSize, GetColorProvider())));
}

void PowerTrayView::OnPowerStatusChanged() {
  const bool bsm_active = PowerStatus::Get()->IsBatterySaverActive();
  UpdateStatus(bsm_active != previous_battery_saver_state_);
  previous_battery_saver_state_ = bsm_active;
}

void PowerTrayView::UpdateStatus(bool icon_color_changed) {
  UpdateImage(icon_color_changed);
  SetVisible(PowerStatus::Get()->IsBatteryPresent());
  UpdateAccessibleName();
  SetTooltipText(PowerStatus::Get()->GetInlinedStatusString());
  // Currently ChromeVox only reads the inner view when touching the icon.
  // As a result this node's accessible node data will not be read.
  image_view()->GetViewAccessibility().SetName(
      GetViewAccessibility().GetCachedName());
}

void PowerTrayView::UpdateImage(bool icon_color_changed) {
  SkColor prev_foreground_color = SK_ColorWHITE,
          prev_badge_color = SK_ColorWHITE;
  if (info_) {
    prev_foreground_color = info_->battery_color_preferences.foreground_color;
    prev_badge_color =
        info_->battery_color_preferences.badge_color.value_or(SK_ColorWHITE);
  }

  PowerStatus::BatteryImageInfo info =
      PowerStatus::Get()->GenerateBatteryImageInfo(prev_foreground_color,
                                                   prev_badge_color);

  // Only change the image when the info changes or the icon color has
  // changed. http://crbug.com/589348
  if (info_ && info_->ApproximatelyEqual(info) && !icon_color_changed)
    return;
  info_ = info;

  UpdateLabelOrImageViewColor(is_active());
}

void PowerTrayView::UpdateAccessibleName() {
  std::u16string accessible_name =
      PowerStatus::Get()->GetAccessibleNameString(/* full_description*/ true);
  if (!accessible_name.empty()) {
    GetViewAccessibility().SetName(accessible_name);
  } else {
    GetViewAccessibility().SetName(
        std::u16string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
  }
}

BEGIN_METADATA(PowerTrayView)
END_METADATA

}  // namespace ash