| 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. | |
| 57 SetLayoutManager(new views::BoxLayout( | |
| 58 views::BoxLayout::kHorizontal, 0, 0, 0)); | |
| 59 label_ = new views::Label; | |
| 60 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 61 label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255)); | |
| 62 UpdateText(); | |
| 63 AddChildView(label_); | |
| 64 set_focusable(actionable_); | |
| 65 } | 53 } |
| 66 | 54 |
| 67 DateView::~DateView() { | 55 } // namespace |
| 56 |
| 57 BaseDateTimeView::~BaseDateTimeView() { |
| 68 timer_.Stop(); | 58 timer_.Stop(); |
| 69 } | 59 } |
| 70 | 60 |
| 71 void DateView::UpdateTimeFormat() { | 61 void BaseDateTimeView::UpdateText() { |
| 72 hour_type_ = ash::Shell::GetInstance()->tray_delegate()->GetHourClockType(); | |
| 73 UpdateText(); | |
| 74 } | |
| 75 | |
| 76 void DateView::SetActionable(bool actionable) { | |
| 77 actionable_ = actionable; | |
| 78 set_focusable(actionable_); | |
| 79 } | |
| 80 | |
| 81 void DateView::UpdateText() { | |
| 82 base::Time now = base::Time::Now(); | 62 base::Time now = base::Time::Now(); |
| 83 gfx::Size old_size = label_->GetPreferredSize(); | 63 gfx::Size old_size = GetPreferredSize(); |
| 84 if (type_ == DATE) { | 64 UpdateTextInternal(now); |
| 85 label_->SetText(FormatNicely(now)); | 65 if (GetWidget() && GetPreferredSize() != old_size) { |
| 86 } else { | 66 // Forcing the widget to the new size is sufficient. The positioning is |
| 87 label_->SetText(base::TimeFormatTimeOfDayWithHourClockType( | 67 // taken care of by the layout manager (ShelfLayoutManager). |
| 88 now, hour_type_, base::kDropAmPm)); | 68 GetWidget()->SetSize(GetPreferredSize()); |
| 89 } | 69 } |
| 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()); | |
| 94 } | |
| 95 | |
| 96 label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); | |
| 97 SchedulePaint(); | 70 SchedulePaint(); |
| 98 | 71 |
| 99 // Try to set the timer to go off at the next change of the minute. We don't | 72 // 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 | 73 // want to have the timer go off more than necessary since that will cause |
| 101 // the CPU to wake up and consume power. | 74 // the CPU to wake up and consume power. |
| 102 base::Time::Exploded exploded; | 75 base::Time::Exploded exploded; |
| 103 now.LocalExplode(&exploded); | 76 now.LocalExplode(&exploded); |
| 104 | 77 |
| 105 // Often this will be called at minute boundaries, and we'll actually want | 78 // Often this will be called at minute boundaries, and we'll actually want |
| 106 // 60 seconds from now. | 79 // 60 seconds from now. |
| 107 int seconds_left = 60 - exploded.second; | 80 int seconds_left = 60 - exploded.second; |
| 108 if (seconds_left == 0) | 81 if (seconds_left == 0) |
| 109 seconds_left = 60; | 82 seconds_left = 60; |
| 110 | 83 |
| 111 // Make sure that the timer fires on the next minute. Without this, if it is | 84 // 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. | 85 // called just a teeny bit early, then it will skip the next minute. |
| 113 seconds_left += kTimerSlopSeconds; | 86 seconds_left += kTimerSlopSeconds; |
| 114 | 87 |
| 115 timer_.Stop(); | 88 timer_.Stop(); |
| 116 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), this, | 89 timer_.Start( |
| 117 &DateView::UpdateText); | 90 FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), |
| 91 this, &BaseDateTimeView::UpdateText); |
| 92 } |
| 93 |
| 94 BaseDateTimeView::BaseDateTimeView() { |
| 95 } |
| 96 |
| 97 void BaseDateTimeView::OnLocaleChanged() { |
| 98 UpdateText(); |
| 99 } |
| 100 |
| 101 DateView::DateView() : actionable_(false) { |
| 102 SetLayoutManager( |
| 103 new views::BoxLayout( |
| 104 views::BoxLayout::kVertical, 0, 0, kTrayPopupTextSpacingVertical)); |
| 105 date_label_ = CreateLabel(); |
| 106 day_of_week_label_ = CreateLabel(); |
| 107 UpdateTextInternal(base::Time::Now()); |
| 108 AddChildView(date_label_); |
| 109 AddChildView(day_of_week_label_); |
| 110 set_focusable(actionable_); |
| 111 } |
| 112 |
| 113 DateView::~DateView() { |
| 114 } |
| 115 |
| 116 void DateView::SetActionable(bool actionable) { |
| 117 actionable_ = actionable; |
| 118 set_focusable(actionable_); |
| 119 } |
| 120 |
| 121 void DateView::UpdateTextInternal(const base::Time& now) { |
| 122 date_label_->SetText(FormatDate(now)); |
| 123 day_of_week_label_->SetText(FormatDayOfWeek(now)); |
| 118 } | 124 } |
| 119 | 125 |
| 120 bool DateView::PerformAction(const views::Event& event) { | 126 bool DateView::PerformAction(const views::Event& event) { |
| 121 if (!actionable_) | 127 if (!actionable_) |
| 122 return false; | 128 return false; |
| 123 | 129 |
| 124 ash::Shell::GetInstance()->tray_delegate()->ShowDateSettings(); | 130 ash::Shell::GetInstance()->tray_delegate()->ShowDateSettings(); |
| 125 return true; | 131 return true; |
| 126 } | 132 } |
| 127 | 133 |
| 128 void DateView::OnLocaleChanged() { | 134 TimeView::TimeView() |
| 135 : hour_type_( |
| 136 ash::Shell::GetInstance()->tray_delegate()->GetHourClockType()) { |
| 137 SetLayoutManager( |
| 138 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
| 139 label_ = CreateLabel(); |
| 140 UpdateTextInternal(base::Time::Now()); |
| 141 AddChildView(label_); |
| 142 set_focusable(true); |
| 143 } |
| 144 |
| 145 TimeView::~TimeView() { |
| 146 } |
| 147 |
| 148 void TimeView::UpdateTimeFormat() { |
| 149 hour_type_ = ash::Shell::GetInstance()->tray_delegate()->GetHourClockType(); |
| 129 UpdateText(); | 150 UpdateText(); |
| 130 } | 151 } |
| 131 | 152 |
| 153 void TimeView::UpdateTextInternal(const base::Time& now) { |
| 154 label_->SetText( |
| 155 base::TimeFormatTimeOfDayWithHourClockType( |
| 156 now, hour_type_, base::kDropAmPm)); |
| 157 label_->SetTooltipText(base::TimeFormatFriendlyDate(now)); |
| 158 } |
| 159 |
| 160 bool TimeView::PerformAction(const views::Event& event) { |
| 161 return false; |
| 162 } |
| 163 |
| 164 |
| 132 } // namespace tray | 165 } // namespace tray |
| 133 } // namespace internal | 166 } // namespace internal |
| 134 } // namespace ash | 167 } // namespace ash |
| OLD | NEW |