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

Side by Side Diff: ash/wm/common/wm_toplevel_window_event_handler.cc

Issue 2001623003: Splits ToplevelWindowEventHandler into two (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix gyp Created 4 years, 7 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
« no previous file with comments | « ash/wm/common/wm_toplevel_window_event_handler.h ('k') | ash/wm/common/wm_window.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/wm/toplevel_window_event_handler.h" 5 #include "ash/wm/common/wm_toplevel_window_event_handler.h"
6 6
7 #include "ash/shell.h"
8 #include "ash/wm/aura/wm_window_aura.h"
9 #include "ash/wm/common/window_resizer.h" 7 #include "ash/wm/common/window_resizer.h"
10 #include "ash/wm/common/window_state.h" 8 #include "ash/wm/common/window_state.h"
11 #include "ash/wm/common/window_state_observer.h" 9 #include "ash/wm/common/window_state_observer.h"
12 #include "ash/wm/common/wm_event.h" 10 #include "ash/wm/common/wm_event.h"
11 #include "ash/wm/common/wm_globals.h"
12 #include "ash/wm/common/wm_window.h"
13 #include "ash/wm/common/wm_window_observer.h" 13 #include "ash/wm/common/wm_window_observer.h"
14 #include "ash/wm/resize_shadow_controller.h"
15 #include "ash/wm/window_state_aura.h"
16 #include "ash/wm/window_util.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/run_loop.h"
19 #include "ui/aura/client/cursor_client.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_delegate.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/aura/window_observer.h"
25 #include "ui/aura/window_tree_host.h"
26 #include "ui/base/cursor/cursor.h"
27 #include "ui/base/hit_test.h" 14 #include "ui/base/hit_test.h"
28 #include "ui/base/ui_base_types.h"
29 #include "ui/events/event.h" 15 #include "ui/events/event.h"
30 #include "ui/events/event_utils.h"
31 #include "ui/events/gestures/gesture_recognizer.h"
32 #include "ui/gfx/geometry/point_conversions.h"
33 16
34 namespace { 17 namespace {
35 const double kMinHorizVelocityForWindowSwipe = 1100; 18 const double kMinHorizVelocityForWindowSwipe = 1100;
36 const double kMinVertVelocityForWindowMinimize = 1000; 19 const double kMinVertVelocityForWindowMinimize = 1000;
37 } 20 }
38 21
39 namespace ash { 22 namespace ash {
23 namespace wm {
40 24
41 namespace { 25 namespace {
42 26
43 // Returns whether |window| can be moved via a two finger drag given 27 // Returns whether |window| can be moved via a two finger drag given
44 // the hittest results of the two fingers. 28 // the hittest results of the two fingers.
45 bool CanStartTwoFingerMove(aura::Window* window, 29 bool CanStartTwoFingerMove(WmWindow* window,
46 int window_component1, 30 int window_component1,
47 int window_component2) { 31 int window_component2) {
48 // We allow moving a window via two fingers when the hittest components are 32 // We allow moving a window via two fingers when the hittest components are
49 // HTCLIENT. This is done so that a window can be dragged via two fingers when 33 // HTCLIENT. This is done so that a window can be dragged via two fingers when
50 // the tab strip is full and hitting the caption area is difficult. We check 34 // the tab strip is full and hitting the caption area is difficult. We check
51 // the window type and the state type so that we do not steal touches from the 35 // the window type and the state type so that we do not steal touches from the
52 // web contents. 36 // web contents.
53 if (!wm::GetWindowState(window)->IsNormalOrSnapped() || 37 if (!window->GetWindowState()->IsNormalOrSnapped() ||
54 window->type() != ui::wm::WINDOW_TYPE_NORMAL) { 38 window->GetType() != ui::wm::WINDOW_TYPE_NORMAL) {
55 return false; 39 return false;
56 } 40 }
57 int component1_behavior = 41 int component1_behavior =
58 WindowResizer::GetBoundsChangeForWindowComponent(window_component1); 42 WindowResizer::GetBoundsChangeForWindowComponent(window_component1);
59 int component2_behavior = 43 int component2_behavior =
60 WindowResizer::GetBoundsChangeForWindowComponent(window_component2); 44 WindowResizer::GetBoundsChangeForWindowComponent(window_component2);
61 return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 && 45 return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 &&
62 (component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0; 46 (component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0;
63 } 47 }
64 48
65 // Returns whether |window| can be moved or resized via one finger given 49 // Returns whether |window| can be moved or resized via one finger given
66 // |window_component|. 50 // |window_component|.
67 bool CanStartOneFingerDrag(int window_component) { 51 bool CanStartOneFingerDrag(int window_component) {
68 return WindowResizer::GetBoundsChangeForWindowComponent( 52 return WindowResizer::GetBoundsChangeForWindowComponent(window_component) !=
69 window_component) != 0; 53 0;
70 }
71
72 gfx::Point ConvertPointToParent(aura::Window* window,
73 const gfx::Point& point) {
74 gfx::Point result(point);
75 aura::Window::ConvertPointToTarget(window, window->parent(), &result);
76 return result;
77 } 54 }
78 55
79 // Returns the window component containing |event|'s location. 56 // Returns the window component containing |event|'s location.
80 int GetWindowComponent(aura::Window* window, const ui::LocatedEvent& event) { 57 int GetWindowComponent(WmWindow* window, const ui::LocatedEvent& event) {
81 return window->delegate()->GetNonClientComponent(event.location()); 58 return window->GetNonClientComponent(event.location());
82 } 59 }
83 60
84 } // namespace 61 } // namespace
85 62
86 // ScopedWindowResizer --------------------------------------------------------- 63 // ScopedWindowResizer ---------------------------------------------------------
87 64
88 // Wraps a WindowResizer and installs an observer on its target window. When 65 // Wraps a WindowResizer and installs an observer on its target window. When
89 // the window is destroyed ResizerWindowDestroyed() is invoked back on the 66 // the window is destroyed ResizerWindowDestroyed() is invoked back on the
90 // ToplevelWindowEventHandler to clean up. 67 // WmToplevelWindowEventHandler to clean up.
91 class ToplevelWindowEventHandler::ScopedWindowResizer 68 class WmToplevelWindowEventHandler::ScopedWindowResizer
92 : public wm::WmWindowObserver, 69 : public wm::WmWindowObserver,
93 public wm::WindowStateObserver { 70 public wm::WindowStateObserver {
94 public: 71 public:
95 ScopedWindowResizer(ToplevelWindowEventHandler* handler, 72 ScopedWindowResizer(WmToplevelWindowEventHandler* handler,
96 WindowResizer* resizer); 73 std::unique_ptr<WindowResizer> resizer);
97 ~ScopedWindowResizer() override; 74 ~ScopedWindowResizer() override;
98 75
99 // Returns true if the drag moves the window and does not resize. 76 // Returns true if the drag moves the window and does not resize.
100 bool IsMove() const; 77 bool IsMove() const;
101 78
102 WindowResizer* resizer() { return resizer_.get(); } 79 WindowResizer* resizer() { return resizer_.get(); }
103 80
104 // WindowObserver overrides: 81 // WindowObserver overrides:
105 void OnWindowDestroying(wm::WmWindow* window) override; 82 void OnWindowDestroying(wm::WmWindow* window) override;
106 83
107 // WindowStateObserver overrides: 84 // WindowStateObserver overrides:
108 void OnPreWindowStateTypeChange(wm::WindowState* window_state, 85 void OnPreWindowStateTypeChange(wm::WindowState* window_state,
109 wm::WindowStateType type) override; 86 wm::WindowStateType type) override;
110 87
111 private: 88 private:
112 ToplevelWindowEventHandler* handler_; 89 WmToplevelWindowEventHandler* handler_;
113 std::unique_ptr<WindowResizer> resizer_; 90 std::unique_ptr<WindowResizer> resizer_;
114 91
115 // Whether ScopedWindowResizer grabbed capture. 92 // Whether ScopedWindowResizer grabbed capture.
116 bool grabbed_capture_; 93 bool grabbed_capture_;
117 94
118 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer); 95 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
119 }; 96 };
120 97
121 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer( 98 WmToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
122 ToplevelWindowEventHandler* handler, 99 WmToplevelWindowEventHandler* handler,
123 WindowResizer* resizer) 100 std::unique_ptr<WindowResizer> resizer)
124 : handler_(handler), 101 : handler_(handler), resizer_(std::move(resizer)), grabbed_capture_(false) {
125 resizer_(resizer),
126 grabbed_capture_(false) {
127 wm::WmWindow* target = resizer_->GetTarget(); 102 wm::WmWindow* target = resizer_->GetTarget();
128 target->AddObserver(this); 103 target->AddObserver(this);
129 target->GetWindowState()->AddObserver(this); 104 target->GetWindowState()->AddObserver(this);
130 105
131 if (!target->HasCapture()) { 106 if (!target->HasCapture()) {
132 grabbed_capture_ = true; 107 grabbed_capture_ = true;
133 target->SetCapture(); 108 target->SetCapture();
134 } 109 }
135 } 110 }
136 111
137 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() { 112 WmToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
138 wm::WmWindow* target = resizer_->GetTarget(); 113 wm::WmWindow* target = resizer_->GetTarget();
139 target->RemoveObserver(this); 114 target->RemoveObserver(this);
140 target->GetWindowState()->RemoveObserver(this); 115 target->GetWindowState()->RemoveObserver(this);
141 if (grabbed_capture_) 116 if (grabbed_capture_)
142 target->ReleaseCapture(); 117 target->ReleaseCapture();
143 } 118 }
144 119
145 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const { 120 bool WmToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
146 return resizer_->details().bounds_change == 121 return resizer_->details().bounds_change ==
147 WindowResizer::kBoundsChange_Repositions; 122 WindowResizer::kBoundsChange_Repositions;
148 } 123 }
149 124
150 void 125 void WmToplevelWindowEventHandler::ScopedWindowResizer::
151 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange( 126 OnPreWindowStateTypeChange(wm::WindowState* window_state,
152 wm::WindowState* window_state, 127 wm::WindowStateType old) {
153 wm::WindowStateType old) { 128 handler_->CompleteDrag(DragResult::SUCCESS);
154 handler_->CompleteDrag(DRAG_COMPLETE);
155 } 129 }
156 130
157 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying( 131 void WmToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
158 wm::WmWindow* window) { 132 wm::WmWindow* window) {
159 DCHECK_EQ(resizer_->GetTarget(), window); 133 DCHECK_EQ(resizer_->GetTarget(), window);
160 handler_->ResizerWindowDestroyed(); 134 handler_->ResizerWindowDestroyed();
161 } 135 }
162 136
163 // ToplevelWindowEventHandler -------------------------------------------------- 137 // WmToplevelWindowEventHandler
138 // --------------------------------------------------
164 139
165 ToplevelWindowEventHandler::ToplevelWindowEventHandler() 140 WmToplevelWindowEventHandler::WmToplevelWindowEventHandler(WmGlobals* globals)
166 : first_finger_hittest_(HTNOWHERE), 141 : globals_(globals), first_finger_hittest_(HTNOWHERE) {
167 in_move_loop_(false), 142 globals_->AddDisplayObserver(this);
168 in_gesture_drag_(false),
169 drag_reverted_(false),
170 destroyed_(NULL) {
171 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
172 } 143 }
173 144
174 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() { 145 WmToplevelWindowEventHandler::~WmToplevelWindowEventHandler() {
175 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); 146 globals_->RemoveDisplayObserver(this);
176 if (destroyed_)
177 *destroyed_ = true;
178 } 147 }
179 148
180 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) { 149 void WmToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
181 if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED && 150 if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
182 event->key_code() == ui::VKEY_ESCAPE) { 151 event->key_code() == ui::VKEY_ESCAPE) {
183 CompleteDrag(DRAG_REVERT); 152 CompleteDrag(DragResult::REVERT);
184 } 153 }
185 } 154 }
186 155
187 void ToplevelWindowEventHandler::OnMouseEvent( 156 void WmToplevelWindowEventHandler::OnMouseEvent(ui::MouseEvent* event,
188 ui::MouseEvent* event) { 157 WmWindow* target) {
189 if (event->handled()) 158 if (event->handled())
190 return; 159 return;
191 if ((event->flags() & 160 if ((event->flags() &
192 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0) 161 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
193 return; 162 return;
194 163
195 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) { 164 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) {
196 // Capture is grabbed when both gesture and mouse drags start. Handle 165 // Capture is grabbed when both gesture and mouse drags start. Handle
197 // capture loss regardless of which type of drag is in progress. 166 // capture loss regardless of which type of drag is in progress.
198 HandleCaptureLost(event); 167 HandleCaptureLost(event);
199 return; 168 return;
200 } 169 }
201 170
202 if (in_gesture_drag_) 171 if (in_gesture_drag_)
203 return; 172 return;
204 173
205 aura::Window* target = static_cast<aura::Window*>(event->target());
206 switch (event->type()) { 174 switch (event->type()) {
207 case ui::ET_MOUSE_PRESSED: 175 case ui::ET_MOUSE_PRESSED:
208 HandleMousePressed(target, event); 176 HandleMousePressed(target, event);
209 break; 177 break;
210 case ui::ET_MOUSE_DRAGGED: 178 case ui::ET_MOUSE_DRAGGED:
211 HandleDrag(target, event); 179 HandleDrag(target, event);
212 break; 180 break;
213 case ui::ET_MOUSE_RELEASED: 181 case ui::ET_MOUSE_RELEASED:
214 HandleMouseReleased(target, event); 182 HandleMouseReleased(target, event);
215 break; 183 break;
216 case ui::ET_MOUSE_MOVED: 184 case ui::ET_MOUSE_MOVED:
217 HandleMouseMoved(target, event); 185 HandleMouseMoved(target, event);
218 break; 186 break;
219 case ui::ET_MOUSE_EXITED: 187 case ui::ET_MOUSE_EXITED:
220 HandleMouseExited(target, event); 188 HandleMouseExited(target, event);
221 break; 189 break;
222 default: 190 default:
223 break; 191 break;
224 } 192 }
225 } 193 }
226 194
227 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) { 195 void WmToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event,
196 WmWindow* target) {
228 if (event->handled()) 197 if (event->handled())
229 return; 198 return;
230 aura::Window* target = static_cast<aura::Window*>(event->target()); 199 if (!target->HasNonClientArea())
231 if (!target->delegate())
232 return; 200 return;
233 201
234 if (window_resizer_.get() && !in_gesture_drag_) 202 if (window_resizer_.get() && !in_gesture_drag_)
235 return; 203 return;
236 204
237 if (window_resizer_.get() && 205 if (window_resizer_.get() &&
238 wm::WmWindowAura::GetAuraWindow( 206 window_resizer_->resizer()->GetTarget() != target) {
239 window_resizer_->resizer()->GetTarget()) != target) {
240 return; 207 return;
241 } 208 }
242 209
243 if (event->details().touch_points() > 2) { 210 if (event->details().touch_points() > 2) {
244 if (CompleteDrag(DRAG_COMPLETE)) 211 if (CompleteDrag(DragResult::SUCCESS))
245 event->StopPropagation(); 212 event->StopPropagation();
246 return; 213 return;
247 } 214 }
248 215
249 switch (event->type()) { 216 switch (event->type()) {
250 case ui::ET_GESTURE_TAP_DOWN: { 217 case ui::ET_GESTURE_TAP_DOWN: {
251 int component = GetWindowComponent(target, *event); 218 int component = GetWindowComponent(target, *event);
252 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) & 219 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
253 WindowResizer::kBoundsChange_Resizes)) 220 WindowResizer::kBoundsChange_Resizes))
254 return; 221 return;
255 ResizeShadowController* controller = 222 target->ShowResizeShadow(component);
256 Shell::GetInstance()->resize_shadow_controller();
257 if (controller)
258 controller->ShowShadow(target, component);
259 return; 223 return;
260 } 224 }
261 case ui::ET_GESTURE_END: { 225 case ui::ET_GESTURE_END: {
262 ResizeShadowController* controller = 226 target->HideResizeShadow();
263 Shell::GetInstance()->resize_shadow_controller();
264 if (controller)
265 controller->HideShadow(target);
266 227
267 if (window_resizer_.get() && 228 if (window_resizer_.get() &&
268 (event->details().touch_points() == 1 || 229 (event->details().touch_points() == 1 ||
269 !CanStartOneFingerDrag(first_finger_hittest_))) { 230 !CanStartOneFingerDrag(first_finger_hittest_))) {
270 CompleteDrag(DRAG_COMPLETE); 231 CompleteDrag(DragResult::SUCCESS);
271 event->StopPropagation(); 232 event->StopPropagation();
272 } 233 }
273 return; 234 return;
274 } 235 }
275 case ui::ET_GESTURE_BEGIN: { 236 case ui::ET_GESTURE_BEGIN: {
276 if (event->details().touch_points() == 1) { 237 if (event->details().touch_points() == 1) {
277 first_finger_hittest_ = GetWindowComponent(target, *event); 238 first_finger_hittest_ = GetWindowComponent(target, *event);
278 } else if (window_resizer_.get()) { 239 } else if (window_resizer_.get()) {
279 if (!window_resizer_->IsMove()) { 240 if (!window_resizer_->IsMove()) {
280 // The transition from resizing with one finger to resizing with two 241 // The transition from resizing with one finger to resizing with two
281 // fingers causes unintended resizing because the location of 242 // fingers causes unintended resizing because the location of
282 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first 243 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
283 // finger to the position in the middle of the two fingers. For this 244 // finger to the position in the middle of the two fingers. For this
284 // reason two finger resizing is not supported. 245 // reason two finger resizing is not supported.
285 CompleteDrag(DRAG_COMPLETE); 246 CompleteDrag(DragResult::SUCCESS);
286 event->StopPropagation(); 247 event->StopPropagation();
287 } 248 }
288 } else { 249 } else {
289 int second_finger_hittest = GetWindowComponent(target, *event); 250 int second_finger_hittest = GetWindowComponent(target, *event);
290 if (CanStartTwoFingerMove( 251 if (CanStartTwoFingerMove(target, first_finger_hittest_,
291 target, first_finger_hittest_, second_finger_hittest)) { 252 second_finger_hittest)) {
292 gfx::Point location_in_parent = 253 gfx::Point location_in_parent =
293 event->details().bounding_box().CenterPoint(); 254 event->details().bounding_box().CenterPoint();
294 AttemptToStartDrag(target, location_in_parent, HTCAPTION, 255 AttemptToStartDrag(target, location_in_parent, HTCAPTION,
295 aura::client::WINDOW_MOVE_SOURCE_TOUCH); 256 aura::client::WINDOW_MOVE_SOURCE_TOUCH,
257 EndClosure());
296 event->StopPropagation(); 258 event->StopPropagation();
297 } 259 }
298 } 260 }
299 return; 261 return;
300 } 262 }
301 case ui::ET_GESTURE_SCROLL_BEGIN: { 263 case ui::ET_GESTURE_SCROLL_BEGIN: {
302 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the 264 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
303 // window jumping upon initiating a two finger drag. When a one finger 265 // window jumping upon initiating a two finger drag. When a one finger
304 // drag is converted to a two finger drag, a jump occurs because the 266 // drag is converted to a two finger drag, a jump occurs because the
305 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single 267 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
306 // finger's position to the position in the middle of the two fingers. 268 // finger's position to the position in the middle of the two fingers.
307 if (window_resizer_.get()) 269 if (window_resizer_.get())
308 return; 270 return;
309 int component = GetWindowComponent(target, *event); 271 int component = GetWindowComponent(target, *event);
310 if (!CanStartOneFingerDrag(component)) 272 if (!CanStartOneFingerDrag(component))
311 return; 273 return;
312 gfx::Point location_in_parent( 274 gfx::Point location_in_parent(
313 ConvertPointToParent(target, event->location())); 275 target->ConvertPointToTarget(target->GetParent(), event->location()));
314 AttemptToStartDrag(target, location_in_parent, component, 276 AttemptToStartDrag(target, location_in_parent, component,
315 aura::client::WINDOW_MOVE_SOURCE_TOUCH); 277 aura::client::WINDOW_MOVE_SOURCE_TOUCH, EndClosure());
316 event->StopPropagation(); 278 event->StopPropagation();
317 return; 279 return;
318 } 280 }
319 default: 281 default:
320 break; 282 break;
321 } 283 }
322 284
323 if (!window_resizer_.get()) 285 if (!window_resizer_.get())
324 return; 286 return;
325 287
326 switch (event->type()) { 288 switch (event->type()) {
327 case ui::ET_GESTURE_SCROLL_UPDATE: 289 case ui::ET_GESTURE_SCROLL_UPDATE:
328 HandleDrag(target, event); 290 HandleDrag(target, event);
329 event->StopPropagation(); 291 event->StopPropagation();
330 return; 292 return;
331 case ui::ET_GESTURE_SCROLL_END: 293 case ui::ET_GESTURE_SCROLL_END:
332 // We must complete the drag here instead of as a result of ET_GESTURE_END 294 // We must complete the drag here instead of as a result of ET_GESTURE_END
333 // because otherwise the drag will be reverted when EndMoveLoop() is 295 // because otherwise the drag will be reverted when EndMoveLoop() is
334 // called. 296 // called.
335 // TODO(pkotwicz): Pass drag completion status to 297 // TODO(pkotwicz): Pass drag completion status to
336 // WindowMoveClient::EndMoveLoop(). 298 // WindowMoveClient::EndMoveLoop().
337 CompleteDrag(DRAG_COMPLETE); 299 CompleteDrag(DragResult::SUCCESS);
338 event->StopPropagation(); 300 event->StopPropagation();
339 return; 301 return;
340 case ui::ET_SCROLL_FLING_START: 302 case ui::ET_SCROLL_FLING_START:
341 CompleteDrag(DRAG_COMPLETE); 303 CompleteDrag(DragResult::SUCCESS);
342 304
343 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check 305 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
344 // window_resizer_->IsMove() instead of the hittest component at |event|'s 306 // window_resizer_->IsMove() instead of the hittest component at |event|'s
345 // location. 307 // location.
346 if (GetWindowComponent(target, *event) != HTCAPTION || 308 if (GetWindowComponent(target, *event) != HTCAPTION ||
347 !wm::GetWindowState(target)->IsNormalOrSnapped()) { 309 !target->GetWindowState()->IsNormalOrSnapped()) {
348 return; 310 return;
349 } 311 }
350 312
351 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) { 313 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
352 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED); 314 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
353 } else if (event->details().velocity_y() < 315 } else if (event->details().velocity_y() <
354 -kMinVertVelocityForWindowMinimize) { 316 -kMinVertVelocityForWindowMinimize) {
355 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED); 317 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
356 } else if (event->details().velocity_x() > 318 } else if (event->details().velocity_x() >
357 kMinHorizVelocityForWindowSwipe) { 319 kMinHorizVelocityForWindowSwipe) {
358 SetWindowStateTypeFromGesture(target, 320 SetWindowStateTypeFromGesture(target,
359 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED); 321 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
360 } else if (event->details().velocity_x() < 322 } else if (event->details().velocity_x() <
361 -kMinHorizVelocityForWindowSwipe) { 323 -kMinHorizVelocityForWindowSwipe) {
362 SetWindowStateTypeFromGesture(target, 324 SetWindowStateTypeFromGesture(target,
363 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED); 325 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
364 } 326 }
365 event->StopPropagation(); 327 event->StopPropagation();
366 return; 328 return;
367 case ui::ET_GESTURE_SWIPE: 329 case ui::ET_GESTURE_SWIPE:
368 DCHECK_GT(event->details().touch_points(), 0); 330 DCHECK_GT(event->details().touch_points(), 0);
369 if (event->details().touch_points() == 1) 331 if (event->details().touch_points() == 1)
370 return; 332 return;
371 if (!wm::GetWindowState(target)->IsNormalOrSnapped()) 333 if (!target->GetWindowState()->IsNormalOrSnapped())
372 return; 334 return;
373 335
374 CompleteDrag(DRAG_COMPLETE); 336 CompleteDrag(DragResult::SUCCESS);
375 337
376 if (event->details().swipe_down()) { 338 if (event->details().swipe_down()) {
377 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED); 339 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
378 } else if (event->details().swipe_up()) { 340 } else if (event->details().swipe_up()) {
379 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED); 341 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
380 } else if (event->details().swipe_right()) { 342 } else if (event->details().swipe_right()) {
381 SetWindowStateTypeFromGesture(target, 343 SetWindowStateTypeFromGesture(target,
382 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED); 344 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
383 } else { 345 } else {
384 SetWindowStateTypeFromGesture(target, 346 SetWindowStateTypeFromGesture(target,
385 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED); 347 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
386 } 348 }
387 event->StopPropagation(); 349 event->StopPropagation();
388 return; 350 return;
389 default: 351 default:
390 return; 352 return;
391 } 353 }
392 } 354 }
393 355
394 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop( 356 bool WmToplevelWindowEventHandler::AttemptToStartDrag(
395 aura::Window* source, 357 WmWindow* window,
396 const gfx::Vector2d& drag_offset,
397 aura::client::WindowMoveSource move_source) {
398 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
399 aura::Window* root_window = source->GetRootWindow();
400 DCHECK(root_window);
401 gfx::Point drag_location;
402 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
403 aura::Env::GetInstance()->is_touch_down()) {
404 gfx::PointF drag_location_f;
405 bool has_point = ui::GestureRecognizer::Get()->
406 GetLastTouchPointForTarget(source, &drag_location_f);
407 drag_location = gfx::ToFlooredPoint(drag_location_f);
408 DCHECK(has_point);
409 } else {
410 drag_location =
411 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
412 aura::Window::ConvertPointToTarget(
413 root_window, source->parent(), &drag_location);
414 }
415 // Set the cursor before calling AttemptToStartDrag(), as that will
416 // eventually call LockCursor() and prevent the cursor from changing.
417 aura::client::CursorClient* cursor_client =
418 aura::client::GetCursorClient(root_window);
419 if (cursor_client)
420 cursor_client->SetCursor(ui::kCursorPointer);
421 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
422 return aura::client::MOVE_CANCELED;
423
424 in_move_loop_ = true;
425 bool destroyed = false;
426 destroyed_ = &destroyed;
427 base::MessageLoop* loop = base::MessageLoop::current();
428 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
429 base::RunLoop run_loop;
430 quit_closure_ = run_loop.QuitClosure();
431 run_loop.Run();
432 if (destroyed)
433 return aura::client::MOVE_CANCELED;
434 destroyed_ = NULL;
435 in_move_loop_ = false;
436 return drag_reverted_ ? aura::client::MOVE_CANCELED :
437 aura::client::MOVE_SUCCESSFUL;
438 }
439
440 void ToplevelWindowEventHandler::EndMoveLoop() {
441 if (in_move_loop_)
442 CompleteDrag(DRAG_REVERT);
443 }
444
445 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
446 CompleteDrag(DRAG_REVERT);
447 }
448
449 bool ToplevelWindowEventHandler::AttemptToStartDrag(
450 aura::Window* window,
451 const gfx::Point& point_in_parent, 358 const gfx::Point& point_in_parent,
452 int window_component, 359 int window_component,
453 aura::client::WindowMoveSource source) { 360 aura::client::WindowMoveSource source,
361 const EndClosure& end_closure) {
454 if (window_resizer_.get()) 362 if (window_resizer_.get())
455 return false; 363 return false;
456 WindowResizer* resizer = 364 std::unique_ptr<WindowResizer> resizer(
457 CreateWindowResizer(wm::WmWindowAura::Get(window), point_in_parent, 365 CreateWindowResizer(window, point_in_parent, window_component, source));
458 window_component, source)
459 .release();
460 if (!resizer) 366 if (!resizer)
461 return false; 367 return false;
462 368
463 window_resizer_.reset(new ScopedWindowResizer(this, resizer)); 369 end_closure_ = end_closure;
370 window_resizer_.reset(new ScopedWindowResizer(this, std::move(resizer)));
464 371
465 pre_drag_window_bounds_ = window->bounds(); 372 pre_drag_window_bounds_ = window->GetBounds();
466 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH); 373 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
467 return true; 374 return true;
468 } 375 }
469 376
470 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) { 377 void WmToplevelWindowEventHandler::RevertDrag() {
378 CompleteDrag(DragResult::REVERT);
379 }
380
381 bool WmToplevelWindowEventHandler::CompleteDrag(DragResult result) {
471 if (!window_resizer_) 382 if (!window_resizer_)
472 return false; 383 return false;
473 384
474 std::unique_ptr<ScopedWindowResizer> resizer(window_resizer_.release()); 385 std::unique_ptr<ScopedWindowResizer> resizer(std::move(window_resizer_));
475 switch (status) { 386 switch (result) {
476 case DRAG_COMPLETE: 387 case DragResult::SUCCESS:
477 resizer->resizer()->CompleteDrag(); 388 resizer->resizer()->CompleteDrag();
478 break; 389 break;
479 case DRAG_REVERT: 390 case DragResult::REVERT:
480 resizer->resizer()->RevertDrag(); 391 resizer->resizer()->RevertDrag();
481 break; 392 break;
482 case DRAG_RESIZER_WINDOW_DESTROYED: 393 case DragResult::WINDOW_DESTROYED:
483 // We explicitly do not invoke RevertDrag() since that may do things to 394 // We explicitly do not invoke RevertDrag() since that may do things to
484 // WindowResizer::GetAuraTarget() which was destroyed. 395 // the window that was destroyed.
485 break; 396 break;
486 } 397 }
487 drag_reverted_ = (status != DRAG_COMPLETE);
488 398
489 first_finger_hittest_ = HTNOWHERE; 399 first_finger_hittest_ = HTNOWHERE;
490 in_gesture_drag_ = false; 400 in_gesture_drag_ = false;
491 if (in_move_loop_) 401 if (!end_closure_.is_null()) {
492 quit_closure_.Run(); 402 // Clear local state in case running the closure deletes us.
403 EndClosure end_closure = end_closure_;
404 end_closure_.Reset();
405 end_closure.Run(result);
406 }
493 return true; 407 return true;
494 } 408 }
495 409
496 void ToplevelWindowEventHandler::HandleMousePressed( 410 void WmToplevelWindowEventHandler::HandleMousePressed(WmWindow* target,
497 aura::Window* target, 411 ui::MouseEvent* event) {
498 ui::MouseEvent* event) { 412 if (event->phase() != ui::EP_PRETARGET || !target->HasNonClientArea())
499 if (event->phase() != ui::EP_PRETARGET || !target->delegate())
500 return; 413 return;
501 414
502 // We also update the current window component here because for the 415 // We also update the current window component here because for the
503 // mouse-drag-release-press case, where the mouse is released and 416 // mouse-drag-release-press case, where the mouse is released and
504 // pressed without mouse move event. 417 // pressed without mouse move event.
505 int component = GetWindowComponent(target, *event); 418 int component = GetWindowComponent(target, *event);
506 if ((event->flags() & 419 if ((event->flags() & (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) ==
507 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 && 420 0 &&
508 WindowResizer::GetBoundsChangeForWindowComponent(component)) { 421 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
509 gfx::Point location_in_parent( 422 gfx::Point location_in_parent(
510 ConvertPointToParent(target, event->location())); 423 target->ConvertPointToTarget(target->GetParent(), event->location()));
511 AttemptToStartDrag(target, location_in_parent, component, 424 AttemptToStartDrag(target, location_in_parent, component,
512 aura::client::WINDOW_MOVE_SOURCE_MOUSE); 425 aura::client::WINDOW_MOVE_SOURCE_MOUSE, EndClosure());
513 // Set as handled so that other event handlers do no act upon the event 426 // Set as handled so that other event handlers do no act upon the event
514 // but still receive it so that they receive both parts of each pressed/ 427 // but still receive it so that they receive both parts of each pressed/
515 // released pair. 428 // released pair.
516 event->SetHandled(); 429 event->SetHandled();
517 } else { 430 } else {
518 CompleteDrag(DRAG_COMPLETE); 431 CompleteDrag(DragResult::SUCCESS);
519 } 432 }
520 } 433 }
521 434
522 void ToplevelWindowEventHandler::HandleMouseReleased( 435 void WmToplevelWindowEventHandler::HandleMouseReleased(WmWindow* target,
523 aura::Window* target, 436 ui::MouseEvent* event) {
524 ui::MouseEvent* event) {
525 if (event->phase() == ui::EP_PRETARGET) 437 if (event->phase() == ui::EP_PRETARGET)
526 CompleteDrag(DRAG_COMPLETE); 438 CompleteDrag(DragResult::SUCCESS);
527 } 439 }
528 440
529 void ToplevelWindowEventHandler::HandleDrag( 441 void WmToplevelWindowEventHandler::HandleDrag(WmWindow* target,
530 aura::Window* target, 442 ui::LocatedEvent* event) {
531 ui::LocatedEvent* event) {
532 // This function only be triggered to move window 443 // This function only be triggered to move window
533 // by mouse drag or touch move event. 444 // by mouse drag or touch move event.
534 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED || 445 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
535 event->type() == ui::ET_TOUCH_MOVED || 446 event->type() == ui::ET_TOUCH_MOVED ||
536 event->type() == ui::ET_GESTURE_SCROLL_UPDATE); 447 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
537 448
538 // Drag actions are performed pre-target handling to prevent spurious mouse 449 // Drag actions are performed pre-target handling to prevent spurious mouse
539 // moves from the move/size operation from being sent to the target. 450 // moves from the move/size operation from being sent to the target.
540 if (event->phase() != ui::EP_PRETARGET) 451 if (event->phase() != ui::EP_PRETARGET)
541 return; 452 return;
542 453
543 if (!window_resizer_) 454 if (!window_resizer_)
544 return; 455 return;
545 window_resizer_->resizer()->Drag( 456 window_resizer_->resizer()->Drag(
546 ConvertPointToParent(target, event->location()), event->flags()); 457 target->ConvertPointToTarget(target->GetParent(), event->location()),
458 event->flags());
547 event->StopPropagation(); 459 event->StopPropagation();
548 } 460 }
549 461
550 void ToplevelWindowEventHandler::HandleMouseMoved( 462 void WmToplevelWindowEventHandler::HandleMouseMoved(WmWindow* target,
551 aura::Window* target, 463 ui::LocatedEvent* event) {
552 ui::LocatedEvent* event) {
553 // Shadow effects are applied after target handling. Note that we don't 464 // Shadow effects are applied after target handling. Note that we don't
554 // respect ER_HANDLED here right now since we have not had a reason to allow 465 // respect ER_HANDLED here right now since we have not had a reason to allow
555 // the target to cancel shadow rendering. 466 // the target to cancel shadow rendering.
556 if (event->phase() != ui::EP_POSTTARGET || !target->delegate()) 467 if (event->phase() != ui::EP_POSTTARGET || !target->HasNonClientArea())
557 return; 468 return;
558 469
559 // TODO(jamescook): Move the resize cursor update code into here from 470 // TODO(jamescook): Move the resize cursor update code into here from
560 // CompoundEventFilter? 471 // CompoundEventFilter?
561 ResizeShadowController* controller = 472 if (event->flags() & ui::EF_IS_NON_CLIENT) {
562 Shell::GetInstance()->resize_shadow_controller(); 473 int component = target->GetNonClientComponent(event->location());
563 if (controller) { 474 target->ShowResizeShadow(component);
564 if (event->flags() & ui::EF_IS_NON_CLIENT) { 475 } else {
565 int component = 476 target->HideResizeShadow();
566 target->delegate()->GetNonClientComponent(event->location());
567 controller->ShowShadow(target, component);
568 } else {
569 controller->HideShadow(target);
570 }
571 } 477 }
572 } 478 }
573 479
574 void ToplevelWindowEventHandler::HandleMouseExited( 480 void WmToplevelWindowEventHandler::HandleMouseExited(WmWindow* target,
575 aura::Window* target, 481 ui::LocatedEvent* event) {
576 ui::LocatedEvent* event) {
577 // Shadow effects are applied after target handling. Note that we don't 482 // Shadow effects are applied after target handling. Note that we don't
578 // respect ER_HANDLED here right now since we have not had a reason to allow 483 // respect ER_HANDLED here right now since we have not had a reason to allow
579 // the target to cancel shadow rendering. 484 // the target to cancel shadow rendering.
580 if (event->phase() != ui::EP_POSTTARGET) 485 if (event->phase() != ui::EP_POSTTARGET)
581 return; 486 return;
582 487
583 ResizeShadowController* controller = 488 target->HideResizeShadow();
584 Shell::GetInstance()->resize_shadow_controller();
585 if (controller)
586 controller->HideShadow(target);
587 } 489 }
588 490
589 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) { 491 void WmToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) {
590 if (event->phase() == ui::EP_PRETARGET) { 492 if (event->phase() == ui::EP_PRETARGET) {
591 // We complete the drag instead of reverting it, as reverting it will result 493 // We complete the drag instead of reverting it, as reverting it will result
592 // in a weird behavior when a dragged tab produces a modal dialog while the 494 // in a weird behavior when a dragged tab produces a modal dialog while the
593 // drag is in progress. crbug.com/558201. 495 // drag is in progress. crbug.com/558201.
594 CompleteDrag(DRAG_COMPLETE); 496 CompleteDrag(DragResult::SUCCESS);
595 } 497 }
596 } 498 }
597 499
598 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture( 500 void WmToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
599 aura::Window* window, 501 WmWindow* window,
600 wm::WindowStateType new_state_type) { 502 wm::WindowStateType new_state_type) {
601 wm::WindowState* window_state = ash::wm::GetWindowState(window); 503 wm::WindowState* window_state = window->GetWindowState();
602 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds, 504 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
603 // SetRestoreBoundsInParent) that modifies the window state 505 // SetRestoreBoundsInParent) that modifies the window state
604 // into WindowState. 506 // into WindowState.
605 switch (new_state_type) { 507 switch (new_state_type) {
606 case wm::WINDOW_STATE_TYPE_MINIMIZED: 508 case wm::WINDOW_STATE_TYPE_MINIMIZED:
607 if (window_state->CanMinimize()) { 509 if (window_state->CanMinimize()) {
608 window_state->Minimize(); 510 window_state->Minimize();
609 window_state->set_unminimize_to_restore_bounds(true); 511 window_state->set_unminimize_to_restore_bounds(true);
610 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); 512 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
611 } 513 }
(...skipping 16 matching lines...) Expand all
628 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); 530 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
629 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT); 531 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
630 window_state->OnWMEvent(&event); 532 window_state->OnWMEvent(&event);
631 } 533 }
632 break; 534 break;
633 default: 535 default:
634 NOTREACHED(); 536 NOTREACHED();
635 } 537 }
636 } 538 }
637 539
638 void ToplevelWindowEventHandler::ResizerWindowDestroyed() { 540 void WmToplevelWindowEventHandler::ResizerWindowDestroyed() {
639 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED); 541 CompleteDrag(DragResult::WINDOW_DESTROYED);
640 } 542 }
641 543
544 void WmToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
545 CompleteDrag(DragResult::REVERT);
546 }
547
548 } // namespace wm
642 } // namespace ash 549 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/common/wm_toplevel_window_event_handler.h ('k') | ash/wm/common/wm_window.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698