Chromium Code Reviews| Index: ash/system/session_length_limit/tray_session_length_limit.cc |
| diff --git a/ash/system/session_length_limit/tray_session_length_limit.cc b/ash/system/session_length_limit/tray_session_length_limit.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..388e46c396c438cdea869db308dc6d328f6123dc |
| --- /dev/null |
| +++ b/ash/system/session_length_limit/tray_session_length_limit.cc |
| @@ -0,0 +1,253 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/system/session_length_limit/tray_session_length_limit.h" |
| + |
| +#include <cmath> |
| + |
| +#include "ash/shelf_types.h" |
| +#include "ash/shell.h" |
| +#include "ash/system/tray/system_tray.h" |
| +#include "ash/system/tray/system_tray_delegate.h" |
| +#include "ash/system/tray/system_tray_notifier.h" |
| +#include "ash/system/tray/tray_constants.h" |
| +#include "ash/system/tray/tray_views.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/string16.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "grit/ash_strings.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/gfx/font.h" |
| +#include "ui/gfx/text_constants.h" |
| +#include "ui/views/border.h" |
| +#include "ui/views/controls/label.h" |
| +#include "ui/views/layout/box_layout.h" |
| +#include "ui/views/layout/grid_layout.h" |
| +#include "ui/views/view.h" |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +namespace tray { |
| + |
| +namespace { |
| + |
| +// Warning threshold for the remaining sessiont time. |
| +const int kRemainingTimeWarningThresholdInSeconds = 5 * 60; // 5 minutes. |
| +// Color in which the remaining session time is normally shown. |
| +const SkColor kRemainingTimeColor = SK_ColorWHITE; |
| +// Color in which the remaining session time is shown when it falls below the |
| +// warning threshold. |
| +const SkColor kRemainingTimeWarningColor = SK_ColorRED; |
| + |
| +views::Label* CreateAndSetupLabel() { |
| + views::Label* label = new views::Label; |
| + label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| + 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.
|
| + SetupLabelForTray(label); |
| + gfx::Font font = label->font(); |
| + label->SetFont(font.DeriveFont(0, font.GetStyle() & ~gfx::Font::BOLD)); |
| + return label; |
| +} |
| + |
| +string16 IntToTwoDigitString(int value) { |
| + DCHECK_GE(value, 0); |
| + DCHECK_LE(value, 99); |
| + if (value < 10) |
| + return ASCIIToUTF16("0") + base::IntToString16(value); |
| + return base::IntToString16(value); |
| +} |
| + |
| +} // namespace |
| + |
| +class RemainingSessionTimeTrayView : public views::View { |
| + public: |
| + RemainingSessionTimeTrayView(bool is_session_length_limited, |
| + const base::TimeDelta& remaining, |
| + bool horizontal_layout); |
| + virtual ~RemainingSessionTimeTrayView(); |
| + |
| + void OnSessionLengthUnlimited(); |
| + 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.
|
| + |
| + void UpdateClockLayout(bool horizontal_layout); |
| + |
| + private: |
| + void SetBorder(bool horizontal_layout); |
| + void SetupLabels(); |
| + |
| + scoped_ptr<views::Label> label_; |
| + scoped_ptr<views::Label> label_hours_left_; |
| + scoped_ptr<views::Label> label_hours_right_; |
| + scoped_ptr<views::Label> label_minutes_left_; |
| + scoped_ptr<views::Label> label_minutes_right_; |
| + scoped_ptr<views::Label> label_seconds_left_; |
| + 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.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(RemainingSessionTimeTrayView); |
| +}; |
| + |
| +RemainingSessionTimeTrayView::RemainingSessionTimeTrayView( |
| + bool is_session_length_limited, |
| + const base::TimeDelta& remaining, |
| + 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.
|
| + SetupLabels(); |
| + if (is_session_length_limited) |
| + OnRemainingSessionTimeChanged(remaining); |
| + else |
| + OnSessionLengthUnlimited(); |
| + UpdateClockLayout(horizontal_layout); |
| +} |
| + |
| +RemainingSessionTimeTrayView::~RemainingSessionTimeTrayView() { |
| +} |
| + |
| +void RemainingSessionTimeTrayView::OnSessionLengthUnlimited() { |
| + SetVisible(false); |
| +} |
| + |
| +void RemainingSessionTimeTrayView::OnRemainingSessionTimeChanged( |
| + const base::TimeDelta& remaining) { |
| + int seconds = round(remaining.InSecondsF()); |
| + DCHECK_LE(seconds, 24 * 60 * 60); // 24 hours. |
| + int minutes = seconds / 60; |
| + seconds %= 60; |
| + int hours = minutes / 60; |
| + minutes %= 60; |
| + |
| + string16 hours_str = IntToTwoDigitString(hours); |
| + string16 minutes_str = IntToTwoDigitString(minutes); |
| + string16 seconds_str = IntToTwoDigitString(seconds); |
| + |
| + label_->SetText(l10n_util::GetStringFUTF16( |
| + IDS_ASH_STATUS_TRAY_REMAINING_SESSION_TIME, |
| + hours_str, minutes_str, seconds_str)); |
| + label_hours_left_->SetText(hours_str.substr(0, 1)); |
| + label_hours_right_->SetText(hours_str.substr(1, 1)); |
| + label_minutes_left_->SetText(minutes_str.substr(0, 1)); |
| + label_minutes_right_->SetText(minutes_str.substr(1, 1)); |
| + label_seconds_left_->SetText(seconds_str.substr(0, 1)); |
| + label_seconds_right_->SetText(seconds_str.substr(1, 1)); |
| + |
| + const SkColor color = seconds < kRemainingTimeWarningThresholdInSeconds ? |
| + kRemainingTimeWarningColor : kRemainingTimeColor; |
| + label_->SetEnabledColor(color); |
| + label_hours_left_->SetEnabledColor(color); |
| + label_hours_right_->SetEnabledColor(color); |
| + label_minutes_left_->SetEnabledColor(color); |
| + label_minutes_right_->SetEnabledColor(color); |
| + label_seconds_left_->SetEnabledColor(color); |
| + label_seconds_right_->SetEnabledColor(color); |
| + |
| + Layout(); |
| + SetVisible(true); |
| +} |
| + |
| +void RemainingSessionTimeTrayView::UpdateClockLayout(bool horizontal_layout) { |
| + SetBorder(horizontal_layout); |
| + if (horizontal_layout) { |
| + RemoveChildView(label_hours_left_.get()); |
| + RemoveChildView(label_hours_right_.get()); |
| + RemoveChildView(label_minutes_left_.get()); |
| + RemoveChildView(label_minutes_right_.get()); |
| + RemoveChildView(label_seconds_left_.get()); |
| + RemoveChildView(label_seconds_right_.get()); |
| + SetLayoutManager( |
| + new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
| + AddChildView(label_.get()); |
| + } else { |
| + RemoveChildView(label_.get()); |
| + views::GridLayout* layout = new views::GridLayout(this); |
| + SetLayoutManager(layout); |
| + views::ColumnSet* columns = layout->AddColumnSet(0); |
| + columns->AddPaddingColumn(0, 6); |
| + columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, |
| + 0, views::GridLayout::USE_PREF, 0, 0); |
| + columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, |
| + 0, views::GridLayout::USE_PREF, 0, 0); |
| + layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); |
| + layout->StartRow(0, 0); |
| + layout->AddView(label_hours_left_.get()); |
| + layout->AddView(label_hours_right_.get()); |
| + layout->StartRow(0, 0); |
| + layout->AddView(label_minutes_left_.get()); |
| + layout->AddView(label_minutes_right_.get()); |
| + layout->StartRow(0, 0); |
| + layout->AddView(label_seconds_left_.get()); |
| + layout->AddView(label_seconds_right_.get()); |
| + layout->AddPaddingRow(0, kTrayLabelItemVerticalPaddingVeriticalAlignment); |
| + } |
| + Layout(); |
| +} |
| + |
| +void RemainingSessionTimeTrayView::SetBorder(bool horizontal_layout) { |
| + if (horizontal_layout) { |
| + set_border(views::Border::CreateEmptyBorder( |
| + 0, kTrayLabelItemHorizontalPaddingBottomAlignment, |
| + 0, kTrayLabelItemHorizontalPaddingBottomAlignment)); |
| + } else { |
| + set_border(NULL); |
| + } |
| +} |
| + |
| +void RemainingSessionTimeTrayView::SetupLabels() { |
| + label_.reset(CreateAndSetupLabel()); |
| + label_hours_left_.reset(CreateAndSetupLabel()); |
| + label_hours_right_.reset(CreateAndSetupLabel()); |
| + label_minutes_left_.reset(CreateAndSetupLabel()); |
| + label_minutes_right_.reset(CreateAndSetupLabel()); |
| + label_seconds_left_.reset(CreateAndSetupLabel()); |
| + label_seconds_right_.reset(CreateAndSetupLabel()); |
| +} |
| + |
| +} // namespace tray |
| + |
| +TraySessionLengthLimit::TraySessionLengthLimit(SystemTray* system_tray) |
| + : SystemTrayItem(system_tray), |
| + tray_view_(NULL) { |
| + Shell::GetInstance()->system_tray_notifier()-> |
| + AddSessionLengthLimitObserver(this); |
| +} |
| + |
| +TraySessionLengthLimit::~TraySessionLengthLimit() { |
| + Shell::GetInstance()->system_tray_notifier()-> |
| + RemoveSessionLengthLimitObserver(this); |
| +} |
| + |
| +views::View* TraySessionLengthLimit::CreateTrayView(user::LoginStatus status) { |
| + CHECK(tray_view_ == NULL); |
| + ash::SystemTrayDelegate* delegate = |
| + ash::Shell::GetInstance()->system_tray_delegate(); |
| + tray_view_ = new tray::RemainingSessionTimeTrayView( |
| + delegate->GetSessionLengthLimited(), |
| + delegate->GetRemainingSessionTime(), |
| + system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM); |
| + return tray_view_; |
| +} |
| + |
| +void TraySessionLengthLimit::DestroyTrayView() { |
| + tray_view_ = NULL; |
| +} |
| + |
| +void TraySessionLengthLimit::UpdateAfterShelfAlignmentChange( |
| + ShelfAlignment alignment) { |
| + if (tray_view_) |
| + tray_view_->UpdateClockLayout(alignment == SHELF_ALIGNMENT_BOTTOM); |
| +} |
| + |
| +void TraySessionLengthLimit::OnSessionLengthUnlimited() { |
| + tray_view_->OnSessionLengthUnlimited(); |
| +} |
| + |
| +void TraySessionLengthLimit::OnRemainingSessionTimeChanged( |
| + const base::TimeDelta& remaining) { |
| + tray_view_->OnRemainingSessionTimeChanged(remaining); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |