1 // Copyright 2019 The Chromium Authors
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/display/display_change_dialog.h"
7 #include "ash/public/cpp/shell_window_ids.h"
8 #include "ash/session/session_controller_impl.h"
10 #include "ash/strings/grit/ash_strings.h"
11 #include "base/functional/bind.h"
12 #include "base/strings/strcat.h"
13 #include "base/strings/string_util.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/views/border.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/fill_layout.h"
20 #include "ui/views/layout/layout_provider.h"
21 #include "ui/views/widget/widget.h"
25 DisplayChangeDialog::DisplayChangeDialog(
26 std::u16string window_title,
27 std::u16string timeout_message_with_placeholder,
28 base::OnceClosure on_accept_callback,
29 CancelCallback on_cancel_callback)
30 : timeout_message_with_placeholder_(
31 std::move(timeout_message_with_placeholder)),
32 on_accept_callback_(std::move(on_accept_callback)),
33 on_cancel_callback_(std::move(on_cancel_callback)) {
34 SetTitle(window_title);
35 SetButtonLabel(ui::DIALOG_BUTTON_OK,
36 l10n_util::GetStringUTF16(IDS_ASH_CONFIRM_BUTTON));
38 SetAcceptCallback(base::BindOnce(&DisplayChangeDialog::OnConfirmButtonClicked,
39 base::Unretained(this)));
40 SetCancelCallback(base::BindOnce(&DisplayChangeDialog::OnCancelButtonClicked,
41 base::Unretained(this)));
42 SetModalType(ui::MODAL_TYPE_SYSTEM);
44 SetLayoutManager(std::make_unique<views::FillLayout>());
45 SetBorder(views::CreateEmptyBorder(
46 views::LayoutProvider::Get()->GetDialogInsetsForContentType(
47 views::DialogContentType::kText, views::DialogContentType::kText)));
49 AddChildView(std::make_unique<views::Label>(GetRevertTimeoutString()));
50 label_->SetMultiLine(true);
52 views::Widget* widget = CreateDialogWidget(
54 Shell::GetContainer(Shell::GetPrimaryRootWindow(),
55 kShellWindowId_SystemModalContainer));
56 // TODO(baileyberro): Verify behavior in kiosk mode.
59 timer_.Start(FROM_HERE, base::Seconds(1), this,
60 &DisplayChangeDialog::OnTimerTick);
63 DisplayChangeDialog::~DisplayChangeDialog() = default;
65 void DisplayChangeDialog::OnConfirmButtonClicked() {
67 std::move(on_accept_callback_).Run();
70 void DisplayChangeDialog::OnCancelButtonClicked() {
72 std::move(on_cancel_callback_).Run(/*display_was_removed=*/false);
75 gfx::Size DisplayChangeDialog::CalculatePreferredSize() const {
76 return gfx::Size(350, 100);
79 void DisplayChangeDialog::OnTimerTick() {
80 if (--timeout_count_ == 0) {
85 label_->SetText(GetRevertTimeoutString());
88 std::u16string DisplayChangeDialog::GetRevertTimeoutString() const {
89 const std::u16string timer = ui::TimeFormat::Simple(
90 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
91 base::Seconds(timeout_count_));
92 return base::ReplaceStringPlaceholders(timeout_message_with_placeholder_,
93 timer, /*offset=*/nullptr);
96 base::WeakPtr<DisplayChangeDialog> DisplayChangeDialog::GetWeakPtr() {
97 return weak_factory_.GetWeakPtr();