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/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 // Padding between battery status text and battery icon on default view. |
| 29 const int kPaddingBetweenBatteryStatusAndIcon = 3; |
| 30 } // namespace |
| 31 |
| 32 PowerStatusView::PowerStatusView(ViewType view_type) |
| 33 : status_label_(NULL), |
| 34 time_label_(NULL), |
| 35 time_status_label_(NULL), |
| 36 icon_(NULL), |
| 37 view_type_(view_type) { |
| 38 if (view_type == VIEW_DEFAULT) { |
| 39 time_status_label_ = new views::Label; |
| 40 LayoutDefaultView(); |
| 41 } else { |
| 42 status_label_ = new views::Label; |
| 43 time_label_ = new views::Label; |
| 44 LayoutNotificationView(); |
| 45 } |
| 46 Update(); |
| 47 } |
| 48 |
| 49 void PowerStatusView::UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 50 supply_status_ = status; |
| 51 // Sanitize. |
| 52 if (supply_status_.battery_is_full) |
| 53 supply_status_.battery_percentage = 100.0; |
| 54 |
| 55 Update(); |
| 56 } |
| 57 |
| 58 void PowerStatusView::LayoutDefaultView() { |
| 59 views::BoxLayout* layout = |
| 60 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, |
| 61 kPaddingBetweenBatteryStatusAndIcon); |
| 62 SetLayoutManager(layout); |
| 63 |
| 64 time_status_label_->SetHorizontalAlignment(views::Label::ALIGN_RIGHT); |
| 65 AddChildView(time_status_label_); |
| 66 |
| 67 icon_ = new views::ImageView; |
| 68 AddChildView(icon_); |
| 69 } |
| 70 |
| 71 void PowerStatusView::LayoutNotificationView() { |
| 72 SetLayoutManager( |
| 73 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); |
| 74 status_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 75 AddChildView(status_label_); |
| 76 |
| 77 time_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 78 AddChildView(time_label_); |
| 79 } |
| 80 |
| 81 void PowerStatusView::UpdateText() { |
| 82 view_type_ == VIEW_DEFAULT ? |
| 83 UpdateTextForDefaultView() : UpdateTextForNotificationView(); |
| 84 } |
| 85 |
| 86 void PowerStatusView::UpdateTextForDefaultView() { |
| 87 int hour = 0; |
| 88 int min = 0; |
| 89 if (!supply_status_.is_calculating_battery_time) { |
| 90 base::TimeDelta time = base::TimeDelta::FromSeconds( |
| 91 supply_status_.averaged_battery_time_to_empty); |
| 92 hour = time.InHours(); |
| 93 min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); |
| 94 } |
| 95 |
| 96 if (supply_status_.line_power_on && supply_status_.battery_is_full) { |
| 97 time_status_label_->SetText( |
| 98 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 99 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); |
| 100 } else { |
| 101 string16 battery_percentage = l10n_util::GetStringFUTF16( |
| 102 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT_ONLY, |
| 103 base::IntToString16( |
| 104 static_cast<int>(supply_status_.battery_percentage))); |
| 105 string16 battery_time = string16(); |
| 106 if (supply_status_.is_calculating_battery_time) { |
| 107 battery_time = |
| 108 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 109 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING); |
| 110 } else if (hour || min){ |
| 111 battery_time = |
| 112 l10n_util::GetStringFUTF16( |
| 113 IDS_ASH_STATUS_TRAY_BATTERY_TIME_ONLY, |
| 114 base::IntToString16(hour), |
| 115 base::IntToString16(min)) + |
| 116 ASCIIToUTF16(" - "); |
| 117 } |
| 118 string16 battery_status = battery_time + battery_percentage; |
| 119 time_status_label_->SetText(battery_status); |
| 120 } |
| 121 } |
| 122 |
| 123 void PowerStatusView::UpdateTextForNotificationView() { |
| 124 int hour = 0; |
| 125 int min = 0; |
| 126 if (!supply_status_.is_calculating_battery_time) { |
| 127 base::TimeDelta time = base::TimeDelta::FromSeconds( |
| 128 supply_status_.line_power_on ? |
| 129 supply_status_.averaged_battery_time_to_full : |
| 130 supply_status_.averaged_battery_time_to_empty); |
| 131 hour = time.InHours(); |
| 132 min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); |
| 133 } |
| 134 |
| 135 if (supply_status_.line_power_on && supply_status_.battery_is_full) { |
| 136 status_label_->SetText( |
| 137 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 138 IDS_ASH_STATUS_TRAY_BATTERY_FULL)); |
| 139 } else { |
| 140 status_label_->SetText( |
| 141 l10n_util::GetStringFUTF16( |
| 142 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT, |
| 143 base::IntToString16( |
| 144 static_cast<int>(supply_status_.battery_percentage)))); |
| 145 } |
| 146 |
| 147 if (supply_status_.is_calculating_battery_time) { |
| 148 time_label_->SetText( |
| 149 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 150 IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING)); |
| 151 } else if (hour || min) { |
| 152 time_label_->SetText( |
| 153 l10n_util::GetStringFUTF16( |
| 154 supply_status_.line_power_on ? |
| 155 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL : |
| 156 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_EMPTY, |
| 157 base::IntToString16(hour), |
| 158 base::IntToString16(min))); |
| 159 } else { |
| 160 time_label_->SetText(string16()); |
| 161 } |
| 162 |
| 163 } |
| 164 |
| 165 void PowerStatusView::UpdateIcon() { |
| 166 if (icon_) { |
| 167 icon_->SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_DARK)); |
| 168 icon_->SetVisible(true); |
| 169 } |
| 170 } |
| 171 |
| 172 void PowerStatusView::Update() { |
| 173 UpdateText(); |
| 174 UpdateIcon(); |
| 175 } |
| 176 |
| 177 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { |
| 178 PreferredSizeChanged(); |
| 179 } |
| 180 |
| 181 gfx::Size PowerStatusView::GetPreferredSize() { |
| 182 gfx::Size size = views::View::GetPreferredSize(); |
| 183 return gfx::Size(size.width(), kTrayPopupItemHeight); |
| 184 } |
| 185 |
| 186 } // namespace internal |
| 187 } // namespace ash |
OLD | NEW |