OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 | |
5 #include "ash/common/system/chromeos/power/power_status_view.h" | |
6 | |
7 #include "ash/common/material_design/material_design_controller.h" | |
8 #include "ash/common/system/chromeos/power/power_status.h" | |
9 #include "ash/common/system/chromeos/power/tray_power.h" | |
10 #include "ash/common/system/tray/fixed_sized_image_view.h" | |
11 #include "ash/common/system/tray/tray_constants.h" | |
12 #include "ash/common/system/tray/tray_popup_item_style.h" | |
13 #include "ash/common/system/tray/tray_popup_utils.h" | |
14 #include "ash/strings/grit/ash_strings.h" | |
15 #include "base/i18n/number_formatting.h" | |
16 #include "base/i18n/time_formatting.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 #include "ui/accessibility/ax_node_data.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 #include "ui/native_theme/native_theme.h" | |
21 #include "ui/views/controls/image_view.h" | |
22 #include "ui/views/controls/label.h" | |
23 #include "ui/views/layout/box_layout.h" | |
24 #include "ui/views/layout/grid_layout.h" | |
25 | |
26 namespace ash { | |
27 | |
28 // Padding between battery status text and battery icon on default view. | |
29 const int kPaddingBetweenBatteryStatusAndIcon = 3; | |
30 | |
31 PowerStatusView::PowerStatusView(bool default_view_right_align) | |
32 : default_view_right_align_(default_view_right_align), | |
33 percentage_label_(new views::Label), | |
34 separator_label_(new views::Label), | |
35 time_status_label_(new views::Label), | |
36 icon_(nullptr) { | |
37 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | |
38 SetFocusBehavior(FocusBehavior::ALWAYS); | |
39 | |
40 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); | |
41 separator_label_->SetEnabledColor(kHeaderTextColorNormal); | |
42 separator_label_->SetText(base::ASCIIToUTF16(" - ")); | |
43 LayoutView(); | |
44 PowerStatus::Get()->AddObserver(this); | |
45 OnPowerStatusChanged(); | |
46 } | |
47 | |
48 PowerStatusView::~PowerStatusView() { | |
49 PowerStatus::Get()->RemoveObserver(this); | |
50 } | |
51 | |
52 void PowerStatusView::OnPowerStatusChanged() { | |
53 UpdateText(); | |
54 | |
55 // We do not show a battery icon in the material design system menu. | |
56 // TODO(tdanderson): Remove the non-MD code and the IconSet enum once | |
57 // material design is enabled by default. See crbug.com/614453. | |
58 if (MaterialDesignController::UseMaterialDesignSystemIcons()) | |
59 return; | |
60 | |
61 const PowerStatus::BatteryImageInfo info = | |
62 PowerStatus::Get()->GetBatteryImageInfo(PowerStatus::ICON_DARK); | |
63 if (info != previous_battery_image_info_) { | |
64 icon_->SetImage(PowerStatus::Get()->GetBatteryImage(info)); | |
65 icon_->SetVisible(true); | |
66 previous_battery_image_info_ = info; | |
67 } | |
68 } | |
69 | |
70 void PowerStatusView::LayoutView() { | |
71 if (default_view_right_align_) { | |
72 views::BoxLayout* layout = | |
73 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
74 kPaddingBetweenBatteryStatusAndIcon); | |
75 SetLayoutManager(layout); | |
76 | |
77 AddChildView(percentage_label_); | |
78 AddChildView(separator_label_); | |
79 AddChildView(time_status_label_); | |
80 | |
81 icon_ = new views::ImageView; | |
82 AddChildView(icon_); | |
83 | |
84 return; | |
85 } | |
86 | |
87 // TODO(tdanderson): Tweak padding values for material design. | |
88 const bool material_design = | |
89 MaterialDesignController::IsSystemTrayMenuMaterial(); | |
90 views::BoxLayout* layout = new views::BoxLayout( | |
91 views::BoxLayout::kHorizontal, material_design ? 12 : 0, 0, | |
92 kTrayPopupPaddingBetweenItems); | |
93 SetLayoutManager(layout); | |
94 | |
95 if (!material_design) { | |
96 icon_ = TrayPopupUtils::CreateMainImageView(); | |
97 AddChildView(icon_); | |
98 } | |
99 | |
100 AddChildView(percentage_label_); | |
101 AddChildView(separator_label_); | |
102 AddChildView(time_status_label_); | |
103 } | |
104 | |
105 void PowerStatusView::UpdateText() { | |
106 const PowerStatus& status = *PowerStatus::Get(); | |
107 base::string16 battery_percentage; | |
108 base::string16 battery_time_status; | |
109 | |
110 if (status.IsBatteryFull()) { | |
111 battery_time_status = | |
112 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); | |
113 } else { | |
114 battery_percentage = base::FormatPercent(status.GetRoundedBatteryPercent()); | |
115 if (status.IsUsbChargerConnected()) { | |
116 battery_time_status = l10n_util::GetStringUTF16( | |
117 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE); | |
118 } else if (status.IsBatteryTimeBeingCalculated()) { | |
119 battery_time_status = | |
120 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); | |
121 } else { | |
122 base::TimeDelta time = status.IsBatteryCharging() | |
123 ? status.GetBatteryTimeToFull() | |
124 : status.GetBatteryTimeToEmpty(); | |
125 if (PowerStatus::ShouldDisplayBatteryTime(time) && | |
126 !status.IsBatteryDischargingOnLinePower()) { | |
127 battery_time_status = l10n_util::GetStringFUTF16( | |
128 status.IsBatteryCharging() | |
129 ? IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_SHORT | |
130 : IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_SHORT, | |
131 TimeDurationFormat(time, base::DURATION_WIDTH_NUMERIC)); | |
132 } | |
133 } | |
134 } | |
135 percentage_label_->SetVisible(!battery_percentage.empty()); | |
136 percentage_label_->SetText(battery_percentage); | |
137 separator_label_->SetVisible(!battery_percentage.empty() && | |
138 !battery_time_status.empty()); | |
139 time_status_label_->SetVisible(!battery_time_status.empty()); | |
140 time_status_label_->SetText(battery_time_status); | |
141 | |
142 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
143 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true); | |
144 | |
145 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO); | |
146 style.SetupLabel(percentage_label_); | |
147 style.SetupLabel(separator_label_); | |
148 style.SetupLabel(time_status_label_); | |
149 } | |
150 } | |
151 | |
152 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { | |
153 PreferredSizeChanged(); | |
154 } | |
155 | |
156 void PowerStatusView::Layout() { | |
157 views::View::Layout(); | |
158 | |
159 // Move the time_status_label_, separator_label_, and percentage_label_ | |
160 // closer to each other. | |
161 if (percentage_label_ && separator_label_ && time_status_label_ && | |
162 percentage_label_->visible() && time_status_label_->visible()) { | |
163 separator_label_->SetX(percentage_label_->bounds().right() + 1); | |
164 time_status_label_->SetX(separator_label_->bounds().right() + 1); | |
165 } | |
166 } | |
167 | |
168 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
169 if (!MaterialDesignController::IsSystemTrayMenuMaterial()) | |
170 return; | |
171 | |
172 node_data->role = ui::AX_ROLE_LABEL_TEXT; | |
173 node_data->SetName(accessible_name_); | |
174 } | |
175 | |
176 } // namespace ash | |
OLD | NEW |