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