| 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/system/chromeos/power/power_status_view.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/fixed_sized_image_view.h" | |
| 8 #include "ash/common/system/tray/tray_constants.h" | |
| 9 #include "ash/shell.h" | |
| 10 #include "ash/shell_delegate.h" | |
| 11 #include "ash/system/chromeos/power/power_status.h" | |
| 12 #include "ash/system/chromeos/power/tray_power.h" | |
| 13 #include "base/i18n/message_formatter.h" | |
| 14 #include "base/i18n/time_formatting.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "grit/ash_strings.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 #include "ui/views/controls/image_view.h" | |
| 19 #include "ui/views/controls/label.h" | |
| 20 #include "ui/views/layout/box_layout.h" | |
| 21 #include "ui/views/layout/grid_layout.h" | |
| 22 | |
| 23 namespace ash { | |
| 24 | |
| 25 // Padding between battery status text and battery icon on default view. | |
| 26 const int kPaddingBetweenBatteryStatusAndIcon = 3; | |
| 27 | |
| 28 PowerStatusView::PowerStatusView(bool default_view_right_align) | |
| 29 : default_view_right_align_(default_view_right_align), | |
| 30 percentage_label_(new views::Label), | |
| 31 separator_label_(new views::Label), | |
| 32 time_status_label_(new views::Label), | |
| 33 icon_(nullptr) { | |
| 34 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); | |
| 35 separator_label_->SetEnabledColor(kHeaderTextColorNormal); | |
| 36 separator_label_->SetText(base::ASCIIToUTF16(" - ")); | |
| 37 LayoutView(); | |
| 38 PowerStatus::Get()->AddObserver(this); | |
| 39 OnPowerStatusChanged(); | |
| 40 } | |
| 41 | |
| 42 PowerStatusView::~PowerStatusView() { | |
| 43 PowerStatus::Get()->RemoveObserver(this); | |
| 44 } | |
| 45 | |
| 46 void PowerStatusView::OnPowerStatusChanged() { | |
| 47 UpdateText(); | |
| 48 const PowerStatus::BatteryImageInfo info = | |
| 49 PowerStatus::Get()->GetBatteryImageInfo(PowerStatus::ICON_DARK); | |
| 50 if (info != previous_battery_image_info_) { | |
| 51 icon_->SetImage( | |
| 52 PowerStatus::Get()->GetBatteryImage(PowerStatus::ICON_DARK)); | |
| 53 icon_->SetVisible(true); | |
| 54 previous_battery_image_info_ = info; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 void PowerStatusView::LayoutView() { | |
| 59 if (default_view_right_align_) { | |
| 60 views::BoxLayout* layout = | |
| 61 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
| 62 kPaddingBetweenBatteryStatusAndIcon); | |
| 63 SetLayoutManager(layout); | |
| 64 | |
| 65 AddChildView(percentage_label_); | |
| 66 AddChildView(separator_label_); | |
| 67 AddChildView(time_status_label_); | |
| 68 | |
| 69 icon_ = new views::ImageView; | |
| 70 AddChildView(icon_); | |
| 71 } else { | |
| 72 // PowerStatusView is left aligned on the system tray pop up item. | |
| 73 views::BoxLayout* layout = | |
| 74 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
| 75 kTrayPopupPaddingBetweenItems); | |
| 76 SetLayoutManager(layout); | |
| 77 | |
| 78 icon_ = new ash::FixedSizedImageView(0, ash::kTrayPopupItemHeight); | |
| 79 AddChildView(icon_); | |
| 80 | |
| 81 AddChildView(percentage_label_); | |
| 82 AddChildView(separator_label_); | |
| 83 AddChildView(time_status_label_); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void PowerStatusView::UpdateText() { | |
| 88 const PowerStatus& status = *PowerStatus::Get(); | |
| 89 base::string16 battery_percentage; | |
| 90 base::string16 battery_time_status; | |
| 91 | |
| 92 if (status.IsBatteryFull()) { | |
| 93 battery_time_status = | |
| 94 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); | |
| 95 } else { | |
| 96 battery_percentage = base::i18n::MessageFormatter::FormatWithNumberedArgs( | |
| 97 base::ASCIIToUTF16("{0,number,percent}"), | |
| 98 static_cast<double>(status.GetRoundedBatteryPercent()) / 100.0); | |
| 99 if (status.IsUsbChargerConnected()) { | |
| 100 battery_time_status = l10n_util::GetStringUTF16( | |
| 101 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE); | |
| 102 } else if (status.IsBatteryTimeBeingCalculated()) { | |
| 103 battery_time_status = | |
| 104 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); | |
| 105 } else { | |
| 106 base::TimeDelta time = status.IsBatteryCharging() ? | |
| 107 status.GetBatteryTimeToFull() : status.GetBatteryTimeToEmpty(); | |
| 108 if (PowerStatus::ShouldDisplayBatteryTime(time) && | |
| 109 !status.IsBatteryDischargingOnLinePower()) { | |
| 110 battery_time_status = l10n_util::GetStringFUTF16( | |
| 111 status.IsBatteryCharging() | |
| 112 ? IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL_SHORT | |
| 113 : IDS_ASH_STATUS_TRAY_BATTERY_TIME_LEFT_SHORT, | |
| 114 TimeDurationFormat(time, base::DURATION_WIDTH_NUMERIC)); | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 percentage_label_->SetVisible(!battery_percentage.empty()); | |
| 119 percentage_label_->SetText(battery_percentage); | |
| 120 separator_label_->SetVisible(!battery_percentage.empty() && | |
| 121 !battery_time_status.empty()); | |
| 122 time_status_label_->SetVisible(!battery_time_status.empty()); | |
| 123 time_status_label_->SetText(battery_time_status); | |
| 124 } | |
| 125 | |
| 126 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { | |
| 127 PreferredSizeChanged(); | |
| 128 } | |
| 129 | |
| 130 gfx::Size PowerStatusView::GetPreferredSize() const { | |
| 131 gfx::Size size = views::View::GetPreferredSize(); | |
| 132 return gfx::Size(size.width(), kTrayPopupItemHeight); | |
| 133 } | |
| 134 | |
| 135 int PowerStatusView::GetHeightForWidth(int width) const { | |
| 136 return kTrayPopupItemHeight; | |
| 137 } | |
| 138 | |
| 139 void PowerStatusView::Layout() { | |
| 140 views::View::Layout(); | |
| 141 | |
| 142 // Move the time_status_label_, separator_label_, and percentage_label_ | |
| 143 // closer to each other. | |
| 144 if (percentage_label_ && separator_label_ && time_status_label_ && | |
| 145 percentage_label_->visible() && time_status_label_->visible()) { | |
| 146 separator_label_->SetX(percentage_label_->bounds().right() + 1); | |
| 147 time_status_label_->SetX(separator_label_->bounds().right() + 1); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 } // namespace ash | |
| OLD | NEW |