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/clock_menu_button.h" | 5 #include "chrome/browser/chromeos/status/clock_menu_button.h" |
6 | 6 |
7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "chrome/browser/chromeos/status/status_area_host.h" | 12 #include "chrome/browser/chromeos/status/status_area_host.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "content/common/notification_details.h" | 16 #include "content/common/notification_details.h" |
17 #include "content/common/notification_source.h" | 17 #include "content/common/notification_source.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
23 #include "views/widget/widget.h" | |
24 #include "views/window/window.h" | |
23 | 25 |
24 namespace chromeos { | 26 using views::MenuItemView; |
27 | |
28 namespace { | |
29 | |
30 // MenuItemView item ids | |
31 enum { | |
32 CLOCK_DISPLAY_ITEM, | |
33 CLOCK_OPEN_OPTIONS_ITEM, | |
34 }; | |
25 | 35 |
26 // Amount of slop to add into the timer to make sure we're into the next minute | 36 // Amount of slop to add into the timer to make sure we're into the next minute |
27 // when the timer goes off. | 37 // when the timer goes off. |
28 const int kTimerSlopSeconds = 1; | 38 const int kTimerSlopSeconds = 1; |
29 | 39 |
30 #if defined(CROS_FONTS_USING_BCI) | 40 #if defined(CROS_FONTS_USING_BCI) |
31 const int kFontSizeDelta = 0; | 41 const int kFontSizeDelta = 0; |
32 #else | 42 #else |
33 const int kFontSizeDelta = 1; | 43 const int kFontSizeDelta = 1; |
34 #endif | 44 #endif |
35 | 45 |
46 } // namespace | |
47 | |
48 namespace chromeos { | |
49 | |
36 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) | 50 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) |
37 : StatusAreaButton(this), | 51 : StatusAreaButton(this), |
38 host_(host) { | 52 host_(host) { |
39 // Add as SystemLibrary observer. We update the clock if timezone changes. | 53 // Add as SystemLibrary observer. We update the clock if timezone changes. |
40 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this); | 54 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this); |
41 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 55 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
42 // Start monitoring the kUse24HourClock preference. | 56 // Start monitoring the kUse24HourClock preference. |
43 if (host->GetProfile()) { // This can be NULL in the login screen. | 57 if (host->GetProfile()) { // This can be NULL in the login screen. |
44 registrar_.Init(host->GetProfile()->GetPrefs()); | 58 registrar_.Init(host->GetProfile()->GetPrefs()); |
45 registrar_.Add(prefs::kUse24HourClock, this); | 59 registrar_.Add(prefs::kUse24HourClock, this); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 const NotificationSource& source, | 122 const NotificationSource& source, |
109 const NotificationDetails& details) { | 123 const NotificationDetails& details) { |
110 if (type == NotificationType::PREF_CHANGED) { | 124 if (type == NotificationType::PREF_CHANGED) { |
111 std::string* pref_name = Details<std::string>(details).ptr(); | 125 std::string* pref_name = Details<std::string>(details).ptr(); |
112 if (*pref_name == prefs::kUse24HourClock) { | 126 if (*pref_name == prefs::kUse24HourClock) { |
113 UpdateText(); | 127 UpdateText(); |
114 } | 128 } |
115 } | 129 } |
116 } | 130 } |
117 | 131 |
118 | 132 // ClockMenuButton, views::MenuDelegate implementation: |
119 //////////////////////////////////////////////////////////////////////////////// | 133 std::wstring ClockMenuButton::GetLabel(int id) const |
120 // ClockMenuButton, ui::MenuModel implementation: | 134 { |
oshima
2011/04/15 17:40:12
move { to pervious line. (and other places)
rhashimoto
2011/04/15 21:20:27
Done.
| |
121 | 135 DCHECK_EQ(CLOCK_DISPLAY_ITEM,id); |
oshima
2011/04/15 17:40:12
space after ,
rhashimoto
2011/04/15 21:20:27
Done.
| |
122 int ClockMenuButton::GetItemCount() const { | 136 const string16 label = base::TimeFormatFriendlyDate(base::Time::Now()); |
123 // If options dialog is unavailable, don't count a separator and configure | 137 return UTF16ToWide(label); |
124 // menu item. | |
125 return host_->ShouldOpenButtonOptions(this) ? 3 : 1; | |
126 } | 138 } |
127 | 139 |
128 ui::MenuModel::ItemType ClockMenuButton::GetTypeAt(int index) const { | 140 bool ClockMenuButton::IsCommandEnabled(int id) const |
129 // There's a separator between the current date and the menu item to open | 141 { |
130 // the options menu. | 142 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
131 return index == 1 ? ui::MenuModel::TYPE_SEPARATOR: | 143 return id == CLOCK_OPEN_OPTIONS_ITEM; |
132 ui::MenuModel::TYPE_COMMAND; | |
133 } | 144 } |
134 | 145 |
135 string16 ClockMenuButton::GetLabelAt(int index) const { | 146 void ClockMenuButton::ExecuteCommand(int id) |
136 if (index == 0) | 147 { |
137 return base::TimeFormatFriendlyDate(base::Time::Now()); | 148 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM,id); |
138 return l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | |
139 } | |
140 | |
141 bool ClockMenuButton::IsEnabledAt(int index) const { | |
142 // The 1st item is the current date, which is disabled. | |
143 return index != 0; | |
144 } | |
145 | |
146 void ClockMenuButton::ActivatedAt(int index) { | |
147 host_->OpenButtonOptions(this); | 149 host_->OpenButtonOptions(this); |
148 } | 150 } |
149 | 151 |
150 /////////////////////////////////////////////////////////////////////////////// | 152 /////////////////////////////////////////////////////////////////////////////// |
151 // ClockMenuButton, PowerLibrary::Observer implementation: | 153 // ClockMenuButton, PowerLibrary::Observer implementation: |
152 | 154 |
153 void ClockMenuButton::SystemResumed() { | 155 void ClockMenuButton::SystemResumed() { |
154 UpdateText(); | 156 UpdateText(); |
155 } | 157 } |
156 | 158 |
157 /////////////////////////////////////////////////////////////////////////////// | 159 /////////////////////////////////////////////////////////////////////////////// |
158 // ClockMenuButton, SystemLibrary::Observer implementation: | 160 // ClockMenuButton, SystemLibrary::Observer implementation: |
159 | 161 |
160 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { | 162 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { |
161 UpdateText(); | 163 UpdateText(); |
162 } | 164 } |
163 | 165 |
164 //////////////////////////////////////////////////////////////////////////////// | 166 //////////////////////////////////////////////////////////////////////////////// |
165 // ClockMenuButton, views::ViewMenuDelegate implementation: | 167 // ClockMenuButton, views::ViewMenuDelegate implementation: |
166 | 168 |
167 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 169 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
168 if (!clock_menu_.get()) | 170 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
169 clock_menu_.reset(new views::Menu2(this)); | 171 DCHECK_EQ(source,this); |
170 else | 172 |
171 clock_menu_->Rebuild(); | 173 EnsureMenu(); |
172 clock_menu_->UpdateStates(); | 174 |
173 clock_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 175 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
176 // top-level widget on the ChromeOS login screen that is a window. | |
177 // The current BackgroundView class for the ChromeOS login screen | |
178 // creates a owning Widget that has a native GtkWindow but is not a | |
179 // Window. This makes it impossible to get the NativeWindow via | |
180 // the views API. This workaround casts the top-level NativeWidget | |
181 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). | |
182 gfx::NativeWindow window; | |
183 if (source->GetWindow()) { | |
184 // This is the normal case with a browser. | |
185 window = source->GetWindow()->GetNativeWindow(); | |
186 } else { | |
187 #if defined(OS_WIN) | |
188 NOTREACHED(); | |
189 #elif defined(USE_X11) | |
190 window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
191 #endif | |
192 } | |
193 | |
194 gfx::Point screen_loc; | |
195 views::View::ConvertPointToScreen(source, &screen_loc); | |
196 gfx::Rect bounds(screen_loc, source->size()); | |
197 menu_->RunMenuAt( | |
198 window, | |
199 this, | |
200 bounds, | |
201 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
202 true); | |
174 } | 203 } |
oshima
2011/04/15 17:40:12
Looks like we can move this to StatusAreaButton (a
rhashimoto
2011/04/15 21:20:27
If we were going to consolidate it, I think I'd pu
oshima
2011/04/15 22:35:18
sgtm
| |
175 | 204 |
176 //////////////////////////////////////////////////////////////////////////////// | 205 //////////////////////////////////////////////////////////////////////////////// |
177 // ClockMenuButton, views::View implementation: | 206 // ClockMenuButton, views::View implementation: |
178 | 207 |
179 void ClockMenuButton::OnLocaleChanged() { | 208 void ClockMenuButton::OnLocaleChanged() { |
180 UpdateText(); | 209 UpdateText(); |
181 } | 210 } |
182 | 211 |
212 void ClockMenuButton::EnsureMenu() | |
213 { | |
214 if (!menu_.get()) { | |
215 menu_.reset(new MenuItemView(this)); | |
216 | |
217 // Text for this item will be set by GetLabel(). | |
218 menu_->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); | |
219 | |
220 // If options dialog is unavailable, don't count a separator and configure | |
221 // menu item. | |
222 if (host_->ShouldOpenButtonOptions(this)) { | |
223 menu_->AppendSeparator(); | |
224 | |
225 const string16 clock_open_options_label = | |
226 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | |
227 menu_->AppendMenuItemWithLabel( | |
228 CLOCK_OPEN_OPTIONS_ITEM, | |
229 UTF16ToWide(clock_open_options_label)); | |
230 } | |
231 } | |
232 } | |
233 | |
183 } // namespace chromeos | 234 } // namespace chromeos |
OLD | NEW |