OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/magnifier/magnification_controller.h" | 5 #include "ash/magnifier/magnification_controller.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ui/aura/event.h" | |
9 #include "ui/aura/event_filter.h" | |
8 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
11 #include "ui/aura/shared/compound_event_filter.h" | |
9 #include "ui/aura/window.h" | 12 #include "ui/aura/window.h" |
10 #include "ui/aura/window_property.h" | 13 #include "ui/aura/window_property.h" |
14 #include "ui/gfx/point3.h" | |
15 #include "ui/compositor/dip_util.h" | |
11 #include "ui/compositor/layer.h" | 16 #include "ui/compositor/layer.h" |
17 #include "ui/compositor/layer_animation_observer.h" | |
12 #include "ui/compositor/scoped_layer_animation_settings.h" | 18 #include "ui/compositor/scoped_layer_animation_settings.h" |
13 | 19 |
14 namespace { | 20 namespace { |
15 | 21 |
16 const float kMaximumMagnifiScale = 4.0f; | 22 const float kMaximumMagnifiScale = 4.0f; |
17 const float kMaximumMagnifiScaleThreshold = 4.0f; | 23 const float kMaximumMagnifiScaleThreshold = 4.0f; |
18 const float kMinimumMagnifiScale = 1.0f; | 24 const float kMinimumMagnifiScale = 1.0f; |
19 const float kMinimumMagnifiScaleThreshold = 1.1f; | 25 const float kMinimumMagnifiScaleThreshold = 1.1f; |
20 | 26 |
21 } // namespace | 27 } // namespace |
22 | 28 |
23 namespace ash { | 29 namespace ash { |
24 namespace internal { | 30 namespace internal { |
25 | 31 |
26 MagnificationController::MagnificationController() | 32 //////////////////////////////////////////////////////////////////////////////// |
27 : scale_(1.0f), x_(0), y_(0) { | 33 // MagnificationControllerImpl: |
28 root_window_ = ash::Shell::GetPrimaryRootWindow(); | 34 |
29 } | 35 class MagnificationControllerImpl : virtual public MagnificationController, |
30 | 36 public ui::ImplicitAnimationObserver { |
31 void MagnificationController::SetScale(float scale) { | 37 public: |
38 MagnificationControllerImpl(); | |
39 virtual ~MagnificationControllerImpl() {} | |
40 | |
41 // MagnificationController overrides: | |
42 virtual void SetScale(float scale, bool animation) OVERRIDE; | |
43 virtual float GetScale() const OVERRIDE { return scale_; } | |
44 virtual void MoveWindow(int x, int y, bool animation) OVERRIDE; | |
45 virtual void MoveWindow(const gfx::Point& point, bool animation) OVERRIDE; | |
46 virtual gfx::Point GetWindowPosition() const OVERRIDE { return origin_; } | |
47 virtual void EnsureShowRect(const gfx::Rect& rect, bool animation) OVERRIDE; | |
48 virtual void EnsureShowPoint(const gfx::Point& point, | |
49 bool animation) OVERRIDE; | |
50 | |
51 private: | |
52 // ui::ImplicitAnimationObserver overrides: | |
53 virtual void OnImplicitAnimationsCompleted() OVERRIDE; | |
54 | |
55 // should be called internally just after the scale and/or the position are | |
56 // changed. | |
sky
2012/06/19 19:47:52
should -> Should
Also, this doesn't actually descr
yoshiki
2012/06/20 00:23:34
Done.
| |
57 bool Redraw(const gfx::Point& position, float scale, bool animation); | |
58 bool RedrawDIP(const gfx::Point& position, float scale, bool animation); | |
59 | |
60 // Ensures that the given point, rect or last mouse location is inside | |
61 // magnification window. If not, the controller moves the window to contain | |
62 // the given point/rect. | |
63 void EnsureShowRectWithScale(const gfx::Rect& target_rect, | |
64 float scale, | |
65 bool animation); | |
66 void EnsureShowRectDIP(const gfx::Rect& target_rect_in_dip, | |
67 float scale, | |
68 bool animation); | |
69 void EnsureShowPointWithScale(const gfx::Point& point, | |
70 float scale, | |
71 bool animation); | |
72 void OnMouseMove(const gfx::Point& location); | |
73 | |
74 // Returns if the magnification scale is 1.0 or not (larger then 1.0). | |
75 bool IsMagnified() const; | |
76 | |
77 // Returns the rect of the magnification window. | |
78 gfx::Rect GetWindowRectDIP(float scale) const; | |
79 // Returns the size of the root window. | |
80 gfx::Size GetHostSizeDIP() const; | |
81 | |
82 // Correct the givin scale value if nessesary. | |
83 void ValidateScale(float* scale); | |
84 | |
85 // aura::EventFilter overrides: | |
86 virtual bool PreHandleKeyEvent(aura::Window* target, | |
87 aura::KeyEvent* event) OVERRIDE; | |
88 virtual bool PreHandleMouseEvent(aura::Window* target, | |
89 aura::MouseEvent* event) OVERRIDE; | |
90 virtual ui::TouchStatus PreHandleTouchEvent(aura::Window* target, | |
91 aura::TouchEvent* event) OVERRIDE; | |
92 virtual ui::GestureStatus PreHandleGestureEvent( | |
93 aura::Window* target, | |
94 aura::GestureEvent* event) OVERRIDE; | |
95 | |
96 aura::RootWindow* root_window_; | |
97 | |
98 // True if the magnified window is in motion of zooming or un-zooming effect. | |
99 // Otherwise, false. | |
100 bool is_on_zooming_; | |
101 | |
102 // Current scale, origin (left-top) position of the magnification window. | |
103 float scale_; | |
104 gfx::Point origin_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerImpl); | |
107 }; | |
108 | |
109 //////////////////////////////////////////////////////////////////////////////// | |
110 // MagnificationControllerImpl: | |
111 | |
112 MagnificationControllerImpl::MagnificationControllerImpl() | |
113 : root_window_(ash::Shell::GetPrimaryRootWindow()), | |
114 is_on_zooming_(false), | |
115 scale_(1.0f) { | |
116 } | |
117 | |
118 bool MagnificationControllerImpl::Redraw(const gfx::Point& position, | |
119 float scale, | |
120 bool animation) { | |
121 const gfx::Point position_in_dip = | |
122 ui::ConvertPointToDIP(root_window_->layer(), position); | |
sky
2012/06/19 19:47:52
indent 4
yoshiki
2012/06/20 00:23:34
Done.
| |
123 return RedrawDIP(position_in_dip, scale, animation); | |
124 } | |
125 | |
126 bool MagnificationControllerImpl::RedrawDIP(const gfx::Point& position_in_dip, | |
127 float scale, | |
128 bool animation) { | |
129 int x = position_in_dip.x(); | |
130 int y = position_in_dip.y(); | |
131 | |
132 ValidateScale(&scale); | |
133 | |
134 if (x < 0) | |
135 x = 0; | |
136 if (y < 0) | |
137 y = 0; | |
138 | |
139 const gfx::Size host_size_in_dip = GetHostSizeDIP(); | |
140 const gfx::Size window_size_in_dip = GetWindowRectDIP(scale).size(); | |
141 int max_x = host_size_in_dip.width() - window_size_in_dip.width(); | |
142 int max_y = host_size_in_dip.height() - window_size_in_dip.height(); | |
143 if (x > max_x) | |
144 x = max_x; | |
145 if (y > max_y) | |
146 y = max_y; | |
147 | |
148 // Ignores 1 px diffirence because it may be error on calculation. | |
149 if (std::abs(origin_.x() - x) <= 1 && | |
150 std::abs(origin_.y() - y) <= 1 && | |
151 scale == scale_) | |
152 return false; | |
153 | |
154 origin_.set_x(x); | |
155 origin_.set_y(y); | |
32 scale_ = scale; | 156 scale_ = scale; |
33 RedrawScreen(true); | |
34 } | |
35 | |
36 void MagnificationController::MoveWindow(int x, int y) { | |
37 y_ = y; | |
38 x_ = x; | |
39 RedrawScreen(true); | |
40 } | |
41 | |
42 void MagnificationController::MoveWindow(const gfx::Point& point) { | |
43 MoveWindow(point.x(), point.y()); | |
44 } | |
45 | |
46 void MagnificationController::EnsureShowRect(const gfx::Rect& target_rect) { | |
47 gfx::Rect rect = GetWindowRect().AdjustToFit(target_rect); | |
48 MoveWindow(rect.x(), rect.y()); | |
49 } | |
50 | |
51 void MagnificationController::EnsureShowPoint(const gfx::Point& point, | |
52 bool animation) { | |
53 gfx::Rect rect = GetWindowRect(); | |
54 | |
55 if (rect.Contains(point)) | |
56 return; | |
57 | |
58 if (point.x() < rect.x()) | |
59 x_ = point.x(); | |
60 else if(rect.right() < point.x()) | |
61 x_ = point.x() - rect.width(); | |
62 | |
63 if (point.y() < rect.y()) | |
64 y_ = point.y(); | |
65 else if(rect.bottom() < point.y()) | |
66 y_ = point.y() - rect.height(); | |
67 | |
68 RedrawScreen(animation); | |
69 } | |
70 | |
71 void MagnificationController::RedrawScreen(bool animation) { | |
72 | |
73 // Adjust the scale to just |kMinimumMagnifiScale| if scale is smaller than | |
74 // |kMinimumMagnifiScaleThreshold|; | |
75 if (scale_ < kMinimumMagnifiScaleThreshold) | |
76 scale_ = kMinimumMagnifiScale; | |
77 // Adjust the scale to just |kMinimumMagnifiScale| if scale is bigger than | |
78 // |kMinimumMagnifiScaleThreshold|; | |
79 if (scale_ > kMaximumMagnifiScaleThreshold) | |
80 scale_ = kMaximumMagnifiScale; | |
81 | |
82 if (x_ < 0) | |
83 x_ = 0; | |
84 if (y_ < 0) | |
85 y_ = 0; | |
86 | |
87 gfx::Size host_size = root_window_->GetHostSize(); | |
88 gfx::Size window_size = GetWindowRect().size(); | |
89 int max_x = host_size.width() - window_size.width(); | |
90 int max_y = host_size.height() - window_size.height(); | |
91 if (x_ > max_x) | |
92 x_ = max_x; | |
93 if (y_ > max_y) | |
94 y_ = max_y; | |
95 | |
96 | |
97 float scale = scale_; | |
98 int x = x_; | |
99 int y = y_; | |
100 | 157 |
101 // Creates transform matrix. | 158 // Creates transform matrix. |
102 ui::Transform transform; | 159 ui::Transform transform; |
103 // Flips the signs intentionally to convert them from the position of the | 160 // Flips the signs intentionally to convert them from the position of the |
104 // magnification window. | 161 // magnification window. |
105 transform.ConcatTranslate(-x, -y); | 162 transform.ConcatTranslate(-origin_.x(), -origin_.y()); |
106 transform.ConcatScale(scale, scale); | 163 transform.ConcatScale(scale_, scale_); |
107 | 164 |
108 if (animation) { | 165 ui::ScopedLayerAnimationSettings settings( |
109 ui::ScopedLayerAnimationSettings settings( | 166 root_window_->layer()->GetAnimator()); |
110 root_window_->layer()->GetAnimator()); | 167 settings.AddObserver(this); |
111 settings.SetPreemptionStrategy( | 168 settings.SetPreemptionStrategy( |
112 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 169 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
113 settings.SetTweenType(ui::Tween::EASE_IN_OUT); | 170 settings.SetTweenType(ui::Tween::EASE_OUT); |
114 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100)); | 171 settings.SetTransitionDuration( |
172 base::TimeDelta::FromMilliseconds(animation ? 100 : 0)); | |
173 | |
174 root_window_->layer()->SetTransform(transform); | |
175 | |
176 return true; | |
177 } | |
178 | |
179 void MagnificationControllerImpl::EnsureShowRectWithScale( | |
180 const gfx::Rect& target_rect, | |
181 float scale, | |
182 bool animation) { | |
183 const gfx::Rect target_rect_in_dip = | |
184 ui::ConvertRectToDIP(root_window_->layer(), target_rect); | |
sky
2012/06/19 19:47:52
indent 4
yoshiki
2012/06/20 00:23:34
Done.
| |
185 EnsureShowRectDIP(target_rect_in_dip, scale, animation); | |
186 } | |
187 | |
188 void MagnificationControllerImpl::EnsureShowRectDIP( | |
189 const gfx::Rect& target_rect, | |
190 float scale, | |
191 bool animation) { | |
192 ValidateScale(&scale); | |
193 | |
194 const gfx::Rect window_rect = GetWindowRectDIP(scale); | |
195 if (scale == scale_ && window_rect.Contains(target_rect)) | |
196 return; | |
197 | |
198 gfx::Rect rect = window_rect; | |
199 if (target_rect.width() > rect.width()) | |
200 rect.set_x(target_rect.CenterPoint().x() - rect.x() / 2); | |
201 else if (target_rect.right() < rect.x()) | |
202 rect.set_x(target_rect.right()); | |
203 else if (rect.right() < target_rect.x()) | |
204 rect.set_x(target_rect.x() - rect.width()); | |
205 | |
206 if (rect.height() > window_rect.height()) | |
207 rect.set_y(target_rect.CenterPoint().y() - rect.y() / 2); | |
208 else if (target_rect.bottom() < rect.y()) | |
209 rect.set_y(target_rect.bottom()); | |
210 else if (rect.bottom() < target_rect.y()) | |
211 rect.set_y(target_rect.y() - rect.height()); | |
212 | |
213 RedrawDIP(rect.origin(), scale, animation); | |
214 } | |
215 | |
216 void MagnificationControllerImpl::EnsureShowPointWithScale( | |
217 const gfx::Point& point, | |
218 float scale, | |
219 bool animation) { | |
220 EnsureShowRectWithScale(gfx::Rect(point, gfx::Size(0, 0)), scale, animation); | |
221 } | |
222 | |
223 void MagnificationControllerImpl::OnMouseMove(const gfx::Point& location) { | |
224 gfx::Point mouse(location); | |
225 | |
226 int x = origin_.x(); | |
227 int y = origin_.y(); | |
228 bool start_zoom = false; | |
229 | |
230 const gfx::Rect window_rect = GetWindowRectDIP(scale_); | |
231 const int left = window_rect.x(); | |
232 const int right = window_rect.right(); | |
233 const int width_margin = static_cast<int>(0.1f * window_rect.width()); | |
234 const int width_offset = static_cast<int>(0.5f * window_rect.width()); | |
235 | |
236 if (mouse.x() < left + width_margin) { | |
237 x -= width_offset; | |
238 start_zoom = true; | |
239 } else if (right - width_margin < mouse.x()) { | |
240 x += width_offset; | |
241 start_zoom = true; | |
115 } | 242 } |
116 | 243 |
117 root_window_->layer()->SetTransform(transform); | 244 const int top = window_rect.y(); |
118 } | 245 const int bottom = window_rect.bottom(); |
119 | 246 // Uses same margin with x-axis's one. |
120 gfx::Rect MagnificationController::GetWindowRect() { | 247 const int height_margin = width_margin; |
121 gfx::Size size = root_window_->GetHostSize(); | 248 const int height_offset = static_cast<int>(0.5f * window_rect.height()); |
122 int width = size.width() / scale_; | 249 |
123 int height = size.height() / scale_; | 250 if (mouse.y() < top + height_margin) { |
124 | 251 y -= height_offset; |
125 return gfx::Rect(x_, y_, width, height); | 252 start_zoom = true; |
253 } else if (bottom - height_margin < mouse.y()) { | |
254 y += height_offset; | |
255 start_zoom = true; | |
256 } | |
257 | |
258 if (start_zoom && !is_on_zooming_) { | |
259 bool ret = Redraw(gfx::Point(x, y), scale_, true); | |
260 | |
261 if (ret) { | |
262 is_on_zooming_ = true; | |
263 | |
264 int x_diff = origin_.x() - window_rect.x(); | |
265 int y_diff = origin_.y() - window_rect.y(); | |
266 // If the magnified region is moved, hides the mouse cursor and moves it. | |
267 if (x_diff != 0 || y_diff != 0) { | |
268 ash::Shell::GetInstance()-> | |
269 env_filter()->set_update_cursor_visibility(false); | |
270 root_window_->ShowCursor(false); | |
271 mouse.set_x(mouse.x() - (origin_.x() - window_rect.x())); | |
272 mouse.set_y(mouse.y() - (origin_.y() - window_rect.y())); | |
273 root_window_->MoveCursorTo(mouse); | |
274 } | |
275 } | |
276 } | |
277 } | |
278 | |
279 gfx::Size MagnificationControllerImpl::GetHostSizeDIP() const { | |
280 return ui::ConvertSizeToDIP(root_window_->layer(), | |
281 root_window_->GetHostSize()); | |
282 } | |
283 | |
284 gfx::Rect MagnificationControllerImpl::GetWindowRectDIP(float scale) const { | |
285 const gfx::Size size_in_dip = | |
286 ui::ConvertSizeToDIP(root_window_->layer(), | |
287 root_window_->GetHostSize()); | |
288 const int width = size_in_dip.width() / scale; | |
289 const int height = size_in_dip.height() / scale; | |
290 | |
291 return gfx::Rect(origin_.x(), origin_.y(), width, height); | |
292 } | |
293 | |
294 bool MagnificationControllerImpl::IsMagnified() const { | |
295 return scale_ >= kMinimumMagnifiScaleThreshold; | |
296 } | |
297 | |
298 void MagnificationControllerImpl::ValidateScale(float* scale) { | |
299 // Adjust the scale to just |kMinimumMagnifiScale| if scale is smaller than | |
300 // |kMinimumMagnifiScaleThreshold|; | |
301 if (*scale < kMinimumMagnifiScaleThreshold) | |
302 *scale = kMinimumMagnifiScale; | |
303 | |
304 // Adjust the scale to just |kMinimumMagnifiScale| if scale is bigger than | |
305 // |kMinimumMagnifiScaleThreshold|; | |
306 if (*scale > kMaximumMagnifiScaleThreshold) | |
307 *scale = kMaximumMagnifiScale; | |
308 } | |
309 | |
310 void MagnificationControllerImpl::OnImplicitAnimationsCompleted() { | |
311 root_window_->ShowCursor(true); | |
312 is_on_zooming_ = false; | |
313 } | |
314 | |
315 //////////////////////////////////////////////////////////////////////////////// | |
316 // MagnificationControllerImpl: MagnificationController implementation | |
317 | |
318 void MagnificationControllerImpl::SetScale(float scale, bool animation) { | |
319 ValidateScale(&scale); | |
320 | |
321 // Try not to change the point which the mouse cursor indicates to. | |
322 const gfx::Rect window_rect = GetWindowRectDIP(scale); | |
323 const gfx::Point mouse = root_window_->last_mouse_location(); | |
324 const gfx::Point origin = gfx::Point(mouse.x() * (1.0f - 1.0f / scale), | |
325 mouse.y() * (1.0f - 1.0f / scale)); | |
326 Redraw(origin, scale, animation); | |
327 } | |
328 | |
329 void MagnificationControllerImpl::MoveWindow(int x, int y, bool animation) { | |
330 Redraw(gfx::Point(x, y), scale_, animation); | |
331 } | |
332 | |
333 void MagnificationControllerImpl::MoveWindow( | |
334 const gfx::Point& point, bool animation) { | |
335 Redraw(point, scale_, animation); | |
336 } | |
337 | |
338 void MagnificationControllerImpl::EnsureShowRect( | |
339 const gfx::Rect& target_rect, bool animation) { | |
340 EnsureShowRectWithScale(target_rect, scale_, animation); | |
341 } | |
342 | |
343 void MagnificationControllerImpl::EnsureShowPoint( | |
344 const gfx::Point& point, bool animation) { | |
345 EnsureShowPointWithScale(point, scale_, animation); | |
346 } | |
347 | |
348 //////////////////////////////////////////////////////////////////////////////// | |
349 // MagnificationControllerImpl: aura::EventFilter implementation | |
350 | |
351 bool MagnificationControllerImpl::PreHandleKeyEvent(aura::Window* target, | |
352 aura::KeyEvent* event) { | |
353 return false; | |
354 } | |
355 | |
356 bool MagnificationControllerImpl::PreHandleMouseEvent(aura::Window* target, | |
357 aura::MouseEvent* event) { | |
358 if (IsMagnified()) | |
359 OnMouseMove(event->root_location()); | |
360 return false; | |
361 } | |
362 | |
363 ui::TouchStatus MagnificationControllerImpl::PreHandleTouchEvent( | |
364 aura::Window* target, | |
365 aura::TouchEvent* event) { | |
366 return ui::TOUCH_STATUS_UNKNOWN; | |
367 } | |
368 | |
369 ui::GestureStatus MagnificationControllerImpl::PreHandleGestureEvent( | |
370 aura::Window* target, | |
371 aura::GestureEvent* event) { | |
372 return ui::GESTURE_STATUS_UNKNOWN; | |
373 } | |
374 | |
375 //////////////////////////////////////////////////////////////////////////////// | |
376 // MagnificationController: | |
377 | |
378 // static | |
379 MagnificationController* MagnificationController::CreateInstance() { | |
380 return new MagnificationControllerImpl(); | |
126 } | 381 } |
127 | 382 |
128 } // namespace internal | 383 } // namespace internal |
129 } // namespace ash | 384 } // namespace ash |
OLD | NEW |