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

Side by Side Diff: ash/common/system/chromeos/brightness/tray_brightness.cc

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase Created 3 years, 9 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 2013 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/common/system/chromeos/brightness/tray_brightness.h"
6
7 #include <algorithm>
8
9 #include "ash/common/material_design/material_design_controller.h"
10 #include "ash/common/shell_observer.h"
11 #include "ash/common/system/brightness_control_delegate.h"
12 #include "ash/common/system/tray/tray_constants.h"
13 #include "ash/common/system/tray/tray_popup_utils.h"
14 #include "ash/common/system/tray/tri_view.h"
15 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
16 #include "ash/common/wm_shell.h"
17 #include "ash/resources/grit/ash_resources.h"
18 #include "ash/resources/vector_icons/vector_icons.h"
19 #include "ash/strings/grit/ash_strings.h"
20 #include "base/bind.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/threading/thread_task_runner_handle.h"
23 #include "chromeos/dbus/dbus_thread_manager.h"
24 #include "chromeos/dbus/power_manager_client.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/display/display.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/paint_vector_icon.h"
29 #include "ui/views/controls/image_view.h"
30 #include "ui/views/controls/label.h"
31 #include "ui/views/controls/slider.h"
32 #include "ui/views/layout/fill_layout.h"
33 #include "ui/views/view.h"
34
35 namespace ash {
36 namespace tray {
37 namespace {
38
39 // We don't let the screen brightness go lower than this when it's being
40 // adjusted via the slider. Otherwise, if the user doesn't know about the
41 // brightness keys, they may turn the backlight off and not know how to turn it
42 // back on.
43 const double kMinBrightnessPercent = 5.0;
44
45 } // namespace
46
47 class BrightnessView : public ShellObserver,
48 public views::View,
49 public views::SliderListener {
50 public:
51 BrightnessView(bool default_view, double initial_percent);
52 ~BrightnessView() override;
53
54 bool is_default_view() const { return is_default_view_; }
55
56 // |percent| is in the range [0.0, 100.0].
57 void SetBrightnessPercent(double percent);
58
59 // ShellObserver:
60 void OnMaximizeModeStarted() override;
61 void OnMaximizeModeEnded() override;
62
63 private:
64 // views::View:
65 void OnBoundsChanged(const gfx::Rect& old_bounds) override;
66
67 // views:SliderListener:
68 void SliderValueChanged(views::Slider* sender,
69 float value,
70 float old_value,
71 views::SliderChangeReason reason) override;
72
73 // views:SliderListener:
74 void SliderDragStarted(views::Slider* slider) override;
75 void SliderDragEnded(views::Slider* slider) override;
76
77 views::Slider* slider_;
78
79 // Is |slider_| currently being dragged?
80 bool dragging_;
81
82 // True if this view is for the default tray view. Used to control hide/show
83 // behaviour of the default view when entering or leaving Maximize Mode.
84 bool is_default_view_;
85
86 // Last brightness level that we observed, in the range [0.0, 100.0].
87 double last_percent_;
88
89 DISALLOW_COPY_AND_ASSIGN(BrightnessView);
90 };
91
92 BrightnessView::BrightnessView(bool default_view, double initial_percent)
93 : dragging_(false),
94 is_default_view_(default_view),
95 last_percent_(initial_percent) {
96 SetLayoutManager(new views::FillLayout);
97 // Use CreateMultiTargetRowView() instead of CreateDefaultRowView() because
98 // that's what the audio row uses and we want the two rows to layout with the
99 // same insets.
100 TriView* tri_view = TrayPopupUtils::CreateMultiTargetRowView();
101 AddChildView(tri_view);
102
103 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
104 views::ImageView* icon = TrayPopupUtils::CreateMainImageView();
105 if (MaterialDesignController::IsSystemTrayMenuMaterial()) {
106 icon->SetImage(
107 gfx::CreateVectorIcon(kSystemMenuBrightnessIcon, kMenuIconColor));
108 } else {
109 icon->SetImage(
110 rb.GetImageNamed(IDR_AURA_UBER_TRAY_BRIGHTNESS).ToImageSkia());
111 }
112 tri_view->AddView(TriView::Container::START, icon);
113
114 slider_ = TrayPopupUtils::CreateSlider(this);
115 slider_->SetValue(static_cast<float>(initial_percent / 100.0));
116 slider_->SetAccessibleName(
117 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_BRIGHTNESS));
118 tri_view->AddView(TriView::Container::CENTER, slider_);
119
120 if (is_default_view_) {
121 WmShell::Get()->AddShellObserver(this);
122 SetVisible(WmShell::Get()
123 ->maximize_mode_controller()
124 ->IsMaximizeModeWindowManagerEnabled());
125 } else {
126 tri_view->SetContainerVisible(TriView::Container::END, false);
127 }
128 }
129
130 BrightnessView::~BrightnessView() {
131 if (is_default_view_)
132 WmShell::Get()->RemoveShellObserver(this);
133 }
134
135 void BrightnessView::SetBrightnessPercent(double percent) {
136 last_percent_ = percent;
137 if (!dragging_)
138 slider_->SetValue(static_cast<float>(percent / 100.0));
139 }
140
141 void BrightnessView::OnMaximizeModeStarted() {
142 SetVisible(true);
143 }
144
145 void BrightnessView::OnMaximizeModeEnded() {
146 SetVisible(false);
147 }
148
149 void BrightnessView::OnBoundsChanged(const gfx::Rect& old_bounds) {
150 int w = width() - slider_->x();
151 slider_->SetSize(gfx::Size(w, slider_->height()));
152 }
153
154 void BrightnessView::SliderValueChanged(views::Slider* sender,
155 float value,
156 float old_value,
157 views::SliderChangeReason reason) {
158 DCHECK_EQ(sender, slider_);
159 if (reason != views::VALUE_CHANGED_BY_USER)
160 return;
161 BrightnessControlDelegate* brightness_control_delegate =
162 WmShell::Get()->brightness_control_delegate();
163 if (brightness_control_delegate) {
164 double percent = std::max(value * 100.0, kMinBrightnessPercent);
165 brightness_control_delegate->SetBrightnessPercent(percent, true);
166 }
167 }
168
169 void BrightnessView::SliderDragStarted(views::Slider* slider) {
170 DCHECK_EQ(slider, slider_);
171 dragging_ = true;
172 }
173
174 void BrightnessView::SliderDragEnded(views::Slider* slider) {
175 DCHECK_EQ(slider, slider_);
176 dragging_ = false;
177 slider_->SetValue(static_cast<float>(last_percent_ / 100.0));
178 }
179
180 } // namespace tray
181
182 TrayBrightness::TrayBrightness(SystemTray* system_tray)
183 : SystemTrayItem(system_tray, UMA_DISPLAY_BRIGHTNESS),
184 brightness_view_(NULL),
185 current_percent_(100.0),
186 got_current_percent_(false),
187 weak_ptr_factory_(this) {
188 // Post a task to get the initial brightness; the BrightnessControlDelegate
189 // isn't created yet.
190 base::ThreadTaskRunnerHandle::Get()->PostTask(
191 FROM_HERE, base::Bind(&TrayBrightness::GetInitialBrightness,
192 weak_ptr_factory_.GetWeakPtr()));
193 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
194 this);
195 }
196
197 TrayBrightness::~TrayBrightness() {
198 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
199 this);
200 }
201
202 void TrayBrightness::GetInitialBrightness() {
203 BrightnessControlDelegate* brightness_control_delegate =
204 WmShell::Get()->brightness_control_delegate();
205 // Worrisome, but happens in unit tests, so don't log anything.
206 if (!brightness_control_delegate)
207 return;
208 brightness_control_delegate->GetBrightnessPercent(
209 base::Bind(&TrayBrightness::HandleInitialBrightness,
210 weak_ptr_factory_.GetWeakPtr()));
211 }
212
213 void TrayBrightness::HandleInitialBrightness(double percent) {
214 if (!got_current_percent_)
215 HandleBrightnessChanged(percent, false);
216 }
217
218 views::View* TrayBrightness::CreateTrayView(LoginStatus status) {
219 return NULL;
220 }
221
222 views::View* TrayBrightness::CreateDefaultView(LoginStatus status) {
223 CHECK(brightness_view_ == NULL);
224 brightness_view_ = new tray::BrightnessView(true, current_percent_);
225 return brightness_view_;
226 }
227
228 views::View* TrayBrightness::CreateDetailedView(LoginStatus status) {
229 CHECK(brightness_view_ == NULL);
230 WmShell::Get()->RecordUserMetricsAction(
231 UMA_STATUS_AREA_DETAILED_BRIGHTNESS_VIEW);
232 brightness_view_ = new tray::BrightnessView(false, current_percent_);
233 return brightness_view_;
234 }
235
236 void TrayBrightness::DestroyTrayView() {}
237
238 void TrayBrightness::DestroyDefaultView() {
239 if (brightness_view_ && brightness_view_->is_default_view())
240 brightness_view_ = NULL;
241 }
242
243 void TrayBrightness::DestroyDetailedView() {
244 if (brightness_view_ && !brightness_view_->is_default_view())
245 brightness_view_ = NULL;
246 }
247
248 void TrayBrightness::UpdateAfterLoginStatusChange(LoginStatus status) {}
249
250 bool TrayBrightness::ShouldShowShelf() const {
251 return false;
252 }
253
254 void TrayBrightness::BrightnessChanged(int level, bool user_initiated) {
255 WmShell::Get()->RecordUserMetricsAction(UMA_STATUS_AREA_BRIGHTNESS_CHANGED);
256 double percent = static_cast<double>(level);
257 HandleBrightnessChanged(percent, user_initiated);
258 }
259
260 void TrayBrightness::HandleBrightnessChanged(double percent,
261 bool user_initiated) {
262 current_percent_ = percent;
263 got_current_percent_ = true;
264
265 if (brightness_view_)
266 brightness_view_->SetBrightnessPercent(percent);
267
268 if (!user_initiated)
269 return;
270
271 // Never show the bubble on systems that lack internal displays: if an
272 // external display's brightness is changed, it may already display the new
273 // level via an on-screen display.
274 if (!display::Display::HasInternalDisplay())
275 return;
276
277 if (brightness_view_ && brightness_view_->visible())
278 SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
279 else
280 PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
281 }
282
283 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698