unified_brightness_slider_controller.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/brightness/unified_brightness_slider_controller.h"
  5. #include "ash/shell.h"
  6. #include "ash/system/brightness/unified_brightness_view.h"
  7. #include "ash/system/brightness_control_delegate.h"
  8. #include "ash/system/unified/unified_system_tray_model.h"
  9. #include "base/memory/scoped_refptr.h"
  10. namespace ash {
  11. namespace {
  12. // We don't let the screen brightness go lower than this when it's being
  13. // adjusted via the slider. Otherwise, if the user doesn't know about the
  14. // brightness keys, they may turn the backlight off and not know how to turn it
  15. // back on.
  16. constexpr double kMinBrightnessPercent = 5.0;
  17. } // namespace
  18. UnifiedBrightnessSliderController::UnifiedBrightnessSliderController(
  19. scoped_refptr<UnifiedSystemTrayModel> model)
  20. : model_(model) {}
  21. UnifiedBrightnessSliderController::~UnifiedBrightnessSliderController() =
  22. default;
  23. views::View* UnifiedBrightnessSliderController::CreateView() {
  24. DCHECK(!slider_);
  25. slider_ = new UnifiedBrightnessView(this, model_);
  26. return slider_;
  27. }
  28. void UnifiedBrightnessSliderController::SliderValueChanged(
  29. views::Slider* sender,
  30. float value,
  31. float old_value,
  32. views::SliderChangeReason reason) {
  33. if (reason != views::SliderChangeReason::kByUser)
  34. return;
  35. BrightnessControlDelegate* brightness_control_delegate =
  36. Shell::Get()->brightness_control_delegate();
  37. if (!brightness_control_delegate)
  38. return;
  39. double percent = value * 100.;
  40. // If previous percentage and current percentage are both below the minimum,
  41. // we don't update the actual brightness.
  42. if (percent < kMinBrightnessPercent &&
  43. previous_percent_ < kMinBrightnessPercent) {
  44. return;
  45. }
  46. // We have to store previous manually set value because |old_value| might be
  47. // set by UnifiedSystemTrayModel::Observer.
  48. previous_percent_ = percent;
  49. percent = std::max(kMinBrightnessPercent, percent);
  50. brightness_control_delegate->SetBrightnessPercent(percent, true);
  51. }
  52. } // namespace ash