keyboard_backlight_toggle_controller.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021 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_toggle_controller.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/system/tray/tray_popup_utils.h"
  9. #include "ash/system/unified/unified_slider_view.h"
  10. #include "ash/system/unified/unified_system_tray_model.h"
  11. #include "ui/base/l10n/l10n_util.h"
  12. #include "ui/views/controls/label.h"
  13. namespace ash {
  14. namespace {
  15. class UnifiedKeyboardBacklightToggleView
  16. : public UnifiedSliderView,
  17. public UnifiedSystemTrayModel::Observer {
  18. public:
  19. UnifiedKeyboardBacklightToggleView(
  20. KeyboardBacklightToggleController* controller,
  21. UnifiedSystemTrayModel* model)
  22. : UnifiedSliderView(views::Button::PressedCallback(),
  23. controller,
  24. kUnifiedMenuKeyboardBrightnessIcon,
  25. IDS_ASH_STATUS_TRAY_BRIGHTNESS,
  26. true /* readonly*/),
  27. model_(model) {
  28. model_->AddObserver(this);
  29. toast_label_ = AddChildView(std::make_unique<views::Label>());
  30. TrayPopupUtils::SetLabelFontList(toast_label_,
  31. TrayPopupUtils::FontStyle::kPodMenuHeader);
  32. slider()->SetVisible(false);
  33. }
  34. UnifiedKeyboardBacklightToggleView(
  35. const UnifiedKeyboardBacklightToggleView&) = delete;
  36. UnifiedKeyboardBacklightToggleView& operator=(
  37. const UnifiedKeyboardBacklightToggleView&) = delete;
  38. ~UnifiedKeyboardBacklightToggleView() override {
  39. model_->RemoveObserver(this);
  40. }
  41. void OnThemeChanged() override {
  42. views::View::OnThemeChanged();
  43. DCHECK(toast_label_);
  44. toast_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  45. AshColorProvider::ContentLayerType::kTextColorPrimary));
  46. }
  47. // UnifiedSystemTrayModel::Observer:
  48. void OnKeyboardBrightnessChanged(
  49. power_manager::BacklightBrightnessChange_Cause cause) override {
  50. DCHECK(toast_label_);
  51. toast_label_->SetText(l10n_util::GetStringUTF16(
  52. cause == power_manager::BacklightBrightnessChange_Cause_USER_TOGGLED_OFF
  53. ? IDS_ASH_STATUS_AREA_TOAST_KBL_OFF
  54. : IDS_ASH_STATUS_AREA_TOAST_KBL_ON));
  55. }
  56. private:
  57. UnifiedSystemTrayModel* const model_;
  58. views::Label* toast_label_ = nullptr;
  59. };
  60. } // namespace
  61. KeyboardBacklightToggleController::KeyboardBacklightToggleController(
  62. UnifiedSystemTrayModel* model)
  63. : model_(model) {}
  64. KeyboardBacklightToggleController::~KeyboardBacklightToggleController() =
  65. default;
  66. views::View* KeyboardBacklightToggleController::CreateView() {
  67. DCHECK(!slider_);
  68. slider_ = new UnifiedKeyboardBacklightToggleView(this, model_);
  69. return slider_;
  70. }
  71. void KeyboardBacklightToggleController::SliderValueChanged(
  72. views::Slider* sender,
  73. float value,
  74. float old_value,
  75. views::SliderChangeReason reason) {
  76. NOTREACHED();
  77. }
  78. } // namespace ash