OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/logout_button/logout_confirmation_dialog_view.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "ash/shell.h" | |
10 #include "ash/system/logout_button/logout_button_tray.h" | |
11 #include "ash/system/tray/system_tray_delegate.h" | |
12 #include "ash/system/tray/tray_constants.h" | |
13 #include "base/location.h" | |
14 #include "grit/ash_strings.h" | |
15 #include "ui/aura/root_window.h" | |
16 #include "ui/base/l10n/l10n_util.h" | |
17 #include "ui/base/l10n/time_format.h" | |
18 #include "ui/base/ui_base_types.h" | |
19 #include "ui/gfx/text_constants.h" | |
20 #include "ui/views/border.h" | |
21 #include "ui/views/controls/label.h" | |
22 #include "ui/views/layout/fill_layout.h" | |
23 #include "ui/views/widget/widget.h" | |
24 | |
25 namespace ash { | |
26 namespace internal { | |
27 | |
28 namespace { | |
29 const int kCountdownUpdateIntervalMs = 1000; // 1 second. | |
30 } // namespace | |
31 | |
32 LogoutConfirmationDialogView::Delegate::Delegate() { | |
33 } | |
34 | |
35 LogoutConfirmationDialogView::Delegate::~Delegate() { | |
36 } | |
37 | |
38 void LogoutConfirmationDialogView::Delegate::LogoutCurrentUser() { | |
39 Shell::GetInstance()->system_tray_delegate()->SignOut(); | |
stevenjb
2013/12/18 21:55:20
Generally a Delegate shouldn't have a default impl
binjin
2013/12/19 16:30:39
Done.
| |
40 } | |
41 | |
42 base::TimeTicks LogoutConfirmationDialogView::Delegate::GetCurrentTime() const { | |
43 return base::TimeTicks::Now(); | |
44 } | |
45 | |
46 LogoutConfirmationDialogView::LogoutConfirmationDialogView( | |
47 LogoutButtonTray* owner, Delegate* delegate) : owner_(owner), | |
48 delegate_(delegate) { | |
49 text_label_ = new views::Label; | |
50 text_label_->set_border( | |
51 views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, | |
52 0, kTrayPopupPaddingHorizontal)); | |
53 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
54 text_label_->SetMultiLine(true); | |
55 | |
56 SetLayoutManager(new views::FillLayout()); | |
57 | |
58 AddChildView(text_label_); | |
59 } | |
60 | |
61 LogoutConfirmationDialogView::~LogoutConfirmationDialogView() { | |
62 } | |
63 | |
64 bool LogoutConfirmationDialogView::Accept() { | |
65 LogoutCurrentUser(); | |
66 return true; | |
67 } | |
68 | |
69 ui::ModalType LogoutConfirmationDialogView::GetModalType() const { | |
70 return ui::MODAL_TYPE_SYSTEM; | |
71 } | |
72 | |
73 string16 LogoutConfirmationDialogView::GetWindowTitle() const { | |
74 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE); | |
75 } | |
76 | |
77 string16 LogoutConfirmationDialogView::GetDialogButtonLabel( | |
78 ui::DialogButton button) const { | |
79 if (button == ui::DIALOG_BUTTON_OK) | |
80 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON); | |
81 return views::DialogDelegateView::GetDialogButtonLabel(button); | |
82 } | |
83 | |
84 void LogoutConfirmationDialogView::OnClosed() { | |
85 owner_->ReleaseConfirmationDialog(); | |
86 owner_ = NULL; | |
87 timer_.Stop(); | |
88 // Nullify the delegate to prevent future activities of the dialog. | |
89 delegate_ = NULL; | |
90 } | |
91 | |
92 void LogoutConfirmationDialogView::DeleteDelegate() { | |
93 if (owner_) | |
94 owner_->ReleaseConfirmationDialog(); | |
95 delete this; | |
96 } | |
97 | |
98 void LogoutConfirmationDialogView::Show(base::TimeDelta duration) { | |
99 if (!delegate_) | |
100 return; | |
101 countdown_start_time_ = delegate_->GetCurrentTime(); | |
102 duration_ = duration; | |
103 | |
104 UpdateCountdown(); | |
105 | |
106 // For testing purpose, only actually display the dialog if an existing | |
107 // ash::Shell is available. | |
stevenjb
2013/12/18 21:55:20
Tests shouldn't generally influence the implementa
binjin
2013/12/19 16:30:39
Done.
| |
108 if (ash::Shell::HasInstance()) { | |
109 views::DialogDelegate::CreateDialogWidget( | |
110 this, ash::Shell::GetPrimaryRootWindow(), NULL); | |
111 GetWidget()->Show(); | |
112 } | |
113 | |
114 timer_.Start(FROM_HERE, | |
115 base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs), | |
116 this, | |
117 &LogoutConfirmationDialogView::UpdateCountdown); | |
118 } | |
119 | |
120 void LogoutConfirmationDialogView::UpdateDialogDuration( | |
121 base::TimeDelta duration) { | |
122 duration_ = duration; | |
123 UpdateCountdown(); | |
124 } | |
125 | |
126 void LogoutConfirmationDialogView::LogoutCurrentUser() { | |
127 if (!delegate_) | |
128 return; | |
129 delegate_->LogoutCurrentUser(); | |
130 } | |
131 | |
132 void LogoutConfirmationDialogView::UpdateCountdown() { | |
133 if (!delegate_) | |
134 return; | |
135 const base::TimeDelta time_remaining = countdown_start_time_ + | |
136 duration_ - delegate_->GetCurrentTime(); | |
137 // Round the remaining time to nearest second, and use this value for | |
138 // the countdown display and actual enforcement. | |
139 int seconds_remaining = round(time_remaining.InSecondsF()); | |
140 if (seconds_remaining > 0) { | |
141 text_label_->SetText(l10n_util::GetStringFUTF16( | |
142 IDS_ASH_LOGOUT_CONFIRMATION_WARNING, | |
143 ui::TimeFormat::TimeDurationLong(base::TimeDelta::FromSeconds( | |
144 seconds_remaining)))); | |
145 } else { | |
146 text_label_->SetText(l10n_util::GetStringUTF16( | |
147 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW)); | |
148 timer_.Stop(); | |
149 LogoutCurrentUser(); | |
150 } | |
151 } | |
152 | |
153 } // namespace internal | |
154 } // namespace ash | |
OLD | NEW |