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: |
oshima
2011/04/15 22:26:51
please use new format
// ClockMenuButton, ...... :
rhashimoto
2011/04/15 23:44:32
Done.
| |
119 //////////////////////////////////////////////////////////////////////////////// | 133 std::wstring ClockMenuButton::GetLabel(int id) const { |
120 // ClockMenuButton, ui::MenuModel implementation: | 134 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); |
121 | 135 const string16 label = base::TimeFormatFriendlyDate(base::Time::Now()); |
122 int ClockMenuButton::GetItemCount() const { | 136 return UTF16ToWide(label); |
123 // If options dialog is unavailable, don't count a separator and configure | |
124 // menu item. | |
125 return host_->ShouldOpenButtonOptions(this) ? 3 : 1; | |
126 } | 137 } |
127 | 138 |
128 ui::MenuModel::ItemType ClockMenuButton::GetTypeAt(int index) const { | 139 bool ClockMenuButton::IsCommandEnabled(int id) const { |
129 // There's a separator between the current date and the menu item to open | 140 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
130 // the options menu. | 141 return id == CLOCK_OPEN_OPTIONS_ITEM; |
131 return index == 1 ? ui::MenuModel::TYPE_SEPARATOR: | |
132 ui::MenuModel::TYPE_COMMAND; | |
133 } | 142 } |
134 | 143 |
135 string16 ClockMenuButton::GetLabelAt(int index) const { | 144 void ClockMenuButton::ExecuteCommand(int id) { |
136 if (index == 0) | 145 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); |
137 return base::TimeFormatFriendlyDate(base::Time::Now()); | |
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); | 146 host_->OpenButtonOptions(this); |
148 } | 147 } |
149 | 148 |
150 /////////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////////// |
151 // ClockMenuButton, PowerLibrary::Observer implementation: | 150 // ClockMenuButton, PowerLibrary::Observer implementation: |
152 | 151 |
153 void ClockMenuButton::SystemResumed() { | 152 void ClockMenuButton::SystemResumed() { |
154 UpdateText(); | 153 UpdateText(); |
155 } | 154 } |
156 | 155 |
157 /////////////////////////////////////////////////////////////////////////////// | 156 /////////////////////////////////////////////////////////////////////////////// |
158 // ClockMenuButton, SystemLibrary::Observer implementation: | 157 // ClockMenuButton, SystemLibrary::Observer implementation: |
159 | 158 |
160 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { | 159 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { |
161 UpdateText(); | 160 UpdateText(); |
162 } | 161 } |
163 | 162 |
164 //////////////////////////////////////////////////////////////////////////////// | 163 //////////////////////////////////////////////////////////////////////////////// |
165 // ClockMenuButton, views::ViewMenuDelegate implementation: | 164 // ClockMenuButton, views::ViewMenuDelegate implementation: |
166 | 165 |
167 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 166 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
168 if (!clock_menu_.get()) | 167 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
169 clock_menu_.reset(new views::Menu2(this)); | 168 DCHECK_EQ(source, this); |
169 | |
170 EnsureMenu(); | |
171 | |
172 // TODO(rhashimoto): Remove this workaround when WebUI provides a | |
173 // top-level widget on the ChromeOS login screen that is a window. | |
174 // The current BackgroundView class for the ChromeOS login screen | |
175 // creates a owning Widget that has a native GtkWindow but is not a | |
176 // Window. This makes it impossible to get the NativeWindow via | |
177 // the views API. This workaround casts the top-level NativeWidget | |
178 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). | |
179 gfx::NativeWindow window; | |
180 if (source->GetWindow()) | |
181 window = source->GetWindow()->GetNativeWindow(); | |
170 else | 182 else |
171 clock_menu_->Rebuild(); | 183 window = GTK_WINDOW(source->GetWidget()->GetNativeView()); |
oshima
2011/04/15 22:26:51
i now understand. all you need is this.
window =
rhashimoto
2011/04/15 23:44:32
I'd like to keep the other line present:
window
oshima
2011/04/15 23:52:19
It will not survive when you move to MenuButton. W
rhashimoto
2011/04/21 19:31:17
Done.
| |
172 clock_menu_->UpdateStates(); | 184 |
173 clock_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 185 gfx::Point screen_loc; |
186 views::View::ConvertPointToScreen(source, &screen_loc); | |
187 gfx::Rect bounds(screen_loc, source->size()); | |
188 menu_->RunMenuAt( | |
189 window, | |
190 this, | |
191 bounds, | |
192 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
193 true); | |
174 } | 194 } |
175 | 195 |
176 //////////////////////////////////////////////////////////////////////////////// | 196 //////////////////////////////////////////////////////////////////////////////// |
177 // ClockMenuButton, views::View implementation: | 197 // ClockMenuButton, views::View implementation: |
178 | 198 |
179 void ClockMenuButton::OnLocaleChanged() { | 199 void ClockMenuButton::OnLocaleChanged() { |
180 UpdateText(); | 200 UpdateText(); |
181 } | 201 } |
182 | 202 |
203 void ClockMenuButton::EnsureMenu() { | |
204 if (!menu_.get()) { | |
205 menu_.reset(new MenuItemView(this)); | |
206 | |
207 // Text for this item will be set by GetLabel(). | |
208 menu_->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); | |
209 | |
210 // If options dialog is unavailable, don't count a separator and configure | |
211 // menu item. | |
212 if (host_->ShouldOpenButtonOptions(this)) { | |
213 menu_->AppendSeparator(); | |
214 | |
215 const string16 clock_open_options_label = | |
216 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | |
217 menu_->AppendMenuItemWithLabel( | |
218 CLOCK_OPEN_OPTIONS_ITEM, | |
219 UTF16ToWide(clock_open_options_label)); | |
220 } | |
221 } | |
222 } | |
223 | |
183 } // namespace chromeos | 224 } // namespace chromeos |
OLD | NEW |