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 UpdateIcon(); | 26 UpdateIcon(); |
27 PowerLibrary::Get()->AddObserver(this); | 27 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
28 } | 28 } |
29 | 29 |
30 PowerMenuButton::~PowerMenuButton() { | 30 PowerMenuButton::~PowerMenuButton() { |
31 PowerLibrary::Get()->RemoveObserver(this); | 31 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); |
32 } | 32 } |
33 | 33 |
34 //////////////////////////////////////////////////////////////////////////////// | 34 //////////////////////////////////////////////////////////////////////////////// |
35 // PowerMenuButton, menus::MenuModel implementation: | 35 // PowerMenuButton, menus::MenuModel implementation: |
36 | 36 |
37 int PowerMenuButton::GetItemCount() const { | 37 int PowerMenuButton::GetItemCount() const { |
38 return 2; | 38 return 2; |
39 } | 39 } |
40 | 40 |
41 menus::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { | 41 menus::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { |
42 return menus::MenuModel::TYPE_COMMAND; | 42 return menus::MenuModel::TYPE_COMMAND; |
43 } | 43 } |
44 | 44 |
45 string16 PowerMenuButton::GetLabelAt(int index) const { | 45 string16 PowerMenuButton::GetLabelAt(int index) const { |
46 PowerLibrary* cros = PowerLibrary::Get(); | 46 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
47 // The first item shows the percentage of battery left. | 47 // The first item shows the percentage of battery left. |
48 if (index == 0) { | 48 if (index == 0) { |
49 // If fully charged, always show 100% even if internal number is a bit less. | 49 // If fully charged, always show 100% even if internal number is a bit less. |
50 double percent = cros->battery_fully_charged() ? 100 : | 50 double percent = cros->battery_fully_charged() ? 100 : |
51 cros->battery_percentage(); | 51 cros->battery_percentage(); |
52 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, | 52 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, |
53 IntToString16(static_cast<int>(percent))); | 53 IntToString16(static_cast<int>(percent))); |
54 } | 54 } |
55 | 55 |
56 // The second item shows the battery is charged if it is. | 56 // The second item shows the battery is charged if it is. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 115 |
116 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { | 116 void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { |
117 // Draw the battery icon 6 pixels down to center it. | 117 // Draw the battery icon 6 pixels down to center it. |
118 // Because the status icon is 24x24 but the images are 24x16. | 118 // 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. | 119 // But since the images are shifted up by 4 pixels, we draw at 6 pixels down. |
120 static const int kIconVerticalPadding = 6; | 120 static const int kIconVerticalPadding = 6; |
121 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); | 121 canvas->DrawBitmapInt(icon, 0, kIconVerticalPadding); |
122 } | 122 } |
123 | 123 |
124 void PowerMenuButton::UpdateIcon() { | 124 void PowerMenuButton::UpdateIcon() { |
125 PowerLibrary* cros = PowerLibrary::Get(); | 125 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
126 int id = IDR_STATUSBAR_BATTERY_UNKNOWN; | 126 int id = IDR_STATUSBAR_BATTERY_UNKNOWN; |
127 if (CrosLibrary::EnsureLoaded()) { | 127 if (CrosLibrary::Get()->EnsureLoaded()) { |
128 if (!cros->battery_is_present()) { | 128 if (!cros->battery_is_present()) { |
129 id = IDR_STATUSBAR_BATTERY_MISSING; | 129 id = IDR_STATUSBAR_BATTERY_MISSING; |
130 } else if (cros->line_power_on() && cros->battery_fully_charged()) { | 130 } else if (cros->line_power_on() && cros->battery_fully_charged()) { |
131 id = IDR_STATUSBAR_BATTERY_CHARGED; | 131 id = IDR_STATUSBAR_BATTERY_CHARGED; |
132 } else { | 132 } else { |
133 // If fully charged, always show 100% even if percentage is a bit less. | 133 // If fully charged, always show 100% even if percentage is a bit less. |
134 double percent = cros->battery_fully_charged() ? 100 : | 134 double percent = cros->battery_fully_charged() ? 100 : |
135 cros->battery_percentage(); | 135 cros->battery_percentage(); |
136 // Gets the power image depending on battery percentage. Percentage is | 136 // 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. | 137 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1. |
138 int index = static_cast<int>(percent / 100.0 * | 138 int index = static_cast<int>(percent / 100.0 * |
139 nextafter(static_cast<float>(kNumPowerImages), 0)); | 139 nextafter(static_cast<float>(kNumPowerImages), 0)); |
140 // Make sure that index is between 0 and kNumWifiImages - 1 | 140 // Make sure that index is between 0 and kNumWifiImages - 1 |
141 if (index < 0) | 141 if (index < 0) |
142 index = 0; | 142 index = 0; |
143 if (index >= kNumPowerImages) | 143 if (index >= kNumPowerImages) |
144 index = kNumPowerImages - 1; | 144 index = kNumPowerImages - 1; |
145 if (cros->line_power_on()) | 145 if (cros->line_power_on()) |
146 id = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; | 146 id = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; |
147 else | 147 else |
148 id = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; | 148 id = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; |
149 } | 149 } |
150 } | 150 } |
151 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); | 151 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); |
152 SchedulePaint(); | 152 SchedulePaint(); |
153 } | 153 } |
154 | 154 |
155 } // namespace chromeos | 155 } // namespace chromeos |
OLD | NEW |