Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: ash/system/night_light/night_light_controller.cc

Issue 2857103007: [Night Light] CL2: Ash and system tray work (Closed)
Patch Set: Rebase on Xiyuan's change, and mpearson's nit. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
James Cook 2017/05/09 02:45:35 Is this comment still true? Is this code path also
afakhry 2017/05/09 16:32:17 Yes, it's still true, and is also used for initial
95 InitFromUserPrefs();
96 }
97
98 void NightLightController::Refresh() {
99 SetColorTemperature(enabled_ ? color_temperature_ : 0.0f);
100 }
101
102 void NightLightController::InitFromUserPrefs() {
103 auto* pref_service = Shell::Get()->GetActiveUserPrefService();
104 if (!pref_service) {
105 // The pref_service can be NULL in ash_unittests.
106 return;
107 }
108
109 const base::DictionaryValue* night_light_prefs =
110 pref_service->GetDictionary(kNightLightPrefsKey);
111 bool enabled = false;
112 night_light_prefs->GetBoolean(kStatusKey, &enabled);
113 enabled_ = enabled;
114
115 double color_temperature = 0.5;
116 night_light_prefs->GetDouble(kColorTemperatureKey, &color_temperature);
117 color_temperature_ = static_cast<float>(color_temperature);
118
119 Refresh();
120 NotifyStatusChanged();
121 }
122
123 void NightLightController::PersistUserPrefs() {
124 auto* pref_service = ash::Shell::Get()->GetActiveUserPrefService();
125 if (!pref_service) {
126 // The pref_service can be NULL in ash_unittests.
127 return;
128 }
129 DictionaryPrefUpdate pref_updater(pref_service, kNightLightPrefsKey);
130
131 base::DictionaryValue* dictionary = pref_updater.Get();
132 dictionary->SetBoolean(kStatusKey, enabled_);
133 dictionary->SetDouble(kColorTemperatureKey,
134 static_cast<double>(color_temperature_));
135 }
136
137 void NightLightController::NotifyStatusChanged() {
138 for (auto& observer : observers_)
139 observer.OnNightLightEnabledChanged(enabled_);
140 }
141
142 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/night_light/night_light_controller.h ('k') | ash/system/night_light/night_light_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698