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 |
| 50 // ClockMenuButton ------------------------------------------------------------ |
| 51 |
36 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) | 52 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) |
37 : StatusAreaButton(this), | 53 : StatusAreaButton(this), |
38 host_(host) { | 54 host_(host) { |
39 // Add as SystemAccess observer. We update the clock if timezone changes. | 55 // Add as SystemAccess observer. We update the clock if timezone changes. |
40 SystemAccess::GetInstance()->AddObserver(this); | 56 SystemAccess::GetInstance()->AddObserver(this); |
41 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 57 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
42 // Start monitoring the kUse24HourClock preference. | 58 // Start monitoring the kUse24HourClock preference. |
43 if (host->GetProfile()) { // This can be NULL in the login screen. | 59 if (host->GetProfile()) { // This can be NULL in the login screen. |
44 registrar_.Init(host->GetProfile()->GetPrefs()); | 60 registrar_.Init(host->GetProfile()->GetPrefs()); |
45 registrar_.Add(prefs::kUse24HourClock, this); | 61 registrar_.Add(prefs::kUse24HourClock, this); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 base::k24HourClock : base::k12HourClock); | 110 base::k24HourClock : base::k12HourClock); |
95 SetText(UTF16ToWide(base::TimeFormatTimeOfDayWithHourClockType( | 111 SetText(UTF16ToWide(base::TimeFormatTimeOfDayWithHourClockType( |
96 time, clock_type))); | 112 time, clock_type))); |
97 } else { | 113 } else { |
98 SetText(UTF16ToWide(base::TimeFormatTimeOfDay(time))); | 114 SetText(UTF16ToWide(base::TimeFormatTimeOfDay(time))); |
99 } | 115 } |
100 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time))); | 116 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time))); |
101 SchedulePaint(); | 117 SchedulePaint(); |
102 } | 118 } |
103 | 119 |
104 //////////////////////////////////////////////////////////////////////////////// | 120 // ClockMenuButton, NotificationObserver implementation: ---------------------- |
105 // ClockMenuButton, NotificationObserver implementation: | |
106 | 121 |
107 void ClockMenuButton::Observe(NotificationType type, | 122 void ClockMenuButton::Observe(NotificationType type, |
108 const NotificationSource& source, | 123 const NotificationSource& source, |
109 const NotificationDetails& details) { | 124 const NotificationDetails& details) { |
110 if (type == NotificationType::PREF_CHANGED) { | 125 if (type == NotificationType::PREF_CHANGED) { |
111 std::string* pref_name = Details<std::string>(details).ptr(); | 126 std::string* pref_name = Details<std::string>(details).ptr(); |
112 if (*pref_name == prefs::kUse24HourClock) { | 127 if (*pref_name == prefs::kUse24HourClock) { |
113 UpdateText(); | 128 UpdateText(); |
114 } | 129 } |
115 } | 130 } |
116 } | 131 } |
117 | 132 |
118 | 133 // ClockMenuButton, views::MenuDelegate implementation: |
119 //////////////////////////////////////////////////////////////////////////////// | 134 std::wstring ClockMenuButton::GetLabel(int id) const { |
120 // ClockMenuButton, ui::MenuModel implementation: | 135 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); |
121 | 136 const string16 label = base::TimeFormatFriendlyDate(base::Time::Now()); |
122 int ClockMenuButton::GetItemCount() const { | 137 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 } | 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 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
130 // the options menu. | 142 return id == CLOCK_OPEN_OPTIONS_ITEM; |
131 return index == 1 ? ui::MenuModel::TYPE_SEPARATOR: | |
132 ui::MenuModel::TYPE_COMMAND; | |
133 } | 143 } |
134 | 144 |
135 string16 ClockMenuButton::GetLabelAt(int index) const { | 145 void ClockMenuButton::ExecuteCommand(int id) { |
136 if (index == 0) | 146 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); | 147 host_->OpenButtonOptions(this); |
148 } | 148 } |
149 | 149 |
150 /////////////////////////////////////////////////////////////////////////////// | 150 // ClockMenuButton, PowerLibrary::Observer implementation: -------------------- |
151 // ClockMenuButton, PowerLibrary::Observer implementation: | |
152 | 151 |
153 void ClockMenuButton::SystemResumed() { | 152 void ClockMenuButton::SystemResumed() { |
154 UpdateText(); | 153 UpdateText(); |
155 } | 154 } |
156 | 155 |
157 /////////////////////////////////////////////////////////////////////////////// | 156 // ClockMenuButton, SystemLibrary::Observer implementation: ------------------- |
158 // ClockMenuButton, SystemAccess::Observer implementation: | |
159 | 157 |
160 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { | 158 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { |
161 UpdateText(); | 159 UpdateText(); |
162 } | 160 } |
163 | 161 |
164 //////////////////////////////////////////////////////////////////////////////// | 162 // ClockMenuButton, views::ViewMenuDelegate implementation: ------------------- |
165 // ClockMenuButton, views::ViewMenuDelegate implementation: | |
166 | 163 |
167 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 164 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
168 if (!clock_menu_.get()) | 165 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
169 clock_menu_.reset(new views::Menu2(this)); | 166 DCHECK_EQ(source, this); |
170 else | 167 |
171 clock_menu_->Rebuild(); | 168 EnsureMenu(); |
172 clock_menu_->UpdateStates(); | 169 |
173 clock_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 170 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
| 171 // top-level widget on the ChromeOS login screen that is a window. |
| 172 // The current BackgroundView class for the ChromeOS login screen |
| 173 // creates a owning Widget that has a native GtkWindow but is not a |
| 174 // Window. This makes it impossible to get the NativeWindow via |
| 175 // the views API. This workaround casts the top-level NativeWidget |
| 176 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
| 177 gfx::NativeWindow window = GTK_WINDOW(source->GetWidget()->GetNativeView()); |
| 178 |
| 179 gfx::Point screen_loc; |
| 180 views::View::ConvertPointToScreen(source, &screen_loc); |
| 181 gfx::Rect bounds(screen_loc, source->size()); |
| 182 menu_->RunMenuAt( |
| 183 window, |
| 184 this, |
| 185 bounds, |
| 186 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, |
| 187 true); |
174 } | 188 } |
175 | 189 |
176 //////////////////////////////////////////////////////////////////////////////// | 190 // ClockMenuButton, views::View implementation: ------------------------------- |
177 // ClockMenuButton, views::View implementation: | |
178 | 191 |
179 void ClockMenuButton::OnLocaleChanged() { | 192 void ClockMenuButton::OnLocaleChanged() { |
180 UpdateText(); | 193 UpdateText(); |
181 } | 194 } |
182 | 195 |
| 196 void ClockMenuButton::EnsureMenu() { |
| 197 if (!menu_.get()) { |
| 198 menu_.reset(new MenuItemView(this)); |
| 199 |
| 200 // Text for this item will be set by GetLabel(). |
| 201 menu_->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); |
| 202 |
| 203 // If options dialog is unavailable, don't count a separator and configure |
| 204 // menu item. |
| 205 if (host_->ShouldOpenButtonOptions(this)) { |
| 206 menu_->AppendSeparator(); |
| 207 |
| 208 const string16 clock_open_options_label = |
| 209 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
| 210 menu_->AppendMenuItemWithLabel( |
| 211 CLOCK_OPEN_OPTIONS_ITEM, |
| 212 UTF16ToWide(clock_open_options_label)); |
| 213 } |
| 214 } |
| 215 } |
| 216 |
183 } // namespace chromeos | 217 } // namespace chromeos |
OLD | NEW |