| OLD | NEW |
| 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/system/chromeos/power/power_status_view.h" | 5 #include "ash/system/chromeos/power/power_status_view.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/shell_delegate.h" | 8 #include "ash/shell_delegate.h" |
| 9 #include "ash/system/chromeos/power/power_status.h" | 9 #include "ash/system/chromeos/power/power_status.h" |
| 10 #include "ash/system/chromeos/power/tray_power.h" | 10 #include "ash/system/chromeos/power/tray_power.h" |
| 11 #include "ash/system/tray/fixed_sized_image_view.h" | 11 #include "ash/system/tray/fixed_sized_image_view.h" |
| 12 #include "ash/system/tray/tray_constants.h" | 12 #include "ash/system/tray/tray_constants.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "grit/ash_strings.h" | 15 #include "grit/ash_strings.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/l10n/time_format.h" | 17 #include "ui/base/l10n/time_format.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
| 20 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/layout/box_layout.h" | 21 #include "ui/views/layout/box_layout.h" |
| 22 #include "ui/views/layout/grid_layout.h" | 22 #include "ui/views/layout/grid_layout.h" |
| 23 | 23 |
| 24 namespace ash { | 24 namespace ash { |
| 25 | 25 |
| 26 // Padding between battery status text and battery icon on default view. | 26 // Padding between battery status text and battery icon on default view. |
| 27 const int kPaddingBetweenBatteryStatusAndIcon = 3; | 27 const int kPaddingBetweenBatteryStatusAndIcon = 3; |
| 28 | 28 |
| 29 PowerStatusView::PowerStatusView(ViewType view_type, | 29 PowerStatusView::PowerStatusView(bool default_view_right_align) |
| 30 bool default_view_right_align) | |
| 31 : default_view_right_align_(default_view_right_align), | 30 : default_view_right_align_(default_view_right_align), |
| 32 status_label_(NULL), | 31 time_status_label_(new views::Label), |
| 33 time_label_(NULL), | 32 percentage_label_(new views::Label), |
| 34 time_status_label_(NULL), | 33 icon_(NULL) { |
| 35 percentage_label_(NULL), | |
| 36 icon_(NULL), | |
| 37 view_type_(view_type) { | |
| 38 PowerStatus::Get()->AddObserver(this); | 34 PowerStatus::Get()->AddObserver(this); |
| 39 if (view_type == VIEW_DEFAULT) { | 35 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); |
| 40 time_status_label_ = new views::Label; | 36 LayoutView(); |
| 41 percentage_label_ = new views::Label; | |
| 42 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); | |
| 43 LayoutDefaultView(); | |
| 44 } else { | |
| 45 status_label_ = new views::Label; | |
| 46 time_label_ = new views::Label; | |
| 47 LayoutNotificationView(); | |
| 48 } | |
| 49 OnPowerStatusChanged(); | 37 OnPowerStatusChanged(); |
| 50 } | 38 } |
| 51 | 39 |
| 52 PowerStatusView::~PowerStatusView() { | 40 PowerStatusView::~PowerStatusView() { |
| 53 PowerStatus::Get()->RemoveObserver(this); | 41 PowerStatus::Get()->RemoveObserver(this); |
| 54 } | 42 } |
| 55 | 43 |
| 56 void PowerStatusView::OnPowerStatusChanged() { | 44 void PowerStatusView::OnPowerStatusChanged() { |
| 57 view_type_ == VIEW_DEFAULT ? | 45 UpdateText(); |
| 58 UpdateTextForDefaultView() : UpdateTextForNotificationView(); | |
| 59 | 46 |
| 60 if (icon_) { | 47 if (icon_) { |
| 61 icon_->SetImage( | 48 icon_->SetImage( |
| 62 PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK)); | 49 PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK)); |
| 63 icon_->SetVisible(true); | 50 icon_->SetVisible(true); |
| 64 } | 51 } |
| 65 } | 52 } |
| 66 | 53 |
| 67 void PowerStatusView::LayoutDefaultView() { | 54 void PowerStatusView::LayoutView() { |
| 68 if (default_view_right_align_) { | 55 if (default_view_right_align_) { |
| 69 views::BoxLayout* layout = | 56 views::BoxLayout* layout = |
| 70 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | 57 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, |
| 71 kPaddingBetweenBatteryStatusAndIcon); | 58 kPaddingBetweenBatteryStatusAndIcon); |
| 72 SetLayoutManager(layout); | 59 SetLayoutManager(layout); |
| 73 | 60 |
| 74 AddChildView(percentage_label_); | 61 AddChildView(percentage_label_); |
| 75 AddChildView(time_status_label_); | 62 AddChildView(time_status_label_); |
| 76 | 63 |
| 77 icon_ = new views::ImageView; | 64 icon_ = new views::ImageView; |
| 78 AddChildView(icon_); | 65 AddChildView(icon_); |
| 79 } else { | 66 } else { |
| 80 // PowerStatusView is left aligned on the system tray pop up item. | 67 // PowerStatusView is left aligned on the system tray pop up item. |
| 81 views::BoxLayout* layout = | 68 views::BoxLayout* layout = |
| 82 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | 69 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, |
| 83 kTrayPopupPaddingBetweenItems); | 70 kTrayPopupPaddingBetweenItems); |
| 84 SetLayoutManager(layout); | 71 SetLayoutManager(layout); |
| 85 | 72 |
| 86 icon_ = new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight); | 73 icon_ = new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight); |
| 87 AddChildView(icon_); | 74 AddChildView(icon_); |
| 88 | 75 |
| 89 AddChildView(percentage_label_); | 76 AddChildView(percentage_label_); |
| 90 AddChildView(time_status_label_); | 77 AddChildView(time_status_label_); |
| 91 } | 78 } |
| 92 } | 79 } |
| 93 | 80 |
| 94 void PowerStatusView::LayoutNotificationView() { | 81 void PowerStatusView::UpdateText() { |
| 95 SetLayoutManager( | |
| 96 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
| 97 status_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 98 AddChildView(status_label_); | |
| 99 | |
| 100 time_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 101 AddChildView(time_label_); | |
| 102 } | |
| 103 | |
| 104 void PowerStatusView::UpdateTextForDefaultView() { | |
| 105 const PowerStatus& status = *PowerStatus::Get(); | 82 const PowerStatus& status = *PowerStatus::Get(); |
| 106 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 83 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 107 base::string16 battery_percentage; | 84 base::string16 battery_percentage; |
| 108 base::string16 battery_time_status; | 85 base::string16 battery_time_status; |
| 109 | 86 |
| 110 if (status.IsBatteryFull()) { | 87 if (status.IsBatteryFull()) { |
| 111 battery_time_status = | 88 battery_time_status = |
| 112 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL); | 89 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BATTERY_FULL); |
| 113 } else { | 90 } else { |
| 114 battery_percentage = l10n_util::GetStringFUTF16( | 91 battery_percentage = l10n_util::GetStringFUTF16( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 141 } | 118 } |
| 142 battery_percentage = battery_time_status.empty() ? | 119 battery_percentage = battery_time_status.empty() ? |
| 143 battery_percentage : battery_percentage + base::ASCIIToUTF16(" - "); | 120 battery_percentage : battery_percentage + base::ASCIIToUTF16(" - "); |
| 144 } | 121 } |
| 145 percentage_label_->SetVisible(!battery_percentage.empty()); | 122 percentage_label_->SetVisible(!battery_percentage.empty()); |
| 146 percentage_label_->SetText(battery_percentage); | 123 percentage_label_->SetText(battery_percentage); |
| 147 time_status_label_->SetVisible(!battery_time_status.empty()); | 124 time_status_label_->SetVisible(!battery_time_status.empty()); |
| 148 time_status_label_->SetText(battery_time_status); | 125 time_status_label_->SetText(battery_time_status); |
| 149 } | 126 } |
| 150 | 127 |
| 151 void PowerStatusView::UpdateTextForNotificationView() { | |
| 152 const PowerStatus& status = *PowerStatus::Get(); | |
| 153 if (status.IsBatteryFull()) { | |
| 154 status_label_->SetText( | |
| 155 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 156 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); | |
| 157 } else { | |
| 158 status_label_->SetText( | |
| 159 l10n_util::GetStringFUTF16( | |
| 160 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, | |
| 161 base::IntToString16(status.GetRoundedBatteryPercent()))); | |
| 162 } | |
| 163 | |
| 164 const base::TimeDelta time = status.IsBatteryCharging() ? | |
| 165 status.GetBatteryTimeToFull() : status.GetBatteryTimeToEmpty(); | |
| 166 | |
| 167 if (status.IsUsbChargerConnected()) { | |
| 168 time_label_->SetText( | |
| 169 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 170 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE)); | |
| 171 } else if (status.IsBatteryTimeBeingCalculated()) { | |
| 172 time_label_->SetText( | |
| 173 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 174 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING)); | |
| 175 } else if (PowerStatus::ShouldDisplayBatteryTime(time) && | |
| 176 !status.IsBatteryDischargingOnLinePower()) { | |
| 177 int hour = 0, min = 0; | |
| 178 PowerStatus::SplitTimeIntoHoursAndMinutes(time, &hour, &min); | |
| 179 if (status.IsBatteryCharging()) { | |
| 180 time_label_->SetText( | |
| 181 l10n_util::GetStringFUTF16( | |
| 182 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL, | |
| 183 base::IntToString16(hour), | |
| 184 base::IntToString16(min))); | |
| 185 } else { | |
| 186 // This is a low battery warning prompting the user in minutes. | |
| 187 time_label_->SetText(ui::TimeFormat::Simple( | |
| 188 ui::TimeFormat::FORMAT_REMAINING, ui::TimeFormat::LENGTH_LONG, | |
| 189 base::TimeDelta::FromMinutes(hour * 60 + min))); | |
| 190 } | |
| 191 } else { | |
| 192 time_label_->SetText(base::string16()); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { | 128 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { |
| 197 PreferredSizeChanged(); | 129 PreferredSizeChanged(); |
| 198 } | 130 } |
| 199 | 131 |
| 200 gfx::Size PowerStatusView::GetPreferredSize() const { | 132 gfx::Size PowerStatusView::GetPreferredSize() const { |
| 201 gfx::Size size = views::View::GetPreferredSize(); | 133 gfx::Size size = views::View::GetPreferredSize(); |
| 202 return gfx::Size(size.width(), kTrayPopupItemHeight); | 134 return gfx::Size(size.width(), kTrayPopupItemHeight); |
| 203 } | 135 } |
| 204 | 136 |
| 205 int PowerStatusView::GetHeightForWidth(int width) const { | 137 int PowerStatusView::GetHeightForWidth(int width) const { |
| 206 return kTrayPopupItemHeight; | 138 return kTrayPopupItemHeight; |
| 207 } | 139 } |
| 208 | 140 |
| 209 void PowerStatusView::Layout() { | 141 void PowerStatusView::Layout() { |
| 210 views::View::Layout(); | 142 views::View::Layout(); |
| 211 | 143 |
| 212 // Move the time_status_label_ closer to percentage_label_. | 144 // Move the time_status_label_ closer to percentage_label_. |
| 213 if (percentage_label_ && time_status_label_ && | 145 if (percentage_label_ && time_status_label_ && |
| 214 percentage_label_->visible() && time_status_label_->visible()) { | 146 percentage_label_->visible() && time_status_label_->visible()) { |
| 215 time_status_label_->SetX(percentage_label_->bounds().right() + 1); | 147 time_status_label_->SetX(percentage_label_->bounds().right() + 1); |
| 216 } | 148 } |
| 217 } | 149 } |
| 218 | 150 |
| 219 } // namespace ash | 151 } // namespace ash |
| OLD | NEW |