fixup! Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / ash / display / display_change_dialog.cc
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.
4
5 #include "ash/display/display_change_dialog.h"
6
7 #include "ash/public/cpp/shell_window_ids.h"
8 #include "ash/session/session_controller_impl.h"
9 #include "ash/shell.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"
22
23 namespace ash {
24
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));
37
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);
43
44   SetLayoutManager(std::make_unique<views::FillLayout>());
45   SetBorder(views::CreateEmptyBorder(
46       views::LayoutProvider::Get()->GetDialogInsetsForContentType(
47           views::DialogContentType::kText, views::DialogContentType::kText)));
48   label_ =
49       AddChildView(std::make_unique<views::Label>(GetRevertTimeoutString()));
50   label_->SetMultiLine(true);
51
52   views::Widget* widget = CreateDialogWidget(
53       this, nullptr,
54       Shell::GetContainer(Shell::GetPrimaryRootWindow(),
55                           kShellWindowId_SystemModalContainer));
56   // TODO(baileyberro): Verify behavior in kiosk mode.
57   widget->Show();
58
59   timer_.Start(FROM_HERE, base::Seconds(1), this,
60                &DisplayChangeDialog::OnTimerTick);
61 }
62
63 DisplayChangeDialog::~DisplayChangeDialog() = default;
64
65 void DisplayChangeDialog::OnConfirmButtonClicked() {
66   timer_.Stop();
67   std::move(on_accept_callback_).Run();
68 }
69
70 void DisplayChangeDialog::OnCancelButtonClicked() {
71   timer_.Stop();
72   std::move(on_cancel_callback_).Run(/*display_was_removed=*/false);
73 }
74
75 gfx::Size DisplayChangeDialog::CalculatePreferredSize() const {
76   return gfx::Size(350, 100);
77 }
78
79 void DisplayChangeDialog::OnTimerTick() {
80   if (--timeout_count_ == 0) {
81     CancelDialog();
82     return;
83   }
84
85   label_->SetText(GetRevertTimeoutString());
86 }
87
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);
94 }
95
96 base::WeakPtr<DisplayChangeDialog> DisplayChangeDialog::GetWeakPtr() {
97   return weak_factory_.GetWeakPtr();
98 }
99
100 }  // namespace ash