Chromium Code Reviews| 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/logging.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/string16.h" | |
| 19 #include "base/string_number_conversions.h" | |
| 20 #include "base/time.h" | |
| 21 #include "base/utf_string_conversions.h" | |
| 22 #include "grit/ash_strings.h" | |
| 23 #include "third_party/skia/include/core/SkColor.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 #include "ui/gfx/font.h" | |
| 26 #include "ui/gfx/text_constants.h" | |
| 27 #include "ui/views/border.h" | |
| 28 #include "ui/views/controls/label.h" | |
| 29 #include "ui/views/layout/box_layout.h" | |
| 30 #include "ui/views/layout/grid_layout.h" | |
| 31 #include "ui/views/view.h" | |
| 32 | |
| 33 namespace ash { | |
| 34 namespace internal { | |
| 35 | |
| 36 namespace tray { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 // Warning threshold for the remaining sessiont time. | |
| 41 const int kRemainingTimeWarningThresholdInSeconds = 5 * 60; // 5 minutes. | |
| 42 // Color in which the remaining session time is normally shown. | |
| 43 const SkColor kRemainingTimeColor = SK_ColorWHITE; | |
| 44 // Color in which the remaining session time is shown when it falls below the | |
| 45 // warning threshold. | |
| 46 const SkColor kRemainingTimeWarningColor = SK_ColorRED; | |
| 47 | |
| 48 views::Label* CreateAndSetupLabel() { | |
| 49 views::Label* label = new views::Label; | |
| 50 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 51 label->set_owned_by_client(); | |
|
stevenjb
2012/12/14 18:02:21
We really shouldn't be using set_owned_by_client u
bartfab (slow)
2012/12/14 20:11:09
Done.
| |
| 52 SetupLabelForTray(label); | |
| 53 gfx::Font font = label->font(); | |
| 54 label->SetFont(font.DeriveFont(0, font.GetStyle() & ~gfx::Font::BOLD)); | |
| 55 return label; | |
| 56 } | |
| 57 | |
| 58 string16 IntToTwoDigitString(int value) { | |
| 59 DCHECK_GE(value, 0); | |
| 60 DCHECK_LE(value, 99); | |
| 61 if (value < 10) | |
| 62 return ASCIIToUTF16("0") + base::IntToString16(value); | |
| 63 return base::IntToString16(value); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 class RemainingSessionTimeTrayView : public views::View { | |
| 69 public: | |
| 70 RemainingSessionTimeTrayView(bool is_session_length_limited, | |
| 71 const base::TimeDelta& remaining, | |
| 72 bool horizontal_layout); | |
| 73 virtual ~RemainingSessionTimeTrayView(); | |
| 74 | |
| 75 void OnSessionLengthUnlimited(); | |
| 76 void OnRemainingSessionTimeChanged(const base::TimeDelta& remaining); | |
|
stevenjb
2012/12/14 18:02:21
The names of these imply that they are callbacks,
bartfab (slow)
2012/12/14 20:11:09
Done.
| |
| 77 | |
| 78 void UpdateClockLayout(bool horizontal_layout); | |
| 79 | |
| 80 private: | |
| 81 void SetBorder(bool horizontal_layout); | |
| 82 void SetupLabels(); | |
| 83 | |
| 84 scoped_ptr<views::Label> label_; | |
| 85 scoped_ptr<views::Label> label_hours_left_; | |
| 86 scoped_ptr<views::Label> label_hours_right_; | |
| 87 scoped_ptr<views::Label> label_minutes_left_; | |
| 88 scoped_ptr<views::Label> label_minutes_right_; | |
| 89 scoped_ptr<views::Label> label_seconds_left_; | |
| 90 scoped_ptr<views::Label> label_seconds_right_; | |
|
stevenjb
2012/12/14 18:02:21
I see that this pattern is copied from TimeView, b
bartfab (slow)
2012/12/14 20:11:09
Done.
| |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(RemainingSessionTimeTrayView); | |
| 93 }; | |
| 94 | |
| 95 RemainingSessionTimeTrayView::RemainingSessionTimeTrayView( | |
| 96 bool is_session_length_limited, | |
| 97 const base::TimeDelta& remaining, | |
| 98 bool horizontal_layout) { | |
|
stevenjb
2012/12/14 18:02:21
Use ShelfAlignment instead of a bool for layout an
bartfab (slow)
2012/12/14 20:11:09
Done.
| |
| 99 SetupLabels(); | |
| 100 if (is_session_length_limited) | |
| 101 OnRemainingSessionTimeChanged(remaining); | |
| 102 else | |
| 103 OnSessionLengthUnlimited(); | |
| 104 UpdateClockLayout(horizontal_layout); | |
| 105 } | |
| 106 | |
| 107 RemainingSessionTimeTrayView::~RemainingSessionTimeTrayView() { | |
| 108 } | |
| 109 | |
| 110 void RemainingSessionTimeTrayView::OnSessionLengthUnlimited() { | |
| 111 SetVisible(false); | |
| 112 } | |
| 113 | |
| 114 void RemainingSessionTimeTrayView::OnRemainingSessionTimeChanged( | |
| 115 const base::TimeDelta& remaining) { | |
| 116 int seconds = round(remaining.InSecondsF()); | |
| 117 DCHECK_LE(seconds, 24 * 60 * 60); // 24 hours. | |
| 118 int minutes = seconds / 60; | |
| 119 seconds %= 60; | |
| 120 int hours = minutes / 60; | |
| 121 minutes %= 60; | |
| 122 | |
| 123 string16 hours_str = IntToTwoDigitString(hours); | |
| 124 string16 minutes_str = IntToTwoDigitString(minutes); | |
| 125 string16 seconds_str = IntToTwoDigitString(seconds); | |
| 126 | |
| 127 label_->SetText(l10n_util::GetStringFUTF16( | |
| 128 IDS_ASH_STATUS_TRAY_REMAINING_SESSION_TIME, | |
| 129 hours_str, minutes_str, seconds_str)); | |
| 130 label_hours_left_->SetText(hours_str.substr(0, 1)); | |
| 131 label_hours_right_->SetText(hours_str.substr(1, 1)); | |
| 132 label_minutes_left_->SetText(minutes_str.substr(0, 1)); | |
| 133 label_minutes_right_->SetText(minutes_str.substr(1, 1)); | |
| 134 label_seconds_left_->SetText(seconds_str.substr(0, 1)); | |
| 135 label_seconds_right_->SetText(seconds_str.substr(1, 1)); | |
| 136 | |
| 137 const SkColor color = seconds < kRemainingTimeWarningThresholdInSeconds ? | |
| 138 kRemainingTimeWarningColor : kRemainingTimeColor; | |
| 139 label_->SetEnabledColor(color); | |
| 140 label_hours_left_->SetEnabledColor(color); | |
| 141 label_hours_right_->SetEnabledColor(color); | |
| 142 label_minutes_left_->SetEnabledColor(color); | |
| 143 label_minutes_right_->SetEnabledColor(color); | |
| 144 label_seconds_left_->SetEnabledColor(color); | |
| 145 label_seconds_right_->SetEnabledColor(color); | |
| 146 | |
| 147 Layout(); | |
| 148 SetVisible(true); | |
| 149 } | |
| 150 | |
| 151 void RemainingSessionTimeTrayView::UpdateClockLayout(bool horizontal_layout) { | |
| 152 SetBorder(horizontal_layout); | |
| 153 if (horizontal_layout) { | |
| 154 RemoveChildView(label_hours_left_.get()); | |
| 155 RemoveChildView(label_hours_right_.get()); | |
| 156 RemoveChildView(label_minutes_left_.get()); | |
| 157 RemoveChildView(label_minutes_right_.get()); | |
| 158 RemoveChildView(label_seconds_left_.get()); | |
| 159 RemoveChildView(label_seconds_right_.get()); | |
| 160 SetLayoutManager( | |
| 161 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | |
| 162 AddChildView(label_.get()); | |
| 163 } else { | |
| 164 RemoveChildView(label_.get()); | |
| 165 views::GridLayout* layout = new views::GridLayout(this); | |
| 166 SetLayoutManager(layout); | |
| 167 views::ColumnSet* columns = layout->AddColumnSet(0); | |
| 168 columns->AddPaddingColumn(0, 6); | |
| 169 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, | |
| 170 0, views::GridLayout::USE_PREF, 0, 0); | |
| 171 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, | |
| 172 0, views::GridLayout::USE_PREF, 0, 0); | |
| 173 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); | |
| 174 layout->StartRow(0, 0); | |
| 175 layout->AddView(label_hours_left_.get()); | |
| 176 layout->AddView(label_hours_right_.get()); | |
| 177 layout->StartRow(0, 0); | |
| 178 layout->AddView(label_minutes_left_.get()); | |
| 179 layout->AddView(label_minutes_right_.get()); | |
| 180 layout->StartRow(0, 0); | |
| 181 layout->AddView(label_seconds_left_.get()); | |
| 182 layout->AddView(label_seconds_right_.get()); | |
| 183 layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); | |
| 184 } | |
| 185 Layout(); | |
| 186 } | |
| 187 | |
| 188 void RemainingSessionTimeTrayView::SetBorder(bool horizontal_layout) { | |
| 189 if (horizontal_layout) { | |
| 190 set_border(views::Border::CreateEmptyBorder( | |
| 191 0, kTrayLabelItemHorizontalPaddingBottomAlignment, | |
| 192 0, kTrayLabelItemHorizontalPaddingBottomAlignment)); | |
| 193 } else { | |
| 194 set_border(NULL); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 void RemainingSessionTimeTrayView::SetupLabels() { | |
| 199 label_.reset(CreateAndSetupLabel()); | |
| 200 label_hours_left_.reset(CreateAndSetupLabel()); | |
| 201 label_hours_right_.reset(CreateAndSetupLabel()); | |
| 202 label_minutes_left_.reset(CreateAndSetupLabel()); | |
| 203 label_minutes_right_.reset(CreateAndSetupLabel()); | |
| 204 label_seconds_left_.reset(CreateAndSetupLabel()); | |
| 205 label_seconds_right_.reset(CreateAndSetupLabel()); | |
| 206 } | |
| 207 | |
| 208 } // namespace tray | |
| 209 | |
| 210 TraySessionLengthLimit::TraySessionLengthLimit(SystemTray* system_tray) | |
| 211 : SystemTrayItem(system_tray), | |
| 212 tray_view_(NULL) { | |
| 213 Shell::GetInstance()->system_tray_notifier()-> | |
| 214 AddSessionLengthLimitObserver(this); | |
| 215 } | |
| 216 | |
| 217 TraySessionLengthLimit::~TraySessionLengthLimit() { | |
| 218 Shell::GetInstance()->system_tray_notifier()-> | |
| 219 RemoveSessionLengthLimitObserver(this); | |
| 220 } | |
| 221 | |
| 222 views::View* TraySessionLengthLimit::CreateTrayView(user::LoginStatus status) { | |
| 223 CHECK(tray_view_ == NULL); | |
| 224 ash::SystemTrayDelegate* delegate = | |
| 225 ash::Shell::GetInstance()->system_tray_delegate(); | |
| 226 tray_view_ = new tray::RemainingSessionTimeTrayView( | |
| 227 delegate->GetSessionLengthLimited(), | |
| 228 delegate->GetRemainingSessionTime(), | |
| 229 system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM); | |
| 230 return tray_view_; | |
| 231 } | |
| 232 | |
| 233 void TraySessionLengthLimit::DestroyTrayView() { | |
| 234 tray_view_ = NULL; | |
| 235 } | |
| 236 | |
| 237 void TraySessionLengthLimit::UpdateAfterShelfAlignmentChange( | |
| 238 ShelfAlignment alignment) { | |
| 239 if (tray_view_) | |
| 240 tray_view_->UpdateClockLayout(alignment == SHELF_ALIGNMENT_BOTTOM); | |
| 241 } | |
| 242 | |
| 243 void TraySessionLengthLimit::OnSessionLengthUnlimited() { | |
| 244 tray_view_->OnSessionLengthUnlimited(); | |
| 245 } | |
| 246 | |
| 247 void TraySessionLengthLimit::OnRemainingSessionTimeChanged( | |
| 248 const base::TimeDelta& remaining) { | |
| 249 tray_view_->OnRemainingSessionTimeChanged(remaining); | |
| 250 } | |
| 251 | |
| 252 } // namespace internal | |
| 253 } // namespace ash | |
| OLD | NEW |