toplevel_window_event_handler.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. #include "ash/wm/toplevel_window_event_handler.h"
  5. #include "ash/constants/app_types.h"
  6. #include "ash/public/cpp/window_properties.h"
  7. #include "ash/shell.h"
  8. #include "ash/wm/multitask_menu_nudge_controller.h"
  9. #include "ash/wm/resize_shadow.h"
  10. #include "ash/wm/resize_shadow_controller.h"
  11. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  12. #include "ash/wm/window_resizer.h"
  13. #include "ash/wm/window_state_observer.h"
  14. #include "ash/wm/window_util.h"
  15. #include "base/bind.h"
  16. #include "base/run_loop.h"
  17. #include "ui/aura/client/aura_constants.h"
  18. #include "ui/aura/env.h"
  19. #include "ui/aura/window.h"
  20. #include "ui/aura/window_delegate.h"
  21. #include "ui/aura/window_event_dispatcher.h"
  22. #include "ui/aura/window_observer.h"
  23. #include "ui/aura/window_tracker.h"
  24. #include "ui/aura/window_tree_host.h"
  25. #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
  26. #include "ui/base/hit_test.h"
  27. #include "ui/events/event.h"
  28. #include "ui/views/widget/widget.h"
  29. #include "ui/wm/core/coordinate_conversion.h"
  30. namespace ash {
  31. namespace {
  32. // How many pixels are reserved for gesture events to start dragging the app
  33. // window from the top of the screen in tablet mode.
  34. constexpr int kDragStartTopEdgeInset = 8;
  35. // Returns whether |window| can be moved via a two finger drag given
  36. // the hittest results of the two fingers.
  37. bool CanStartTwoFingerMove(aura::Window* window,
  38. int window_component1,
  39. int window_component2) {
  40. // We allow moving a window via two fingers when the hittest components are
  41. // HTCLIENT. This is done so that a window can be dragged via two fingers when
  42. // the tab strip is full and hitting the caption area is difficult. We check
  43. // the window type and the state type so that we do not steal touches from the
  44. // web contents.
  45. if (window->GetType() != aura::client::WINDOW_TYPE_NORMAL ||
  46. !WindowState::Get(window) ||
  47. !WindowState::Get(window)->IsNormalOrSnapped()) {
  48. return false;
  49. }
  50. int component1_behavior =
  51. WindowResizer::GetBoundsChangeForWindowComponent(window_component1);
  52. int component2_behavior =
  53. WindowResizer::GetBoundsChangeForWindowComponent(window_component2);
  54. return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 &&
  55. (component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0;
  56. }
  57. // Returns whether |window| can be moved or resized via one finger given
  58. // |window_component|.
  59. bool CanStartOneFingerDrag(int window_component) {
  60. return WindowResizer::GetBoundsChangeForWindowComponent(window_component) !=
  61. 0;
  62. }
  63. void ShowResizeShadow(aura::Window* window, int component) {
  64. // Don't show resize shadow if
  65. // 1) the window is not toplevel.
  66. // 2) the device is in tablet mode.
  67. // 3) the window is not resizable.
  68. if (Shell::Get()->tablet_mode_controller()->InTabletMode() ||
  69. window != window->GetToplevelWindow() ||
  70. ((window->GetProperty(aura::client::kResizeBehaviorKey) &
  71. aura::client::kResizeBehaviorCanResize) == 0)) {
  72. return;
  73. }
  74. ResizeShadowController* resize_shadow_controller =
  75. Shell::Get()->resize_shadow_controller();
  76. if (resize_shadow_controller)
  77. resize_shadow_controller->ShowShadow(window, component);
  78. }
  79. void HideResizeShadow(aura::Window* window) {
  80. ResizeShadowController* resize_shadow_controller =
  81. Shell::Get()->resize_shadow_controller();
  82. if (resize_shadow_controller &&
  83. window->GetProperty(kResizeShadowTypeKey) == ResizeShadowType::kUnlock) {
  84. resize_shadow_controller->HideShadow(window);
  85. }
  86. }
  87. // Called once the drag completes.
  88. void OnDragCompleted(
  89. ToplevelWindowEventHandler::DragResult* result_return_value,
  90. base::RunLoop* run_loop,
  91. ToplevelWindowEventHandler::DragResult result) {
  92. *result_return_value = result;
  93. run_loop->Quit();
  94. }
  95. } // namespace
  96. // -----------------------------------------------------------------------------
  97. // ToplevelWindowEventHandler::ScopedWindowResizer:
  98. // Wraps a WindowResizer and installs an observer on its target window. When
  99. // the window is destroyed ResizerWindowDestroyed() is invoked back on the
  100. // ToplevelWindowEventHandler to clean up.
  101. class ToplevelWindowEventHandler::ScopedWindowResizer
  102. : public aura::WindowObserver,
  103. public WindowStateObserver {
  104. public:
  105. ScopedWindowResizer(ToplevelWindowEventHandler* handler,
  106. std::unique_ptr<WindowResizer> resizer,
  107. bool grab_capture);
  108. ScopedWindowResizer(const ScopedWindowResizer&) = delete;
  109. ScopedWindowResizer& operator=(const ScopedWindowResizer&) = delete;
  110. ~ScopedWindowResizer() override;
  111. // Returns true if the drag moves the window and does not resize.
  112. bool IsMove() const;
  113. // Returns true if the window may be resized.
  114. bool IsResize() const;
  115. WindowResizer* resizer() { return resizer_.get(); }
  116. // WindowObserver overrides:
  117. void OnWindowDestroying(aura::Window* window) override;
  118. // WindowStateObserver overrides:
  119. void OnPreWindowStateTypeChange(WindowState* window_state,
  120. chromeos::WindowStateType type) override;
  121. private:
  122. ToplevelWindowEventHandler* handler_;
  123. std::unique_ptr<WindowResizer> resizer_;
  124. // Whether ScopedWindowResizer grabbed capture.
  125. bool grabbed_capture_;
  126. // Set to true if OnWindowDestroying() is received.
  127. bool window_destroying_ = false;
  128. };
  129. ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
  130. ToplevelWindowEventHandler* handler,
  131. std::unique_ptr<WindowResizer> resizer,
  132. bool grab_capture)
  133. : handler_(handler), resizer_(std::move(resizer)), grabbed_capture_(false) {
  134. aura::Window* target = resizer_->GetTarget();
  135. target->AddObserver(this);
  136. WindowState::Get(target)->AddObserver(this);
  137. if (IsResize())
  138. target->NotifyResizeLoopStarted();
  139. if (grab_capture && !target->HasCapture()) {
  140. grabbed_capture_ = true;
  141. target->SetCapture();
  142. }
  143. }
  144. ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
  145. aura::Window* target = resizer_->GetTarget();
  146. target->RemoveObserver(this);
  147. WindowState::Get(target)->RemoveObserver(this);
  148. if (grabbed_capture_)
  149. target->ReleaseCapture();
  150. if (!window_destroying_ && IsResize())
  151. target->NotifyResizeLoopEnded();
  152. }
  153. bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
  154. return resizer_->details().bounds_change ==
  155. WindowResizer::kBoundsChange_Repositions;
  156. }
  157. bool ToplevelWindowEventHandler::ScopedWindowResizer::IsResize() const {
  158. return (resizer_->details().bounds_change &
  159. WindowResizer::kBoundsChange_Resizes) != 0;
  160. }
  161. void ToplevelWindowEventHandler::ScopedWindowResizer::
  162. OnPreWindowStateTypeChange(WindowState* window_state,
  163. chromeos::WindowStateType old) {
  164. handler_->CompleteDrag(DragResult::SUCCESS);
  165. }
  166. void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
  167. aura::Window* window) {
  168. DCHECK_EQ(resizer_->GetTarget(), window);
  169. window_destroying_ = true;
  170. handler_->ResizerWindowDestroyed();
  171. }
  172. // -----------------------------------------------------------------------------
  173. // ToplevelWindowEventHandler:
  174. ToplevelWindowEventHandler::ToplevelWindowEventHandler()
  175. : first_finger_hittest_(HTNOWHERE) {
  176. Shell::Get()->window_tree_host_manager()->AddObserver(this);
  177. }
  178. ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
  179. Shell::Get()->window_tree_host_manager()->RemoveObserver(this);
  180. }
  181. void ToplevelWindowEventHandler::OnDisplayMetricsChanged(
  182. const display::Display& display,
  183. uint32_t metrics) {
  184. if (!window_resizer_ || !(metrics & DISPLAY_METRIC_ROTATION))
  185. return;
  186. display::Display current_display =
  187. display::Screen::GetScreen()->GetDisplayNearestWindow(
  188. window_resizer_->resizer()->GetTarget());
  189. if (display.id() != current_display.id())
  190. return;
  191. RevertDrag();
  192. }
  193. void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
  194. if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
  195. event->key_code() == ui::VKEY_ESCAPE) {
  196. CompleteDrag(DragResult::REVERT);
  197. }
  198. }
  199. void ToplevelWindowEventHandler::OnMouseEvent(ui::MouseEvent* event) {
  200. UpdateGestureTarget(nullptr);
  201. if (event->handled())
  202. return;
  203. if ((event->flags() &
  204. (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
  205. return;
  206. if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) {
  207. // Capture is grabbed when both gesture and mouse drags start. Handle
  208. // capture loss regardless of which type of drag is in progress.
  209. HandleCaptureLost(event);
  210. return;
  211. }
  212. if (in_gesture_drag_)
  213. return;
  214. aura::Window* target = static_cast<aura::Window*>(event->target());
  215. switch (event->type()) {
  216. case ui::ET_MOUSE_PRESSED:
  217. HandleMousePressed(target, event);
  218. break;
  219. case ui::ET_MOUSE_DRAGGED:
  220. HandleDrag(target, event);
  221. break;
  222. case ui::ET_MOUSE_RELEASED:
  223. HandleMouseReleased(target, event);
  224. break;
  225. case ui::ET_MOUSE_MOVED:
  226. HandleMouseMoved(target, event);
  227. break;
  228. case ui::ET_MOUSE_EXITED:
  229. HandleMouseExited(target, event);
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
  236. if (event->phase() != ui::EP_PRETARGET)
  237. return;
  238. aura::Window* target = static_cast<aura::Window*>(event->target());
  239. int component = window_util::GetNonClientComponent(target, event->location());
  240. gfx::PointF event_location = event->location_f();
  241. aura::Window* original_target = target;
  242. bool client_area_drag = false;
  243. if (component == HTCLIENT) {
  244. // When dragging on a client area starts a gesture drag, |this| stops the
  245. // propagation of the ET_GESTURE_SCROLL_BEGIN event. Subsequent gestures on
  246. // the HTCLIENT area should also be stopped lest the client receive an
  247. // ET_GESTURE_SCROLL_UPDATE without the ET_GESTURE_SCROLL_BEGIN.
  248. if (in_gesture_drag_ && target != gesture_target_) {
  249. event->StopPropagation();
  250. return;
  251. }
  252. aura::Window* new_target = GetTargetForClientAreaGesture(event, target);
  253. client_area_drag = !!new_target;
  254. if (new_target && (target != new_target)) {
  255. DCHECK_EQ(ui::ET_GESTURE_SCROLL_BEGIN, event->type());
  256. aura::Window::ConvertPointToTarget(target, new_target, &event_location);
  257. aura::Env::GetInstance()->gesture_recognizer()->TransferEventsTo(
  258. original_target, new_target, ui::TransferTouchesBehavior::kCancel);
  259. UpdateGestureTarget(new_target, event_location);
  260. target = new_target;
  261. }
  262. }
  263. if (event->type() == ui::ET_GESTURE_END)
  264. UpdateGestureTarget(nullptr);
  265. else if (event->type() == ui::ET_GESTURE_BEGIN)
  266. UpdateGestureTarget(target, event_location);
  267. if (event->handled())
  268. return;
  269. if (!target->delegate())
  270. return;
  271. if (window_resizer_ && !in_gesture_drag_)
  272. return;
  273. if (window_resizer_ && window_resizer_->resizer()->GetTarget() != target &&
  274. !target->bounds().IsEmpty())
  275. return;
  276. if (event->details().touch_points() > 2) {
  277. if (CompleteDrag(DragResult::SUCCESS))
  278. event->StopPropagation();
  279. return;
  280. }
  281. switch (event->type()) {
  282. case ui::ET_GESTURE_TAP_DOWN: {
  283. if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
  284. WindowResizer::kBoundsChange_Resizes) ||
  285. (!client_area_drag && !CanStartOneFingerDrag(component)))
  286. return;
  287. ShowResizeShadow(target, component);
  288. gfx::PointF location_in_parent = event_location;
  289. aura::Window::ConvertPointToTarget(target, target->parent(),
  290. &location_in_parent);
  291. AttemptToStartDrag(target, location_in_parent, component,
  292. ::wm::WINDOW_MOVE_SOURCE_TOUCH, EndClosure(),
  293. /*update_gesture_target=*/false);
  294. event->StopPropagation();
  295. return;
  296. }
  297. case ui::ET_GESTURE_END: {
  298. HideResizeShadow(target);
  299. if (window_resizer_ && (event->details().touch_points() == 1 ||
  300. !CanStartOneFingerDrag(first_finger_hittest_))) {
  301. CompleteDrag(DragResult::SUCCESS);
  302. event->StopPropagation();
  303. }
  304. return;
  305. }
  306. case ui::ET_GESTURE_BEGIN: {
  307. if (event->details().touch_points() == 1) {
  308. first_finger_touch_point_ = event_location;
  309. aura::Window::ConvertPointToTarget(target, target->parent(),
  310. &first_finger_touch_point_);
  311. first_finger_hittest_ = component;
  312. } else if (window_resizer_) {
  313. if (!window_resizer_->IsMove()) {
  314. // The transition from resizing with one finger to resizing with two
  315. // fingers causes unintended resizing because the location of
  316. // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
  317. // finger to the position in the middle of the two fingers. For this
  318. // reason two finger resizing is not supported.
  319. CompleteDrag(DragResult::SUCCESS);
  320. event->StopPropagation();
  321. }
  322. } else {
  323. int second_finger_hittest = component;
  324. if (CanStartTwoFingerMove(target, first_finger_hittest_,
  325. second_finger_hittest)) {
  326. AttemptToStartDrag(target, first_finger_touch_point_, HTCAPTION,
  327. ::wm::WINDOW_MOVE_SOURCE_TOUCH, EndClosure(),
  328. /*update_gesture_target=*/false);
  329. event->StopPropagation();
  330. }
  331. }
  332. return;
  333. }
  334. case ui::ET_GESTURE_SCROLL_BEGIN: {
  335. // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
  336. // window jumping upon initiating a two finger drag. When a one finger
  337. // drag is converted to a two finger drag, a jump occurs because the
  338. // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
  339. // finger's position to the position in the middle of the two fingers.
  340. if (window_resizer_.get())
  341. return;
  342. if (!client_area_drag && !CanStartOneFingerDrag(component))
  343. return;
  344. gfx::PointF location_in_parent = event_location;
  345. aura::Window::ConvertPointToTarget(target, target->parent(),
  346. &location_in_parent);
  347. AttemptToStartDrag(target, location_in_parent, component,
  348. ::wm::WINDOW_MOVE_SOURCE_TOUCH, EndClosure(),
  349. /*update_gesture_target=*/false);
  350. event->StopPropagation();
  351. return;
  352. }
  353. default:
  354. break;
  355. }
  356. if (!window_resizer_.get())
  357. return;
  358. switch (event->type()) {
  359. case ui::ET_GESTURE_SCROLL_UPDATE: {
  360. gfx::Rect bounds_in_screen = target->GetRootWindow()->GetBoundsInScreen();
  361. gfx::PointF screen_location = event->location_f();
  362. ::wm::ConvertPointToScreen(target, &screen_location);
  363. // It is physically not possible to move a touch pointer from one display
  364. // to another, so constrain the bounds to the display. This is important,
  365. // as it is possible for touch points to extend outside the bounds of the
  366. // display (as happens with gestures on the bezel), and dragging via touch
  367. // should not trigger moving to a new display.(see
  368. // https://crbug.com/917060)
  369. if (!bounds_in_screen.Contains(gfx::ToRoundedPoint(screen_location))) {
  370. float x = std::max(
  371. std::min(screen_location.x(), bounds_in_screen.right() - 1.f),
  372. static_cast<float>(bounds_in_screen.x()));
  373. float y = std::max(
  374. std::min(screen_location.y(), bounds_in_screen.bottom() - 1.f),
  375. static_cast<float>(bounds_in_screen.y()));
  376. gfx::PointF updated_location(x, y);
  377. ::wm::ConvertPointFromScreen(target, &updated_location);
  378. event->set_location_f(updated_location);
  379. }
  380. HandleDrag(target, event);
  381. event->StopPropagation();
  382. return;
  383. }
  384. case ui::ET_GESTURE_SCROLL_END:
  385. // We must complete the drag here instead of as a result of ET_GESTURE_END
  386. // because otherwise the drag will be reverted when EndMoveLoop() is
  387. // called.
  388. // TODO(pkotwicz): Pass drag completion status to
  389. // WindowMoveClient::EndMoveLoop().
  390. CompleteDrag(DragResult::SUCCESS);
  391. event->StopPropagation();
  392. return;
  393. case ui::ET_SCROLL_FLING_START:
  394. [[fallthrough]];
  395. case ui::ET_GESTURE_SWIPE:
  396. HandleFlingOrSwipe(event);
  397. return;
  398. default:
  399. return;
  400. }
  401. }
  402. wm::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
  403. aura::Window* source,
  404. const gfx::Vector2d& drag_offset,
  405. ::wm::WindowMoveSource move_source) {
  406. DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
  407. aura::Window* root_window = source->GetRootWindow();
  408. DCHECK(root_window);
  409. gfx::PointF drag_location;
  410. if (move_source == ::wm::WINDOW_MOVE_SOURCE_TOUCH &&
  411. aura::Env::GetInstance()->is_touch_down()) {
  412. gfx::PointF drag_location_f;
  413. bool has_point = aura::Env::GetInstance()
  414. ->gesture_recognizer()
  415. ->GetLastTouchPointForTarget(source, &drag_location_f);
  416. drag_location = drag_location_f;
  417. DCHECK(has_point);
  418. } else {
  419. drag_location = gfx::PointF(
  420. root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
  421. aura::Window::ConvertPointToTarget(root_window, source->parent(),
  422. &drag_location);
  423. }
  424. // Set the cursor before calling AttemptToStartDrag(), as that will
  425. // eventually call LockCursor() and prevent the cursor from changing.
  426. aura::client::CursorClient* cursor_client =
  427. aura::client::GetCursorClient(root_window);
  428. if (cursor_client)
  429. cursor_client->SetCursor(ui::mojom::CursorType::kPointer);
  430. base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  431. DragResult result = DragResult::SUCCESS;
  432. if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source,
  433. base::BindOnce(OnDragCompleted, &result, &run_loop),
  434. /*update_gesture_target=*/false)) {
  435. return ::wm::MOVE_CANCELED;
  436. }
  437. in_move_loop_ = true;
  438. base::WeakPtr<ToplevelWindowEventHandler> weak_ptr(
  439. weak_factory_.GetWeakPtr());
  440. // Disable window position auto management while dragging and restore it
  441. // aftrewards.
  442. WindowState* window_state = WindowState::Get(source);
  443. const bool window_position_managed = window_state->GetWindowPositionManaged();
  444. window_state->SetWindowPositionManaged(false);
  445. aura::WindowTracker tracker({source});
  446. run_loop.Run();
  447. if (!weak_ptr)
  448. return ::wm::MOVE_CANCELED;
  449. // Make sure the window hasn't been deleted.
  450. if (tracker.Contains(source))
  451. window_state->SetWindowPositionManaged(window_position_managed);
  452. in_move_loop_ = false;
  453. return result == DragResult::SUCCESS ? ::wm::MOVE_SUCCESSFUL
  454. : ::wm::MOVE_CANCELED;
  455. }
  456. void ToplevelWindowEventHandler::EndMoveLoop() {
  457. if (in_move_loop_)
  458. RevertDrag();
  459. }
  460. bool ToplevelWindowEventHandler::AttemptToStartDrag(
  461. aura::Window* window,
  462. const gfx::PointF& point_in_parent,
  463. int window_component,
  464. ToplevelWindowEventHandler::EndClosure end_closure) {
  465. ::wm::WindowMoveSource source = gesture_target_
  466. ? ::wm::WINDOW_MOVE_SOURCE_TOUCH
  467. : ::wm::WINDOW_MOVE_SOURCE_MOUSE;
  468. return AttemptToStartDrag(window, point_in_parent, window_component, source,
  469. std::move(end_closure),
  470. /*update_gesture_target=*/true);
  471. }
  472. bool ToplevelWindowEventHandler::AttemptToStartDrag(
  473. aura::Window* window,
  474. const gfx::PointF& point_in_parent,
  475. int window_component,
  476. ::wm::WindowMoveSource source,
  477. EndClosure end_closure,
  478. bool update_gesture_target,
  479. bool grab_capture) {
  480. if (gesture_target_ != nullptr && update_gesture_target) {
  481. DCHECK_EQ(source, ::wm::WINDOW_MOVE_SOURCE_TOUCH);
  482. // Transfer events for gesture if switching to new target.
  483. aura::Env::GetInstance()->gesture_recognizer()->TransferEventsTo(
  484. gesture_target_, window, ui::TransferTouchesBehavior::kDontCancel);
  485. }
  486. if (!PrepareForDrag(window, point_in_parent, window_component, source,
  487. grab_capture)) {
  488. // Treat failure to start as a revert.
  489. if (end_closure)
  490. std::move(end_closure).Run(DragResult::REVERT);
  491. return false;
  492. }
  493. end_closure_ = std::move(end_closure);
  494. in_gesture_drag_ = (source == ::wm::WINDOW_MOVE_SOURCE_TOUCH);
  495. // |gesture_target_| needs to be updated if the drag originated from a
  496. // client (i.e. |this| never handled ET_GESTURE_EVENT_BEGIN).
  497. if (in_gesture_drag_ && (!gesture_target_ || update_gesture_target)) {
  498. UpdateGestureTarget(window);
  499. }
  500. return true;
  501. }
  502. void ToplevelWindowEventHandler::RevertDrag() {
  503. CompleteDrag(DragResult::REVERT);
  504. }
  505. aura::Window* ToplevelWindowEventHandler::GetTargetForClientAreaGesture(
  506. ui::GestureEvent* event,
  507. aura::Window* target) {
  508. if (event->type() != ui::ET_GESTURE_SCROLL_BEGIN)
  509. return nullptr;
  510. views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(target);
  511. if (!widget)
  512. return nullptr;
  513. aura::Window* toplevel = widget->GetNativeWindow();
  514. if (!Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  515. return nullptr;
  516. }
  517. WindowState* window_state = WindowState::Get(toplevel);
  518. if (!window_state ||
  519. (!window_state->IsMaximized() && !window_state->IsFullscreen() &&
  520. !window_state->IsSnapped())) {
  521. return nullptr;
  522. }
  523. if (toplevel->GetProperty(aura::client::kAppType) ==
  524. static_cast<int>(AppType::BROWSER)) {
  525. return nullptr;
  526. }
  527. if (event->details().scroll_y_hint() < 0)
  528. return nullptr;
  529. const gfx::Point location_in_screen =
  530. event->target()->GetScreenLocation(*event);
  531. const gfx::Rect work_area_bounds =
  532. display::Screen::GetScreen()
  533. ->GetDisplayNearestWindow(static_cast<aura::Window*>(event->target()))
  534. .work_area();
  535. gfx::Rect hit_bounds_in_screen(work_area_bounds);
  536. hit_bounds_in_screen.set_height(kDragStartTopEdgeInset);
  537. // There may be a bezel sensor off screen logically above
  538. // |hit_bounds_in_screen|. Handles the ET_GESTURE_SCROLL_BEGIN event
  539. // triggered in the bezel area too.
  540. bool in_bezel = location_in_screen.y() < hit_bounds_in_screen.y() &&
  541. location_in_screen.x() >= hit_bounds_in_screen.x() &&
  542. location_in_screen.x() < hit_bounds_in_screen.right();
  543. if (hit_bounds_in_screen.Contains(location_in_screen) || in_bezel)
  544. return toplevel;
  545. return nullptr;
  546. }
  547. bool ToplevelWindowEventHandler::PrepareForDrag(
  548. aura::Window* window,
  549. const gfx::PointF& point_in_parent,
  550. int window_component,
  551. ::wm::WindowMoveSource source,
  552. bool grab_capture) {
  553. // Do not allow resizing if there is already one in progress or if the
  554. // window's state is not managed by the window manager.
  555. if (window_resizer_ || !WindowState::Get(window))
  556. return false;
  557. std::unique_ptr<WindowResizer> resizer(
  558. CreateWindowResizer(window, point_in_parent, window_component, source));
  559. if (!resizer)
  560. return false;
  561. window_resizer_ = std::make_unique<ScopedWindowResizer>(
  562. this, std::move(resizer), grab_capture);
  563. return true;
  564. }
  565. bool ToplevelWindowEventHandler::CompleteDrag(DragResult result) {
  566. UpdateGestureTarget(nullptr);
  567. if (!window_resizer_)
  568. return false;
  569. std::unique_ptr<ScopedWindowResizer> resizer(std::move(window_resizer_));
  570. switch (result) {
  571. case DragResult::SUCCESS:
  572. resizer->resizer()->CompleteDrag();
  573. break;
  574. case DragResult::REVERT:
  575. resizer->resizer()->RevertDrag();
  576. break;
  577. case DragResult::WINDOW_DESTROYED:
  578. // We explicitly do not invoke RevertDrag() since that may do things to
  579. // the window that was destroyed.
  580. break;
  581. }
  582. first_finger_hittest_ = HTNOWHERE;
  583. in_gesture_drag_ = false;
  584. if (end_closure_)
  585. std::move(end_closure_).Run(result);
  586. return true;
  587. }
  588. void ToplevelWindowEventHandler::HandleMousePressed(aura::Window* target,
  589. ui::MouseEvent* event) {
  590. if (event->phase() != ui::EP_PRETARGET || !target->delegate())
  591. return;
  592. // We also update the current window component here because for the
  593. // mouse-drag-release-press case, where the mouse is released and
  594. // pressed without mouse move event.
  595. int component = window_util::GetNonClientComponent(target, event->location());
  596. if ((event->flags() & (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) ==
  597. 0 &&
  598. WindowResizer::GetBoundsChangeForWindowComponent(component)) {
  599. gfx::PointF location_in_parent = event->location_f();
  600. aura::Window::ConvertPointToTarget(target, target->parent(),
  601. &location_in_parent);
  602. AttemptToStartDrag(target, location_in_parent, component,
  603. ::wm::WINDOW_MOVE_SOURCE_MOUSE, EndClosure(),
  604. /*update_gesture_target=*/false);
  605. // Set as handled so that other event handlers do no act upon the event
  606. // but still receive it so that they receive both parts of each pressed/
  607. // released pair.
  608. event->SetHandled();
  609. } else {
  610. CompleteDrag(DragResult::SUCCESS);
  611. }
  612. }
  613. void ToplevelWindowEventHandler::HandleMouseReleased(aura::Window* target,
  614. ui::MouseEvent* event) {
  615. if (event->phase() == ui::EP_PRETARGET)
  616. CompleteDrag(DragResult::SUCCESS);
  617. }
  618. void ToplevelWindowEventHandler::HandleDrag(aura::Window* target,
  619. ui::LocatedEvent* event) {
  620. // This function only be triggered to move window
  621. // by mouse drag or touch move event.
  622. DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
  623. event->type() == ui::ET_TOUCH_MOVED ||
  624. event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
  625. // Drag actions are performed pre-target handling to prevent spurious mouse
  626. // moves from the move/size operation from being sent to the target.
  627. if (event->phase() != ui::EP_PRETARGET)
  628. return;
  629. if (!window_resizer_)
  630. return;
  631. gfx::PointF location_in_parent = event->location_f();
  632. aura::Window::ConvertPointToTarget(
  633. target, window_resizer_->resizer()->GetTarget()->parent(),
  634. &location_in_parent);
  635. window_resizer_->resizer()->Drag(location_in_parent, event->flags());
  636. event->StopPropagation();
  637. // Dragging may change the window that has capture, invalidating
  638. // `window_resizer_`.
  639. if (window_resizer_ && window_resizer_->IsResize())
  640. Shell::Get()->multitask_menu_nudge_controller()->MaybeShowNudge(target);
  641. }
  642. void ToplevelWindowEventHandler::HandleMouseMoved(aura::Window* target,
  643. ui::LocatedEvent* event) {
  644. // Shadow effects are applied after target handling. Note that we don't
  645. // respect ER_HANDLED here right now since we have not had a reason to allow
  646. // the target to cancel shadow rendering.
  647. if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
  648. return;
  649. // TODO(jamescook): Move the resize cursor update code into here from
  650. // CompoundEventFilter?
  651. if (event->flags() & ui::EF_IS_NON_CLIENT) {
  652. int component =
  653. window_util::GetNonClientComponent(target, event->location());
  654. ShowResizeShadow(target, component);
  655. } else {
  656. HideResizeShadow(target);
  657. }
  658. }
  659. void ToplevelWindowEventHandler::HandleMouseExited(aura::Window* target,
  660. ui::LocatedEvent* event) {
  661. // Shadow effects are applied after target handling. Note that we don't
  662. // respect ER_HANDLED here right now since we have not had a reason to allow
  663. // the target to cancel shadow rendering.
  664. if (event->phase() != ui::EP_POSTTARGET)
  665. return;
  666. HideResizeShadow(target);
  667. }
  668. void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) {
  669. if (event->phase() == ui::EP_PRETARGET) {
  670. // We complete the drag instead of reverting it, as reverting it will result
  671. // in a weird behavior when a dragged tab produces a modal dialog while the
  672. // drag is in progress. crbug.com/558201.
  673. CompleteDrag(DragResult::SUCCESS);
  674. }
  675. }
  676. void ToplevelWindowEventHandler::HandleFlingOrSwipe(ui::GestureEvent* event) {
  677. UpdateGestureTarget(nullptr);
  678. if (!window_resizer_)
  679. return;
  680. std::unique_ptr<ScopedWindowResizer> resizer(std::move(window_resizer_));
  681. resizer->resizer()->FlingOrSwipe(event);
  682. first_finger_hittest_ = HTNOWHERE;
  683. in_gesture_drag_ = false;
  684. if (end_closure_)
  685. std::move(end_closure_).Run(DragResult::SUCCESS);
  686. }
  687. void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
  688. CompleteDrag(DragResult::WINDOW_DESTROYED);
  689. }
  690. void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
  691. CompleteDrag(DragResult::REVERT);
  692. }
  693. void ToplevelWindowEventHandler::OnWindowDestroying(aura::Window* window) {
  694. DCHECK_EQ(gesture_target_, window);
  695. if (gesture_target_ == window)
  696. UpdateGestureTarget(nullptr);
  697. }
  698. void ToplevelWindowEventHandler::UpdateGestureTarget(
  699. aura::Window* target,
  700. const gfx::PointF& location) {
  701. event_location_in_gesture_target_ = location;
  702. if (gesture_target_ == target)
  703. return;
  704. if (gesture_target_)
  705. gesture_target_->RemoveObserver(this);
  706. gesture_target_ = target;
  707. if (gesture_target_)
  708. gesture_target_->AddObserver(this);
  709. }
  710. } // namespace ash