remove_query_confirmation_dialog.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2019 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/app_list/views/remove_query_confirmation_dialog.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/accessibility/accessibility_controller_impl.h"
  8. #include "ash/public/cpp/ash_typography.h"
  9. #include "ash/public/cpp/view_shadow.h"
  10. #include "ash/shell.h"
  11. #include "ash/strings/grit/ash_strings.h"
  12. #include "ash/style/ash_color_provider.h"
  13. #include "ash/style/pill_button.h"
  14. #include "base/bind.h"
  15. #include "ui/accessibility/ax_node_data.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/compositor/layer.h"
  18. #include "ui/gfx/geometry/insets.h"
  19. #include "ui/strings/grit/ui_strings.h"
  20. #include "ui/views/accessibility/view_accessibility.h"
  21. #include "ui/views/background.h"
  22. #include "ui/views/controls/label.h"
  23. #include "ui/views/highlight_border.h"
  24. #include "ui/views/layout/box_layout.h"
  25. #include "ui/views/style/typography.h"
  26. #include "ui/views/view_class_properties.h"
  27. #include "ui/views/widget/widget.h"
  28. namespace ash {
  29. namespace {
  30. constexpr int kDialogWidth = 360;
  31. constexpr gfx::Insets kDialogContentInsets = gfx::Insets::VH(20, 24);
  32. constexpr float kDialogRoundedCornerRadius = 16.0f;
  33. constexpr int kDialogShadowElevation = 3;
  34. constexpr int kMarginBetweenTitleAndBody = 8;
  35. constexpr int kMarginBetweenBodyAndButtons = 20;
  36. constexpr int kMarginBetweenButtons = 8;
  37. } // namespace
  38. RemoveQueryConfirmationDialog::RemoveQueryConfirmationDialog(
  39. RemovalConfirmationCallback confirm_callback,
  40. const std::u16string& result_title)
  41. : confirm_callback_(std::move(confirm_callback)) {
  42. SetModalType(ui::MODAL_TYPE_WINDOW);
  43. SetPaintToLayer();
  44. layer()->SetBackgroundBlur(ColorProvider::kBackgroundBlurSigma);
  45. layer()->SetBackdropFilterQuality(ColorProvider::kBackgroundBlurQuality);
  46. view_shadow_ = std::make_unique<ViewShadow>(this, kDialogShadowElevation);
  47. view_shadow_->SetRoundedCornerRadius(kDialogRoundedCornerRadius);
  48. SetLayoutManager(std::make_unique<views::BoxLayout>(
  49. views::BoxLayout::Orientation::kVertical, kDialogContentInsets));
  50. // Add dialog title.
  51. title_ = AddChildView(std::make_unique<views::Label>(
  52. l10n_util::GetStringUTF16(IDS_REMOVE_ZERO_STATE_SUGGESTION_TITLE)));
  53. title_->SetTextContext(views::style::CONTEXT_DIALOG_TITLE);
  54. title_->SetTextStyle(views::style::STYLE_EMPHASIZED);
  55. title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  56. title_->SetAutoColorReadabilityEnabled(false);
  57. // Needs to paint to layer so it's stacked above `this` view.
  58. title_->SetPaintToLayer();
  59. title_->layer()->SetFillsBoundsOpaquely(false);
  60. // Ignore labels for accessibility - the accessible name is defined for the
  61. // whole dialog view.
  62. title_->GetViewAccessibility().OverrideIsIgnored(true);
  63. // Add dialog body.
  64. body_ =
  65. AddChildView(std::make_unique<views::Label>(l10n_util::GetStringFUTF16(
  66. IDS_REMOVE_ZERO_STATE_SUGGESTION_DETAILS, result_title)));
  67. body_->SetProperty(views::kMarginsKey,
  68. gfx::Insets::TLBR(kMarginBetweenTitleAndBody, 0,
  69. kMarginBetweenBodyAndButtons, 0));
  70. body_->SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT);
  71. body_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  72. body_->SetMultiLine(true);
  73. body_->SetAllowCharacterBreak(true);
  74. body_->SetAutoColorReadabilityEnabled(false);
  75. // Needs to paint to layer so it's stacked above `this` view.
  76. body_->SetPaintToLayer();
  77. body_->layer()->SetFillsBoundsOpaquely(false);
  78. // Ignore labels for accessibility - the accessible name is defined for the
  79. // whole dialog view.
  80. body_->GetViewAccessibility().OverrideIsIgnored(true);
  81. auto run_callback = [](RemoveQueryConfirmationDialog* dialog, bool accept) {
  82. if (!dialog->confirm_callback_)
  83. return;
  84. if (accept) {
  85. Shell::Get()
  86. ->accessibility_controller()
  87. ->TriggerAccessibilityAlertWithMessage(
  88. l10n_util::GetStringUTF8(IDS_REMOVE_SUGGESTION_ANNOUNCEMENT));
  89. }
  90. std::move(dialog->confirm_callback_).Run(accept);
  91. dialog->GetWidget()->CloseWithReason(
  92. accept ? views::Widget::ClosedReason::kAcceptButtonClicked
  93. : views::Widget::ClosedReason::kCancelButtonClicked);
  94. };
  95. // Add button row.
  96. auto* button_row = AddChildView(std::make_unique<views::View>());
  97. button_row
  98. ->SetLayoutManager(std::make_unique<views::BoxLayout>(
  99. views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
  100. kMarginBetweenButtons))
  101. ->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kEnd);
  102. cancel_button_ = button_row->AddChildView(std::make_unique<ash::PillButton>(
  103. views::Button::PressedCallback(
  104. base::BindRepeating(run_callback, base::Unretained(this), false)),
  105. l10n_util::GetStringUTF16(IDS_APP_CANCEL), PillButton::Type::kIconless,
  106. nullptr));
  107. accept_button_ = button_row->AddChildView(std::make_unique<ash::PillButton>(
  108. views::Button::PressedCallback(
  109. base::BindRepeating(run_callback, base::Unretained(this), true)),
  110. l10n_util::GetStringUTF16(IDS_REMOVE_SUGGESTION_BUTTON_LABEL),
  111. PillButton::Type::kIconlessProminent, nullptr));
  112. }
  113. RemoveQueryConfirmationDialog::~RemoveQueryConfirmationDialog() = default;
  114. const char* RemoveQueryConfirmationDialog::GetClassName() const {
  115. return "RemoveQueryConfirmationDialog";
  116. }
  117. gfx::Size RemoveQueryConfirmationDialog::CalculatePreferredSize() const {
  118. const int default_width = kDialogWidth;
  119. return gfx::Size(default_width, GetHeightForWidth(default_width));
  120. }
  121. void RemoveQueryConfirmationDialog::OnThemeChanged() {
  122. views::WidgetDelegateView::OnThemeChanged();
  123. SetBackground(views::CreateRoundedRectBackground(
  124. AshColorProvider::Get()->GetBaseLayerColor(
  125. AshColorProvider::BaseLayerType::kTransparent80),
  126. kDialogRoundedCornerRadius));
  127. SetBorder(std::make_unique<views::HighlightBorder>(
  128. kDialogRoundedCornerRadius,
  129. views::HighlightBorder::Type::kHighlightBorder1,
  130. /*use_light_colors=*/false));
  131. title_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  132. AshColorProvider::ContentLayerType::kTextColorPrimary));
  133. body_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  134. AshColorProvider::ContentLayerType::kTextColorPrimary));
  135. }
  136. void RemoveQueryConfirmationDialog::GetAccessibleNodeData(
  137. ui::AXNodeData* node_data) {
  138. if (!GetVisible())
  139. return;
  140. node_data->role = ax::mojom::Role::kAlertDialog;
  141. node_data->SetDefaultActionVerb(ax::mojom::DefaultActionVerb::kClick);
  142. node_data->SetName(base::JoinString(
  143. {l10n_util::GetStringUTF16(IDS_REMOVE_ZERO_STATE_SUGGESTION_TITLE),
  144. l10n_util::GetStringUTF16(IDS_REMOVE_ZERO_STATE_SUGGESTION_DETAILS)},
  145. u", "));
  146. }
  147. } // namespace ash