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

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

Issue 2795823002: Remove pre-MD code from PowerStatusView. (Closed)
Patch Set: remove todo 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 if (MaterialDesignController::IsSystemTrayMenuMaterial()) 30 SetFocusBehavior(FocusBehavior::ALWAYS);
37 SetFocusBehavior(FocusBehavior::ALWAYS);
38 31
39 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); 32 percentage_label_->SetEnabledColor(kHeaderTextColorNormal);
40 separator_label_->SetEnabledColor(kHeaderTextColorNormal); 33 separator_label_->SetEnabledColor(kHeaderTextColorNormal);
41 separator_label_->SetText(base::ASCIIToUTF16(" - ")); 34 separator_label_->SetText(base::ASCIIToUTF16(" - "));
42 LayoutView(); 35
36 views::BoxLayout* layout = new views::BoxLayout(
37 views::BoxLayout::kHorizontal, 12, 0, kTrayPopupPaddingBetweenItems);
38 SetLayoutManager(layout);
39
40 AddChildView(percentage_label_);
41 AddChildView(separator_label_);
42 AddChildView(time_status_label_);
43
43 PowerStatus::Get()->AddObserver(this); 44 PowerStatus::Get()->AddObserver(this);
44 OnPowerStatusChanged(); 45 OnPowerStatusChanged();
45 } 46 }
46 47
47 PowerStatusView::~PowerStatusView() { 48 PowerStatusView::~PowerStatusView() {
48 PowerStatus::Get()->RemoveObserver(this); 49 PowerStatus::Get()->RemoveObserver(this);
49 } 50 }
50 51
51 void PowerStatusView::OnPowerStatusChanged() { 52 void PowerStatusView::OnPowerStatusChanged() {
52 UpdateText(); 53 UpdateText();
53 } 54 }
54 55
55 void PowerStatusView::LayoutView() {
56 if (default_view_right_align_) {
57 views::BoxLayout* layout =
58 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
59 kPaddingBetweenBatteryStatusAndIcon);
60 SetLayoutManager(layout);
61
62 AddChildView(percentage_label_);
63 AddChildView(separator_label_);
64 AddChildView(time_status_label_);
65 AddChildView(new views::ImageView);
66
67 return;
68 }
69
70 // TODO(tdanderson): Tweak padding values for material design.
71 const bool material_design =
72 MaterialDesignController::IsSystemTrayMenuMaterial();
73 views::BoxLayout* layout = new views::BoxLayout(
74 views::BoxLayout::kHorizontal, material_design ? 12 : 0, 0,
75 kTrayPopupPaddingBetweenItems);
76 SetLayoutManager(layout);
77
78 if (!material_design)
79 AddChildView(TrayPopupUtils::CreateMainImageView());
80
81 AddChildView(percentage_label_);
82 AddChildView(separator_label_);
83 AddChildView(time_status_label_);
84 }
85
86 void PowerStatusView::UpdateText() { 56 void PowerStatusView::UpdateText() {
87 const PowerStatus& status = *PowerStatus::Get(); 57 const PowerStatus& status = *PowerStatus::Get();
88 base::string16 battery_percentage; 58 base::string16 battery_percentage;
89 base::string16 battery_time_status; 59 base::string16 battery_time_status;
90 60
91 if (status.IsBatteryFull()) { 61 if (status.IsBatteryFull()) {
92 battery_time_status = 62 battery_time_status =
93 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); 63 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL);
94 } else { 64 } else {
95 battery_percentage = base::FormatPercent(status.GetRoundedBatteryPercent()); 65 battery_percentage = base::FormatPercent(status.GetRoundedBatteryPercent());
(...skipping 21 matching lines...) Expand all
117 } 87 }
118 } 88 }
119 } 89 }
120 percentage_label_->SetVisible(!battery_percentage.empty()); 90 percentage_label_->SetVisible(!battery_percentage.empty());
121 percentage_label_->SetText(battery_percentage); 91 percentage_label_->SetText(battery_percentage);
122 separator_label_->SetVisible(!battery_percentage.empty() && 92 separator_label_->SetVisible(!battery_percentage.empty() &&
123 !battery_time_status.empty()); 93 !battery_time_status.empty());
124 time_status_label_->SetVisible(!battery_time_status.empty()); 94 time_status_label_->SetVisible(!battery_time_status.empty());
125 time_status_label_->SetText(battery_time_status); 95 time_status_label_->SetText(battery_time_status);
126 96
127 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { 97 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true);
128 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true);
129 98
130 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO); 99 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO);
131 style.SetupLabel(percentage_label_); 100 style.SetupLabel(percentage_label_);
132 style.SetupLabel(separator_label_); 101 style.SetupLabel(separator_label_);
133 style.SetupLabel(time_status_label_); 102 style.SetupLabel(time_status_label_);
134 }
135 } 103 }
136 104
137 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { 105 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) {
138 PreferredSizeChanged(); 106 PreferredSizeChanged();
139 } 107 }
140 108
141 void PowerStatusView::Layout() { 109 void PowerStatusView::Layout() {
142 views::View::Layout(); 110 views::View::Layout();
143 111
144 // Move the time_status_label_, separator_label_, and percentage_label_ 112 // Move the time_status_label_, separator_label_, and percentage_label_
145 // closer to each other. 113 // closer to each other.
146 if (percentage_label_ && separator_label_ && time_status_label_ && 114 if (percentage_label_ && separator_label_ && time_status_label_ &&
147 percentage_label_->visible() && time_status_label_->visible()) { 115 percentage_label_->visible() && time_status_label_->visible()) {
148 separator_label_->SetX(percentage_label_->bounds().right() + 1); 116 separator_label_->SetX(percentage_label_->bounds().right() + 1);
149 time_status_label_->SetX(separator_label_->bounds().right() + 1); 117 time_status_label_->SetX(separator_label_->bounds().right() + 1);
150 } 118 }
151 } 119 }
152 120
153 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) { 121 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
154 if (!MaterialDesignController::IsSystemTrayMenuMaterial())
155 return;
156
157 node_data->role = ui::AX_ROLE_LABEL_TEXT; 122 node_data->role = ui::AX_ROLE_LABEL_TEXT;
158 node_data->SetName(accessible_name_); 123 node_data->SetName(accessible_name_);
159 } 124 }
160 125
161 } // namespace ash 126 } // 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