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.
5 #include "ash/system/chromeos/session/logout_confirmation_dialog.h"
8 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
9 #include "ash/system/tray/tray_constants.h"
10 #include "base/location.h"
11 #include "base/time/tick_clock.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/l10n/time_format.h"
15 #include "ui/base/ui_base_types.h"
16 #include "ui/gfx/text_constants.h"
17 #include "ui/views/border.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/fill_layout.h"
20 #include "ui/views/widget/widget.h"
25 const int kCountdownUpdateIntervalMs
= 1000; // 1 second.
27 const int kHalfSecondInMs
= 500; // Half a second.
31 LogoutConfirmationDialog::LogoutConfirmationDialog(
32 LogoutConfirmationController
* controller
,
33 base::TimeTicks logout_time
)
34 : controller_(controller
),
35 logout_time_(logout_time
) {
36 SetLayoutManager(new views::FillLayout());
38 label_
= new views::Label
;
39 label_
->SetBorder(views::Border::CreateEmptyBorder(
40 0, kTrayPopupPaddingHorizontal
, 0, kTrayPopupPaddingHorizontal
));
41 label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
42 label_
->SetMultiLine(true);
47 CreateDialogWidget(this, ash::Shell::GetPrimaryRootWindow(), NULL
);
52 base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs
),
54 &LogoutConfirmationDialog::UpdateLabel
);
57 LogoutConfirmationDialog::~LogoutConfirmationDialog() {
60 void LogoutConfirmationDialog::Update(base::TimeTicks logout_time
) {
61 logout_time_
= logout_time
;
65 bool LogoutConfirmationDialog::Accept() {
66 logout_time_
= controller_
->clock()->NowTicks();
68 controller_
->OnLogoutConfirmed();
72 ui::ModalType
LogoutConfirmationDialog::GetModalType() const {
73 return ui::MODAL_TYPE_SYSTEM
;
76 base::string16
LogoutConfirmationDialog::GetWindowTitle() const {
77 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE
);
80 base::string16
LogoutConfirmationDialog::GetDialogButtonLabel(
81 ui::DialogButton button
) const {
82 if (button
== ui::DIALOG_BUTTON_OK
)
83 return l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON
);
84 return views::DialogDelegateView::GetDialogButtonLabel(button
);
87 void LogoutConfirmationDialog::OnClosed() {
89 controller_
->OnDialogClosed();
92 void LogoutConfirmationDialog::DeleteDelegate() {
96 void LogoutConfirmationDialog::UpdateLabel() {
97 const base::TimeDelta time_remaining
=
98 logout_time_
- controller_
->clock()->NowTicks();
99 if (time_remaining
>= base::TimeDelta::FromMilliseconds(kHalfSecondInMs
)) {
100 label_
->SetText(l10n_util::GetStringFUTF16(
101 IDS_ASH_LOGOUT_CONFIRMATION_WARNING
,
102 ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION
,
103 ui::TimeFormat::LENGTH_LONG
,
107 label_
->SetText(l10n_util::GetStringUTF16(
108 IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW
));
109 update_timer_
.Stop();