| 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_view_chromeos.h" |
| 13 #include "chrome/browser/chromeos/view_ids.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/font.h" | 23 #include "ui/gfx/font.h" |
| 23 #include "unicode/datefmt.h" | 24 #include "unicode/datefmt.h" |
| 24 #include "views/controls/menu/menu_runner.h" | 25 #include "views/controls/menu/menu_runner.h" |
| 25 #include "views/widget/widget.h" | 26 #include "views/widget/widget.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // views::MenuItemView item ids | 30 // views::MenuItemView item ids |
| 30 enum ClockMenuItem { | 31 enum ClockMenuItem { |
| 31 CLOCK_DISPLAY_ITEM, | 32 CLOCK_DISPLAY_ITEM, |
| 32 CLOCK_OPEN_OPTIONS_ITEM | 33 CLOCK_OPEN_OPTIONS_ITEM |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 } // namespace | 36 } // namespace |
| 36 | 37 |
| 37 namespace chromeos { | 38 namespace chromeos { |
| 38 | 39 |
| 39 // Amount of slop to add into the timer to make sure we're into the next minute | 40 // Amount of slop to add into the timer to make sure we're into the next minute |
| 40 // when the timer goes off. | 41 // when the timer goes off. |
| 41 const int kTimerSlopSeconds = 1; | 42 const int kTimerSlopSeconds = 1; |
| 42 | 43 |
| 43 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) | 44 ClockMenuButton::ClockMenuButton(StatusAreaButton::Delegate* delegate) |
| 44 : StatusAreaButton(host, this), | 45 : StatusAreaButton(delegate, this), |
| 45 default_use_24hour_clock_(false) { | 46 default_use_24hour_clock_(false) { |
| 46 // Add as TimezoneSettings observer. We update the clock if timezone changes. | 47 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); |
| 47 system::TimezoneSettings::GetInstance()->AddObserver(this); | |
| 48 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | |
| 49 // Start monitoring the kUse24HourClock preference. | 48 // Start monitoring the kUse24HourClock preference. |
| 50 if (host->GetProfile()) { // This can be NULL in the login screen. | 49 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 51 registrar_.Init(host->GetProfile()->GetPrefs()); | 50 if (profile) { // This can be NULL in the login screen. |
| 51 registrar_.Init(profile->GetPrefs()); |
| 52 registrar_.Add(prefs::kUse24HourClock, this); | 52 registrar_.Add(prefs::kUse24HourClock, this); |
| 53 } | 53 } |
| 54 | 54 |
| 55 UpdateTextAndSetNextTimer(); | 55 UpdateTextAndSetNextTimer(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 ClockMenuButton::~ClockMenuButton() { | 58 ClockMenuButton::~ClockMenuButton() { |
| 59 timer_.Stop(); | 59 timer_.Stop(); |
| 60 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); | |
| 61 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | |
| 62 } | 60 } |
| 63 | 61 |
| 64 void ClockMenuButton::UpdateTextAndSetNextTimer() { | 62 void ClockMenuButton::UpdateTextAndSetNextTimer() { |
| 65 UpdateText(); | 63 UpdateText(); |
| 66 | 64 |
| 67 // Try to set the timer to go off at the next change of the minute. We don't | 65 // Try to set the timer to go off at the next change of the minute. We don't |
| 68 // want to have the timer go off more than necessary since that will cause | 66 // want to have the timer go off more than necessary since that will cause |
| 69 // the CPU to wake up and consume power. | 67 // the CPU to wake up and consume power. |
| 70 base::Time now = base::Time::Now(); | 68 base::Time now = base::Time::Now(); |
| 71 base::Time::Exploded exploded; | 69 base::Time::Exploded exploded; |
| 72 now.LocalExplode(&exploded); | 70 now.LocalExplode(&exploded); |
| 73 | 71 |
| 74 // Often this will be called at minute boundaries, and we'll actually want | 72 // Often this will be called at minute boundaries, and we'll actually want |
| 75 // 60 seconds from now. | 73 // 60 seconds from now. |
| 76 int seconds_left = 60 - exploded.second; | 74 int seconds_left = 60 - exploded.second; |
| 77 if (seconds_left == 0) | 75 if (seconds_left == 0) |
| 78 seconds_left = 60; | 76 seconds_left = 60; |
| 79 | 77 |
| 80 // Make sure that the timer fires on the next minute. Without this, if it is | 78 // Make sure that the timer fires on the next minute. Without this, if it is |
| 81 // called just a teeny bit early, then it will skip the next minute. | 79 // called just a teeny bit early, then it will skip the next minute. |
| 82 seconds_left += kTimerSlopSeconds; | 80 seconds_left += kTimerSlopSeconds; |
| 83 | 81 |
| 84 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | 82 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, |
| 85 &ClockMenuButton::UpdateTextAndSetNextTimer); | 83 &ClockMenuButton::UpdateTextAndSetNextTimer); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void ClockMenuButton::UpdateText() { | 86 void ClockMenuButton::UpdateText() { |
| 89 base::Time time(base::Time::Now()); | 87 base::Time time(base::Time::Now()); |
| 88 bool use_24hour_clock = default_use_24hour_clock_; |
| 89 #if defined(OS_CHROMEOS) |
| 90 // If the profie is present, check the use 24-hour clock preference. | 90 // If the profie is present, check the use 24-hour clock preference. |
| 91 const bool use_24hour_clock = host_->GetProfile() ? | 91 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 92 host_->GetProfile()->GetPrefs()->GetBoolean(prefs::kUse24HourClock) : | 92 if (profile) |
| 93 default_use_24hour_clock_; | 93 use_24hour_clock = profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
| 94 #endif |
| 94 SetText(base::TimeFormatTimeOfDayWithHourClockType( | 95 SetText(base::TimeFormatTimeOfDayWithHourClockType( |
| 95 time, | 96 time, |
| 96 use_24hour_clock ? base::k24HourClock : base::k12HourClock, | 97 use_24hour_clock ? base::k24HourClock : base::k12HourClock, |
| 97 base::kDropAmPm)); | 98 base::kDropAmPm)); |
| 98 SetTooltipText(base::TimeFormatFriendlyDateAndTime(time)); | 99 SetTooltipText(base::TimeFormatFriendlyDateAndTime(time)); |
| 99 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); | 100 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); |
| 100 SchedulePaint(); | 101 SchedulePaint(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void ClockMenuButton::SetDefaultUse24HourClock(bool use_24hour_clock) { | 104 void ClockMenuButton::SetDefaultUse24HourClock(bool use_24hour_clock) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 127 return base::TimeFormatFriendlyDate(base::Time::Now()); | 128 return base::TimeFormatFriendlyDate(base::Time::Now()); |
| 128 } | 129 } |
| 129 | 130 |
| 130 bool ClockMenuButton::IsCommandEnabled(int id) const { | 131 bool ClockMenuButton::IsCommandEnabled(int id) const { |
| 131 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); | 132 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
| 132 return id == CLOCK_OPEN_OPTIONS_ITEM; | 133 return id == CLOCK_OPEN_OPTIONS_ITEM; |
| 133 } | 134 } |
| 134 | 135 |
| 135 void ClockMenuButton::ExecuteCommand(int id) { | 136 void ClockMenuButton::ExecuteCommand(int id) { |
| 136 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); | 137 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); |
| 137 host_->OpenButtonOptions(this); | 138 delegate()->ExecuteStatusAreaCommand( |
| 138 } | 139 this, StatusAreaViewChromeos::SHOW_SYSTEM_OPTIONS); |
| 139 | |
| 140 // ClockMenuButton, PowerLibrary::Observer implementation: | |
| 141 | |
| 142 void ClockMenuButton::SystemResumed() { | |
| 143 UpdateText(); | |
| 144 } | |
| 145 | |
| 146 // ClockMenuButton, SystemLibrary::Observer implementation: | |
| 147 | |
| 148 void ClockMenuButton::TimezoneChanged(const icu::TimeZone& timezone) { | |
| 149 UpdateText(); | |
| 150 } | 140 } |
| 151 | 141 |
| 152 int ClockMenuButton::horizontal_padding() { | 142 int ClockMenuButton::horizontal_padding() { |
| 153 return 3; | 143 return 3; |
| 154 } | 144 } |
| 155 | 145 |
| 156 // ClockMenuButton, views::ViewMenuDelegate implementation: | 146 // ClockMenuButton, views::ViewMenuDelegate implementation: |
| 157 | 147 |
| 158 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 148 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| 159 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. | 149 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 181 if (menu_runner_.get()) | 171 if (menu_runner_.get()) |
| 182 return; | 172 return; |
| 183 | 173 |
| 184 views::MenuItemView* menu = new views::MenuItemView(this); | 174 views::MenuItemView* menu = new views::MenuItemView(this); |
| 185 // menu_runner_ takes ownership of menu. | 175 // menu_runner_ takes ownership of menu. |
| 186 menu_runner_.reset(new views::MenuRunner(menu)); | 176 menu_runner_.reset(new views::MenuRunner(menu)); |
| 187 | 177 |
| 188 // Text for this item will be set by GetLabel(). | 178 // Text for this item will be set by GetLabel(). |
| 189 menu->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); | 179 menu->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); |
| 190 | 180 |
| 191 // If options dialog is unavailable, don't show a separator and configure | 181 // If options UI is available, show a separator and configure menu item. |
| 192 // menu item. | 182 if (delegate()->ShouldExecuteStatusAreaCommand( |
| 193 if (host_->ShouldOpenButtonOptions(this)) { | 183 this, StatusAreaViewChromeos::SHOW_SYSTEM_OPTIONS)) { |
| 194 menu->AppendSeparator(); | 184 menu->AppendSeparator(); |
| 195 | 185 |
| 196 const string16 clock_open_options_label = | 186 const string16 clock_open_options_label = |
| 197 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | 187 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
| 198 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, | 188 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, |
| 199 clock_open_options_label); | 189 clock_open_options_label); |
| 200 } | 190 } |
| 201 } | 191 } |
| 202 | 192 |
| 203 } // namespace chromeos | 193 } // namespace chromeos |
| OLD | NEW |