| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/system/session_length_limit/tray_session_length_limit.h" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "ash/shelf_types.h" |
| 10 #include "ash/shell.h" |
| 11 #include "ash/system/tray/system_tray.h" |
| 12 #include "ash/system/tray/system_tray_delegate.h" |
| 13 #include "ash/system/tray/system_tray_notifier.h" |
| 14 #include "ash/system/tray/tray_constants.h" |
| 15 #include "ash/system/tray/tray_views.h" |
| 16 #include "base/string16.h" |
| 17 #include "base/string_number_conversions.h" |
| 18 #include "base/time.h" |
| 19 #include "base/timer.h" |
| 20 #include "base/utf_string_conversions.h" |
| 21 #include "grit/ash_strings.h" |
| 22 #include "third_party/skia/include/core/SkColor.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/gfx/font.h" |
| 25 #include "ui/gfx/text_constants.h" |
| 26 #include "ui/views/border.h" |
| 27 #include "ui/views/controls/label.h" |
| 28 #include "ui/views/layout/box_layout.h" |
| 29 #include "ui/views/layout/grid_layout.h" |
| 30 #include "ui/views/view.h" |
| 31 |
| 32 namespace ash { |
| 33 namespace internal { |
| 34 |
| 35 namespace tray { |
| 36 |
| 37 namespace { |
| 38 |
| 39 // Warning threshold for the remaining sessiont time. |
| 40 const int kRemainingTimeWarningThresholdInSeconds = 5 * 60; // 5 minutes. |
| 41 // Color in which the remaining session time is normally shown. |
| 42 const SkColor kRemainingTimeColor = SK_ColorWHITE; |
| 43 // Color in which the remaining session time is shown when it falls below the |
| 44 // warning threshold. |
| 45 const SkColor kRemainingTimeWarningColor = SK_ColorRED; |
| 46 |
| 47 views::Label* CreateAndSetupLabel() { |
| 48 views::Label* label = new views::Label; |
| 49 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 50 SetupLabelForTray(label); |
| 51 gfx::Font font = label->font(); |
| 52 label->SetFont(font.DeriveFont(0, font.GetStyle() & ~gfx::Font::BOLD)); |
| 53 return label; |
| 54 } |
| 55 |
| 56 string16 IntToTwoDigitString(int value) { |
| 57 DCHECK_GE(value, 0); |
| 58 DCHECK_LE(value, 99); |
| 59 if (value < 10) |
| 60 return ASCIIToUTF16("0") + base::IntToString16(value); |
| 61 return base::IntToString16(value); |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
| 66 class RemainingSessionTimeTrayView : public views::View { |
| 67 public: |
| 68 RemainingSessionTimeTrayView(const base::Time& session_start_time, |
| 69 const base::TimeDelta& limit, |
| 70 ShelfAlignment shelf_alignment); |
| 71 virtual ~RemainingSessionTimeTrayView(); |
| 72 |
| 73 void SetSessionStartTime(const base::Time& session_start_time); |
| 74 void SetSessionLengthLimit(const base::TimeDelta& limit); |
| 75 |
| 76 void UpdateClockLayout(ShelfAlignment shelf_alignment); |
| 77 |
| 78 private: |
| 79 void SetBorder(ShelfAlignment shelf_alignment); |
| 80 |
| 81 // Update the label text only. |
| 82 void UpdateText(); |
| 83 // Update the timer state, label text and visibility. |
| 84 void UpdateState(); |
| 85 |
| 86 views::Label* horizontal_layout_label_; |
| 87 views::Label* vertical_layout_label_hours_left_; |
| 88 views::Label* vertical_layout_label_hours_right_; |
| 89 views::Label* vertical_layout_label_minutes_left_; |
| 90 views::Label* vertical_layout_label_minutes_right_; |
| 91 views::Label* vertical_layout_label_seconds_left_; |
| 92 views::Label* vertical_layout_label_seconds_right_; |
| 93 |
| 94 base::Time session_start_time_; |
| 95 base::TimeDelta limit_; |
| 96 base::RepeatingTimer<RemainingSessionTimeTrayView> timer_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(RemainingSessionTimeTrayView); |
| 99 }; |
| 100 |
| 101 RemainingSessionTimeTrayView::RemainingSessionTimeTrayView( |
| 102 const base::Time& session_start_time, |
| 103 const base::TimeDelta& limit, |
| 104 ShelfAlignment shelf_alignment) |
| 105 : horizontal_layout_label_(NULL), |
| 106 vertical_layout_label_hours_left_(NULL), |
| 107 vertical_layout_label_hours_right_(NULL), |
| 108 vertical_layout_label_minutes_left_(NULL), |
| 109 vertical_layout_label_minutes_right_(NULL), |
| 110 vertical_layout_label_seconds_left_(NULL), |
| 111 vertical_layout_label_seconds_right_(NULL), |
| 112 session_start_time_(session_start_time), |
| 113 limit_(limit) { |
| 114 UpdateClockLayout(shelf_alignment); |
| 115 UpdateState(); |
| 116 } |
| 117 |
| 118 RemainingSessionTimeTrayView::~RemainingSessionTimeTrayView() { |
| 119 } |
| 120 |
| 121 void RemainingSessionTimeTrayView::SetSessionStartTime( |
| 122 const base::Time& session_start_time) { |
| 123 session_start_time_ = session_start_time; |
| 124 UpdateState(); |
| 125 } |
| 126 |
| 127 void RemainingSessionTimeTrayView::SetSessionLengthLimit( |
| 128 const base::TimeDelta& limit) { |
| 129 limit_ = limit; |
| 130 UpdateState(); |
| 131 } |
| 132 |
| 133 void RemainingSessionTimeTrayView::UpdateClockLayout( |
| 134 ShelfAlignment shelf_alignment) { |
| 135 SetBorder(shelf_alignment); |
| 136 const bool horizontal_layout = shelf_alignment == SHELF_ALIGNMENT_BOTTOM; |
| 137 if (horizontal_layout && !horizontal_layout_label_) { |
| 138 // Remove labels used for vertical layout. |
| 139 RemoveAllChildViews(true); |
| 140 vertical_layout_label_hours_left_ = NULL; |
| 141 vertical_layout_label_hours_right_ = NULL; |
| 142 vertical_layout_label_minutes_left_ = NULL; |
| 143 vertical_layout_label_minutes_right_ = NULL; |
| 144 vertical_layout_label_seconds_left_ = NULL; |
| 145 vertical_layout_label_seconds_right_ = NULL; |
| 146 |
| 147 // Create label used for horizontal layout. |
| 148 horizontal_layout_label_ = CreateAndSetupLabel(); |
| 149 |
| 150 // Construct layout. |
| 151 SetLayoutManager( |
| 152 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
| 153 AddChildView(horizontal_layout_label_); |
| 154 |
| 155 } else if (!horizontal_layout && horizontal_layout_label_) { |
| 156 // Remove label used for horizontal layout. |
| 157 RemoveAllChildViews(true); |
| 158 horizontal_layout_label_ = NULL; |
| 159 |
| 160 // Create labels used for vertical layout. |
| 161 vertical_layout_label_hours_left_ = CreateAndSetupLabel(); |
| 162 vertical_layout_label_hours_right_ = CreateAndSetupLabel(); |
| 163 vertical_layout_label_minutes_left_ = CreateAndSetupLabel(); |
| 164 vertical_layout_label_minutes_right_ = CreateAndSetupLabel(); |
| 165 vertical_layout_label_seconds_left_ = CreateAndSetupLabel(); |
| 166 vertical_layout_label_seconds_right_ = CreateAndSetupLabel(); |
| 167 |
| 168 // Construct layout. |
| 169 views::GridLayout* layout = new views::GridLayout(this); |
| 170 SetLayoutManager(layout); |
| 171 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 172 columns->AddPaddingColumn(0, 6); |
| 173 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, |
| 174 0, views::GridLayout::USE_PREF, 0, 0); |
| 175 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, |
| 176 0, views::GridLayout::USE_PREF, 0, 0); |
| 177 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); |
| 178 layout->StartRow(0, 0); |
| 179 layout->AddView(vertical_layout_label_hours_left_); |
| 180 layout->AddView(vertical_layout_label_hours_right_); |
| 181 layout->StartRow(0, 0); |
| 182 layout->AddView(vertical_layout_label_minutes_left_); |
| 183 layout->AddView(vertical_layout_label_minutes_right_); |
| 184 layout->StartRow(0, 0); |
| 185 layout->AddView(vertical_layout_label_seconds_left_); |
| 186 layout->AddView(vertical_layout_label_seconds_right_); |
| 187 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); |
| 188 } |
| 189 UpdateText(); |
| 190 } |
| 191 |
| 192 void RemainingSessionTimeTrayView::SetBorder(ShelfAlignment shelf_alignment) { |
| 193 if (shelf_alignment == SHELF_ALIGNMENT_BOTTOM) { |
| 194 set_border(views::Border::CreateEmptyBorder( |
| 195 0, kTrayLabelItemHorizontalPaddingBottomAlignment, |
| 196 0, kTrayLabelItemHorizontalPaddingBottomAlignment)); |
| 197 } else { |
| 198 set_border(NULL); |
| 199 } |
| 200 } |
| 201 |
| 202 void RemainingSessionTimeTrayView::UpdateText() { |
| 203 if (!visible()) |
| 204 return; |
| 205 |
| 206 // Calculate the remaining session time, clamping so that it never falls below |
| 207 // zero or exceeds 99 hours. |
| 208 int seconds = |
| 209 round((limit_ - (base::Time::Now() - session_start_time_)).InSecondsF()); |
| 210 seconds = std::min(std::max(seconds, 0), 99 * 60 * 60); |
| 211 int minutes = seconds / 60; |
| 212 seconds %= 60; |
| 213 const int hours = minutes / 60; |
| 214 minutes %= 60; |
| 215 |
| 216 const string16 hours_str = IntToTwoDigitString(hours); |
| 217 const string16 minutes_str = IntToTwoDigitString(minutes); |
| 218 const string16 seconds_str = IntToTwoDigitString(seconds); |
| 219 const SkColor color = seconds < kRemainingTimeWarningThresholdInSeconds ? |
| 220 kRemainingTimeWarningColor : kRemainingTimeColor; |
| 221 |
| 222 if (horizontal_layout_label_) { |
| 223 horizontal_layout_label_->SetText(l10n_util::GetStringFUTF16( |
| 224 IDS_ASH_STATUS_TRAY_REMAINING_SESSION_TIME, |
| 225 hours_str, minutes_str, seconds_str)); |
| 226 horizontal_layout_label_->SetEnabledColor(color); |
| 227 } else if (vertical_layout_label_hours_left_) { |
| 228 vertical_layout_label_hours_left_->SetText(hours_str.substr(0, 1)); |
| 229 vertical_layout_label_hours_right_->SetText(hours_str.substr(1, 1)); |
| 230 vertical_layout_label_minutes_left_->SetText(minutes_str.substr(0, 1)); |
| 231 vertical_layout_label_minutes_right_->SetText(minutes_str.substr(1, 1)); |
| 232 vertical_layout_label_seconds_left_->SetText(seconds_str.substr(0, 1)); |
| 233 vertical_layout_label_seconds_right_->SetText(seconds_str.substr(1, 1)); |
| 234 vertical_layout_label_hours_left_->SetEnabledColor(color); |
| 235 vertical_layout_label_hours_right_->SetEnabledColor(color); |
| 236 vertical_layout_label_minutes_left_->SetEnabledColor(color); |
| 237 vertical_layout_label_minutes_right_->SetEnabledColor(color); |
| 238 vertical_layout_label_seconds_left_->SetEnabledColor(color); |
| 239 vertical_layout_label_seconds_right_->SetEnabledColor(color); |
| 240 } |
| 241 |
| 242 Layout(); |
| 243 } |
| 244 |
| 245 void RemainingSessionTimeTrayView::UpdateState() { |
| 246 const bool show = session_start_time_ != base::Time() && |
| 247 limit_ != base::TimeDelta(); |
| 248 SetVisible(show); |
| 249 UpdateText(); |
| 250 if (show && !timer_.IsRunning()) { |
| 251 // Set timer to update the text once per second. |
| 252 timer_.Start(FROM_HERE, |
| 253 base::TimeDelta::FromSeconds(1), |
| 254 this, |
| 255 &RemainingSessionTimeTrayView::UpdateText); |
| 256 } else if (!show && timer_.IsRunning()) { |
| 257 timer_.Stop(); |
| 258 } |
| 259 } |
| 260 |
| 261 } // namespace tray |
| 262 |
| 263 TraySessionLengthLimit::TraySessionLengthLimit(SystemTray* system_tray) |
| 264 : SystemTrayItem(system_tray), |
| 265 tray_view_(NULL) { |
| 266 Shell::GetInstance()->system_tray_notifier()-> |
| 267 AddSessionLengthLimitObserver(this); |
| 268 } |
| 269 |
| 270 TraySessionLengthLimit::~TraySessionLengthLimit() { |
| 271 Shell::GetInstance()->system_tray_notifier()-> |
| 272 RemoveSessionLengthLimitObserver(this); |
| 273 } |
| 274 |
| 275 views::View* TraySessionLengthLimit::CreateTrayView(user::LoginStatus status) { |
| 276 CHECK(tray_view_ == NULL); |
| 277 ash::SystemTrayDelegate* delegate = |
| 278 ash::Shell::GetInstance()->system_tray_delegate(); |
| 279 tray_view_ = new tray::RemainingSessionTimeTrayView( |
| 280 delegate->GetSessionStartTime(), |
| 281 delegate->GetSessionLengthLimit(), |
| 282 system_tray()->shelf_alignment()); |
| 283 return tray_view_; |
| 284 } |
| 285 |
| 286 void TraySessionLengthLimit::DestroyTrayView() { |
| 287 tray_view_ = NULL; |
| 288 } |
| 289 |
| 290 void TraySessionLengthLimit::UpdateAfterShelfAlignmentChange( |
| 291 ShelfAlignment alignment) { |
| 292 if (tray_view_) |
| 293 tray_view_->UpdateClockLayout(alignment); |
| 294 } |
| 295 |
| 296 void TraySessionLengthLimit::OnSessionStartTimeChanged( |
| 297 const base::Time& start_time) { |
| 298 tray_view_->SetSessionStartTime(start_time); |
| 299 } |
| 300 |
| 301 void TraySessionLengthLimit::OnSessionLengthLimitChanged( |
| 302 const base::TimeDelta& limit) { |
| 303 tray_view_->SetSessionLengthLimit(limit); |
| 304 } |
| 305 |
| 306 } // namespace internal |
| 307 } // namespace ash |
| OLD | NEW |