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/system/date/date_view.h" | 5 #include "ash/system/date/date_view.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/tray/system_tray_delegate.h" | 8 #include "ash/system/tray/system_tray_delegate.h" |
9 #include "ash/system/tray/tray_constants.h" | |
9 #include "base/time.h" | 10 #include "base/time.h" |
10 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
11 #include "ui/views/layout/box_layout.h" | 12 #include "ui/views/layout/box_layout.h" |
12 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
13 #include "unicode/datefmt.h" | 14 #include "unicode/datefmt.h" |
14 | 15 |
15 namespace ash { | 16 namespace ash { |
16 namespace internal { | 17 namespace internal { |
17 namespace tray { | 18 namespace tray { |
18 | 19 |
19 namespace { | 20 namespace { |
21 | |
20 // Amount of slop to add into the timer to make sure we're into the next minute | 22 // Amount of slop to add into the timer to make sure we're into the next minute |
21 // when the timer goes off. | 23 // when the timer goes off. |
22 const int kTimerSlopSeconds = 1; | 24 const int kTimerSlopSeconds = 1; |
23 | 25 |
24 string16 FormatNicely(const base::Time& time) { | 26 string16 FormatDate(const base::Time& time) { |
25 icu::UnicodeString date_string; | 27 icu::UnicodeString date_string; |
26 | |
27 scoped_ptr<icu::DateFormat> formatter( | 28 scoped_ptr<icu::DateFormat> formatter( |
28 icu::DateFormat::createDateInstance(icu::DateFormat::kFull)); | |
29 icu::FieldPosition position; | |
30 position.setField(UDAT_DAY_OF_WEEK_FIELD); | |
31 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), | |
32 date_string, | |
33 position); | |
34 icu::UnicodeString day = date_string.retainBetween(position.getBeginIndex(), | |
35 position.getEndIndex()); | |
36 | |
37 date_string.remove(); | |
38 formatter.reset( | |
39 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); | 29 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); |
40 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); | 30 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); |
41 | |
42 date_string += "\n"; | |
43 date_string += day; | |
44 | |
45 return string16(date_string.getBuffer(), | 31 return string16(date_string.getBuffer(), |
46 static_cast<size_t>(date_string.length())); | 32 static_cast<size_t>(date_string.length())); |
47 } | 33 } |
48 | 34 |
35 string16 FormatDayOfWeek(const base::Time& time) { | |
36 scoped_ptr<icu::DateFormat> formatter( | |
37 icu::DateFormat::createDateInstance(icu::DateFormat::kFull)); | |
38 icu::UnicodeString date_string; | |
39 icu::FieldPosition position; | |
40 position.setField(UDAT_DAY_OF_WEEK_FIELD); | |
41 formatter->format( | |
42 static_cast<UDate>(time.ToDoubleT() * 1000), date_string, position); | |
43 icu::UnicodeString day = date_string.retainBetween(position.getBeginIndex(), | |
44 position.getEndIndex()); | |
45 return string16(day.getBuffer(), static_cast<size_t>(day.length())); | |
49 } | 46 } |
50 | 47 |
51 DateView::DateView(TimeType type) | 48 views::Label* CreateLabel() { |
52 : hour_type_(ash::Shell::GetInstance()->tray_delegate()-> | 49 views::Label* label = new views::Label; |
53 GetHourClockType()), | 50 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
54 type_(type), | 51 label->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255)); |
55 actionable_(false) { | 52 return label; |
56 // TODO(flackr): Investigate respecting the view's border in FillLayout. | 53 } |
57 SetLayoutManager(new views::BoxLayout( | 54 |
58 views::BoxLayout::kHorizontal, 0, 0, 0)); | 55 } // namespace |
59 label_ = new views::Label; | 56 |
60 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 57 DateView::DateView() : actionable_(false) { |
61 label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255)); | 58 SetLayoutManager( |
62 UpdateText(); | 59 new views::BoxLayout( |
63 AddChildView(label_); | 60 views::BoxLayout::kVertical, 0, 0, kTrayPopupTextSpacingVertical)); |
61 date_label_ = CreateLabel(); | |
62 day_of_week_label_ = CreateLabel(); | |
63 UpdateTextInternal(base::Time::Now()); | |
64 AddChildView(date_label_); | |
65 AddChildView(day_of_week_label_); | |
64 set_focusable(actionable_); | 66 set_focusable(actionable_); |
65 } | 67 } |
66 | 68 |
67 DateView::~DateView() { | 69 DateView::~DateView() { |
68 timer_.Stop(); | |
69 } | |
70 | |
71 void DateView::UpdateTimeFormat() { | |
72 hour_type_ = ash::Shell::GetInstance()->tray_delegate()->GetHourClockType(); | |
73 UpdateText(); | |
74 } | 70 } |
75 | 71 |
76 void DateView::SetActionable(bool actionable) { | 72 void DateView::SetActionable(bool actionable) { |
77 actionable_ = actionable; | 73 actionable_ = actionable; |
78 set_focusable(actionable_); | 74 set_focusable(actionable_); |
79 } | 75 } |
80 | 76 |
81 void DateView::UpdateText() { | 77 void DateView::UpdateTextInternal(const base::Time& now) { |
78 date_label_->SetText(FormatDate(now)); | |
79 day_of_week_label_->SetText(FormatDayOfWeek(now)); | |
80 } | |
81 | |
82 bool DateView::PerformAction(const views::Event& event) { | |
83 if (!actionable_) | |
84 return false; | |
85 | |
86 ash::Shell::GetInstance()->tray_delegate()->ShowDateSettings(); | |
87 return true; | |
88 } | |
89 | |
90 | |
91 TimeView::TimeView() | |
92 : hour_type_( | |
93 ash::Shell::GetInstance()->tray_delegate()->GetHourClockType()) { | |
94 SetLayoutManager( | |
95 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | |
96 label_ = CreateLabel(); | |
97 UpdateTextInternal(base::Time::Now()); | |
98 AddChildView(label_); | |
99 set_focusable(true); | |
100 } | |
101 | |
102 TimeView::~TimeView() { | |
103 } | |
104 | |
105 void TimeView::UpdateTimeFormat() { | |
106 hour_type_ = ash::Shell::GetInstance()->tray_delegate()->GetHourClockType(); | |
107 UpdateText(); | |
108 } | |
109 | |
110 void TimeView::UpdateTextInternal(const base::Time& now) { | |
111 label_->SetText( | |
112 base::TimeFormatTimeOfDayWithHourClockType( | |
113 now, hour_type_, base::kDropAmPm)); | |
114 label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); | |
115 } | |
116 | |
117 bool TimeView::PerformAction(const views::Event& event) { | |
118 return false; | |
119 } | |
120 | |
121 | |
sadrul
2012/04/30 15:05:52
-newline. Perhaps move implementation of BaseDateT
Daniel Erat
2012/04/30 17:12:43
Done.
| |
122 BaseDateTimeView::~BaseDateTimeView() { | |
123 timer_.Stop(); | |
124 } | |
125 | |
126 void BaseDateTimeView::UpdateText() { | |
82 base::Time now = base::Time::Now(); | 127 base::Time now = base::Time::Now(); |
83 gfx::Size old_size = label_->GetPreferredSize(); | 128 gfx::Size old_size = GetPreferredSize(); |
84 if (type_ == DATE) { | 129 UpdateTextInternal(now); |
85 label_->SetText(FormatNicely(now)); | 130 if (GetWidget() && |
86 } else { | 131 GetWidget()->GetContentsView()->GetPreferredSize() != old_size) { |
sadrul
2012/04/30 15:05:52
I believe this should just be 'GetPreferredSize()
Daniel Erat
2012/04/30 17:12:43
Done. It looks like I still need to call SetSize(
| |
87 label_->SetText(base::TimeFormatTimeOfDayWithHourClockType( | 132 // Forcing the widget to the new size is sufficient. The positioning is |
88 now, hour_type_, base::kDropAmPm)); | 133 // taken care of by the layout manager (ShelfLayoutManager). |
89 } | |
90 if (label_->GetPreferredSize() != old_size && GetWidget()) { | |
91 // Forcing the widget to the new size is sufficient. The positing is taken | |
92 // care of by the layout manager (ShelfLayoutManager). | |
93 GetWidget()->SetSize(GetWidget()->GetContentsView()->GetPreferredSize()); | 134 GetWidget()->SetSize(GetWidget()->GetContentsView()->GetPreferredSize()); |
94 } | 135 } |
95 | |
96 label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); | |
97 SchedulePaint(); | 136 SchedulePaint(); |
98 | 137 |
99 // Try to set the timer to go off at the next change of the minute. We don't | 138 // Try to set the timer to go off at the next change of the minute. We don't |
100 // want to have the timer go off more than necessary since that will cause | 139 // want to have the timer go off more than necessary since that will cause |
101 // the CPU to wake up and consume power. | 140 // the CPU to wake up and consume power. |
102 base::Time::Exploded exploded; | 141 base::Time::Exploded exploded; |
103 now.LocalExplode(&exploded); | 142 now.LocalExplode(&exploded); |
104 | 143 |
105 // Often this will be called at minute boundaries, and we'll actually want | 144 // Often this will be called at minute boundaries, and we'll actually want |
106 // 60 seconds from now. | 145 // 60 seconds from now. |
107 int seconds_left = 60 - exploded.second; | 146 int seconds_left = 60 - exploded.second; |
108 if (seconds_left == 0) | 147 if (seconds_left == 0) |
109 seconds_left = 60; | 148 seconds_left = 60; |
110 | 149 |
111 // Make sure that the timer fires on the next minute. Without this, if it is | 150 // Make sure that the timer fires on the next minute. Without this, if it is |
112 // called just a teeny bit early, then it will skip the next minute. | 151 // called just a teeny bit early, then it will skip the next minute. |
113 seconds_left += kTimerSlopSeconds; | 152 seconds_left += kTimerSlopSeconds; |
114 | 153 |
115 timer_.Stop(); | 154 timer_.Stop(); |
116 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | 155 timer_.Start( |
117 &DateView::UpdateText); | 156 FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), |
157 this, &BaseDateTimeView::UpdateText); | |
118 } | 158 } |
119 | 159 |
120 bool DateView::PerformAction(const views::Event& event) { | 160 BaseDateTimeView::BaseDateTimeView() { |
121 if (!actionable_) | |
122 return false; | |
123 | |
124 ash::Shell::GetInstance()->tray_delegate()->ShowDateSettings(); | |
125 return true; | |
126 } | 161 } |
127 | 162 |
128 void DateView::OnLocaleChanged() { | 163 void BaseDateTimeView::OnLocaleChanged() { |
129 UpdateText(); | 164 UpdateText(); |
130 } | 165 } |
131 | 166 |
132 } // namespace tray | 167 } // namespace tray |
133 } // namespace internal | 168 } // namespace internal |
134 } // namespace ash | 169 } // namespace ash |
OLD | NEW |