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 "ui/aura/event_filter.h" | |
9 #include "ui/aura/root_window.h" | |
10 #include "ui/aura/shared/compound_event_filter.h" | |
11 #include "ui/aura/window.h" | |
12 #include "ui/aura/window_property.h" | |
13 #include "ui/gfx/point3.h" | |
14 #include "ui/gfx/screen.h" | |
15 #include "ui/compositor/layer.h" | |
16 #include "ui/views/widget/widget.h" | |
17 #include "ui/views/widget/widget_delegate.h" | |
18 | |
19 namespace { | |
20 | |
21 const float kMaxPartialMagnifiedScale = 4.0f; | |
22 const float kMaxPartialMagnifiedScaleThreshold = 4.0f; | |
23 const float kMinPartialMagnifiedScaleThreshold = 1.1f; | |
24 | |
25 } // namespace | |
26 | |
27 namespace ash { | |
28 namespace internal { | |
29 | |
30 class ZoomDelegateView : public views::WidgetDelegateView { | |
31 public: | |
32 explicit ZoomDelegateView(); | |
33 virtual ~ZoomDelegateView(); | |
34 | |
35 // views::View overrides | |
36 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
37 virtual void Layout() OVERRIDE; | |
38 | |
39 // views::WidgetZoomDelegateView overrides: | |
40 virtual bool CanActivate() const OVERRIDE { | |
41 return false; | |
42 } | |
43 | |
44 private: | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(ZoomDelegateView); | |
47 }; | |
48 | |
49 ZoomDelegateView::ZoomDelegateView() { | |
50 } | |
51 | |
52 ZoomDelegateView::~ZoomDelegateView() { | |
53 } | |
54 | |
55 gfx::Size ZoomDelegateView::GetPreferredSize() { | |
56 return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size(); | |
57 } | |
58 | |
59 void ZoomDelegateView::Layout() { | |
60 if (child_count() == 0) | |
61 return; | |
62 child_at(0)->SetBounds(0, 0, width(), height()); | |
63 } | |
64 | |
65 | |
66 //////////////////////////////////////////////////////////////////////////////// | |
67 // PartialMagnificationControllerImpl: | |
68 | |
69 class PartialMagnificationControllerImpl | |
70 : public PartialMagnificationController, | |
71 public aura::EventFilter { | |
72 public: | |
73 PartialMagnificationControllerImpl(); | |
74 virtual ~PartialMagnificationControllerImpl(); | |
75 | |
76 // PartialMagnificationController overrides: | |
77 virtual void SetEnabled(bool enabled) OVERRIDE; | |
78 virtual bool IsEnabled() OVERRIDE; | |
79 virtual void SetScale(float scale) OVERRIDE; | |
80 virtual float GetScale() const OVERRIDE { return scale_; } | |
81 | |
82 private: | |
83 void OnMouseMove(const gfx::Point& location); | |
84 | |
85 // Switch PartialMagnified RootWindow to |new_root_window|. This does | |
86 // following: | |
87 // - Remove the magnifier from the current root window. | |
88 // - Create a magnifier in the new root_window |new_root_window|. | |
89 // - Switch the target window from current window to |new_root_window|. | |
90 void SwitchTargetRootWindow(aura::RootWindow* new_root_window); | |
91 | |
92 // Returns if the magnification scale is 1.0 or not (larger then 1.0). | |
oshima
2012/09/07 22:30:55
s/then/than
but this description is not clear if i
Zachary Kuznia
2012/09/13 11:49:02
Done.
| |
93 bool IsPartialMagnified() const; | |
94 | |
95 // Correct the givin scale value if nessesary. | |
96 void ValidateScale(float* scale); | |
oshima
2012/09/07 22:30:55
CorrectScale, EnsureScale ?
validate sounds like
Zachary Kuznia
2012/09/13 11:49:02
Removed.
| |
97 | |
98 // aura::EventFilter overrides: | |
99 virtual bool PreHandleKeyEvent(aura::Window* target, | |
100 ui::KeyEvent* event) OVERRIDE; | |
101 virtual bool PreHandleMouseEvent(aura::Window* target, | |
102 ui::MouseEvent* event) OVERRIDE; | |
103 virtual ui::TouchStatus PreHandleTouchEvent( | |
104 aura::Window* target, | |
105 ui::TouchEvent* event) OVERRIDE; | |
106 virtual ui::EventResult PreHandleGestureEvent( | |
107 aura::Window* target, | |
108 ui::GestureEvent* event) OVERRIDE; | |
109 | |
110 aura::RootWindow* root_window_; | |
sky
2012/09/07 16:06:59
What happens if root_window_ is destroyed?
Zachary Kuznia
2012/09/13 11:49:02
Added self as an observer.
| |
111 | |
112 // True if the magnified window is in motion of zooming or un-zooming effect. | |
113 // Otherwise, false. | |
114 bool is_on_zooming_; | |
115 | |
116 bool is_enabled_; | |
117 | |
118 // Current scale, origin (left-top) position of the magnification window. | |
119 float scale_; | |
120 gfx::Point origin_; | |
121 | |
122 aura::Window* zoom_window_; | |
123 views::Widget* zoom_widget_; | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerImpl); | |
126 }; | |
127 | |
128 //////////////////////////////////////////////////////////////////////////////// | |
129 // PartialMagnificationControllerImpl: | |
130 | |
131 PartialMagnificationControllerImpl::PartialMagnificationControllerImpl() | |
132 : root_window_(ash::Shell::GetPrimaryRootWindow()), | |
oshima
2012/09/07 22:30:55
Do you need to set this to primary first? Or can w
Zachary Kuznia
2012/09/13 11:49:02
Deferred to using GetActiveRootWindow() when enabl
| |
133 is_on_zooming_(false), | |
134 is_enabled_(false), | |
135 scale_(kNonPartialMagnifiedScale), | |
136 zoom_window_(NULL), | |
137 zoom_widget_(NULL) { | |
138 Shell::GetInstance()->AddEnvEventFilter(this); | |
139 } | |
140 | |
141 PartialMagnificationControllerImpl::~PartialMagnificationControllerImpl() { | |
142 Shell::GetInstance()->RemoveEnvEventFilter(this); | |
sky
2012/09/07 16:06:59
What happens if zoom_window_/zoom_widget_ are non-
Zachary Kuznia
2012/09/13 11:49:02
Fixed.
| |
143 } | |
144 | |
145 void PartialMagnificationControllerImpl::OnMouseMove( | |
146 const gfx::Point& location) { | |
147 int x = location.x(); | |
148 int y = location.y(); | |
149 | |
150 if (zoom_window_) { | |
151 zoom_widget_->SetBounds(gfx::Rect(x - 100, y - 100, 200, 200)); | |
152 } | |
oshima
2012/09/07 22:30:55
nuke {} and use const for size.
Zachary Kuznia
2012/09/13 11:49:02
Done.
| |
153 } | |
154 | |
155 bool PartialMagnificationControllerImpl::IsPartialMagnified() const { | |
156 return scale_ >= kMinPartialMagnifiedScaleThreshold; | |
157 } | |
158 | |
159 void PartialMagnificationControllerImpl::ValidateScale(float* scale) { | |
160 // Adjust the scale to adjust |kNonPartialMagnifiedScale| if scale is | |
161 // smaller than |kMinPartialMagnifiedScaleThreshold|; | |
162 if (*scale < kMinPartialMagnifiedScaleThreshold) | |
163 *scale = kNonPartialMagnifiedScale; | |
sky
2012/09/07 16:06:59
How come you have Threshold and Scale? And the com
Zachary Kuznia
2012/09/13 11:49:02
Removed this function for now, since magnification
| |
164 | |
165 // Adjust the scale to adjust |kMinPartialMagnifiedScale| if scale is bigger | |
166 // than |kMinPartialMagnifiedScaleThreshold|; | |
167 if (*scale > kMaxPartialMagnifiedScaleThreshold) | |
oshima
2012/09/07 22:30:55
else if
Zachary Kuznia
2012/09/13 11:49:02
Removed this function for now, since magnification
| |
168 *scale = kMaxPartialMagnifiedScale; | |
169 } | |
170 | |
171 void PartialMagnificationControllerImpl::SwitchTargetRootWindow( | |
172 aura::RootWindow* new_root_window) { | |
173 if (new_root_window == root_window_) | |
174 return; | |
175 | |
176 float scale = GetScale(); | |
177 | |
178 SetScale(1.0f); | |
179 root_window_ = new_root_window; | |
180 SetScale(scale); | |
181 } | |
182 | |
183 //////////////////////////////////////////////////////////////////////////////// | |
184 // PartialMagnificationControllerImpl: | |
185 // PartialMagnificationController implementation | |
186 | |
187 void PartialMagnificationControllerImpl::SetScale(float scale) { | |
188 if (!is_enabled_) | |
189 return; | |
190 | |
191 ValidateScale(&scale); | |
192 | |
193 scale_ = scale; | |
194 | |
195 const int width = 200; | |
196 const int height = 200; | |
197 if (IsPartialMagnified()) { | |
198 if (!zoom_window_) { | |
199 gfx::Point mouse(root_window_->GetLastMouseLocationInRoot()); | |
200 | |
201 zoom_window_ = new aura::Window(NULL); | |
sky
2012/09/07 16:06:59
Who deletes this?
Zachary Kuznia
2012/09/13 11:49:02
Added the delete after close.
| |
202 zoom_window_->Init(ui::LAYER_NOT_DRAWN); | |
203 root_window_->AddChild(zoom_window_); | |
204 | |
205 zoom_widget_ = new views::Widget; | |
206 views::Widget::InitParams params( | |
207 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
208 params.transparent = true; | |
209 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
sky
2012/09/07 16:06:59
You never delete zoom_widget_, which means because
oshima
2012/09/07 22:30:55
To add a bit more explanation, widget with WIDGET_
Zachary Kuznia
2012/09/13 11:49:02
Fixed.
| |
210 params.parent = zoom_window_; | |
211 ZoomDelegateView* delegate_view = new ZoomDelegateView; | |
212 params.delegate = delegate_view; | |
213 zoom_widget_->Init(params); | |
214 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - width / 2, | |
sky
2012/09/07 16:06:59
You should make sure this does what you want when
Zachary Kuznia
2012/09/13 11:49:02
It seems to. What strange behavior might happen?
| |
215 mouse.y() - height / 2, | |
216 width, height)); | |
217 zoom_widget_->set_focus_on_creation(false); | |
218 zoom_widget_->SetContentsView(delegate_view); | |
219 zoom_widget_->Show(); | |
220 zoom_window_->layer()->SetBounds(gfx::Rect(0, 0, width, height)); | |
221 zoom_window_->Show(); | |
222 | |
223 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( | |
224 (width - (width / scale_)) / 2, | |
225 (height - (height / scale_)) / 2, | |
226 scale_); | |
227 } | |
228 } else { | |
229 if (zoom_window_) { | |
230 zoom_widget_->Close(); | |
231 zoom_window_ = NULL; | |
232 zoom_widget_ = NULL; | |
233 } | |
234 } | |
235 } | |
236 | |
237 void PartialMagnificationControllerImpl::SetEnabled(bool enabled) { | |
238 if (enabled) { | |
239 is_enabled_ = enabled; | |
240 SetScale(kDefaultPartialMagnifiedScale); | |
241 } else { | |
242 SetScale(kNonPartialMagnifiedScale); | |
243 is_enabled_ = enabled; | |
244 } | |
245 } | |
246 | |
247 bool PartialMagnificationControllerImpl::IsEnabled() { | |
248 return is_enabled_; | |
249 } | |
250 | |
251 | |
252 //////////////////////////////////////////////////////////////////////////////// | |
253 // PartialMagnificationControllerImpl: aura::EventFilter implementation | |
254 | |
255 bool PartialMagnificationControllerImpl::PreHandleKeyEvent( | |
256 aura::Window* target, | |
257 ui::KeyEvent* event) { | |
258 return false; | |
259 } | |
260 | |
261 bool PartialMagnificationControllerImpl::PreHandleMouseEvent( | |
262 aura::Window* target, | |
263 ui::MouseEvent* event) { | |
264 if (event->type() == ui::ET_SCROLL && event->IsAltDown()) { | |
265 ui::ScrollEvent* scroll_event = static_cast<ui::ScrollEvent*>(event); | |
266 if (scroll_event->y_offset() > 0) | |
267 SetScale(kDefaultPartialMagnifiedScale); | |
268 else | |
269 SetScale(1); | |
270 | |
271 return true; | |
272 } | |
273 | |
274 if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) { | |
275 aura::RootWindow* current_root = target->GetRootWindow(); | |
276 gfx::Rect root_bounds = current_root->bounds(); | |
277 | |
278 if (root_bounds.Contains(event->root_location())) { | |
oshima
2012/09/07 22:30:55
I believe this root location is native X's, and no
Zachary Kuznia
2012/09/13 11:49:02
This seems to be aura's root location. When I con
| |
279 if (current_root != root_window_) | |
280 SwitchTargetRootWindow(current_root); | |
281 | |
282 OnMouseMove(event->root_location()); | |
283 } | |
284 } | |
285 | |
286 return false; | |
287 } | |
288 | |
289 ui::TouchStatus PartialMagnificationControllerImpl::PreHandleTouchEvent( | |
290 aura::Window* target, | |
291 ui::TouchEvent* event) { | |
292 return ui::TOUCH_STATUS_UNKNOWN; | |
293 } | |
294 | |
295 ui::EventResult PartialMagnificationControllerImpl::PreHandleGestureEvent( | |
296 aura::Window* target, | |
297 ui::GestureEvent* event) { | |
298 return ui::ER_UNHANDLED; | |
299 } | |
300 | |
301 //////////////////////////////////////////////////////////////////////////////// | |
302 // PartialMagnificationController: | |
303 | |
304 // static | |
305 PartialMagnificationController* | |
306 PartialMagnificationController::CreateInstance() { | |
307 return new PartialMagnificationControllerImpl(); | |
308 } | |
309 | |
310 } // namespace internal | |
311 } // namespace ash | |
OLD | NEW |