OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "ash/system/night_light/night_light_controller.h" |
| 6 |
| 7 #include "ash/session/session_controller.h" |
| 8 #include "ash/shell.h" |
| 9 #include "base/time/time.h" |
| 10 #include "base/values.h" |
| 11 #include "components/prefs/pref_registry_simple.h" |
| 12 #include "components/prefs/scoped_user_pref_update.h" |
| 13 #include "ui/compositor/layer.h" |
| 14 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 15 |
| 16 namespace ash { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // The key of the dictionary value in the user's pref service that contains all |
| 21 // the NightLight settings. |
| 22 constexpr char kNightLightPrefsKey[] = "prefs.night_light_prefs"; |
| 23 |
| 24 // Keys to the various NightLight settings inside its dictionary value. |
| 25 constexpr char kStatusKey[] = "night_light_status"; |
| 26 constexpr char kColorTemperatureKey[] = "night_light_color_temperature"; |
| 27 |
| 28 // The duration of the temperature change animation when the change is a result |
| 29 // of a manual user setting. |
| 30 // TODO(afakhry): Add automatic schedule animation duration when you implement |
| 31 // that part. It should be large enough (20 seconds as agreed) to give the user |
| 32 // a nice smooth transition. |
| 33 constexpr int kManualToggleAnimationDurationSec = 2; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 NightLightController::NightLightController( |
| 38 SessionController* session_controller) |
| 39 : session_controller_(session_controller) { |
| 40 session_controller_->AddObserver(this); |
| 41 } |
| 42 |
| 43 NightLightController::~NightLightController() { |
| 44 session_controller_->RemoveObserver(this); |
| 45 } |
| 46 |
| 47 // static |
| 48 void NightLightController::RegisterPrefs(PrefRegistrySimple* registry) { |
| 49 registry->RegisterDictionaryPref(kNightLightPrefsKey); |
| 50 } |
| 51 |
| 52 void NightLightController::AddObserver(Observer* observer) { |
| 53 observers_.AddObserver(observer); |
| 54 } |
| 55 |
| 56 void NightLightController::RemoveObserver(Observer* observer) { |
| 57 observers_.RemoveObserver(observer); |
| 58 } |
| 59 |
| 60 void NightLightController::Toggle() { |
| 61 SetEnabled(!enabled_); |
| 62 } |
| 63 |
| 64 void NightLightController::SetEnabled(bool enabled) { |
| 65 if (enabled_ == enabled) |
| 66 return; |
| 67 |
| 68 enabled_ = enabled; |
| 69 Refresh(); |
| 70 NotifyStatusChanged(); |
| 71 PersistUserPrefs(); |
| 72 } |
| 73 |
| 74 void NightLightController::SetColorTemperature(float temperature) { |
| 75 // TODO(afakhry): Spport changing the temperature when you implement the |
| 76 // settings part of this feature. Right now we'll keep it fixed at the value |
| 77 // |color_temperature_| whenever NightLight is turned on. |
| 78 |
| 79 for (aura::Window* root_window : Shell::GetAllRootWindows()) { |
| 80 ui::Layer* layer = root_window->layer(); |
| 81 |
| 82 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 83 settings.SetTransitionDuration( |
| 84 base::TimeDelta::FromSeconds(kManualToggleAnimationDurationSec)); |
| 85 settings.SetPreemptionStrategy( |
| 86 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 87 |
| 88 layer->SetLayerTemperature(temperature); |
| 89 } |
| 90 } |
| 91 |
| 92 void NightLightController::OnActiveUserSessionChanged( |
| 93 const AccountId& account_id) { |
| 94 // When user is switched in multi profiles. |
| 95 InitFromUserPrefs(); |
| 96 } |
| 97 |
| 98 void NightLightController::Refresh() { |
| 99 SetColorTemperature(enabled_ ? color_temperature_ : 0.0f); |
| 100 } |
| 101 |
| 102 void NightLightController::OnSessionStateChanged( |
| 103 session_manager::SessionState state) { |
| 104 if (state == session_manager::SessionState::ACTIVE) |
| 105 InitFromUserPrefs(); |
| 106 } |
| 107 |
| 108 void NightLightController::InitFromUserPrefs() { |
| 109 auto* pref_service = Shell::Get()->GetActiveUserPrefService(); |
| 110 if (!pref_service) { |
| 111 // The pref_service can be NULL in ash_unittests. It also can be NULL for |
| 112 // the primary user login as it's profile is not ready yet at the time |
| 113 // we receive OnActiveUserSessionChanged(). crbug.com/718458. |
| 114 return; |
| 115 } |
| 116 |
| 117 const base::DictionaryValue* night_light_prefs = |
| 118 pref_service->GetDictionary(kNightLightPrefsKey); |
| 119 bool enabled = false; |
| 120 night_light_prefs->GetBoolean(kStatusKey, &enabled); |
| 121 enabled_ = enabled; |
| 122 |
| 123 double color_temperature = 0.5; |
| 124 night_light_prefs->GetDouble(kColorTemperatureKey, &color_temperature); |
| 125 color_temperature_ = static_cast<float>(color_temperature); |
| 126 |
| 127 Refresh(); |
| 128 NotifyStatusChanged(); |
| 129 } |
| 130 |
| 131 void NightLightController::PersistUserPrefs() { |
| 132 auto* pref_service = ash::Shell::Get()->GetActiveUserPrefService(); |
| 133 if (!pref_service) { |
| 134 // The pref_service can be NULL in ash_unittests. |
| 135 return; |
| 136 } |
| 137 DictionaryPrefUpdate pref_updater(pref_service, kNightLightPrefsKey); |
| 138 |
| 139 base::DictionaryValue* dictionary = pref_updater.Get(); |
| 140 dictionary->SetBoolean(kStatusKey, enabled_); |
| 141 dictionary->SetDouble(kColorTemperatureKey, |
| 142 static_cast<double>(color_temperature_)); |
| 143 } |
| 144 |
| 145 void NightLightController::NotifyStatusChanged() { |
| 146 for (auto& observer : observers_) |
| 147 observer.OnNightLightEnabledChanged(enabled_); |
| 148 } |
| 149 |
| 150 } // namespace ash |
OLD | NEW |