logout_confirmation_dialog.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "ash/system/session/logout_confirmation_dialog.h"
  5. #include "ash/public/cpp/shell_window_ids.h"
  6. #include "ash/root_window_controller.h"
  7. #include "ash/shell.h"
  8. #include "ash/strings/grit/ash_strings.h"
  9. #include "ash/system/session/logout_confirmation_controller.h"
  10. #include "base/bind.h"
  11. #include "base/location.h"
  12. #include "base/time/tick_clock.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/geometry/rect.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/layout/layout_provider.h"
  22. #include "ui/views/widget/widget.h"
  23. namespace ash {
  24. namespace {
  25. constexpr int kDefaultWidth = 448; // Default width of the dialog.
  26. constexpr int kCountdownUpdateIntervalMs = 1000; // 1 second.
  27. constexpr int kHalfSecondInMs = 500; // Half a second.
  28. } // namespace
  29. LogoutConfirmationDialog::LogoutConfirmationDialog(
  30. LogoutConfirmationController* controller,
  31. base::TimeTicks logout_time)
  32. : controller_(controller), logout_time_(logout_time) {
  33. SetModalType(ui::MODAL_TYPE_SYSTEM);
  34. SetTitle(l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_TITLE));
  35. SetShowCloseButton(false);
  36. SetButtonLabel(ui::DIALOG_BUTTON_OK,
  37. l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_BUTTON));
  38. SetAcceptCallback(base::BindOnce(&LogoutConfirmationDialog::OnDialogAccepted,
  39. base::Unretained(this)));
  40. SetLayoutManager(std::make_unique<views::FillLayout>());
  41. SetBorder(views::CreateEmptyBorder(
  42. views::LayoutProvider::Get()->GetDialogInsetsForContentType(
  43. views::DialogContentType::kText, views::DialogContentType::kText)));
  44. label_ = new views::Label;
  45. label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  46. label_->SetMultiLine(true);
  47. AddChildView(label_);
  48. UpdateLabel();
  49. views::Widget* widget = new views::Widget;
  50. views::Widget::InitParams params =
  51. GetDialogWidgetInitParams(this, nullptr, nullptr, gfx::Rect());
  52. params.parent = Shell::GetPrimaryRootWindow()->GetChildById(
  53. kShellWindowId_SystemModalContainer);
  54. widget->Init(std::move(params));
  55. widget->Show();
  56. update_timer_.Start(FROM_HERE, base::Milliseconds(kCountdownUpdateIntervalMs),
  57. this, &LogoutConfirmationDialog::UpdateLabel);
  58. }
  59. LogoutConfirmationDialog::~LogoutConfirmationDialog() = default;
  60. void LogoutConfirmationDialog::Update(base::TimeTicks logout_time) {
  61. logout_time_ = logout_time;
  62. UpdateLabel();
  63. }
  64. void LogoutConfirmationDialog::ControllerGone() {
  65. controller_ = nullptr;
  66. GetWidget()->Close();
  67. }
  68. void LogoutConfirmationDialog::WindowClosing() {
  69. update_timer_.Stop();
  70. if (controller_)
  71. controller_->OnDialogClosed();
  72. }
  73. gfx::Size LogoutConfirmationDialog::CalculatePreferredSize() const {
  74. return gfx::Size(
  75. kDefaultWidth,
  76. GetLayoutManager()->GetPreferredHeightForWidth(this, kDefaultWidth));
  77. }
  78. const char* LogoutConfirmationDialog::GetClassName() const {
  79. return "LogoutConfirmationDialog";
  80. }
  81. void LogoutConfirmationDialog::UpdateLabel() {
  82. const base::TimeDelta time_remaining =
  83. logout_time_ - controller_->clock()->NowTicks();
  84. if (time_remaining >= base::Milliseconds(kHalfSecondInMs)) {
  85. label_->SetText(l10n_util::GetStringFUTF16(
  86. IDS_ASH_LOGOUT_CONFIRMATION_WARNING,
  87. l10n_util::GetStringFUTF16Int(
  88. IDS_ASH_STATUS_TRAY_NEARBY_SHARE_REMAINING_SECONDS,
  89. (int)time_remaining.InSeconds())));
  90. } else {
  91. label_->SetText(
  92. l10n_util::GetStringUTF16(IDS_ASH_LOGOUT_CONFIRMATION_WARNING_NOW));
  93. update_timer_.Stop();
  94. }
  95. }
  96. void LogoutConfirmationDialog::OnDialogAccepted() {
  97. logout_time_ = controller_->clock()->NowTicks();
  98. UpdateLabel();
  99. controller_->OnLogoutConfirmed();
  100. }
  101. } // namespace ash