OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/common/system/date/date_view.h" | 5 #include "ash/common/system/date/date_view.h" |
6 | 6 |
7 #include "ash/common/system/tray/system_tray_delegate.h" | 7 #include "ash/common/system/tray/system_tray_delegate.h" |
8 #include "ash/common/system/tray/tray_constants.h" | 8 #include "ash/common/system/tray/tray_constants.h" |
9 #include "ash/common/system/tray/tray_utils.h" | 9 #include "ash/common/system/tray/tray_utils.h" |
10 #include "ash/common/wm_shell.h" | 10 #include "ash/common/wm_shell.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // Offset used to bring the minutes line closer to the hours line in the | 43 // Offset used to bring the minutes line closer to the hours line in the |
44 // vertical clock. | 44 // vertical clock. |
45 const int kVerticalClockMinutesTopOffset = -4; | 45 const int kVerticalClockMinutesTopOffset = -4; |
46 | 46 |
47 base::string16 FormatDate(const base::Time& time) { | 47 base::string16 FormatDate(const base::Time& time) { |
48 icu::UnicodeString date_string; | 48 icu::UnicodeString date_string; |
49 std::unique_ptr<icu::DateFormat> formatter( | 49 std::unique_ptr<icu::DateFormat> formatter( |
50 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); | 50 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); |
51 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); | 51 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); |
52 return base::string16(date_string.getBuffer(), | 52 return base::string16(date_string.getBuffer(), |
53 static_cast<size_t>(date_string.length())); | 53 static_cast<size_t>(date_string.length())); |
54 } | 54 } |
55 | 55 |
56 base::string16 FormatDayOfWeek(const base::Time& time) { | 56 base::string16 FormatDayOfWeek(const base::Time& time) { |
57 UErrorCode status = U_ZERO_ERROR; | 57 UErrorCode status = U_ZERO_ERROR; |
58 std::unique_ptr<icu::DateTimePatternGenerator> generator( | 58 std::unique_ptr<icu::DateTimePatternGenerator> generator( |
59 icu::DateTimePatternGenerator::createInstance(status)); | 59 icu::DateTimePatternGenerator::createInstance(status)); |
60 DCHECK(U_SUCCESS(status)); | 60 DCHECK(U_SUCCESS(status)); |
61 const char kBasePattern[] = "EEE"; | 61 const char kBasePattern[] = "EEE"; |
62 icu::UnicodeString generated_pattern = | 62 icu::UnicodeString generated_pattern = |
63 generator->getBestPattern(icu::UnicodeString(kBasePattern), status); | 63 generator->getBestPattern(icu::UnicodeString(kBasePattern), status); |
64 DCHECK(U_SUCCESS(status)); | 64 DCHECK(U_SUCCESS(status)); |
65 icu::SimpleDateFormat simple_formatter(generated_pattern, status); | 65 icu::SimpleDateFormat simple_formatter(generated_pattern, status); |
66 DCHECK(U_SUCCESS(status)); | 66 DCHECK(U_SUCCESS(status)); |
67 icu::UnicodeString date_string; | 67 icu::UnicodeString date_string; |
68 simple_formatter.format( | 68 simple_formatter.format(static_cast<UDate>(time.ToDoubleT() * 1000), |
69 static_cast<UDate>(time.ToDoubleT() * 1000), date_string, status); | 69 date_string, status); |
70 DCHECK(U_SUCCESS(status)); | 70 DCHECK(U_SUCCESS(status)); |
71 return base::string16( | 71 return base::string16(date_string.getBuffer(), |
72 date_string.getBuffer(), static_cast<size_t>(date_string.length())); | 72 static_cast<size_t>(date_string.length())); |
73 } | 73 } |
74 | 74 |
75 views::Label* CreateLabel() { | 75 views::Label* CreateLabel() { |
76 views::Label* label = new views::Label; | 76 views::Label* label = new views::Label; |
77 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 77 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
78 label->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255)); | 78 label->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255)); |
79 return label; | 79 return label; |
80 } | 80 } |
81 | 81 |
82 } // namespace | 82 } // namespace |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // 60 seconds from now. | 114 // 60 seconds from now. |
115 int seconds_left = 60 - exploded.second; | 115 int seconds_left = 60 - exploded.second; |
116 if (seconds_left == 0) | 116 if (seconds_left == 0) |
117 seconds_left = 60; | 117 seconds_left = 60; |
118 | 118 |
119 // Make sure that the timer fires on the next minute. Without this, if it is | 119 // Make sure that the timer fires on the next minute. Without this, if it is |
120 // called just a teeny bit early, then it will skip the next minute. | 120 // called just a teeny bit early, then it will skip the next minute. |
121 seconds_left += kTimerSlopSeconds; | 121 seconds_left += kTimerSlopSeconds; |
122 | 122 |
123 timer_.Stop(); | 123 timer_.Stop(); |
124 timer_.Start( | 124 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, |
125 FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), | 125 &BaseDateTimeView::UpdateText); |
126 this, &BaseDateTimeView::UpdateText); | |
127 } | 126 } |
128 | 127 |
129 void BaseDateTimeView::UpdateTextInternal(const base::Time& now) { | 128 void BaseDateTimeView::UpdateTextInternal(const base::Time& now) { |
130 SetAccessibleName(base::TimeFormatTimeOfDayWithHourClockType( | 129 SetAccessibleName(base::TimeFormatTimeOfDayWithHourClockType( |
131 now, hour_type_, base::kKeepAmPm) + | 130 now, hour_type_, base::kKeepAmPm) + |
132 base::ASCIIToUTF16(", ") + | 131 base::ASCIIToUTF16(", ") + |
133 base::TimeFormatFriendlyDate(now)); | 132 base::TimeFormatFriendlyDate(now)); |
134 | 133 |
135 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); | 134 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); |
136 } | 135 } |
137 | 136 |
138 void BaseDateTimeView::ChildPreferredSizeChanged(views::View* child) { | 137 void BaseDateTimeView::ChildPreferredSizeChanged(views::View* child) { |
139 PreferredSizeChanged(); | 138 PreferredSizeChanged(); |
140 } | 139 } |
141 | 140 |
142 void BaseDateTimeView::OnLocaleChanged() { | 141 void BaseDateTimeView::OnLocaleChanged() { |
143 UpdateText(); | 142 UpdateText(); |
144 } | 143 } |
145 | 144 |
146 /////////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////////// |
147 | 146 |
148 DateView::DateView() : action_(TrayDate::NONE) { | 147 DateView::DateView() : action_(TrayDate::NONE) { |
149 SetLayoutManager( | 148 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
150 new views::BoxLayout( | |
151 views::BoxLayout::kVertical, 0, 0, 0)); | |
152 date_label_ = CreateLabel(); | 149 date_label_ = CreateLabel(); |
153 date_label_->SetEnabledColor(kHeaderTextColorNormal); | 150 date_label_->SetEnabledColor(kHeaderTextColorNormal); |
154 UpdateTextInternal(base::Time::Now()); | 151 UpdateTextInternal(base::Time::Now()); |
155 AddChildView(date_label_); | 152 AddChildView(date_label_); |
156 } | 153 } |
157 | 154 |
158 DateView::~DateView() { | 155 DateView::~DateView() {} |
159 } | |
160 | 156 |
161 void DateView::SetAction(TrayDate::DateAction action) { | 157 void DateView::SetAction(TrayDate::DateAction action) { |
162 if (action == action_) | 158 if (action == action_) |
163 return; | 159 return; |
164 if (IsMouseHovered()) { | 160 if (IsMouseHovered()) { |
165 date_label_->SetEnabledColor( | 161 date_label_->SetEnabledColor(action == TrayDate::NONE |
166 action == TrayDate::NONE ? kHeaderTextColorNormal : | 162 ? kHeaderTextColorNormal |
167 kHeaderTextColorHover); | 163 : kHeaderTextColorHover); |
168 SchedulePaint(); | 164 SchedulePaint(); |
169 } | 165 } |
170 action_ = action; | 166 action_ = action; |
171 SetFocusBehavior(action_ != TrayDate::NONE ? FocusBehavior::ALWAYS | 167 SetFocusBehavior(action_ != TrayDate::NONE ? FocusBehavior::ALWAYS |
172 : FocusBehavior::NEVER); | 168 : FocusBehavior::NEVER); |
173 } | 169 } |
174 | 170 |
175 void DateView::UpdateTimeFormat() { | 171 void DateView::UpdateTimeFormat() { |
176 hour_type_ = WmShell::Get()->system_tray_delegate()->GetHourClockType(); | 172 hour_type_ = WmShell::Get()->system_tray_delegate()->GetHourClockType(); |
177 UpdateText(); | 173 UpdateText(); |
178 } | 174 } |
179 | 175 |
180 base::HourClockType DateView::GetHourTypeForTesting() const { | 176 base::HourClockType DateView::GetHourTypeForTesting() const { |
181 return hour_type_; | 177 return hour_type_; |
182 } | 178 } |
183 | 179 |
184 void DateView::SetActive(bool active) { | 180 void DateView::SetActive(bool active) { |
185 date_label_->SetEnabledColor(active ? kHeaderTextColorHover | 181 date_label_->SetEnabledColor(active ? kHeaderTextColorHover |
186 : kHeaderTextColorNormal); | 182 : kHeaderTextColorNormal); |
187 SchedulePaint(); | 183 SchedulePaint(); |
188 } | 184 } |
189 | 185 |
190 void DateView::UpdateTextInternal(const base::Time& now) { | 186 void DateView::UpdateTextInternal(const base::Time& now) { |
191 BaseDateTimeView::UpdateTextInternal(now); | 187 BaseDateTimeView::UpdateTextInternal(now); |
192 date_label_->SetText( | 188 date_label_->SetText(l10n_util::GetStringFUTF16( |
193 l10n_util::GetStringFUTF16( | 189 IDS_ASH_STATUS_TRAY_DATE, FormatDayOfWeek(now), FormatDate(now))); |
194 IDS_ASH_STATUS_TRAY_DATE, FormatDayOfWeek(now), FormatDate(now))); | |
195 } | 190 } |
196 | 191 |
197 bool DateView::PerformAction(const ui::Event& event) { | 192 bool DateView::PerformAction(const ui::Event& event) { |
198 if (action_ == TrayDate::NONE) | 193 if (action_ == TrayDate::NONE) |
199 return false; | 194 return false; |
200 if (action_ == TrayDate::SHOW_DATE_SETTINGS) | 195 if (action_ == TrayDate::SHOW_DATE_SETTINGS) |
201 WmShell::Get()->system_tray_delegate()->ShowDateSettings(); | 196 WmShell::Get()->system_tray_delegate()->ShowDateSettings(); |
202 else if (action_ == TrayDate::SET_SYSTEM_TIME) | 197 else if (action_ == TrayDate::SET_SYSTEM_TIME) |
203 WmShell::Get()->system_tray_delegate()->ShowSetTimeDialog(); | 198 WmShell::Get()->system_tray_delegate()->ShowSetTimeDialog(); |
204 return true; | 199 return true; |
(...skipping 24 matching lines...) Expand all Loading... |
229 } | 224 } |
230 | 225 |
231 /////////////////////////////////////////////////////////////////////////////// | 226 /////////////////////////////////////////////////////////////////////////////// |
232 | 227 |
233 TimeView::TimeView(TrayDate::ClockLayout clock_layout) { | 228 TimeView::TimeView(TrayDate::ClockLayout clock_layout) { |
234 SetupLabels(); | 229 SetupLabels(); |
235 UpdateTextInternal(base::Time::Now()); | 230 UpdateTextInternal(base::Time::Now()); |
236 UpdateClockLayout(clock_layout); | 231 UpdateClockLayout(clock_layout); |
237 } | 232 } |
238 | 233 |
239 TimeView::~TimeView() { | 234 TimeView::~TimeView() {} |
240 } | |
241 | 235 |
242 void TimeView::UpdateTimeFormat() { | 236 void TimeView::UpdateTimeFormat() { |
243 hour_type_ = WmShell::Get()->system_tray_delegate()->GetHourClockType(); | 237 hour_type_ = WmShell::Get()->system_tray_delegate()->GetHourClockType(); |
244 UpdateText(); | 238 UpdateText(); |
245 } | 239 } |
246 | 240 |
247 base::HourClockType TimeView::GetHourTypeForTesting() const { | 241 base::HourClockType TimeView::GetHourTypeForTesting() const { |
248 return hour_type_; | 242 return hour_type_; |
249 } | 243 } |
250 | 244 |
(...skipping 11 matching lines...) Expand all Loading... |
262 now, hour_type_, base::kDropAmPm); | 256 now, hour_type_, base::kDropAmPm); |
263 horizontal_label_->SetText(current_time); | 257 horizontal_label_->SetText(current_time); |
264 horizontal_label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); | 258 horizontal_label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); |
265 | 259 |
266 // Calculate vertical clock layout labels. | 260 // Calculate vertical clock layout labels. |
267 size_t colon_pos = current_time.find(base::ASCIIToUTF16(":")); | 261 size_t colon_pos = current_time.find(base::ASCIIToUTF16(":")); |
268 base::string16 hour = current_time.substr(0, colon_pos); | 262 base::string16 hour = current_time.substr(0, colon_pos); |
269 base::string16 minute = current_time.substr(colon_pos + 1); | 263 base::string16 minute = current_time.substr(colon_pos + 1); |
270 | 264 |
271 // Sometimes pad single-digit hours with a zero for aesthetic reasons. | 265 // Sometimes pad single-digit hours with a zero for aesthetic reasons. |
272 if (hour.length() == 1 && | 266 if (hour.length() == 1 && hour_type_ == base::k24HourClock && |
273 hour_type_ == base::k24HourClock && | |
274 !base::i18n::IsRTL()) | 267 !base::i18n::IsRTL()) |
275 hour = base::ASCIIToUTF16("0") + hour; | 268 hour = base::ASCIIToUTF16("0") + hour; |
276 | 269 |
277 vertical_label_hours_->SetText(hour); | 270 vertical_label_hours_->SetText(hour); |
278 vertical_label_minutes_->SetText(minute); | 271 vertical_label_minutes_->SetText(minute); |
279 Layout(); | 272 Layout(); |
280 } | 273 } |
281 | 274 |
282 bool TimeView::PerformAction(const ui::Event& event) { | 275 bool TimeView::PerformAction(const ui::Event& event) { |
283 return false; | 276 return false; |
(...skipping 27 matching lines...) Expand all Loading... |
311 layout->StartRow(0, kColumnId); | 304 layout->StartRow(0, kColumnId); |
312 layout->AddView(vertical_label_minutes_.get()); | 305 layout->AddView(vertical_label_minutes_.get()); |
313 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVerticalAlignment); | 306 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVerticalAlignment); |
314 } | 307 } |
315 Layout(); | 308 Layout(); |
316 } | 309 } |
317 | 310 |
318 void TimeView::SetBorderFromLayout(TrayDate::ClockLayout clock_layout) { | 311 void TimeView::SetBorderFromLayout(TrayDate::ClockLayout clock_layout) { |
319 if (clock_layout == TrayDate::HORIZONTAL_CLOCK) | 312 if (clock_layout == TrayDate::HORIZONTAL_CLOCK) |
320 SetBorder(views::Border::CreateEmptyBorder( | 313 SetBorder(views::Border::CreateEmptyBorder( |
321 0, | 314 0, kTrayLabelItemHorizontalPaddingBottomAlignment, 0, |
322 kTrayLabelItemHorizontalPaddingBottomAlignment, | |
323 0, | |
324 kTrayLabelItemHorizontalPaddingBottomAlignment)); | 315 kTrayLabelItemHorizontalPaddingBottomAlignment)); |
325 else | 316 else |
326 SetBorder(views::Border::NullBorder()); | 317 SetBorder(views::Border::NullBorder()); |
327 } | 318 } |
328 | 319 |
329 void TimeView::SetupLabels() { | 320 void TimeView::SetupLabels() { |
330 horizontal_label_.reset(CreateLabel()); | 321 horizontal_label_.reset(CreateLabel()); |
331 SetupLabel(horizontal_label_.get()); | 322 SetupLabel(horizontal_label_.get()); |
332 vertical_label_hours_.reset(CreateLabel()); | 323 vertical_label_hours_.reset(CreateLabel()); |
333 SetupLabel(vertical_label_hours_.get()); | 324 SetupLabel(vertical_label_hours_.get()); |
334 vertical_label_minutes_.reset(CreateLabel()); | 325 vertical_label_minutes_.reset(CreateLabel()); |
335 SetupLabel(vertical_label_minutes_.get()); | 326 SetupLabel(vertical_label_minutes_.get()); |
336 vertical_label_minutes_->SetEnabledColor(kVerticalClockMinuteColor); | 327 vertical_label_minutes_->SetEnabledColor(kVerticalClockMinuteColor); |
337 // Pull the minutes up closer to the hours by using a negative top border. | 328 // Pull the minutes up closer to the hours by using a negative top border. |
338 vertical_label_minutes_->SetBorder(views::Border::CreateEmptyBorder( | 329 vertical_label_minutes_->SetBorder(views::Border::CreateEmptyBorder( |
339 kVerticalClockMinutesTopOffset, 0, 0, 0)); | 330 kVerticalClockMinutesTopOffset, 0, 0, 0)); |
340 } | 331 } |
341 | 332 |
342 void TimeView::SetupLabel(views::Label* label) { | 333 void TimeView::SetupLabel(views::Label* label) { |
343 label->set_owned_by_client(); | 334 label->set_owned_by_client(); |
344 SetupLabelForTray(label); | 335 SetupLabelForTray(label); |
345 label->SetElideBehavior(gfx::NO_ELIDE); | 336 label->SetElideBehavior(gfx::NO_ELIDE); |
346 } | 337 } |
347 | 338 |
348 } // namespace tray | 339 } // namespace tray |
349 } // namespace ash | 340 } // namespace ash |
OLD | NEW |