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 night light settings. | |
James Cook
2017/05/05 17:12:31
super nit: You're inconsistent in comments about "
afakhry
2017/05/05 20:04:10
Done.
| |
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 // True if animations are disabled for tests. | |
36 bool animations_disabled_for_tests = false; | |
James Cook
2017/05/05 17:12:31
optional: g_animations_disabled_for_test
not requ
afakhry
2017/05/05 20:04:10
A reviewer once told me that since such a variable
| |
37 | |
38 } // namespace | |
39 | |
40 NightLightController::NightLightController( | |
41 SessionController* session_controller) | |
James Cook
2017/05/05 17:12:31
Just for my knowledge, any particular reason you'r
afakhry
2017/05/05 20:04:10
I'm using it to observer user login and switch to
| |
42 : session_controller_(session_controller) { | |
43 session_controller_->AddObserver(this); | |
44 } | |
45 | |
46 NightLightController::~NightLightController() { | |
47 session_controller_->RemoveObserver(this); | |
48 } | |
49 | |
50 // static | |
51 void NightLightController::RegisterPrefs(PrefRegistrySimple* registry) { | |
52 registry->RegisterDictionaryPref(kNightLightPrefsKey); | |
53 } | |
54 | |
55 // static | |
56 void NightLightController::DisableAnimationsForTests() { | |
57 animations_disabled_for_tests = true; | |
58 } | |
59 | |
60 void NightLightController::AddObserver(Observer* observer) { | |
61 observers_.AddObserver(observer); | |
62 } | |
63 | |
64 void NightLightController::RemoveObserver(Observer* observer) { | |
65 observers_.RemoveObserver(observer); | |
66 } | |
67 | |
68 void NightLightController::Toggle() { | |
69 SetEnabled(!enabled_); | |
70 } | |
71 | |
72 void NightLightController::SetEnabled(bool enabled) { | |
73 if (enabled_ == enabled) | |
74 return; | |
75 | |
76 enabled_ = enabled; | |
77 Refresh(); | |
78 NotifyStatusChanged(); | |
79 PersistUserPrefs(); | |
80 } | |
81 | |
82 void NightLightController::SetColorTemperature(float temperature) { | |
83 // TODO(afakhry): Spport changing the temperature when you implement the | |
84 // settings part of this feature. Right now we'll keep it fixed at the value | |
85 // |color_temperature_| whenever NightLight is turned on. | |
86 | |
87 for (aura::Window* root_window : ash::Shell::GetAllRootWindows()) { | |
James Cook
2017/05/05 17:12:30
nit: no ash::
afakhry
2017/05/05 20:04:10
Done.
| |
88 ui::Layer* layer = root_window->layer(); | |
89 | |
90 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
91 settings.SetTransitionDuration(base::TimeDelta::FromSeconds( | |
92 animations_disabled_for_tests ? 0 : kManualToggleAnimationDurationSec)); | |
James Cook
2017/05/05 17:12:31
I'm a little surprised you have to have your own d
afakhry
2017/05/05 20:04:10
Oh, I didn't know about that. I was just paranoid
| |
93 settings.SetPreemptionStrategy( | |
94 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
95 | |
96 layer->SetLayerTemperature(temperature); | |
97 } | |
98 } | |
99 | |
100 void NightLightController::OnActiveUserSessionChanged( | |
101 const AccountId& account_id) { | |
102 // When user is switched in multi profiles. | |
103 InitFromUserPrefs(); | |
104 } | |
105 | |
106 void NightLightController::Refresh() { | |
107 SetColorTemperature(enabled_ ? color_temperature_ : 0.0f); | |
108 } | |
109 | |
110 void NightLightController::OnSessionStateChanged( | |
111 session_manager::SessionState state) { | |
112 if (state == session_manager::SessionState::ACTIVE) | |
113 InitFromUserPrefs(); | |
114 } | |
115 | |
116 void NightLightController::InitFromUserPrefs() { | |
117 auto* pref_service = Shell::Get()->GetActiveUserPrefService(); | |
118 if (!pref_service) { | |
119 LOG(ERROR) << "Can't initialize NightLight user settings. No " | |
James Cook
2017/05/05 17:12:31
Can this be a CHECK()? It seems unexpected to try
afakhry
2017/05/05 20:04:10
It is expected in ash tests where there's no pref_
James Cook
2017/05/05 20:47:43
So does this log line print when you run ash_unitt
afakhry
2017/05/06 00:49:34
Done.
| |
120 << "PrefService available."; | |
121 return; | |
122 } | |
123 | |
124 const base::DictionaryValue* night_light_prefs = | |
125 pref_service->GetDictionary(kNightLightPrefsKey); | |
126 bool enabled = false; | |
127 night_light_prefs->GetBoolean(kStatusKey, &enabled); | |
128 enabled_ = enabled; | |
129 | |
130 double color_temperature = 0.5; | |
131 night_light_prefs->GetDouble(kColorTemperatureKey, &color_temperature); | |
132 color_temperature_ = static_cast<float>(color_temperature); | |
133 | |
134 Refresh(); | |
135 NotifyStatusChanged(); | |
136 } | |
137 | |
138 void NightLightController::PersistUserPrefs() { | |
139 auto* pref_service = ash::Shell::Get()->GetActiveUserPrefService(); | |
140 if (!pref_service) { | |
James Cook
2017/05/05 17:12:31
ditto
afakhry
2017/05/05 20:04:10
Same reason as above.
| |
141 LOG(ERROR) << "Can't persist NightLight user settings. No PrefService " | |
142 << "Available."; | |
143 return; | |
144 } | |
145 DictionaryPrefUpdate pref_updater(pref_service, kNightLightPrefsKey); | |
146 | |
147 base::DictionaryValue* dictionary = pref_updater.Get(); | |
148 dictionary->SetBoolean(kStatusKey, enabled_); | |
149 dictionary->SetDouble(kColorTemperatureKey, | |
150 static_cast<double>(color_temperature_)); | |
151 } | |
152 | |
153 void NightLightController::NotifyStatusChanged() { | |
154 for (auto& obs : observers_) | |
James Cook
2017/05/05 17:12:31
super nit: obs -> observer
afakhry
2017/05/05 20:04:10
Done.
| |
155 obs.OnStatusChanged(enabled_); | |
156 } | |
157 | |
158 } // namespace ash | |
OLD | NEW |