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

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

Issue 10540041: Add battery status to settings row in uber tray bubble. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
(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/system/power/power_status_view.h"
6
7 #include "ash/system/power/tray_power.h"
8 #include "ash/system/tray/tray_constants.h"
9 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h"
11 #include "grit/ash_strings.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/views/controls/image_view.h"
15 #include "ui/views/controls/label.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/layout/grid_layout.h"
18
19 namespace ash {
20 namespace internal {
21
22 namespace {
23
24 // Top/bottom padding of the text items.
25 const int kPaddingVertical = 10;
26 // Specify min width of status label for layout.
27 const int kLabelMinWidth = 120;
28
29 } // namespace
30
31 namespace tray {
32
33 PowerStatusView::PowerStatusView(ViewType view_type) :
sadrul 2012/06/07 15:14:35 : in the next line
jennyz 2012/06/07 22:06:52 Done.
34 icon_(NULL),
35 view_type_(view_type) {
36 if (view_type == VIEW_DEFAULT) {
37 time_status_label_ = new views::Label;
38 LayoutDefaultView();
39 } else {
40 status_label_ = new views::Label;
41 time_label_ = new views::Label;
42 LayoutNotificationView();
43 }
stevenjb 2012/06/07 01:31:18 I realize that I set the precedent, but I now that
jennyz 2012/06/07 22:06:52 Yes, I feel this is a bit unclean. Maybe it is bet
44 Update();
45 }
46
47 void PowerStatusView::UpdatePowerStatus(const PowerSupplyStatus& status) {
48 supply_status_ = status;
49 // Sanitize.
50 if (supply_status_.battery_is_full)
51 supply_status_.battery_percentage = 100.0;
52
53 Update();
54 }
55
56 void PowerStatusView::LayoutDefaultView() {
57 views::BoxLayout* layout =
58 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
59 3 /* padding between time_status_label_ and icon_ */
stevenjb 2012/06/07 01:31:18 nit: This should really be a const variable with t
jennyz 2012/06/07 22:06:52 Done.
60 );
61 SetLayoutManager(layout);
62
63 time_status_label_->SetHorizontalAlignment(views::Label::ALIGN_RIGHT);
64 AddChildView(time_status_label_);
65
66 icon_ = new views::ImageView;
67 AddChildView(icon_);
68 }
69
70 void PowerStatusView::LayoutNotificationView() {
71 SetLayoutManager(
72 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
73 status_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
74 AddChildView(status_label_);
75
76 time_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
77 AddChildView(time_label_);
78 }
79
80 void PowerStatusView::UpdateText() {
81 view_type_ == VIEW_DEFAULT ?
82 UpdateTextForDefaultView() : UpdateTextForNotificationView();
83 }
84
85 void PowerStatusView::UpdateTextForDefaultView() {
86 base::TimeDelta time = base::TimeDelta::FromSeconds(
87 supply_status_.averaged_battery_time_to_empty);
88 int hour = time.InHours();
89 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
90
91 if (supply_status_.line_power_on && !hour && !min) {
92 time_status_label_->SetText(
93 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
94 IDS_ASH_STATUS_TRAY_BATTERY_FULL));
95 } else {
96 string16 battery_percentage = l10n_util::GetStringFUTF16(
97 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY,
98 base::IntToString16(
99 static_cast<int>(supply_status_.battery_percentage)));
100 string16 battery_time = string16();
101 if (supply_status_.is_calculating_battery_time) {
102 battery_time =
103 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
104 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING);
105 } else if (hour || min){
106 battery_time =
107 l10n_util::GetStringFUTF16(
108 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ONLY,
109 base::IntToString16(hour),
110 base::IntToString16(min)) +
111 ASCIIToUTF16(" - ");
112 }
113 string16 battery_status = battery_time + battery_percentage;
114 time_status_label_->SetText(battery_status);
115 }
116 }
117
118 void PowerStatusView::UpdateTextForNotificationView() {
119 base::TimeDelta time = base::TimeDelta::FromSeconds(
120 supply_status_.line_power_on ?
121 supply_status_.averaged_battery_time_to_full :
122 supply_status_.averaged_battery_time_to_empty);
123 int hour = time.InHours();
124 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
125
126 if (supply_status_.line_power_on && !hour && !min) {
127 status_label_->SetText(
128 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
129 IDS_ASH_STATUS_TRAY_BATTERY_FULL));
130 } else {
131 status_label_->SetText(
132 l10n_util::GetStringFUTF16(
133 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT,
134 base::IntToString16(
135 static_cast<int>(supply_status_.battery_percentage))));
136 }
137
138 if (supply_status_.is_calculating_battery_time) {
139 time_label_->SetText(
140 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
141 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING));
142 } else if (hour || min) {
143 time_label_->SetText(
144 l10n_util::GetStringFUTF16(
145 supply_status_.line_power_on ?
146 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL :
147 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_EMPTY,
148 base::IntToString16(hour),
149 base::IntToString16(min)));
150 } else {
151 time_label_->SetText(string16());
152 }
153
154 }
155
156 void PowerStatusView::UpdateIcon() {
157 if (icon_) {
158 icon_->SetImage(GetBatteryImage(supply_status_, ICON_DARK));
159 icon_->SetVisible(true);
160 }
161 }
162
163 void PowerStatusView::Update() {
164 UpdateText();
165 UpdateIcon();
166 }
167
168 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) {
169 PreferredSizeChanged();
170 }
171
172 gfx::Size PowerStatusView::GetPreferredSize() {
173 gfx::Size size = views::View::GetPreferredSize();
174 return gfx::Size(size.width(), kTrayPopupItemHeight);
175 }
176
177 } // namespace tray
178 } // namespace internal
179 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698