Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 12 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
| 16 #include "views/widget/widget.h" | |
| 17 #include "views/window/window.h" | |
| 18 | |
| 19 using views::MenuItemView; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Menu item ids. | |
| 24 enum { | |
| 25 POWER_BATTERY_PERCENTAGE_ITEM, | |
| 26 POWER_BATTERY_IS_CHARGED_ITEM, | |
| 27 }; | |
| 28 | |
| 29 } // namespace | |
| 16 | 30 |
| 17 namespace chromeos { | 31 namespace chromeos { |
| 18 | 32 |
| 33 // static | |
| 34 const int PowerMenuButton::kNumPowerImages = 16; | |
|
oshima
2011/04/15 17:40:12
looks like this can move to anonymous namespace (n
rhashimoto
2011/04/15 21:20:27
Done.
| |
| 35 | |
| 19 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
| 20 // PowerMenuButton | 37 // PowerMenuButton |
| 21 | 38 |
| 22 // static | |
| 23 const int PowerMenuButton::kNumPowerImages = 16; | |
| 24 | |
| 25 PowerMenuButton::PowerMenuButton() | 39 PowerMenuButton::PowerMenuButton() |
| 26 : StatusAreaButton(this), | 40 : StatusAreaButton(this), |
| 27 battery_is_present_(false), | 41 battery_is_present_(false), |
| 28 line_power_on_(false), | 42 line_power_on_(false), |
| 29 battery_fully_charged_(false), | 43 battery_fully_charged_(false), |
| 30 battery_percentage_(0.0), | 44 battery_percentage_(0.0), |
| 31 icon_id_(-1), | 45 icon_id_(-1) { |
| 32 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) { | |
| 33 UpdateIconAndLabelInfo(); | 46 UpdateIconAndLabelInfo(); |
| 34 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 47 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
| 35 } | 48 } |
| 36 | 49 |
| 37 PowerMenuButton::~PowerMenuButton() { | 50 PowerMenuButton::~PowerMenuButton() { |
| 38 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); | 51 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); |
| 39 } | 52 } |
| 40 | 53 |
| 41 //////////////////////////////////////////////////////////////////////////////// | 54 //////////////////////////////////////////////////////////////////////////////// |
| 42 // PowerMenuButton, ui::MenuModel implementation: | 55 // PowerMenuButton, views::MenuDelegate implementation: |
| 56 std::wstring PowerMenuButton::GetLabel(int id) const { | |
| 57 string16 label; | |
| 58 switch (id) { | |
| 59 case POWER_BATTERY_PERCENTAGE_ITEM: | |
| 60 label = GetBatteryPercentageText(); | |
| 61 break; | |
| 62 case POWER_BATTERY_IS_CHARGED_ITEM: | |
| 63 label = GetBatteryIsChargedText(); | |
| 64 break; | |
| 65 default: | |
| 66 NOTREACHED(); | |
| 67 } | |
| 43 | 68 |
| 44 int PowerMenuButton::GetItemCount() const { | 69 return UTF16ToWide(label); |
| 45 return 2; | |
| 46 } | 70 } |
| 47 | 71 |
| 48 ui::MenuModel::ItemType PowerMenuButton::GetTypeAt(int index) const { | 72 bool PowerMenuButton::IsCommandEnabled(int id) const { |
| 49 return ui::MenuModel::TYPE_COMMAND; | 73 return false; |
| 50 } | |
| 51 | |
| 52 string16 PowerMenuButton::GetLabelAt(int index) const { | |
| 53 // The first item shows the percentage of battery left. | |
| 54 if (index == 0) { | |
| 55 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE, | |
| 56 base::IntToString16(static_cast<int>(battery_percentage_))); | |
| 57 } else if (index == 1) { | |
| 58 // The second item shows the battery is charged if it is. | |
| 59 if (battery_fully_charged_) | |
| 60 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED); | |
| 61 | |
| 62 // If battery is in an intermediate charge state, show how much time left. | |
| 63 base::TimeDelta time = line_power_on_ ? battery_time_to_full_ : | |
| 64 battery_time_to_empty_; | |
| 65 if (time.InSeconds() == 0) { | |
| 66 // If time is 0, then that means we are still calculating how much time. | |
| 67 // Depending if line power is on, we either show a message saying that we | |
| 68 // are calculating time until full or calculating remaining time. | |
| 69 int msg = line_power_on_ ? | |
| 70 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL : | |
| 71 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY; | |
| 72 return l10n_util::GetStringUTF16(msg); | |
| 73 } else { | |
| 74 // Depending if line power is on, we either show a message saying XX:YY | |
| 75 // until full or XX:YY remaining where XX is number of hours and YY is | |
| 76 // number of minutes. | |
| 77 int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL : | |
| 78 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY; | |
| 79 int hour = time.InHours(); | |
| 80 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); | |
| 81 string16 hour_str = base::IntToString16(hour); | |
| 82 string16 min_str = base::IntToString16(min); | |
| 83 // Append a "0" before the minute if it's only a single digit. | |
| 84 if (min < 10) | |
| 85 min_str = ASCIIToUTF16("0") + min_str; | |
| 86 return l10n_util::GetStringFUTF16(msg, hour_str, min_str); | |
| 87 } | |
| 88 } else { | |
| 89 NOTREACHED(); | |
| 90 return string16(); | |
| 91 } | |
| 92 } | 74 } |
| 93 | 75 |
| 94 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
| 95 // PowerMenuButton, views::View implementation: | 77 // PowerMenuButton, views::View implementation: |
| 96 void PowerMenuButton::OnLocaleChanged() { | 78 void PowerMenuButton::OnLocaleChanged() { |
| 97 UpdateIconAndLabelInfo(); | 79 UpdateIconAndLabelInfo(); |
| 98 } | 80 } |
| 99 | 81 |
| 100 //////////////////////////////////////////////////////////////////////////////// | 82 //////////////////////////////////////////////////////////////////////////////// |
| 101 // PowerMenuButton, views::ViewMenuDelegate implementation: | 83 // PowerMenuButton, views::ViewMenuDelegate implementation: |
| 102 | 84 |
| 103 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 85 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| 104 power_menu_.Rebuild(); | 86 // View passed in must be a views::MenuButton, i.e. the PowerMenuButton. |
| 105 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 87 DCHECK(source == this); |
|
oshima
2011/04/15 17:40:12
DCHECK_EQ
rhashimoto
2011/04/15 21:20:27
Done.
| |
| 88 | |
| 89 if (!menu_.get()) { | |
| 90 menu_.reset(new MenuItemView(this)); | |
| 91 | |
| 92 // Create menu items whose text will be supplied by GetLabel(). | |
| 93 menu_->AppendDelegateMenuItem(POWER_BATTERY_PERCENTAGE_ITEM); | |
| 94 menu_->AppendDelegateMenuItem(POWER_BATTERY_IS_CHARGED_ITEM); | |
| 95 } | |
| 96 | |
| 97 // TODO(rhashimoto): Remove this workaround when WebUI provides a | |
| 98 // top-level widget on the ChromeOS login screen that is a window. | |
| 99 // The current BackgroundView class for the ChromeOS login screen | |
| 100 // creates a owning Widget that has a native GtkWindow but is not a | |
| 101 // Window. This makes it impossible to get the NativeWindow via | |
| 102 // the views API. This workaround casts the top-level NativeWidget | |
| 103 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). | |
| 104 gfx::NativeWindow window; | |
| 105 if (source->GetWindow()) { | |
| 106 // This is the normal case with a browser. | |
| 107 window = source->GetWindow()->GetNativeWindow(); | |
| 108 } else { | |
| 109 #if defined(OS_WIN) | |
| 110 NOTREACHED(); | |
| 111 #elif defined(USE_X11) | |
| 112 window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
| 113 #endif | |
| 114 } | |
| 115 | |
| 116 gfx::Point screen_loc; | |
| 117 views::View::ConvertPointToScreen(source, &screen_loc); | |
| 118 gfx::Rect bounds(screen_loc, source->size()); | |
| 119 menu_->RunMenuAt( | |
| 120 window, | |
| 121 this, | |
| 122 bounds, | |
| 123 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
| 124 true); | |
| 106 } | 125 } |
| 107 | 126 |
| 108 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
| 109 // PowerMenuButton, PowerLibrary::Observer implementation: | 128 // PowerMenuButton, PowerLibrary::Observer implementation: |
| 110 | 129 |
| 111 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { | 130 void PowerMenuButton::PowerChanged(PowerLibrary* obj) { |
| 112 UpdateIconAndLabelInfo(); | 131 UpdateIconAndLabelInfo(); |
| 113 } | 132 } |
| 114 | 133 |
| 115 //////////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////////// |
| 116 // PowerMenuButton, StatusAreaButton implementation: | 135 // PowerMenuButton implementation: |
| 136 | |
| 137 string16 PowerMenuButton::GetBatteryPercentageText() const | |
| 138 { | |
| 139 return l10n_util::GetStringFUTF16( | |
| 140 IDS_STATUSBAR_BATTERY_PERCENTAGE, | |
| 141 base::IntToString16(static_cast<int>(battery_percentage_))); | |
| 142 } | |
| 143 | |
| 144 string16 PowerMenuButton::GetBatteryIsChargedText() const | |
| 145 { | |
| 146 if (battery_fully_charged_) | |
| 147 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED); | |
| 148 | |
| 149 // If battery is in an intermediate charge state, show how | |
| 150 // much time left. | |
| 151 base::TimeDelta time = line_power_on_ ? battery_time_to_full_ : | |
| 152 battery_time_to_empty_; | |
| 153 if (time.InSeconds() == 0) { | |
| 154 // If time is 0, then that means we are still calculating how much time. | |
| 155 // Depending if line power is on, we either show a message saying that we | |
| 156 // are calculating time until full or calculating remaining time. | |
| 157 int msg = line_power_on_ ? | |
| 158 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL : | |
| 159 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY; | |
| 160 return l10n_util::GetStringUTF16(msg); | |
| 161 } else { | |
| 162 // Depending if line power is on, we either show a message saying XX:YY | |
| 163 // until full or XX:YY remaining where XX is number of hours and YY is | |
| 164 // number of minutes. | |
| 165 int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL : | |
| 166 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY; | |
| 167 int hour = time.InHours(); | |
| 168 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes(); | |
| 169 string16 hour_str = base::IntToString16(hour); | |
| 170 string16 min_str = base::IntToString16(min); | |
| 171 // Append a "0" before the minute if it's only a single digit. | |
| 172 if (min < 10) | |
| 173 min_str = ASCIIToUTF16("0") + min_str; | |
| 174 return l10n_util::GetStringFUTF16(msg, hour_str, min_str); | |
| 175 } | |
| 176 } | |
| 117 | 177 |
| 118 void PowerMenuButton::UpdateIconAndLabelInfo() { | 178 void PowerMenuButton::UpdateIconAndLabelInfo() { |
| 119 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); | 179 PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); |
| 120 if (!cros) | 180 if (!cros) |
| 121 return; | 181 return; |
| 122 | 182 |
| 123 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded(); | 183 bool cros_loaded = CrosLibrary::Get()->EnsureLoaded(); |
| 124 if (cros_loaded) { | 184 if (cros_loaded) { |
| 125 battery_is_present_ = cros->battery_is_present(); | 185 battery_is_present_ = cros->battery_is_present(); |
| 126 line_power_on_ = cros->line_power_on(); | 186 line_power_on_ = cros->line_power_on(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 }; | 243 }; |
| 184 | 244 |
| 185 int index = static_cast<int>(battery_percentage_ / 100.0 * | 245 int index = static_cast<int>(battery_percentage_ / 100.0 * |
| 186 nextafter(static_cast<float>(kNumPowerImages), 0)); | 246 nextafter(static_cast<float>(kNumPowerImages), 0)); |
| 187 index = std::max(std::min(index, kNumPowerImages - 1), 0); | 247 index = std::max(std::min(index, kNumPowerImages - 1), 0); |
| 188 icon_id_ = line_power_on_ ? | 248 icon_id_ = line_power_on_ ? |
| 189 kChargingImages[index] : kDischargingImages[index]; | 249 kChargingImages[index] : kDischargingImages[index]; |
| 190 } | 250 } |
| 191 | 251 |
| 192 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); | 252 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); |
| 193 SetTooltipText(UTF16ToWide(GetLabelAt(0))); | 253 SetTooltipText(UTF16ToWide(GetBatteryPercentageText())); |
| 194 power_menu_.Rebuild(); | |
| 195 SchedulePaint(); | 254 SchedulePaint(); |
| 196 } | 255 } |
| 197 | 256 |
| 198 } // namespace chromeos | 257 } // namespace chromeos |
| OLD | NEW |