| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 // Note: this file is used by Aura on all platforms, even though it is currently | |
| 6 // in a chromeos specific location. | |
| 7 | |
| 8 #include "chrome/browser/chromeos/status/clock_menu_button.h" | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/i18n/time_formatting.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/time.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "chrome/browser/chromeos/status/status_area_view_chromeos.h" | |
| 16 #include "chrome/browser/chromeos/view_ids.h" | |
| 17 #include "chrome/browser/prefs/pref_service.h" | |
| 18 #include "chrome/browser/profiles/profile_manager.h" | |
| 19 #include "chrome/common/chrome_notification_types.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "content/public/browser/notification_details.h" | |
| 22 #include "content/public/browser/notification_source.h" | |
| 23 #include "grit/generated_resources.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 #include "ui/gfx/canvas.h" | |
| 26 #include "ui/gfx/font.h" | |
| 27 #include "ui/views/controls/menu/menu_runner.h" | |
| 28 #include "ui/views/widget/widget.h" | |
| 29 #include "unicode/datefmt.h" | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // views::MenuItemView item ids | |
| 34 enum ClockMenuItem { | |
| 35 CLOCK_DISPLAY_ITEM, | |
| 36 CLOCK_OPEN_OPTIONS_ITEM | |
| 37 }; | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 // Amount of slop to add into the timer to make sure we're into the next minute | |
| 42 // when the timer goes off. | |
| 43 const int kTimerSlopSeconds = 1; | |
| 44 | |
| 45 ClockMenuButton::ClockMenuButton(StatusAreaButton::Delegate* delegate) | |
| 46 : StatusAreaButton(delegate, this), | |
| 47 pref_service_(NULL), | |
| 48 use_24hour_clock_(false) { | |
| 49 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); | |
| 50 UpdateProfile(); | |
| 51 UpdateTextAndSetNextTimer(); | |
| 52 } | |
| 53 | |
| 54 ClockMenuButton::~ClockMenuButton() { | |
| 55 timer_.Stop(); | |
| 56 } | |
| 57 | |
| 58 void ClockMenuButton::UpdateProfile() { | |
| 59 #if defined(OS_CHROMEOS) // See note at top of file | |
| 60 // Start monitoring the kUse24HourClock preference. | |
| 61 Profile* profile = ProfileManager::GetDefaultProfile(); | |
| 62 if (profile && profile->GetPrefs() != pref_service_) { | |
| 63 pref_service_ = profile->GetPrefs(); | |
| 64 use_24hour_clock_ = pref_service_->GetBoolean(prefs::kUse24HourClock); | |
| 65 registrar_.reset(new PrefChangeRegistrar); | |
| 66 registrar_->Init(pref_service_); | |
| 67 registrar_->Add(prefs::kUse24HourClock, this); | |
| 68 UpdateText(); | |
| 69 } | |
| 70 #endif | |
| 71 } | |
| 72 | |
| 73 void ClockMenuButton::UpdateTextAndSetNextTimer() { | |
| 74 UpdateText(); | |
| 75 | |
| 76 // Try to set the timer to go off at the next change of the minute. We don't | |
| 77 // want to have the timer go off more than necessary since that will cause | |
| 78 // the CPU to wake up and consume power. | |
| 79 base::Time now = base::Time::Now(); | |
| 80 base::Time::Exploded exploded; | |
| 81 now.LocalExplode(&exploded); | |
| 82 | |
| 83 // Often this will be called at minute boundaries, and we'll actually want | |
| 84 // 60 seconds from now. | |
| 85 int seconds_left = 60 - exploded.second; | |
| 86 if (seconds_left == 0) | |
| 87 seconds_left = 60; | |
| 88 | |
| 89 // Make sure that the timer fires on the next minute. Without this, if it is | |
| 90 // called just a teeny bit early, then it will skip the next minute. | |
| 91 seconds_left += kTimerSlopSeconds; | |
| 92 | |
| 93 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | |
| 94 &ClockMenuButton::UpdateTextAndSetNextTimer); | |
| 95 } | |
| 96 | |
| 97 void ClockMenuButton::UpdateText() { | |
| 98 base::Time time(base::Time::Now()); | |
| 99 SetText(base::TimeFormatTimeOfDayWithHourClockType( | |
| 100 time, | |
| 101 use_24hour_clock_ ? base::k24HourClock : base::k12HourClock, | |
| 102 base::kDropAmPm)); | |
| 103 SetTooltipText(base::TimeFormatFriendlyDate(time)); | |
| 104 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); | |
| 105 SchedulePaint(); | |
| 106 } | |
| 107 | |
| 108 void ClockMenuButton::SetUse24HourClock(bool use_24hour_clock) { | |
| 109 if (use_24hour_clock_ == use_24hour_clock) | |
| 110 return; | |
| 111 use_24hour_clock_ = use_24hour_clock; | |
| 112 UpdateText(); | |
| 113 } | |
| 114 | |
| 115 // ClockMenuButton, content::NotificationObserver implementation: | |
| 116 | |
| 117 void ClockMenuButton::Observe(int type, | |
| 118 const content::NotificationSource& source, | |
| 119 const content::NotificationDetails& details) { | |
| 120 #if defined(OS_CHROMEOS) // See note at top of file | |
| 121 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
| 122 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
| 123 if (*pref_name == prefs::kUse24HourClock) { | |
| 124 Profile* profile = ProfileManager::GetDefaultProfile(); | |
| 125 if (profile) { | |
| 126 SetUse24HourClock( | |
| 127 profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock)); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 #endif | |
| 132 } | |
| 133 | |
| 134 // ClockMenuButton, views::MenuDelegate implementation: | |
| 135 string16 ClockMenuButton::GetLabel(int id) const { | |
| 136 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); | |
| 137 return base::TimeFormatFriendlyDate(base::Time::Now()); | |
| 138 } | |
| 139 | |
| 140 bool ClockMenuButton::IsCommandEnabled(int id) const { | |
| 141 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); | |
| 142 return id == CLOCK_OPEN_OPTIONS_ITEM; | |
| 143 } | |
| 144 | |
| 145 void ClockMenuButton::ExecuteCommand(int id) { | |
| 146 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); | |
| 147 delegate()->ExecuteStatusAreaCommand( | |
| 148 this, StatusAreaButton::Delegate::SHOW_DATE_OPTIONS); | |
| 149 } | |
| 150 | |
| 151 // StatusAreaButton implementation | |
| 152 void ClockMenuButton::SetMenuActive(bool active) { | |
| 153 // Activation gets updated when we change login state, so profile may change. | |
| 154 if (active) | |
| 155 UpdateProfile(); | |
| 156 StatusAreaButton::SetMenuActive(active); | |
| 157 } | |
| 158 | |
| 159 int ClockMenuButton::horizontal_padding() { | |
| 160 return 3; | |
| 161 } | |
| 162 | |
| 163 // ClockMenuButton, views::MenuButtonListener implementation: | |
| 164 | |
| 165 void ClockMenuButton::OnMenuButtonClicked(views::View* source, | |
| 166 const gfx::Point& point) { | |
| 167 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. | |
| 168 DCHECK_EQ(source, this); | |
| 169 | |
| 170 scoped_ptr<views::MenuRunner> menu_runner(CreateMenu()); | |
| 171 | |
| 172 gfx::Point screen_location; | |
| 173 views::View::ConvertPointToScreen(source, &screen_location); | |
| 174 gfx::Rect bounds(screen_location, source->size()); | |
| 175 ignore_result(menu_runner->RunMenuAt( | |
| 176 source->GetWidget()->GetTopLevelWidget(), | |
| 177 this, | |
| 178 bounds, | |
| 179 views::MenuItemView::TOPRIGHT, | |
| 180 views::MenuRunner::HAS_MNEMONICS)); | |
| 181 } | |
| 182 | |
| 183 // ClockMenuButton, views::View implementation: | |
| 184 | |
| 185 void ClockMenuButton::OnLocaleChanged() { | |
| 186 UpdateText(); | |
| 187 } | |
| 188 | |
| 189 views::MenuRunner* ClockMenuButton::CreateMenu() { | |
| 190 views::MenuItemView* menu = new views::MenuItemView(this); | |
| 191 // menu_runner takes ownership of menu. | |
| 192 views::MenuRunner* menu_runner = new views::MenuRunner(menu); | |
| 193 | |
| 194 // Text for this item will be set by GetLabel(). | |
| 195 menu->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); | |
| 196 | |
| 197 // If options UI is available, show a separator and configure menu item. | |
| 198 if (delegate()->ShouldExecuteStatusAreaCommand( | |
| 199 this, StatusAreaButton::Delegate::SHOW_DATE_OPTIONS)) { | |
| 200 menu->AppendSeparator(); | |
| 201 | |
| 202 const string16 clock_open_options_label = | |
| 203 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | |
| 204 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, | |
| 205 clock_open_options_label); | |
| 206 } | |
| 207 return menu_runner; | |
| 208 } | |
| OLD | NEW |