Chromium Code Reviews| 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/power/tray_power.h" | 5 #include "ash/system/power/tray_power.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/system/date/date_view.h" | 9 #include "ash/system/date/date_view.h" |
| 10 #include "ash/system/power/power_status_view.h" | 10 #include "ash/system/power/power_status_view.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 const int kLowPowerSeconds = 15 * 60; | 56 const int kLowPowerSeconds = 15 * 60; |
| 57 const int kNoWarningSeconds = 30 * 60; | 57 const int kNoWarningSeconds = 30 * 60; |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 namespace tray { | 61 namespace tray { |
| 62 | 62 |
| 63 // This view is used only for the tray. | 63 // This view is used only for the tray. |
| 64 class PowerTrayView : public views::ImageView { | 64 class PowerTrayView : public views::ImageView { |
| 65 public: | 65 public: |
| 66 PowerTrayView() { | 66 PowerTrayView() |
| 67 : battery_icon_index_(-1) { | |
|
Daniel Erat
2012/09/11 22:17:09
nit: move to end of previous line
| |
| 67 UpdateImage(); | 68 UpdateImage(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 virtual ~PowerTrayView() { | 71 virtual ~PowerTrayView() { |
| 71 } | 72 } |
| 72 | 73 |
| 73 void UpdatePowerStatus(const PowerSupplyStatus& status) { | 74 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 74 supply_status_ = status; | 75 supply_status_ = status; |
| 75 // Sanitize. | 76 // Sanitize. |
| 76 if (supply_status_.battery_is_full) | 77 if (supply_status_.battery_is_full) |
| 77 supply_status_.battery_percentage = 100.0; | 78 supply_status_.battery_percentage = 100.0; |
| 78 | 79 |
| 79 UpdateImage(); | 80 UpdateImage(); |
| 80 SetVisible(status.battery_is_present); | 81 SetVisible(status.battery_is_present); |
| 81 } | 82 } |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 void UpdateImage() { | 85 void UpdateImage() { |
| 85 SetImage(TrayPower::GetBatteryImage(supply_status_, ICON_LIGHT, | 86 int index = TrayPower::GetBatteryImageIndex(supply_status_); |
| 86 GetImage())); | 87 if (battery_icon_index_ != index) { |
| 88 battery_icon_index_ = index; | |
| 89 if (battery_icon_index_ == -1) | |
| 90 SetImage(GetImage()); | |
|
Daniel Erat
2012/09/11 22:17:09
don't understand this either
| |
| 91 else | |
| 92 SetImage(TrayPower::GetBatteryImage(battery_icon_index_, ICON_LIGHT)); | |
| 93 } | |
| 87 } | 94 } |
| 88 | 95 |
| 89 PowerSupplyStatus supply_status_; | 96 PowerSupplyStatus supply_status_; |
| 90 | 97 |
| 98 // index of the current icon in the icon array image | |
|
Daniel Erat
2012/09/11 22:17:09
nit: capitalize, add period, and mention what -1 m
| |
| 99 int battery_icon_index_; | |
| 100 | |
| 91 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 101 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| 92 }; | 102 }; |
| 93 | 103 |
| 94 class PowerNotificationView : public TrayNotificationView { | 104 class PowerNotificationView : public TrayNotificationView { |
| 95 public: | 105 public: |
| 96 explicit PowerNotificationView(TrayPower* tray) | 106 explicit PowerNotificationView(TrayPower* tray) |
| 97 : TrayNotificationView(tray, 0) { | 107 : TrayNotificationView(tray, 0), |
| 108 battery_icon_index_(-1) { | |
| 98 power_status_view_ = | 109 power_status_view_ = |
| 99 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); | 110 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); |
| 100 InitView(power_status_view_); | 111 InitView(power_status_view_); |
| 101 } | 112 } |
| 102 | 113 |
| 103 void UpdatePowerStatus(const PowerSupplyStatus& status) { | 114 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 104 SetIconImage(TrayPower::GetBatteryImage(status, ICON_DARK, | 115 int index = TrayPower::GetBatteryImageIndex(status); |
| 105 GetIconImage())); | 116 if (battery_icon_index_ != index) { |
| 117 battery_icon_index_ = index; | |
| 118 if (battery_icon_index_ == -1) | |
|
Daniel Erat
2012/09/11 22:17:09
nit: add curly brackets
| |
| 119 SetIconImage(GetIconImage()); | |
|
Daniel Erat
2012/09/11 22:17:09
same as above
| |
| 120 else | |
| 121 SetIconImage(TrayPower::GetBatteryImage(battery_icon_index_, | |
| 122 ICON_DARK)); | |
| 123 } | |
| 106 power_status_view_->UpdatePowerStatus(status); | 124 power_status_view_->UpdatePowerStatus(status); |
| 107 } | 125 } |
| 108 | 126 |
| 109 private: | 127 private: |
| 110 PowerStatusView* power_status_view_; | 128 PowerStatusView* power_status_view_; |
| 111 | 129 |
| 130 // index of the current icon in the icon array image | |
|
Daniel Erat
2012/09/11 22:17:09
nit: capitalize and period
| |
| 131 int battery_icon_index_; | |
| 132 | |
| 112 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); | 133 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); |
| 113 }; | 134 }; |
| 114 | 135 |
| 115 } // namespace tray | 136 } // namespace tray |
| 116 | 137 |
| 117 using tray::PowerNotificationView; | 138 using tray::PowerNotificationView; |
| 118 | 139 |
| 119 TrayPower::TrayPower() | 140 TrayPower::TrayPower() |
| 120 : power_tray_(NULL), | 141 : power_tray_(NULL), |
| 121 notification_view_(NULL), | 142 notification_view_(NULL), |
| 122 notification_state_(NOTIFICATION_NONE) { | 143 notification_state_(NOTIFICATION_NONE) { |
| 123 } | 144 } |
| 124 | 145 |
| 125 TrayPower::~TrayPower() { | 146 TrayPower::~TrayPower() { |
| 126 } | 147 } |
| 127 | 148 |
| 128 // static | 149 // static |
| 129 gfx::ImageSkia TrayPower::GetBatteryImage( | 150 int TrayPower::GetBatteryImageIndex(const PowerSupplyStatus& supply_status) { |
| 130 const PowerSupplyStatus& supply_status, | |
| 131 IconSet icon_set, | |
| 132 const gfx::ImageSkia& default_image) { | |
| 133 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 134 icon_set == ICON_DARK ? | |
| 135 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); | |
| 136 | |
| 137 int image_index = 0; | 151 int image_index = 0; |
| 138 if (supply_status.battery_percentage >= 100) { | 152 if (supply_status.battery_percentage >= 100) { |
| 139 image_index = kNumPowerImages - 1; | 153 image_index = kNumPowerImages - 1; |
| 140 } else if (!supply_status.battery_is_present) { | 154 } else if (!supply_status.battery_is_present) { |
| 141 image_index = kNumPowerImages; | 155 image_index = kNumPowerImages; |
| 142 } else { | 156 } else { |
| 143 // If power supply is calculating battery time, the battery percentage | 157 // If power supply is calculating battery time, the battery percentage |
| 144 // is uncertain, just return |default_image|. | 158 // is uncertain, just return -1. |
| 145 if (supply_status.is_calculating_battery_time) | 159 if (supply_status.is_calculating_battery_time) |
| 146 return default_image; | 160 return -1; |
| 147 image_index = static_cast<int>(supply_status.battery_percentage / | 161 image_index = static_cast<int>(supply_status.battery_percentage / |
| 148 100.0 * (kNumPowerImages - 1)); | 162 100.0 * (kNumPowerImages - 1)); |
| 149 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); | 163 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); |
| 150 } | 164 } |
| 165 return (image_index << 1) | (supply_status.line_power_on ? 1 : 0); | |
| 166 } | |
| 167 | |
| 168 // static | |
| 169 gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, IconSet icon_set) { | |
| 170 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 171 icon_set == ICON_DARK ? | |
| 172 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); | |
| 151 | 173 |
| 152 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See | 174 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See |
| 153 // crbug.com/119832. | 175 // crbug.com/119832. |
| 154 gfx::Rect region( | 176 gfx::Rect region( |
| 155 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, | 177 ((image_index & 0x1) ? kBatteryImageWidth : 0) + 2, |
| 156 image_index * kBatteryImageHeight, | 178 (image_index >> 1) * kBatteryImageHeight, |
| 157 kBatteryImageWidth - 2, kBatteryImageHeight); | 179 kBatteryImageWidth - 2, kBatteryImageHeight); |
| 158 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); | 180 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); |
| 159 } | 181 } |
| 160 | 182 |
| 161 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { | 183 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { |
| 162 // There may not be enough information when this is created about whether | 184 // There may not be enough information when this is created about whether |
| 163 // there is a battery or not. So always create this, and adjust visibility as | 185 // there is a battery or not. So always create this, and adjust visibility as |
| 164 // necessary. | 186 // necessary. |
| 165 PowerSupplyStatus power_status = | 187 PowerSupplyStatus power_status = |
| 166 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); | 188 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 return false; | 276 return false; |
| 255 case NOTIFICATION_CRITICAL: | 277 case NOTIFICATION_CRITICAL: |
| 256 return false; | 278 return false; |
| 257 } | 279 } |
| 258 NOTREACHED(); | 280 NOTREACHED(); |
| 259 return false; | 281 return false; |
| 260 } | 282 } |
| 261 | 283 |
| 262 } // namespace internal | 284 } // namespace internal |
| 263 } // namespace ash | 285 } // namespace ash |
| OLD | NEW |