| 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_image_index_(-1) { |
| 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_); |
| 87 if (battery_image_index_ != index) { |
| 88 battery_image_index_ = index; |
| 89 SetImage(TrayPower::GetBatteryImage(battery_image_index_, ICON_LIGHT)); |
| 90 } |
| 86 } | 91 } |
| 87 | 92 |
| 88 PowerSupplyStatus supply_status_; | 93 PowerSupplyStatus supply_status_; |
| 94 int battery_image_index_; |
| 89 | 95 |
| 90 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 96 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| 91 }; | 97 }; |
| 92 | 98 |
| 93 class PowerNotificationView : public TrayNotificationView { | 99 class PowerNotificationView : public TrayNotificationView { |
| 94 public: | 100 public: |
| 95 explicit PowerNotificationView(TrayPower* tray) | 101 explicit PowerNotificationView(TrayPower* tray) |
| 96 : TrayNotificationView(tray, 0) { | 102 : TrayNotificationView(tray, 0), |
| 103 battery_image_index_(-1) { |
| 97 power_status_view_ = | 104 power_status_view_ = |
| 98 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); | 105 new PowerStatusView(PowerStatusView::VIEW_NOTIFICATION, true); |
| 99 InitView(power_status_view_); | 106 InitView(power_status_view_); |
| 100 } | 107 } |
| 101 | 108 |
| 102 void UpdatePowerStatus(const PowerSupplyStatus& status) { | 109 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| 103 SetIconImage(TrayPower::GetBatteryImage(status, ICON_DARK)); | 110 int index = TrayPower::GetBatteryImageIndex(status); |
| 111 if (battery_image_index_ != index) { |
| 112 battery_image_index_ = index; |
| 113 SetIconImage(TrayPower::GetBatteryImage(battery_image_index_, ICON_DARK)); |
| 114 } |
| 104 power_status_view_->UpdatePowerStatus(status); | 115 power_status_view_->UpdatePowerStatus(status); |
| 105 } | 116 } |
| 106 | 117 |
| 107 private: | 118 private: |
| 108 PowerStatusView* power_status_view_; | 119 PowerStatusView* power_status_view_; |
| 120 int battery_image_index_; |
| 109 | 121 |
| 110 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); | 122 DISALLOW_COPY_AND_ASSIGN(PowerNotificationView); |
| 111 }; | 123 }; |
| 112 | 124 |
| 113 } // namespace tray | 125 } // namespace tray |
| 114 | 126 |
| 115 using tray::PowerNotificationView; | 127 using tray::PowerNotificationView; |
| 116 | 128 |
| 117 TrayPower::TrayPower() | 129 TrayPower::TrayPower() |
| 118 : power_tray_(NULL), | 130 : power_tray_(NULL), |
| 119 notification_view_(NULL), | 131 notification_view_(NULL), |
| 120 notification_state_(NOTIFICATION_NONE) { | 132 notification_state_(NOTIFICATION_NONE) { |
| 121 } | 133 } |
| 122 | 134 |
| 123 TrayPower::~TrayPower() { | 135 TrayPower::~TrayPower() { |
| 124 } | 136 } |
| 125 | 137 |
| 126 // static | 138 // static |
| 127 gfx::ImageSkia TrayPower::GetBatteryImage( | 139 int TrayPower::GetBatteryImageIndex(const PowerSupplyStatus& supply_status) { |
| 128 const PowerSupplyStatus& supply_status, | |
| 129 IconSet icon_set) { | |
| 130 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 131 icon_set == ICON_DARK ? | |
| 132 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); | |
| 133 | |
| 134 int image_index = 0; | 140 int image_index = 0; |
| 135 if (supply_status.battery_percentage >= 100) { | 141 if (supply_status.battery_percentage >= 100) { |
| 136 image_index = kNumPowerImages - 1; | 142 image_index = kNumPowerImages - 1; |
| 137 } else if (!supply_status.battery_is_present) { | 143 } else if (!supply_status.battery_is_present) { |
| 138 image_index = kNumPowerImages; | 144 image_index = kNumPowerImages; |
| 139 } else { | 145 } else { |
| 140 double percentage = supply_status.is_calculating_battery_time ? 100.0 : | 146 double percentage = supply_status.is_calculating_battery_time ? 100.0 : |
| 141 supply_status.battery_percentage; | 147 supply_status.battery_percentage; |
| 142 image_index = static_cast<int>(percentage / 100.0 * (kNumPowerImages - 1)); | 148 image_index = static_cast<int>(percentage / 100.0 * (kNumPowerImages - 1)); |
| 143 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); | 149 image_index = std::max(std::min(image_index, kNumPowerImages - 2), 0); |
| 144 } | 150 } |
| 151 return (image_index << 1) | (supply_status.line_power_on ? 1 : 0); |
| 152 } |
| 153 |
| 154 // static |
| 155 gfx::ImageSkia TrayPower::GetBatteryImage(int image_index, IconSet icon_set) { |
| 156 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 157 icon_set == ICON_DARK ? |
| 158 IDR_AURA_UBER_TRAY_POWER_SMALL_DARK : IDR_AURA_UBER_TRAY_POWER_SMALL); |
| 145 | 159 |
| 146 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See | 160 // TODO(mbolohan): Remove the 2px offset when the assets are centered. See |
| 147 // crbug.com/119832. | 161 // crbug.com/119832. |
| 148 gfx::Rect region( | 162 gfx::Rect region( |
| 149 (supply_status.line_power_on ? kBatteryImageWidth : 0) + 2, | 163 ((image_index & 0x1) ? kBatteryImageWidth : 0) + 2, |
| 150 image_index * kBatteryImageHeight, | 164 (image_index >> 1) * kBatteryImageHeight, |
| 151 kBatteryImageWidth - 2, kBatteryImageHeight); | 165 kBatteryImageWidth - 2, kBatteryImageHeight); |
| 152 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); | 166 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); |
| 153 } | 167 } |
| 154 | 168 |
| 155 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { | 169 views::View* TrayPower::CreateTrayView(user::LoginStatus status) { |
| 156 // There may not be enough information when this is created about whether | 170 // There may not be enough information when this is created about whether |
| 157 // there is a battery or not. So always create this, and adjust visibility as | 171 // there is a battery or not. So always create this, and adjust visibility as |
| 158 // necessary. | 172 // necessary. |
| 159 PowerSupplyStatus power_status = | 173 PowerSupplyStatus power_status = |
| 160 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); | 174 ash::Shell::GetInstance()->tray_delegate()->GetPowerSupplyStatus(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 return false; | 262 return false; |
| 249 case NOTIFICATION_CRITICAL: | 263 case NOTIFICATION_CRITICAL: |
| 250 return false; | 264 return false; |
| 251 } | 265 } |
| 252 NOTREACHED(); | 266 NOTREACHED(); |
| 253 return false; | 267 return false; |
| 254 } | 268 } |
| 255 | 269 |
| 256 } // namespace internal | 270 } // namespace internal |
| 257 } // namespace ash | 271 } // namespace ash |
| OLD | NEW |