keyboard_backlight_color_nudge_controller.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2022 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/keyboard_brightness/keyboard_backlight_color_nudge_controller.h"
  5. #include "ash/bubble/bubble_constants.h"
  6. #include "ash/controls/contextual_nudge.h"
  7. #include "ash/controls/contextual_tooltip.h"
  8. #include "ash/public/cpp/shelf_config.h"
  9. #include "ash/public/cpp/shell_window_ids.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/shell.h"
  12. #include "ash/strings/grit/ash_strings.h"
  13. #include "ash/style/ash_color_provider.h"
  14. #include "base/bind.h"
  15. #include "base/callback_helpers.h"
  16. #include "chromeos/strings/grit/chromeos_strings.h"
  17. #include "components/prefs/pref_service.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. #include "ui/compositor/layer.h"
  20. namespace ash {
  21. namespace {
  22. const int kEducationBubblePreferredWidth = 300;
  23. PrefService* GetActivePrefService() {
  24. return Shell::Get()->session_controller()->GetActivePrefService();
  25. }
  26. } // namespace
  27. KeyboardBacklightColorNudgeController::KeyboardBacklightColorNudgeController() =
  28. default;
  29. KeyboardBacklightColorNudgeController::
  30. ~KeyboardBacklightColorNudgeController() = default;
  31. // static
  32. bool KeyboardBacklightColorNudgeController::ShouldShowWallpaperColorNudge() {
  33. return contextual_tooltip::ShouldShowNudge(
  34. GetActivePrefService(),
  35. contextual_tooltip::TooltipType::kKeyboardBacklightWallpaperColor,
  36. /*recheck_delay=*/nullptr);
  37. }
  38. // static
  39. void KeyboardBacklightColorNudgeController::HandleWallpaperColorNudgeShown() {
  40. HandleNudgeShown(
  41. GetActivePrefService(),
  42. contextual_tooltip::TooltipType::kKeyboardBacklightWallpaperColor);
  43. }
  44. void KeyboardBacklightColorNudgeController::MaybeShowEducationNudge(
  45. views::View* keyboard_brightness_slider_view) {
  46. if (!keyboard_brightness_slider_view)
  47. return;
  48. if (education_nudge_)
  49. return;
  50. if (!contextual_tooltip::ShouldShowNudge(
  51. GetActivePrefService(),
  52. contextual_tooltip::TooltipType::kKeyboardBacklightColor,
  53. /*recheck_delay=*/nullptr)) {
  54. return;
  55. }
  56. // The nudge is anchored on the top right of
  57. // |keyboard_brightness_slider_view|. It owns itself and destructed when
  58. // closed.
  59. education_nudge_ = new ContextualNudge(
  60. /*anchor=*/nullptr,
  61. Shell::GetContainer(Shell::GetPrimaryRootWindow(),
  62. kShellWindowId_SettingBubbleContainer),
  63. ContextualNudge::Position::kTop, gfx::Insets(16),
  64. l10n_util::GetStringFUTF16(
  65. IDS_ASH_KEYBOARD_BACKLIGHT_COLOR_EDUCATION_NUDGE_TEXT,
  66. l10n_util::GetStringUTF16(
  67. IDS_PERSONALIZATION_APP_PERSONALIZATION_HUB_TITLE)),
  68. AshColorProvider::Get()->GetContentLayerColor(
  69. AshColorProvider::ContentLayerType::kTextColorPrimary),
  70. base::BindRepeating(
  71. &KeyboardBacklightColorNudgeController::CloseEducationNudge,
  72. weak_factory_.GetWeakPtr()));
  73. views::Label* label = education_nudge_->label();
  74. label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  75. label->SetMultiLine(true);
  76. label->SizeToFit(kEducationBubblePreferredWidth);
  77. education_nudge_->SetPaintToLayer(ui::LAYER_SOLID_COLOR);
  78. ui::Layer* layer = education_nudge_->layer();
  79. layer->SetColor(ShelfConfig::Get()->GetDefaultShelfColor());
  80. layer->SetRoundedCornerRadius(
  81. gfx::RoundedCornersF{static_cast<float>(kBubbleCornerRadius)});
  82. layer->SetBackgroundBlur(ColorProvider::kBackgroundBlurSigma);
  83. gfx::Rect anchor_rect =
  84. keyboard_brightness_slider_view->GetAnchorBoundsInScreen();
  85. // Update the anchor rect and add a vertical gap between the anchor view and
  86. // nudge.
  87. education_nudge_->UpdateAnchorRect(
  88. gfx::Rect(anchor_rect.x(), anchor_rect.y() - 8, anchor_rect.width(),
  89. anchor_rect.height()));
  90. education_nudge_->SetArrow(views::BubbleBorder::BOTTOM_RIGHT);
  91. education_nudge_->GetWidget()->Show();
  92. contextual_tooltip::HandleNudgeShown(
  93. GetActivePrefService(),
  94. contextual_tooltip::TooltipType::kKeyboardBacklightColor);
  95. StartAutoCloseTimer();
  96. }
  97. void KeyboardBacklightColorNudgeController::CloseEducationNudge() {
  98. autoclose_.Stop();
  99. if (!education_nudge_ || education_nudge_->GetWidget()->IsClosed()) {
  100. return;
  101. }
  102. education_nudge_->GetWidget()->Close();
  103. education_nudge_ = nullptr;
  104. }
  105. void KeyboardBacklightColorNudgeController::SetUserPerformedAction() {
  106. // Indicate the user has selected a color so we won't show the education nudge
  107. // anymore.
  108. contextual_tooltip::HandleGesturePerformed(
  109. Shell::Get()->session_controller()->GetActivePrefService(),
  110. contextual_tooltip::TooltipType::kKeyboardBacklightColor);
  111. }
  112. void KeyboardBacklightColorNudgeController::StartAutoCloseTimer() {
  113. autoclose_.Stop();
  114. base::TimeDelta nudge_duration = contextual_tooltip::GetNudgeTimeout(
  115. GetActivePrefService(),
  116. contextual_tooltip::TooltipType::kKeyboardBacklightColor);
  117. if (!nudge_duration.is_zero()) {
  118. autoclose_.Start(
  119. FROM_HERE, nudge_duration, this,
  120. &KeyboardBacklightColorNudgeController::CloseEducationNudge);
  121. } else {
  122. CloseEducationNudge();
  123. }
  124. }
  125. } // namespace ash