| 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 // Note: this file is used by Aura on all platforms, even though it is currently |
| 6 // in a chromeos specific location. |
| 7 |
| 5 #include "chrome/browser/chromeos/status/clock_menu_button.h" | 8 #include "chrome/browser/chromeos/status/clock_menu_button.h" |
| 6 | 9 |
| 7 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
| 8 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 9 #include "base/time.h" | 12 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 12 #include "chrome/browser/chromeos/status/status_area_view_chromeos.h" | 14 #include "chrome/browser/chromeos/status/status_area_view_chromeos.h" |
| 13 #include "chrome/browser/chromeos/view_ids.h" | 15 #include "chrome/browser/chromeos/view_ids.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 20 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/gfx/canvas.h" | 24 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/font.h" | 25 #include "ui/gfx/font.h" |
| 24 #include "unicode/datefmt.h" | 26 #include "unicode/datefmt.h" |
| 25 #include "views/controls/menu/menu_runner.h" | 27 #include "views/controls/menu/menu_runner.h" |
| 26 #include "views/widget/widget.h" | 28 #include "views/widget/widget.h" |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // views::MenuItemView item ids | 32 // views::MenuItemView item ids |
| 31 enum ClockMenuItem { | 33 enum ClockMenuItem { |
| 32 CLOCK_DISPLAY_ITEM, | 34 CLOCK_DISPLAY_ITEM, |
| 33 CLOCK_OPEN_OPTIONS_ITEM | 35 CLOCK_OPEN_OPTIONS_ITEM |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| 37 | 39 |
| 38 namespace chromeos { | |
| 39 | |
| 40 // 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 |
| 41 // when the timer goes off. | 41 // when the timer goes off. |
| 42 const int kTimerSlopSeconds = 1; | 42 const int kTimerSlopSeconds = 1; |
| 43 | 43 |
| 44 ClockMenuButton::ClockMenuButton(StatusAreaButton::Delegate* delegate) | 44 ClockMenuButton::ClockMenuButton(StatusAreaButton::Delegate* delegate) |
| 45 : StatusAreaButton(delegate, this), | 45 : StatusAreaButton(delegate, this), |
| 46 default_use_24hour_clock_(false) { | 46 default_use_24hour_clock_(false) { |
| 47 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); | 47 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); |
| 48 |
| 49 #if defined(OS_CHROMEOS) // See note at top of file |
| 48 // Start monitoring the kUse24HourClock preference. | 50 // Start monitoring the kUse24HourClock preference. |
| 49 Profile* profile = ProfileManager::GetDefaultProfile(); | 51 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 50 if (profile) { // This can be NULL in the login screen. | 52 if (profile) { // This can be NULL in the login screen. |
| 51 registrar_.Init(profile->GetPrefs()); | 53 registrar_.Init(profile->GetPrefs()); |
| 52 registrar_.Add(prefs::kUse24HourClock, this); | 54 registrar_.Add(prefs::kUse24HourClock, this); |
| 53 } | 55 } |
| 54 | 56 #endif |
| 55 UpdateTextAndSetNextTimer(); | 57 UpdateTextAndSetNextTimer(); |
| 56 } | 58 } |
| 57 | 59 |
| 58 ClockMenuButton::~ClockMenuButton() { | 60 ClockMenuButton::~ClockMenuButton() { |
| 59 timer_.Stop(); | 61 timer_.Stop(); |
| 60 } | 62 } |
| 61 | 63 |
| 62 void ClockMenuButton::UpdateTextAndSetNextTimer() { | 64 void ClockMenuButton::UpdateTextAndSetNextTimer() { |
| 63 UpdateText(); | 65 UpdateText(); |
| 64 | 66 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 79 // called just a teeny bit early, then it will skip the next minute. | 81 // called just a teeny bit early, then it will skip the next minute. |
| 80 seconds_left += kTimerSlopSeconds; | 82 seconds_left += kTimerSlopSeconds; |
| 81 | 83 |
| 82 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | 84 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, |
| 83 &ClockMenuButton::UpdateTextAndSetNextTimer); | 85 &ClockMenuButton::UpdateTextAndSetNextTimer); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void ClockMenuButton::UpdateText() { | 88 void ClockMenuButton::UpdateText() { |
| 87 base::Time time(base::Time::Now()); | 89 base::Time time(base::Time::Now()); |
| 88 bool use_24hour_clock = default_use_24hour_clock_; | 90 bool use_24hour_clock = default_use_24hour_clock_; |
| 89 #if defined(OS_CHROMEOS) | 91 #if defined(OS_CHROMEOS) // See note at top of file |
| 90 // If the profie is present, check the use 24-hour clock preference. | 92 // If the profie is present, check the use 24-hour clock preference. |
| 91 Profile* profile = ProfileManager::GetDefaultProfile(); | 93 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 92 if (profile) | 94 if (profile) |
| 93 use_24hour_clock = profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | 95 use_24hour_clock = profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
| 94 #endif | 96 #endif |
| 95 SetText(base::TimeFormatTimeOfDayWithHourClockType( | 97 SetText(base::TimeFormatTimeOfDayWithHourClockType( |
| 96 time, | 98 time, |
| 97 use_24hour_clock ? base::k24HourClock : base::k12HourClock, | 99 use_24hour_clock ? base::k24HourClock : base::k12HourClock, |
| 98 base::kDropAmPm)); | 100 base::kDropAmPm)); |
| 99 SetTooltipText(base::TimeFormatFriendlyDateAndTime(time)); | 101 SetTooltipText(base::TimeFormatFriendlyDateAndTime(time)); |
| 100 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); | 102 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); |
| 101 SchedulePaint(); | 103 SchedulePaint(); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void ClockMenuButton::SetDefaultUse24HourClock(bool use_24hour_clock) { | 106 void ClockMenuButton::SetDefaultUse24HourClock(bool use_24hour_clock) { |
| 105 if (default_use_24hour_clock_ == use_24hour_clock) | 107 if (default_use_24hour_clock_ == use_24hour_clock) |
| 106 return; | 108 return; |
| 107 | 109 |
| 108 default_use_24hour_clock_ = use_24hour_clock; | 110 default_use_24hour_clock_ = use_24hour_clock; |
| 109 UpdateText(); | 111 UpdateText(); |
| 110 } | 112 } |
| 111 | 113 |
| 112 // ClockMenuButton, content::NotificationObserver implementation: | 114 // ClockMenuButton, content::NotificationObserver implementation: |
| 113 | 115 |
| 114 void ClockMenuButton::Observe(int type, | 116 void ClockMenuButton::Observe(int type, |
| 115 const content::NotificationSource& source, | 117 const content::NotificationSource& source, |
| 116 const content::NotificationDetails& details) { | 118 const content::NotificationDetails& details) { |
| 119 #if defined(OS_CHROMEOS) // See note at top of file |
| 117 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 120 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 118 std::string* pref_name = content::Details<std::string>(details).ptr(); | 121 std::string* pref_name = content::Details<std::string>(details).ptr(); |
| 119 if (*pref_name == prefs::kUse24HourClock) { | 122 if (*pref_name == prefs::kUse24HourClock) { |
| 120 UpdateText(); | 123 UpdateText(); |
| 121 } | 124 } |
| 122 } | 125 } |
| 126 #endif |
| 123 } | 127 } |
| 124 | 128 |
| 125 // ClockMenuButton, views::MenuDelegate implementation: | 129 // ClockMenuButton, views::MenuDelegate implementation: |
| 126 string16 ClockMenuButton::GetLabel(int id) const { | 130 string16 ClockMenuButton::GetLabel(int id) const { |
| 127 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); | 131 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); |
| 128 return base::TimeFormatFriendlyDate(base::Time::Now()); | 132 return base::TimeFormatFriendlyDate(base::Time::Now()); |
| 129 } | 133 } |
| 130 | 134 |
| 131 bool ClockMenuButton::IsCommandEnabled(int id) const { | 135 bool ClockMenuButton::IsCommandEnabled(int id) const { |
| 132 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); | 136 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
| 133 return id == CLOCK_OPEN_OPTIONS_ITEM; | 137 return id == CLOCK_OPEN_OPTIONS_ITEM; |
| 134 } | 138 } |
| 135 | 139 |
| 136 void ClockMenuButton::ExecuteCommand(int id) { | 140 void ClockMenuButton::ExecuteCommand(int id) { |
| 137 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); | 141 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); |
| 138 delegate()->ExecuteStatusAreaCommand( | 142 delegate()->ExecuteStatusAreaCommand( |
| 139 this, StatusAreaViewChromeos::SHOW_SYSTEM_OPTIONS); | 143 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS); |
| 140 } | 144 } |
| 141 | 145 |
| 142 int ClockMenuButton::horizontal_padding() { | 146 int ClockMenuButton::horizontal_padding() { |
| 143 return 3; | 147 return 3; |
| 144 } | 148 } |
| 145 | 149 |
| 146 // ClockMenuButton, views::ViewMenuDelegate implementation: | 150 // ClockMenuButton, views::ViewMenuDelegate implementation: |
| 147 | 151 |
| 148 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 152 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| 149 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. | 153 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 173 | 177 |
| 174 views::MenuItemView* menu = new views::MenuItemView(this); | 178 views::MenuItemView* menu = new views::MenuItemView(this); |
| 175 // menu_runner_ takes ownership of menu. | 179 // menu_runner_ takes ownership of menu. |
| 176 menu_runner_.reset(new views::MenuRunner(menu)); | 180 menu_runner_.reset(new views::MenuRunner(menu)); |
| 177 | 181 |
| 178 // Text for this item will be set by GetLabel(). | 182 // Text for this item will be set by GetLabel(). |
| 179 menu->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); | 183 menu->AppendDelegateMenuItem(CLOCK_DISPLAY_ITEM); |
| 180 | 184 |
| 181 // If options UI is available, show a separator and configure menu item. | 185 // If options UI is available, show a separator and configure menu item. |
| 182 if (delegate()->ShouldExecuteStatusAreaCommand( | 186 if (delegate()->ShouldExecuteStatusAreaCommand( |
| 183 this, StatusAreaViewChromeos::SHOW_SYSTEM_OPTIONS)) { | 187 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS)) { |
| 184 menu->AppendSeparator(); | 188 menu->AppendSeparator(); |
| 185 | 189 |
| 186 const string16 clock_open_options_label = | 190 const string16 clock_open_options_label = |
| 187 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | 191 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
| 188 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, | 192 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, |
| 189 clock_open_options_label); | 193 clock_open_options_label); |
| 190 } | 194 } |
| 191 } | 195 } |
| 192 | |
| 193 } // namespace chromeos | |
| OLD | NEW |