1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96

ash / system / brightness / unified_brightness_slider_controller.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/brightness/unified_brightness_slider_controller.h"

#include <memory>
#include <utility>

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/shell.h"
#include "ash/system/brightness/unified_brightness_view.h"
#include "ash/system/brightness_control_delegate.h"
#include "ash/system/unified/unified_system_tray_model.h"
#include "base/dcheck_is_on.h"
#include "base/memory/scoped_refptr.h"

namespace ash {

namespace {

// We don't let the screen brightness go lower than this when it's being
// adjusted via the slider.  Otherwise, if the user doesn't know about the
// brightness keys, they may turn the backlight off and not know how to turn
// it back on.
static constexpr double kMinBrightnessPercent = 5.0;

}  // namespace

UnifiedBrightnessSliderController::UnifiedBrightnessSliderController(
    scoped_refptr<UnifiedSystemTrayModel> model,
    views::Button::PressedCallback callback)
    : model_(model), callback_(std::move(callback)) {}

UnifiedBrightnessSliderController::~UnifiedBrightnessSliderController() =
    default;

std::unique_ptr<UnifiedBrightnessView>
UnifiedBrightnessSliderController::CreateBrightnessSlider() {
  return std::make_unique<UnifiedBrightnessView>(this, model_);
}

std::unique_ptr<UnifiedSliderView>
UnifiedBrightnessSliderController::CreateView() {
#if DCHECK_IS_ON()
  DCHECK(!created_view_);
  created_view_ = true;
#endif
  // Consuming `callback_` is safe here; `CreateView()` should only be called
  // once per controller instance per the DCHECK() above.
  return std::make_unique<UnifiedBrightnessView>(this, model_,
                                                 std::move(callback_));
}

QsSliderCatalogName UnifiedBrightnessSliderController::GetCatalogName() {
  return QsSliderCatalogName::kBrightness;
}

void UnifiedBrightnessSliderController::SliderValueChanged(
    views::Slider* sender,
    float value,
    float old_value,
    views::SliderChangeReason reason) {
  if (reason != views::SliderChangeReason::kByUser) {
    return;
  }

  BrightnessControlDelegate* brightness_control_delegate =
      Shell::Get()->brightness_control_delegate();
  if (!brightness_control_delegate) {
    return;
  }

  double percent = value * 100.;
  // If previous percentage and current percentage are both below the minimum,
  // we don't update the actual brightness.
  if (percent < kMinBrightnessPercent &&
      previous_percent_ < kMinBrightnessPercent) {
    return;
  }

  if (previous_percent_ != percent) {
    TrackValueChangeUMA(/*going_up=*/percent > previous_percent_);
  }

  // We have to store previous manually set value because |old_value| might be
  // set by UnifiedSystemTrayModel::Observer.
  previous_percent_ = percent;

  percent = std::max(kMinBrightnessPercent, percent);
  brightness_control_delegate->SetBrightnessPercent(
      percent, /*gradual=*/true, /*source=*/
      BrightnessControlDelegate::BrightnessChangeSource::kQuickSettings);
}

}  // namespace ash