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

Side by Side Diff: ash/magnifier/partial_magnification_controller.cc

Issue 10915140: Add the partial screen magnifier to Chrome OS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Code review fixes. Created 8 years, 2 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 (c) 2012 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/magnifier/partial_magnification_controller.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/event_filter.h"
10 #include "ui/aura/root_window.h"
11 #include "ui/aura/shared/compound_event_filter.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_observer.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/gfx/point3.h"
16 #include "ui/gfx/screen.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_delegate.h"
20
21 namespace {
22
23 const float kMinPartialMagnifiedScaleThreshold = 1.1f;
24
25 // Number of pixels to make the border of the magnified area.
26 const int kZoomInset = 16;
27
28 // Width of the magnified area.
29 const int kMagnifierWidth = 200;
30
31 // Height of the magnified area.
32 const int kMagnifierHeight = 200;
33
34 } // namespace
35
36 namespace ash {
37 namespace internal {
38
39 class ZoomDelegateView : public views::WidgetDelegateView {
40 public:
41 ZoomDelegateView();
42 virtual ~ZoomDelegateView();
43
44 // views::View overrides
45 virtual gfx::Size GetPreferredSize() OVERRIDE;
46 virtual void Layout() OVERRIDE;
47
48 // views::WidgetZoomDelegateView overrides:
49 virtual bool CanActivate() const OVERRIDE {
50 return false;
51 }
52
53 private:
54
55 DISALLOW_COPY_AND_ASSIGN(ZoomDelegateView);
56 };
57
58 ZoomDelegateView::ZoomDelegateView() {
59 }
60
61 ZoomDelegateView::~ZoomDelegateView() {
62 }
63
64 gfx::Size ZoomDelegateView::GetPreferredSize() {
65 return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size();
66 }
67
68 void ZoomDelegateView::Layout() {
69 if (child_count() == 0)
70 return;
71 child_at(0)->SetBounds(0, 0, width(), height());
oshima 2012/10/10 19:55:52 if (child_count()) child_at(0)->SetBounds(0, 0,
Zachary Kuznia 2012/10/11 08:57:24 Done.
72 }
73
74
75 ////////////////////////////////////////////////////////////////////////////////
76 // PartialMagnificationControllerImpl:
77
78 class PartialMagnificationControllerImpl
79 : public PartialMagnificationController,
80 public aura::EventFilter,
81 public aura::WindowObserver {
82 public:
83 PartialMagnificationControllerImpl();
84 virtual ~PartialMagnificationControllerImpl();
85
86 // PartialMagnificationController overrides:
87 virtual void SetEnabled(bool enabled) OVERRIDE;
88 virtual bool IsEnabled() const OVERRIDE;
89 virtual void SetScale(float scale) OVERRIDE;
90 virtual float GetScale() const OVERRIDE { return scale_; }
91
92 private:
93 void OnMouseMove(const gfx::Point& location_in_root);
94
95 // Switch PartialMagnified RootWindow to |new_root_window|. This does
96 // following:
97 // - Remove the magnifier from the current root window.
98 // - Create a magnifier in the new root_window |new_root_window|.
99 // - Switch the target window from current window to |new_root_window|.
100 void SwitchTargetRootWindow(aura::RootWindow* new_root_window);
101
102 // Returns the root window that contains the mouse cursor.
103 aura::RootWindow* GetCurrentRootWindow();
104
105 // Return true if the magnification scale > kMinPartialMagnifiedScaleThreshold
106 bool IsPartialMagnified() const;
107
108 // Cleans up the window if needed.
109 void CloseMagnifierWindow();
110
111 // aura::EventFilter overrides:
112 virtual bool PreHandleKeyEvent(aura::Window* target,
113 ui::KeyEvent* event) OVERRIDE;
114 virtual bool PreHandleMouseEvent(aura::Window* target,
115 ui::MouseEvent* event) OVERRIDE;
116 virtual ui::TouchStatus PreHandleTouchEvent(
117 aura::Window* target,
118 ui::TouchEvent* event) OVERRIDE;
119 virtual ui::EventResult PreHandleGestureEvent(
120 aura::Window* target,
121 ui::GestureEvent* event) OVERRIDE;
122
123 // Overridden from WindowObserver:
124 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
125
126 // True if the magnified window is in motion of zooming or un-zooming effect.
127 // Otherwise, false.
128 bool is_on_zooming_;
129
130 bool is_enabled_;
131
132 // Current scale, origin (left-top) position of the magnification window.
133 float scale_;
134 gfx::Point origin_;
135
136 aura::Window* zoom_window_;
137 views::Widget* zoom_widget_;
138
139 DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerImpl);
140 };
141
142 ////////////////////////////////////////////////////////////////////////////////
143 // PartialMagnificationControllerImpl:
144
145 PartialMagnificationControllerImpl::PartialMagnificationControllerImpl()
146 : is_on_zooming_(false),
147 is_enabled_(false),
148 scale_(kNonPartialMagnifiedScale),
149 zoom_window_(NULL),
150 zoom_widget_(NULL) {
151 Shell::GetInstance()->AddEnvEventFilter(this);
152 }
153
154 PartialMagnificationControllerImpl::~PartialMagnificationControllerImpl() {
155 CloseMagnifierWindow();
156
157 Shell::GetInstance()->RemoveEnvEventFilter(this);
158 }
159
160 void PartialMagnificationControllerImpl::OnMouseMove(
161 const gfx::Point& location_in_root) {
162 gfx::Point origin(location_in_root);
163
164 origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2);
165
166 if (zoom_window_)
167 zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(),
168 kMagnifierWidth, kMagnifierHeight));
oshima 2012/10/10 19:55:52 you need {} in this case.
Zachary Kuznia 2012/10/11 08:57:24 Done.
169 }
170
171 bool PartialMagnificationControllerImpl::IsPartialMagnified() const {
172 return scale_ >= kMinPartialMagnifiedScaleThreshold;
173 }
174
175 void PartialMagnificationControllerImpl::CloseMagnifierWindow() {
176 if (zoom_window_) {
177 zoom_window_->RemoveObserver(this);
178 aura::RootWindow* root_window = zoom_window_->GetRootWindow();
179 if (root_window)
180 root_window->RemoveObserver(this);
181
182 zoom_widget_->Close();
183 delete zoom_window_;
184 zoom_window_ = NULL;
185 zoom_widget_ = NULL;
186 }
187 }
188
189 void PartialMagnificationControllerImpl::SwitchTargetRootWindow(
190 aura::RootWindow* new_root_window) {
191 if (zoom_window_ && new_root_window == zoom_window_->GetRootWindow())
192 return;
193
194 float scale = GetScale();
195
196 CloseMagnifierWindow();
197 SetScale(scale);
198 }
199
200 aura::RootWindow* PartialMagnificationControllerImpl::GetCurrentRootWindow() {
201 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
202 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
203 iter != root_windows.end(); ++iter) {
204 aura::RootWindow* root_window = *iter;
205 if (root_window->ContainsPointInRoot(
206 root_window->GetLastMouseLocationInRoot()))
oshima 2012/10/10 19:55:52 indent
Zachary Kuznia 2012/10/11 08:57:24 Done.
207 return root_window;
208 }
209 return NULL;
210 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213 // PartialMagnificationControllerImpl:
214 // PartialMagnificationController implementation
215
216 void PartialMagnificationControllerImpl::SetScale(float scale) {
217 if (!is_enabled_)
218 return;
219
220 scale_ = scale;
221
222 if (IsPartialMagnified()) {
223 if (!zoom_window_) {
224 aura::RootWindow* root_window = GetCurrentRootWindow();
225 if (!root_window)
226 return;
227
228 root_window->AddObserver(this);
229
230 gfx::Point mouse(root_window->GetLastMouseLocationInRoot());
231
232 zoom_window_ = new aura::Window(NULL);
233 zoom_window_->Init(ui::LAYER_NOT_DRAWN);
234 root_window->AddChild(zoom_window_);
235
236 zoom_widget_ = new views::Widget;
237 views::Widget::InitParams params(
238 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
239 params.transparent = true;
240 params.parent = zoom_window_;
241 ZoomDelegateView* delegate_view = new ZoomDelegateView;
242 params.delegate = delegate_view;
243 zoom_widget_->Init(params);
244 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2,
245 mouse.y() - kMagnifierHeight / 2,
246 kMagnifierWidth, kMagnifierHeight));
247 zoom_widget_->set_focus_on_creation(false);
248 zoom_widget_->SetContentsView(delegate_view);
249 zoom_widget_->Show();
250 zoom_window_->layer()->SetBounds(gfx::Rect(0, 0,
251 kMagnifierWidth,
252 kMagnifierHeight));
253 zoom_window_->Show();
254
255 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
256 (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2,
257 (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2,
258 scale_,
259 kZoomInset);
260
261 zoom_window_->AddObserver(this);
262 }
263 } else {
264 CloseMagnifierWindow();
265 }
266 }
267
268 void PartialMagnificationControllerImpl::SetEnabled(bool enabled) {
269 if (enabled) {
270 is_enabled_ = enabled;
271 SetScale(kDefaultPartialMagnifiedScale);
272 } else {
273 SetScale(kNonPartialMagnifiedScale);
274 is_enabled_ = enabled;
275 }
276 }
277
278 bool PartialMagnificationControllerImpl::IsEnabled() const {
279 return is_enabled_;
280 }
281
282
283 ////////////////////////////////////////////////////////////////////////////////
284 // PartialMagnificationControllerImpl: aura::EventFilter implementation
285
286 bool PartialMagnificationControllerImpl::PreHandleKeyEvent(
287 aura::Window* target,
288 ui::KeyEvent* event) {
289 return false;
290 }
291
292 bool PartialMagnificationControllerImpl::PreHandleMouseEvent(
293 aura::Window* target,
294 ui::MouseEvent* event) {
295 if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) {
296 aura::RootWindow* current_root = target->GetRootWindow();
297 gfx::Rect root_bounds = current_root->bounds();
298
299 if (root_bounds.Contains(event->root_location())) {
300 SwitchTargetRootWindow(current_root);
301
302 OnMouseMove(event->root_location());
303 }
304 }
305
306 return false;
307 }
308
309 ui::TouchStatus PartialMagnificationControllerImpl::PreHandleTouchEvent(
310 aura::Window* target,
311 ui::TouchEvent* event) {
312 return ui::TOUCH_STATUS_UNKNOWN;
313 }
314
315 ui::EventResult PartialMagnificationControllerImpl::PreHandleGestureEvent(
316 aura::Window* target,
317 ui::GestureEvent* event) {
318 return ui::ER_UNHANDLED;
319 }
320
321 ////////////////////////////////////////////////////////////////////////////////
322 // PartialMagnificationControllerImpl: aura::WindowObserver implementation
323
324 void PartialMagnificationControllerImpl::OnWindowDestroying(
325 aura::Window* window) {
326 if (window == zoom_window_) {
327 aura::RootWindow* root_window = zoom_window_->GetRootWindow();
328 if (root_window)
329 root_window->RemoveObserver(this);
330 zoom_window_ = NULL;
331 zoom_widget_ = NULL;
332 } else {
333 CloseMagnifierWindow();
334
335 aura::RootWindow* new_root_window = GetCurrentRootWindow();
336 if (new_root_window != window)
337 SwitchTargetRootWindow(new_root_window);
338 }
339 }
340
341 ////////////////////////////////////////////////////////////////////////////////
342 // PartialMagnificationController:
343
344 // static
345 PartialMagnificationController*
346 PartialMagnificationController::CreateInstance() {
347 return new PartialMagnificationControllerImpl();
348 }
349
350 } // namespace internal
351 } // namespace ash
OLDNEW
« no previous file with comments | « ash/magnifier/partial_magnification_controller.h ('k') | ash/shell.h » ('j') | ash/shell.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698