Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: chrome/browser/chromeos/power_menu_button.cc

Issue 251099: Refactor cros library code into central location and have the UI elements obs... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/power_menu_button.h"
6
7 #include "app/l10n_util.h"
8 #include "app/resource_bundle.h"
9 #include "base/time.h"
10 #include "grit/generated_resources.h"
11 #include "grit/theme_resources.h"
12
13 ////////////////////////////////////////////////////////////////////////////////
14 // PowerMenuButton
15
16 // static
17 const int PowerMenuButton::kNumPowerImages = 8;
18
19 PowerMenuButton::PowerMenuButton()
20 : MenuButton(NULL, std::wstring(), this, false),
21 ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) {
22 SetShowHighlighted(false);
23 UpdateIcon();
24 CrosPowerLibrary::Get()->AddObserver(this);
25 }
26
27 PowerMenuButton::~PowerMenuButton() {
28 CrosPowerLibrary::Get()->RemoveObserver(this);
29 }
30
31 ////////////////////////////////////////////////////////////////////////////////
32 // PowerMenuButton, views::Menu2Model implementation:
33
34 int PowerMenuButton::GetItemCount() const {
35 return 2;
36 }
37
38 views::Menu2Model::ItemType PowerMenuButton::GetTypeAt(int index) const {
39 return views::Menu2Model::TYPE_COMMAND;
40 }
41
42 string16 PowerMenuButton::GetLabelAt(int index) const {
43 CrosPowerLibrary* cros = CrosPowerLibrary::Get();
44 // The first item shows the percentage of battery left.
45 if (index == 0) {
46 // If fully charged, always show 100% even if internal number is a bit less.
47 double percent = cros->battery_fully_charged() ? 100 :
48 cros->battery_percentage();
49 return l10n_util::GetStringFUTF16(IDS_STATUSBAR_BATTERY_PERCENTAGE,
50 IntToString16(static_cast<int>(percent)));
51 }
52
53 // The second item shows the battery is charged if it is.
54 if (cros->battery_fully_charged())
55 return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);
56
57 // If battery is in an intermediate charge state, we show how much time left.
58 base::TimeDelta time = cros->line_power_on() ? cros->battery_time_to_full() :
59 cros->battery_time_to_empty();
60 if (time.InSeconds() == 0) {
61 // If time is 0, then that means we are still calculating how much time.
62 // Depending if line power is on, we either show a message saying that we
63 // are calculating time until full or calculating remaining time.
64 int msg = cros->line_power_on() ?
65 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
66 IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
67 return l10n_util::GetStringUTF16(msg);
68 } else {
69 // Depending if line power is on, we either show a message saying XX:YY
70 // until full or XX:YY remaining where XX is number of hours and YY is
71 // number of minutes.
72 int msg = cros->line_power_on() ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
73 IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
74 int hour = time.InHours();
75 int min = (time - base::TimeDelta::FromHours(hour)).InMinutes();
76 string16 hour_str = IntToString16(hour);
77 string16 min_str = IntToString16(min);
78 // Append a "0" before the minute if it's only a single digit.
79 if (min < 10)
80 min_str = ASCIIToUTF16("0") + min_str;
81 return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
82 }
83 }
84
85 ////////////////////////////////////////////////////////////////////////////////
86 // PowerMenuButton, views::ViewMenuDelegate implementation:
87
88 void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt,
89 gfx::NativeView hwnd) {
90 power_menu_.Rebuild();
91 power_menu_.UpdateStates();
92 power_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
93 }
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // PowerMenuButton, CrosPowerLibrary::Observer implementation:
97
98 void PowerMenuButton::PowerChanged(CrosPowerLibrary* obj) {
99 UpdateIcon();
100 }
101
102 void PowerMenuButton::UpdateIcon() {
103 CrosPowerLibrary* cros = CrosPowerLibrary::Get();
104 int id = IDR_STATUSBAR_BATTERY_UNKNOWN;
105 if (CrosPowerLibrary::loaded()) {
106 if (cros->battery_fully_charged()) {
107 id = IDR_STATUSBAR_BATTERY_CHARGED;
108 } else {
109 // If fully charged, always show 100% even if percentage is a bit less.
110 double percent = cros->battery_fully_charged() ? 100 :
111 cros->battery_percentage();
112 // Gets the power image depending on battery percentage. Percentage is
113 // from 0 to 100, so we need to convert that to 0 to kNumPowerImages - 1.
114 int index = floor(percent / (100.0 / kNumPowerImages));
115 // This can happen if the battery is 100% full.
116 if (index == kNumPowerImages)
117 index--;
118 if (cros->line_power_on())
119 id = IDR_STATUSBAR_BATTERY_CHARGING_1 + index;
120 else
121 id = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index;
122 }
123 }
124 SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id));
125 SchedulePaint();
126 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/power_menu_button.h ('k') | chrome/browser/chromeos/settings_contents_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698