teleport_warning_dialog.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 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. #include "ash/session/teleport_warning_dialog.h"
  5. #include "ash/shell.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "base/bind.h"
  8. #include "ui/base/l10n/l10n_util.h"
  9. #include "ui/base/resource/resource_bundle.h"
  10. #include "ui/views/border.h"
  11. #include "ui/views/controls/button/checkbox.h"
  12. #include "ui/views/controls/label.h"
  13. #include "ui/views/layout/box_layout.h"
  14. #include "ui/views/layout/layout_provider.h"
  15. #include "ui/views/widget/widget.h"
  16. namespace ash {
  17. namespace {
  18. // Default width of the dialog.
  19. constexpr int kDefaultWidth = 448;
  20. } // namespace
  21. TeleportWarningDialog::TeleportWarningDialog(OnAcceptCallback callback)
  22. : never_show_again_checkbox_(new views::Checkbox(
  23. l10n_util::GetStringUTF16(IDS_ASH_DIALOG_DONT_SHOW_AGAIN))),
  24. on_accept_(std::move(callback)) {
  25. never_show_again_checkbox_->SetChecked(true);
  26. SetShowCloseButton(false);
  27. SetModalType(ui::MODAL_TYPE_SYSTEM);
  28. SetTitle(l10n_util::GetStringUTF16(IDS_ASH_TELEPORT_WARNING_TITLE));
  29. SetAcceptCallback(base::BindOnce(
  30. [](TeleportWarningDialog* dialog) {
  31. std::move(dialog->on_accept_)
  32. .Run(true, dialog->never_show_again_checkbox_->GetChecked());
  33. },
  34. base::Unretained(this)));
  35. SetCancelCallback(base::BindOnce(
  36. [](TeleportWarningDialog* dialog) {
  37. std::move(dialog->on_accept_).Run(false, false);
  38. },
  39. base::Unretained(this)));
  40. }
  41. TeleportWarningDialog::~TeleportWarningDialog() = default;
  42. // static
  43. void TeleportWarningDialog::Show(OnAcceptCallback callback) {
  44. TeleportWarningDialog* dialog_view =
  45. new TeleportWarningDialog(std::move(callback));
  46. dialog_view->InitDialog();
  47. views::DialogDelegate::CreateDialogWidget(
  48. dialog_view, Shell::GetRootWindowForNewWindows(), nullptr);
  49. views::Widget* widget = dialog_view->GetWidget();
  50. DCHECK(widget);
  51. widget->Show();
  52. }
  53. gfx::Size TeleportWarningDialog::CalculatePreferredSize() const {
  54. return gfx::Size(
  55. kDefaultWidth,
  56. GetLayoutManager()->GetPreferredHeightForWidth(this, kDefaultWidth));
  57. }
  58. void TeleportWarningDialog::InitDialog() {
  59. const views::LayoutProvider* provider = views::LayoutProvider::Get();
  60. SetBorder(views::CreateEmptyBorder(provider->GetDialogInsetsForContentType(
  61. views::DialogContentType::kText, views::DialogContentType::kControl)));
  62. SetLayoutManager(std::make_unique<views::BoxLayout>(
  63. views::BoxLayout::Orientation::kVertical, gfx::Insets(),
  64. provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));
  65. // Explanation string
  66. views::Label* label = new views::Label(
  67. l10n_util::GetStringUTF16(IDS_ASH_TELEPORT_WARNING_MESSAGE));
  68. label->SetMultiLine(true);
  69. label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  70. AddChildView(label);
  71. AddChildView(never_show_again_checkbox_);
  72. }
  73. } // namespace ash