Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: ash/common/system/chromeos/power/power_status_view.cc

Issue 2795823002: Remove pre-MD code from PowerStatusView. (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/common/system/chromeos/power/power_status_view.h" 5 #include "ash/common/system/chromeos/power/power_status_view.h"
6 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" 7 #include "ash/common/system/chromeos/power/tray_power.h"
10 #include "ash/common/system/tray/fixed_sized_image_view.h" 8 #include "ash/common/system/tray/fixed_sized_image_view.h"
11 #include "ash/common/system/tray/tray_constants.h" 9 #include "ash/common/system/tray/tray_constants.h"
12 #include "ash/common/system/tray/tray_popup_item_style.h" 10 #include "ash/common/system/tray/tray_popup_item_style.h"
13 #include "ash/common/system/tray/tray_popup_utils.h" 11 #include "ash/common/system/tray/tray_popup_utils.h"
14 #include "ash/strings/grit/ash_strings.h" 12 #include "ash/strings/grit/ash_strings.h"
15 #include "base/i18n/number_formatting.h" 13 #include "base/i18n/number_formatting.h"
16 #include "base/i18n/time_formatting.h" 14 #include "base/i18n/time_formatting.h"
17 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
18 #include "ui/accessibility/ax_node_data.h" 16 #include "ui/accessibility/ax_node_data.h"
19 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/native_theme/native_theme.h" 18 #include "ui/native_theme/native_theme.h"
21 #include "ui/views/controls/image_view.h" 19 #include "ui/views/controls/image_view.h"
22 #include "ui/views/controls/label.h" 20 #include "ui/views/controls/label.h"
23 #include "ui/views/layout/box_layout.h" 21 #include "ui/views/layout/box_layout.h"
24 #include "ui/views/layout/grid_layout.h" 22 #include "ui/views/layout/grid_layout.h"
25 23
26 namespace ash { 24 namespace ash {
27 25
28 // Padding between battery status text and battery icon on default view. 26 PowerStatusView::PowerStatusView()
29 const int kPaddingBetweenBatteryStatusAndIcon = 3; 27 : percentage_label_(new views::Label),
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), 28 separator_label_(new views::Label),
35 time_status_label_(new views::Label), 29 time_status_label_(new views::Label) {
36 icon_(nullptr) { 30 SetFocusBehavior(FocusBehavior::ALWAYS);
37 if (MaterialDesignController::IsSystemTrayMenuMaterial())
38 SetFocusBehavior(FocusBehavior::ALWAYS);
39 31
40 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); 32 percentage_label_->SetEnabledColor(kHeaderTextColorNormal);
41 separator_label_->SetEnabledColor(kHeaderTextColorNormal); 33 separator_label_->SetEnabledColor(kHeaderTextColorNormal);
42 separator_label_->SetText(base::ASCIIToUTF16(" - ")); 34 separator_label_->SetText(base::ASCIIToUTF16(" - "));
43 LayoutView(); 35
36 // TODO(tdanderson): Tweak padding values for material design.
37 views::BoxLayout* layout = new views::BoxLayout(
38 views::BoxLayout::kHorizontal, 12, 0, kTrayPopupPaddingBetweenItems);
39 SetLayoutManager(layout);
40
41 AddChildView(percentage_label_);
42 AddChildView(separator_label_);
43 AddChildView(time_status_label_);
44
44 PowerStatus::Get()->AddObserver(this); 45 PowerStatus::Get()->AddObserver(this);
45 OnPowerStatusChanged(); 46 OnPowerStatusChanged();
46 } 47 }
47 48
48 PowerStatusView::~PowerStatusView() { 49 PowerStatusView::~PowerStatusView() {
49 PowerStatus::Get()->RemoveObserver(this); 50 PowerStatus::Get()->RemoveObserver(this);
50 } 51 }
51 52
52 void PowerStatusView::OnPowerStatusChanged() { 53 void PowerStatusView::OnPowerStatusChanged() {
53 UpdateText(); 54 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())
tdanderson 2017/04/03 22:18:02 I think this is the last call site of MaterialDesi
Evan Stade 2017/04/04 00:51:53 Done.
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;
tdanderson 2017/04/03 22:18:02 Can you rebase this CL against the latest ToT? The
Evan Stade 2017/04/04 00:51:53 Done.
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 } 55 }
104 56
105 void PowerStatusView::UpdateText() { 57 void PowerStatusView::UpdateText() {
106 const PowerStatus& status = *PowerStatus::Get(); 58 const PowerStatus& status = *PowerStatus::Get();
107 base::string16 battery_percentage; 59 base::string16 battery_percentage;
108 base::string16 battery_time_status; 60 base::string16 battery_time_status;
109 61
110 if (status.IsBatteryFull()) { 62 if (status.IsBatteryFull()) {
111 battery_time_status = 63 battery_time_status =
112 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); 64 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL);
(...skipping 23 matching lines...) Expand all
136 } 88 }
137 } 89 }
138 } 90 }
139 percentage_label_->SetVisible(!battery_percentage.empty()); 91 percentage_label_->SetVisible(!battery_percentage.empty());
140 percentage_label_->SetText(battery_percentage); 92 percentage_label_->SetText(battery_percentage);
141 separator_label_->SetVisible(!battery_percentage.empty() && 93 separator_label_->SetVisible(!battery_percentage.empty() &&
142 !battery_time_status.empty()); 94 !battery_time_status.empty());
143 time_status_label_->SetVisible(!battery_time_status.empty()); 95 time_status_label_->SetVisible(!battery_time_status.empty());
144 time_status_label_->SetText(battery_time_status); 96 time_status_label_->SetText(battery_time_status);
145 97
146 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { 98 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true);
147 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true);
148 99
149 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO); 100 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO);
150 style.SetupLabel(percentage_label_); 101 style.SetupLabel(percentage_label_);
151 style.SetupLabel(separator_label_); 102 style.SetupLabel(separator_label_);
152 style.SetupLabel(time_status_label_); 103 style.SetupLabel(time_status_label_);
153 }
154 } 104 }
155 105
156 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { 106 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) {
157 PreferredSizeChanged(); 107 PreferredSizeChanged();
158 } 108 }
159 109
160 void PowerStatusView::Layout() { 110 void PowerStatusView::Layout() {
161 views::View::Layout(); 111 views::View::Layout();
162 112
163 // Move the time_status_label_, separator_label_, and percentage_label_ 113 // Move the time_status_label_, separator_label_, and percentage_label_
164 // closer to each other. 114 // closer to each other.
165 if (percentage_label_ && separator_label_ && time_status_label_ && 115 if (percentage_label_ && separator_label_ && time_status_label_ &&
166 percentage_label_->visible() && time_status_label_->visible()) { 116 percentage_label_->visible() && time_status_label_->visible()) {
167 separator_label_->SetX(percentage_label_->bounds().right() + 1); 117 separator_label_->SetX(percentage_label_->bounds().right() + 1);
168 time_status_label_->SetX(separator_label_->bounds().right() + 1); 118 time_status_label_->SetX(separator_label_->bounds().right() + 1);
169 } 119 }
170 } 120 }
171 121
172 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) { 122 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
173 if (!MaterialDesignController::IsSystemTrayMenuMaterial())
174 return;
175
176 node_data->role = ui::AX_ROLE_LABEL_TEXT; 123 node_data->role = ui::AX_ROLE_LABEL_TEXT;
177 node_data->SetName(accessible_name_); 124 node_data->SetName(accessible_name_);
178 } 125 }
179 126
180 } // namespace ash 127 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/power/power_status_view.h ('k') | ash/common/system/chromeos/power/power_status_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698