OLD | NEW |
---|---|
(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 | |
sky
2012/10/11 17:00:49
remove newline
Zachary Kuznia
2012/10/12 08:41:35
Done.
| |
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(); | |
sky
2012/10/11 17:00:49
Set the layout manager to FillLayout and you can r
Zachary Kuznia
2012/10/12 08:41:35
Done.
| |
66 } | |
67 | |
68 void ZoomDelegateView::Layout() { | |
69 if (child_count()) | |
70 child_at(0)->SetBounds(0, 0, width(), height()); | |
71 } | |
72 | |
73 | |
74 //////////////////////////////////////////////////////////////////////////////// | |
75 // PartialMagnificationControllerImpl: | |
76 | |
77 class PartialMagnificationControllerImpl | |
78 : public PartialMagnificationController, | |
79 public aura::EventFilter, | |
80 public aura::WindowObserver { | |
81 public: | |
82 PartialMagnificationControllerImpl(); | |
83 virtual ~PartialMagnificationControllerImpl(); | |
84 | |
85 // PartialMagnificationController overrides: | |
86 virtual void SetEnabled(bool enabled) OVERRIDE; | |
87 virtual bool IsEnabled() const OVERRIDE; | |
88 virtual void SetScale(float scale) OVERRIDE; | |
89 virtual float GetScale() const OVERRIDE { return scale_; } | |
90 | |
91 private: | |
92 void OnMouseMove(const gfx::Point& location_in_root); | |
93 | |
94 // Switch PartialMagnified RootWindow to |new_root_window|. This does | |
95 // following: | |
96 // - Remove the magnifier from the current root window. | |
97 // - Create a magnifier in the new root_window |new_root_window|. | |
98 // - Switch the target window from current window to |new_root_window|. | |
99 void SwitchTargetRootWindow(aura::RootWindow* new_root_window); | |
100 | |
101 // Returns the root window that contains the mouse cursor. | |
102 aura::RootWindow* GetCurrentRootWindow(); | |
103 | |
104 // Return true if the magnification scale > kMinPartialMagnifiedScaleThreshold | |
105 bool IsPartialMagnified() const; | |
106 | |
107 // Cleans up the window if needed. | |
108 void CloseMagnifierWindow(); | |
109 | |
110 // aura::EventFilter overrides: | |
111 virtual bool PreHandleKeyEvent(aura::Window* target, | |
112 ui::KeyEvent* event) OVERRIDE; | |
113 virtual bool PreHandleMouseEvent(aura::Window* target, | |
114 ui::MouseEvent* event) OVERRIDE; | |
115 virtual ui::TouchStatus PreHandleTouchEvent( | |
116 aura::Window* target, | |
117 ui::TouchEvent* event) OVERRIDE; | |
118 virtual ui::EventResult PreHandleGestureEvent( | |
119 aura::Window* target, | |
120 ui::GestureEvent* event) OVERRIDE; | |
121 | |
122 // Overridden from WindowObserver: | |
123 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE; | |
124 | |
125 // True if the magnified window is in motion of zooming or un-zooming effect. | |
126 // Otherwise, false. | |
127 bool is_on_zooming_; | |
128 | |
129 bool is_enabled_; | |
130 | |
131 // Current scale, origin (left-top) position of the magnification window. | |
132 float scale_; | |
133 gfx::Point origin_; | |
134 | |
135 aura::Window* zoom_window_; | |
sky
2012/10/11 17:00:49
scoped_ptr
sky
2012/10/11 17:00:49
Why do you need zoom_window_? AFAICT you create it
Zachary Kuznia
2012/10/12 08:41:35
Done.
| |
136 views::Widget* zoom_widget_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerImpl); | |
139 }; | |
140 | |
141 //////////////////////////////////////////////////////////////////////////////// | |
142 // PartialMagnificationControllerImpl: | |
143 | |
144 PartialMagnificationControllerImpl::PartialMagnificationControllerImpl() | |
145 : is_on_zooming_(false), | |
146 is_enabled_(false), | |
147 scale_(kNonPartialMagnifiedScale), | |
148 zoom_window_(NULL), | |
149 zoom_widget_(NULL) { | |
150 Shell::GetInstance()->AddEnvEventFilter(this); | |
151 } | |
152 | |
153 PartialMagnificationControllerImpl::~PartialMagnificationControllerImpl() { | |
154 CloseMagnifierWindow(); | |
155 | |
156 Shell::GetInstance()->RemoveEnvEventFilter(this); | |
157 } | |
158 | |
159 void PartialMagnificationControllerImpl::OnMouseMove( | |
sky
2012/10/11 17:00:49
What if someone is using touch input?
Zachary Kuznia
2012/10/12 08:41:35
Support for touch input will come in a future patc
| |
160 const gfx::Point& location_in_root) { | |
161 gfx::Point origin(location_in_root); | |
162 | |
163 origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2); | |
164 | |
165 if (zoom_window_) { | |
166 zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(), | |
167 kMagnifierWidth, kMagnifierHeight)); | |
168 } | |
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())) | |
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(); | |
sky
2012/10/11 17:00:49
Move all this into a function, maybe CreateZoomWin
Zachary Kuznia
2012/10/12 08:41:35
Done.
| |
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_; | |
sky
2012/10/11 17:00:49
Do you want to set can_activate and accept_events
Zachary Kuznia
2012/10/12 08:41:35
Done.
| |
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); | |
sky
2012/10/11 17:00:49
You should override GetContentsView() on ZoomDeleg
Zachary Kuznia
2012/10/12 08:41:35
Doing this causes it to crash in FillLayout(). Do
sky
2012/10/12 17:09:19
what is the crash. Maybe FillLayoutGetPreferredSiz
Zachary Kuznia
2012/10/15 08:28:40
Actually, it turns out that since I removed zoom_w
| |
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) { | |
sky
2012/10/11 17:00:49
Do you need both is_enabled_ and the scale? If you
Zachary Kuznia
2012/10/12 08:41:35
They are different. When it's enabled, you can to
| |
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 | |
OLD | NEW |