| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/chromeos/status/power_menu_button.h" | 5 #include "chrome/browser/chromeos/status/power_menu_button.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "gfx/canvas.h" | 11 #include "gfx/canvas.h" |
| 12 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
| 14 | 14 |
| 15 namespace chromeos { | 15 namespace chromeos { |
| 16 | 16 |
| 17 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
| 18 // PowerMenuButton | 18 // PowerMenuButton |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 const int PowerMenuButton::kNumPowerImages = 12; | 21 const int PowerMenuButton::kNumPowerImages = 12; |
| 22 | 22 |
| 23 PowerMenuButton::PowerMenuButton() | 23 PowerMenuButton::PowerMenuButton() |
| 24 : StatusAreaButton(this), | 24 : StatusAreaButton(this), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) { | 25 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)), |
| 26 icon_id_(-1) { |
| 26 UpdateIcon(); | 27 UpdateIcon(); |
| 27 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 28 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
| 28 } | 29 } |
| 29 | 30 |
| 30 PowerMenuButton::~PowerMenuButton() { | 31 PowerMenuButton::~PowerMenuButton() { |
| 31 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); | 32 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); |
| 32 } | 33 } |
| 33 | 34 |
| 34 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
| 35 // PowerMenuButton, menus::MenuModel implementation: | 36 // PowerMenuButton, menus::MenuModel implementation: |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { | 117 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { |
| 117 // Draw the battery icon 6 pixels down to center it. | 118 // Draw the battery icon 6 pixels down to center it. |
| 118 // Because the status icon is 24x24 but the images are 24x16. | 119 // Because the status icon is 24x24 but the images are 24x16. |
| 119 // But since the images are shifted up by 4 pixels, we draw at 6 pixels down. | 120 // But since the images are shifted up by 4 pixels, we draw at 6 pixels down. |
| 120 static const int kIconVerticalPadding = 6; | 121 static const int kIconVerticalPadding = 6; |
| 121 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); | 122 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void PowerMenuButton::UpdateIcon() { | 125 void PowerMenuButton::UpdateIcon() { |
| 125 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); | 126 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
| 126 int id = IDR_STATUSBAR_BATTERY_UNKNOWN; | 127 icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN; |
| 127 if (CrosLibrary::Get()->EnsureLoaded()) { | 128 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 128 if (!cros->battery_is_present()) { | 129 if (!cros->battery_is_present()) { |
| 129 id = IDR_STATUSBAR_BATTERY_MISSING; | 130 icon_id_ = IDR_STATUSBAR_BATTERY_MISSING; |
| 130 } else if (cros->line_power_on() && cros->battery_fully_charged()) { | 131 } else if (cros->line_power_on() && cros->battery_fully_charged()) { |
| 131 id = IDR_STATUSBAR_BATTERY_CHARGED; | 132 icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED; |
| 132 } else { | 133 } else { |
| 133 // If fully charged, always show 100% even if percentage is a bit less. | 134 // If fully charged, always show 100% even if percentage is a bit less. |
| 134 double percent = cros->battery_fully_charged() ? 100 : | 135 double percent = cros->battery_fully_charged() ? 100 : |
| 135 cros->battery_percentage(); | 136 cros->battery_percentage(); |
| 136 // Gets the power image depending on battery percentage. Percentage is | 137 // Gets the power image depending on battery percentage. Percentage is |
| 137 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1. | 138 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1. |
| 138 int index = static_cast<int>(percent / 100.0 * | 139 int index = static_cast<int>(percent / 100.0 * |
| 139 nextafter(static_cast<float>(kNumPowerImages), 0)); | 140 nextafter(static_cast<float>(kNumPowerImages), 0)); |
| 140 // Make sure that index is between 0 and kNumWifiImages - 1 | 141 // Make sure that index is between 0 and kNumWifiImages - 1 |
| 141 if (index < 0) | 142 if (index < 0) |
| 142 index = 0; | 143 index = 0; |
| 143 if (index >= kNumPowerImages) | 144 if (index >= kNumPowerImages) |
| 144 index = kNumPowerImages - 1; | 145 index = kNumPowerImages - 1; |
| 145 if (cros->line_power_on()) | 146 if (cros->line_power_on()) |
| 146 id = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; | 147 icon_id_ = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; |
| 147 else | 148 else |
| 148 id = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; | 149 icon_id_ = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; |
| 149 } | 150 } |
| 150 } | 151 } |
| 151 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); | 152 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); |
| 152 SchedulePaint(); | 153 SchedulePaint(); |
| 153 } | 154 } |
| 154 | 155 |
| 155 } // namespace chromeos | 156 } // namespace chromeos |
| OLD | NEW |