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 | 5 // Note: this file is used by Aura on all platforms, even though it is currently |
6 // in a chromeos specific location. | 6 // in a chromeos specific location. |
7 | 7 |
8 #include "chrome/browser/chromeos/status/clock_menu_button.h" | 8 #include "chrome/browser/chromeos/status/clock_menu_button.h" |
9 | 9 |
10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 }; | 36 }; |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 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 pref_service_(NULL), |
| 47 use_24hour_clock_(false) { |
47 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); | 48 set_id(VIEW_ID_STATUS_BUTTON_CLOCK); |
48 | 49 UpdateProfile(); |
49 #if defined(OS_CHROMEOS) // See note at top of file | |
50 // Start monitoring the kUse24HourClock preference. | |
51 Profile* profile = ProfileManager::GetDefaultProfile(); | |
52 if (profile) { // This can be NULL in the login screen. | |
53 registrar_.Init(profile->GetPrefs()); | |
54 registrar_.Add(prefs::kUse24HourClock, this); | |
55 } | |
56 #endif | |
57 UpdateTextAndSetNextTimer(); | 50 UpdateTextAndSetNextTimer(); |
58 } | 51 } |
59 | 52 |
60 ClockMenuButton::~ClockMenuButton() { | 53 ClockMenuButton::~ClockMenuButton() { |
61 timer_.Stop(); | 54 timer_.Stop(); |
62 } | 55 } |
63 | 56 |
| 57 void ClockMenuButton::UpdateProfile() { |
| 58 #if defined(OS_CHROMEOS) // See note at top of file |
| 59 // Start monitoring the kUse24HourClock preference. |
| 60 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 61 if (profile && profile->GetPrefs() != pref_service_) { |
| 62 pref_service_ = profile->GetPrefs(); |
| 63 use_24hour_clock_ = pref_service_->GetBoolean(prefs::kUse24HourClock); |
| 64 registrar_.reset(new PrefChangeRegistrar); |
| 65 registrar_->Init(pref_service_); |
| 66 registrar_->Add(prefs::kUse24HourClock, this); |
| 67 UpdateText(); |
| 68 } |
| 69 #endif |
| 70 } |
| 71 |
64 void ClockMenuButton::UpdateTextAndSetNextTimer() { | 72 void ClockMenuButton::UpdateTextAndSetNextTimer() { |
65 UpdateText(); | 73 UpdateText(); |
66 | 74 |
67 // Try to set the timer to go off at the next change of the minute. We don't | 75 // 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 | 76 // want to have the timer go off more than necessary since that will cause |
69 // the CPU to wake up and consume power. | 77 // the CPU to wake up and consume power. |
70 base::Time now = base::Time::Now(); | 78 base::Time now = base::Time::Now(); |
71 base::Time::Exploded exploded; | 79 base::Time::Exploded exploded; |
72 now.LocalExplode(&exploded); | 80 now.LocalExplode(&exploded); |
73 | 81 |
74 // Often this will be called at minute boundaries, and we'll actually want | 82 // Often this will be called at minute boundaries, and we'll actually want |
75 // 60 seconds from now. | 83 // 60 seconds from now. |
76 int seconds_left = 60 - exploded.second; | 84 int seconds_left = 60 - exploded.second; |
77 if (seconds_left == 0) | 85 if (seconds_left == 0) |
78 seconds_left = 60; | 86 seconds_left = 60; |
79 | 87 |
80 // Make sure that the timer fires on the next minute. Without this, if it is | 88 // 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. | 89 // called just a teeny bit early, then it will skip the next minute. |
82 seconds_left += kTimerSlopSeconds; | 90 seconds_left += kTimerSlopSeconds; |
83 | 91 |
84 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | 92 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, |
85 &ClockMenuButton::UpdateTextAndSetNextTimer); | 93 &ClockMenuButton::UpdateTextAndSetNextTimer); |
86 } | 94 } |
87 | 95 |
88 void ClockMenuButton::UpdateText() { | 96 void ClockMenuButton::UpdateText() { |
89 base::Time time(base::Time::Now()); | 97 base::Time time(base::Time::Now()); |
90 bool use_24hour_clock = default_use_24hour_clock_; | |
91 #if defined(OS_CHROMEOS) // See note at top of file | |
92 // If the profie is present, check the use 24-hour clock preference. | |
93 Profile* profile = ProfileManager::GetDefaultProfile(); | |
94 if (profile) | |
95 use_24hour_clock = profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | |
96 #endif | |
97 SetText(base::TimeFormatTimeOfDayWithHourClockType( | 98 SetText(base::TimeFormatTimeOfDayWithHourClockType( |
98 time, | 99 time, |
99 use_24hour_clock ? base::k24HourClock : base::k12HourClock, | 100 use_24hour_clock_ ? base::k24HourClock : base::k12HourClock, |
100 base::kDropAmPm)); | 101 base::kDropAmPm)); |
101 SetTooltipText(base::TimeFormatFriendlyDateAndTime(time)); | 102 string16 friendly_time_string(base::TimeFormatFriendlyDateAndTime(time)); |
102 SetAccessibleName(base::TimeFormatFriendlyDateAndTime(time)); | 103 SetTooltipText(friendly_time_string); |
| 104 SetAccessibleName(friendly_time_string); |
103 SchedulePaint(); | 105 SchedulePaint(); |
104 } | 106 } |
105 | 107 |
106 void ClockMenuButton::SetDefaultUse24HourClock(bool use_24hour_clock) { | 108 void ClockMenuButton::SetUse24HourClock(bool use_24hour_clock) { |
107 if (default_use_24hour_clock_ == use_24hour_clock) | 109 if (use_24hour_clock_ == use_24hour_clock) |
108 return; | 110 return; |
109 | 111 use_24hour_clock_ = use_24hour_clock; |
110 default_use_24hour_clock_ = use_24hour_clock; | |
111 UpdateText(); | 112 UpdateText(); |
112 } | 113 } |
113 | 114 |
114 // ClockMenuButton, content::NotificationObserver implementation: | 115 // ClockMenuButton, content::NotificationObserver implementation: |
115 | 116 |
116 void ClockMenuButton::Observe(int type, | 117 void ClockMenuButton::Observe(int type, |
117 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
118 const content::NotificationDetails& details) { | 119 const content::NotificationDetails& details) { |
119 #if defined(OS_CHROMEOS) // See note at top of file | 120 #if defined(OS_CHROMEOS) // See note at top of file |
120 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 121 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
121 std::string* pref_name = content::Details<std::string>(details).ptr(); | 122 std::string* pref_name = content::Details<std::string>(details).ptr(); |
122 if (*pref_name == prefs::kUse24HourClock) { | 123 if (*pref_name == prefs::kUse24HourClock) { |
123 UpdateText(); | 124 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 125 if (profile) { |
| 126 SetUse24HourClock( |
| 127 profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock)); |
| 128 } |
124 } | 129 } |
125 } | 130 } |
126 #endif | 131 #endif |
127 } | 132 } |
128 | 133 |
129 // ClockMenuButton, views::MenuDelegate implementation: | 134 // ClockMenuButton, views::MenuDelegate implementation: |
130 string16 ClockMenuButton::GetLabel(int id) const { | 135 string16 ClockMenuButton::GetLabel(int id) const { |
131 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); | 136 DCHECK_EQ(CLOCK_DISPLAY_ITEM, id); |
132 return base::TimeFormatFriendlyDate(base::Time::Now()); | 137 return base::TimeFormatFriendlyDate(base::Time::Now()); |
133 } | 138 } |
134 | 139 |
135 bool ClockMenuButton::IsCommandEnabled(int id) const { | 140 bool ClockMenuButton::IsCommandEnabled(int id) const { |
136 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); | 141 DCHECK(id == CLOCK_DISPLAY_ITEM || id == CLOCK_OPEN_OPTIONS_ITEM); |
137 return id == CLOCK_OPEN_OPTIONS_ITEM; | 142 return id == CLOCK_OPEN_OPTIONS_ITEM; |
138 } | 143 } |
139 | 144 |
140 void ClockMenuButton::ExecuteCommand(int id) { | 145 void ClockMenuButton::ExecuteCommand(int id) { |
141 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); | 146 DCHECK_EQ(CLOCK_OPEN_OPTIONS_ITEM, id); |
142 delegate()->ExecuteStatusAreaCommand( | 147 delegate()->ExecuteStatusAreaCommand( |
143 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS); | 148 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS); |
144 } | 149 } |
145 | 150 |
| 151 // StatusAreaButton implementation |
| 152 void ClockMenuButton::SetMenuActive(bool active) { |
| 153 // Activation gets updated when we change login state, so profile may change. |
| 154 UpdateProfile(); |
| 155 StatusAreaButton::SetMenuActive(active); |
| 156 } |
| 157 |
146 int ClockMenuButton::horizontal_padding() { | 158 int ClockMenuButton::horizontal_padding() { |
147 return 3; | 159 return 3; |
148 } | 160 } |
149 | 161 |
150 // ClockMenuButton, views::ViewMenuDelegate implementation: | 162 // ClockMenuButton, views::ViewMenuDelegate implementation: |
151 | 163 |
152 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 164 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
153 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. | 165 // View passed in must be a views::MenuButton, i.e. the ClockMenuButton. |
154 DCHECK_EQ(source, this); | 166 DCHECK_EQ(source, this); |
155 | 167 |
(...skipping 30 matching lines...) Expand all Loading... |
186 if (delegate()->ShouldExecuteStatusAreaCommand( | 198 if (delegate()->ShouldExecuteStatusAreaCommand( |
187 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS)) { | 199 this, StatusAreaButton::Delegate::SHOW_SYSTEM_OPTIONS)) { |
188 menu->AppendSeparator(); | 200 menu->AppendSeparator(); |
189 | 201 |
190 const string16 clock_open_options_label = | 202 const string16 clock_open_options_label = |
191 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); | 203 l10n_util::GetStringUTF16(IDS_STATUSBAR_CLOCK_OPEN_OPTIONS_DIALOG); |
192 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, | 204 menu->AppendMenuItemWithLabel(CLOCK_OPEN_OPTIONS_ITEM, |
193 clock_open_options_label); | 205 clock_open_options_label); |
194 } | 206 } |
195 } | 207 } |
OLD | NEW |