unified_keyboard_brightness_slider_controller.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2018 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/unified_keyboard_brightness_slider_controller.h"
  5. #include <memory>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/constants/personalization_entry_point.h"
  8. #include "ash/public/cpp/new_window_delegate.h"
  9. #include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
  10. #include "ash/resources/vector_icons/vector_icons.h"
  11. #include "ash/rgb_keyboard/rgb_keyboard_manager.h"
  12. #include "ash/rgb_keyboard/rgb_keyboard_util.h"
  13. #include "ash/session/session_controller_impl.h"
  14. #include "ash/shell.h"
  15. #include "ash/strings/grit/ash_strings.h"
  16. #include "ash/style/ash_color_provider.h"
  17. #include "ash/system/keyboard_brightness/keyboard_backlight_color_controller.h"
  18. #include "ash/system/unified/unified_system_tray_model.h"
  19. #include "ash/webui/personalization_app/mojom/personalization_app.mojom-forward.h"
  20. #include "base/bind.h"
  21. #include "base/memory/weak_ptr.h"
  22. #include "base/metrics/histogram_functions.h"
  23. #include "chromeos/dbus/power/power_manager_client.h"
  24. #include "ui/base/resource/resource_bundle.h"
  25. #include "ui/gfx/color_utils.h"
  26. namespace ash {
  27. namespace {
  28. // Only applicable when rgb keyboard is supported.
  29. const SkColor keyboardBrightnessIconBackgroundColor =
  30. SkColorSetRGB(138, 180, 248);
  31. class UnifiedKeyboardBrightnessView : public UnifiedSliderView,
  32. public UnifiedSystemTrayModel::Observer {
  33. public:
  34. UnifiedKeyboardBrightnessView(
  35. UnifiedKeyboardBrightnessSliderController* controller,
  36. UnifiedSystemTrayModel* model)
  37. : UnifiedSliderView(views::Button::PressedCallback(),
  38. controller,
  39. kUnifiedMenuKeyboardBrightnessIcon,
  40. IDS_ASH_STATUS_TRAY_BRIGHTNESS,
  41. true /* readonly*/),
  42. model_(model) {
  43. if (features::IsRgbKeyboardEnabled() &&
  44. Shell::Get()->rgb_keyboard_manager()->IsRgbKeyboardSupported() &&
  45. features::IsPersonalizationHubEnabled()) {
  46. button()->SetBackgroundColor(keyboardBrightnessIconBackgroundColor);
  47. AddChildView(CreateKeyboardBacklightColorButton());
  48. }
  49. model_->AddObserver(this);
  50. OnKeyboardBrightnessChanged(
  51. power_manager::BacklightBrightnessChange_Cause_OTHER);
  52. }
  53. UnifiedKeyboardBrightnessView(const UnifiedKeyboardBrightnessView&) = delete;
  54. UnifiedKeyboardBrightnessView& operator=(
  55. const UnifiedKeyboardBrightnessView&) = delete;
  56. ~UnifiedKeyboardBrightnessView() override { model_->RemoveObserver(this); }
  57. // UnifiedSystemTrayModel::Observer:
  58. void OnKeyboardBrightnessChanged(
  59. power_manager::BacklightBrightnessChange_Cause cause) override {
  60. SetSliderValue(
  61. model_->keyboard_brightness(),
  62. cause == power_manager::BacklightBrightnessChange_Cause_USER_REQUEST);
  63. }
  64. private:
  65. std::unique_ptr<views::ImageButton> CreateKeyboardBacklightColorButton() {
  66. auto button = std::make_unique<IconButton>(
  67. base::BindRepeating(
  68. &UnifiedKeyboardBrightnessView::OnKeyboardBacklightColorIconPressed,
  69. weak_factory_.GetWeakPtr()),
  70. IconButton::Type::kSmall, &kUnifiedMenuKeyboardBacklightIcon,
  71. IDS_ASH_STATUS_TRAY_KEYBOARD_BACKLIGHT_ACCESSIBLE_NAME);
  72. personalization_app::mojom::BacklightColor backlight_color =
  73. Shell::Get()->keyboard_backlight_color_controller()->GetBacklightColor(
  74. Shell::Get()->session_controller()->GetActiveAccountId());
  75. if (backlight_color ==
  76. personalization_app::mojom::BacklightColor::kRainbow) {
  77. ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  78. auto* image =
  79. rb.GetImageSkiaNamed(IDR_SETTINGS_RGB_KEYBOARD_RAINBOW_COLOR_48_PNG);
  80. button->SetBackgroundImage(*image);
  81. button->SetIconColor(gfx::kGoogleGrey900);
  82. } else {
  83. SkColor color =
  84. ConvertBacklightColorToIconBackgroundColor(backlight_color);
  85. button->SetBackgroundColor(color);
  86. button->SetIconColor(color_utils::GetLuma(color) < 125
  87. ? gfx::kGoogleGrey200
  88. : gfx::kGoogleGrey900);
  89. }
  90. button->SetBorder(views::CreateRoundedRectBorder(
  91. /*thickness=*/4, /*corner_radius=*/16,
  92. AshColorProvider::Get()->GetContentLayerColor(
  93. AshColorProvider::ContentLayerType::kSeparatorColor)));
  94. return button;
  95. }
  96. void OnKeyboardBacklightColorIconPressed() {
  97. // Record entry point metric to Personalization Hub.
  98. base::UmaHistogramEnumeration(
  99. kPersonalizationEntryPointHistogramName,
  100. PersonalizationEntryPoint::kKeyboardBrightnessSlider);
  101. NewWindowDelegate* primary_delegate = NewWindowDelegate::GetPrimary();
  102. primary_delegate->OpenPersonalizationHub();
  103. return;
  104. }
  105. UnifiedSystemTrayModel* const model_;
  106. base::WeakPtrFactory<UnifiedKeyboardBrightnessView> weak_factory_{this};
  107. };
  108. } // namespace
  109. UnifiedKeyboardBrightnessSliderController::
  110. UnifiedKeyboardBrightnessSliderController(UnifiedSystemTrayModel* model)
  111. : model_(model) {}
  112. UnifiedKeyboardBrightnessSliderController::
  113. ~UnifiedKeyboardBrightnessSliderController() = default;
  114. views::View* UnifiedKeyboardBrightnessSliderController::CreateView() {
  115. DCHECK(!slider_);
  116. slider_ = new UnifiedKeyboardBrightnessView(this, model_);
  117. return slider_;
  118. }
  119. void UnifiedKeyboardBrightnessSliderController::SliderValueChanged(
  120. views::Slider* sender,
  121. float value,
  122. float old_value,
  123. views::SliderChangeReason reason) {
  124. // This slider is read-only.
  125. }
  126. } // namespace ash