workspace_window_resizer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #ifndef ASH_WM_WORKSPACE_WORKSPACE_WINDOW_RESIZER_H_
  5. #define ASH_WM_WORKSPACE_WORKSPACE_WINDOW_RESIZER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "ash/wm/window_resizer.h"
  10. #include "ash/wm/workspace/magnetism_matcher.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/timer/timer.h"
  13. #include "ui/aura/window_tracker.h"
  14. #include "ui/compositor/presentation_time_recorder.h"
  15. #include "ui/display/display.h"
  16. #include "ui/gfx/geometry/point_f.h"
  17. namespace ash {
  18. class PhantomWindowController;
  19. class WindowSize;
  20. class WindowState;
  21. // WindowResizer implementation for workspaces. This enforces that windows are
  22. // not allowed to vertically move or resize outside of the work area. As windows
  23. // are moved outside the work area they are shrunk. We remember the height of
  24. // the window before it was moved so that if the window is again moved up we
  25. // attempt to restore the old height.
  26. class ASH_EXPORT WorkspaceWindowResizer : public WindowResizer {
  27. public:
  28. // Possible states the window can end up in after a drag is complete.
  29. enum class SnapType { kPrimary, kSecondary, kMaximize, kNone };
  30. // Min height we'll force on screen when dragging the caption.
  31. // TODO: this should come from a property on the window.
  32. static constexpr int kMinOnscreenHeight = 32;
  33. ~WorkspaceWindowResizer() override;
  34. static std::unique_ptr<WorkspaceWindowResizer> Create(
  35. WindowState* window_state,
  36. const std::vector<aura::Window*>& attached_windows);
  37. // WindowResizer:
  38. void Drag(const gfx::PointF& location_in_parent, int event_flags) override;
  39. void CompleteDrag() override;
  40. void RevertDrag() override;
  41. void FlingOrSwipe(ui::GestureEvent* event) override;
  42. private:
  43. friend class WorkspaceWindowResizerTest;
  44. FRIEND_TEST_ALL_PREFIXES(HapticsUtilTest, HapticFeedbackForNormalWindowSnap);
  45. WorkspaceWindowResizer(WindowState* window_state,
  46. const std::vector<aura::Window*>& attached_windows);
  47. WorkspaceWindowResizer(const WorkspaceWindowResizer&) = delete;
  48. WorkspaceWindowResizer& operator=(const WorkspaceWindowResizer&) = delete;
  49. // Lays out the attached windows. |bounds| is the bounds of the main window.
  50. void LayoutAttachedWindows(gfx::Rect* bounds);
  51. // Calculates the new sizes of the attached windows, given that the main
  52. // window has been resized (along the primary axis) by |delta|.
  53. // |available_size| is the maximum length of the space that the attached
  54. // windows are allowed to occupy (ie: the distance between the right/bottom
  55. // edge of the primary window and the right/bottom of the desktop area).
  56. // Populates |sizes| with the desired sizes of the attached windows, and
  57. // returns the number of pixels that couldn't be allocated to the attached
  58. // windows (due to min/max size constraints).
  59. // Note the return value can be positive or negative, a negative value
  60. // indicating that that many pixels couldn't be removed from the attached
  61. // windows.
  62. int CalculateAttachedSizes(int delta,
  63. int available_size,
  64. std::vector<int>* sizes) const;
  65. // Divides |amount| evenly between |sizes|. If |amount| is negative it
  66. // indicates how many pixels |sizes| should be shrunk by.
  67. // Returns how many pixels failed to be allocated/removed from |sizes|.
  68. int GrowFairly(int amount, std::vector<WindowSize>* sizes) const;
  69. // Calculate the ratio of pixels that each WindowSize in |sizes| should
  70. // receive when growing or shrinking.
  71. void CalculateGrowthRatios(const std::vector<WindowSize*>& sizes,
  72. std::vector<float>* out_ratios) const;
  73. // Adds a WindowSize to |sizes| for each attached window.
  74. void CreateBucketsForAttached(std::vector<WindowSize>* sizes) const;
  75. // If possible snaps the window to a neary window in |display|. Updates
  76. // |bounds| if there was a close enough window. |display| should be the
  77. // display containing the last event location.
  78. void MagneticallySnapToOtherWindows(const display::Display& display,
  79. gfx::Rect* bounds);
  80. // If possible snaps the resize to a neary window in |display|. Updates
  81. // |bounds| if there was a close enough window. |display| should be the
  82. // display containing the last event location.
  83. void MagneticallySnapResizeToOtherWindows(const display::Display& display,
  84. gfx::Rect* bounds);
  85. // Finds the neareset window in |display| to magentically snap to. Updates
  86. // |magnetism_window_| and |magnetism_edge_| appropriately. |edges| is a
  87. // bitmask of the MagnetismEdges to match again. Returns true if a match is
  88. // found.
  89. bool UpdateMagnetismWindow(const display::Display& display,
  90. const gfx::Rect& bounds,
  91. uint32_t edges);
  92. // Adjusts the bounds of the window: magnetically snapping, ensuring the
  93. // window has enough on screen... |snap_size| is the distance from an edge of
  94. // the work area before the window is snapped. A value of 0 results in no
  95. // snapping.
  96. void AdjustBoundsForMainWindow(int snap_size, gfx::Rect* bounds);
  97. // Stick the window bounds to the work area during a move.
  98. bool StickToWorkAreaOnMove(const gfx::Rect& work_area,
  99. int sticky_size,
  100. gfx::Rect* bounds) const;
  101. // Stick the window bounds to the work area during a resize.
  102. void StickToWorkAreaOnResize(const gfx::Rect& work_area,
  103. int sticky_size,
  104. gfx::Rect* bounds) const;
  105. // Returns a coordinate along the primary axis. Used to share code for
  106. // left/right multi window resize and top/bottom resize.
  107. int PrimaryAxisSize(const gfx::Size& size) const;
  108. int PrimaryAxisCoordinate(int x, int y) const;
  109. // From a given snap |type| and a current display orientation, returns true
  110. // if the snap is snap top or maximize. This function is used to decide if
  111. // we need to show the phantom window for top snap or maximize or not.
  112. bool IsSnapTopOrMaximize(SnapType type) const;
  113. // Updates the bounds of the phantom window where the snap bounds are
  114. // calculated from GetSnappedWindowBounds() given a |target_snap_type| and
  115. // maximize bounds is from full display work area.
  116. void UpdateSnapPhantomWindow(const SnapType target_snap_type);
  117. // Restacks the windows z-order position so that one of the windows is at the
  118. // top of the z-order, and the rest directly underneath it.
  119. void RestackWindows();
  120. // Returns the edge to which the window should be snapped to if the user does
  121. // no more dragging. kSnapNone is returned if the window should not be
  122. // snapped, whether it has not been dragged to the correct region, or the
  123. // window does not allow for snapping.
  124. SnapType GetSnapType(const display::Display& display,
  125. const gfx::PointF& location_in_screen) const;
  126. // Returns true if |window| bounds are valid bounds for a snap state and snap
  127. // ratio in |window_state_|.
  128. bool AreBoundsValidSnappedBounds(aura::Window* window) const;
  129. // Sets |window|'s state type to |new_state_type|. Called after the drag has
  130. // been completed for fling/swipe gestures.
  131. void SetWindowStateTypeFromGesture(aura::Window* window,
  132. chromeos::WindowStateType new_state_type);
  133. // Start/End drag for attached windows if there is any.
  134. void StartDragForAttachedWindows();
  135. void EndDragForAttachedWindows(bool revert_drag);
  136. // Gets the display associated with GetTarget() if touch dragging. Gets the
  137. // display associated with the cursor if mouse dragging.
  138. display::Display GetDisplay() const;
  139. WindowState* window_state() { return window_state_; }
  140. const WindowState* window_state() const { return window_state_; }
  141. // Returns the currently used instance for test.
  142. static WorkspaceWindowResizer* GetInstanceForTest();
  143. const std::vector<aura::Window*> attached_windows_;
  144. bool did_lock_cursor_ = false;
  145. // Set to true once Drag() is invoked and the bounds of the window change.
  146. bool did_move_or_resize_ = false;
  147. // Tracks whether a window can be maximized depending on distance dragged.
  148. // Set to true when Drag() has a vertical move more than
  149. // kSnapTriggerVerticalMoveThreshold.
  150. bool can_snap_to_maximize_ = false;
  151. // True if the window initially had |bounds_changed_by_user_| set in state.
  152. const bool initial_bounds_changed_by_user_;
  153. // The initial size of each of the windows in |attached_windows_| along the
  154. // primary axis.
  155. std::vector<int> initial_size_;
  156. // Sum of the minimum sizes of the attached windows.
  157. int total_min_ = 0;
  158. // Sum of the sizes in |initial_size_|.
  159. int total_initial_size_ = 0;
  160. // Gives a previews of where the the window will end up. Only used if there
  161. // is a grid and the caption is being dragged.
  162. std::unique_ptr<PhantomWindowController> snap_phantom_window_controller_;
  163. // The edge to which the window should be snapped to at the end of the drag.
  164. SnapType snap_type_ = SnapType::kNone;
  165. // Timer for dwell time countdown.
  166. base::OneShotTimer dwell_countdown_timer_;
  167. // The location for drag maximize in screen.
  168. absl::optional<gfx::PointF> dwell_location_in_screen_;
  169. // The location in parent passed to `Drag()`.
  170. gfx::PointF last_location_in_parent_;
  171. // Window the drag has magnetically attached to.
  172. aura::Window* magnetism_window_ = nullptr;
  173. // Used to verify |magnetism_window_| is still valid.
  174. aura::WindowTracker window_tracker_;
  175. // If |magnetism_window_| is non-NULL this indicates how the two windows
  176. // should attach.
  177. MatchedEdge magnetism_edge_;
  178. // The window bounds when the drag was started. When a window is minimized,
  179. // maximized or snapped via a swipe/fling gesture, the restore bounds should
  180. // be set to the bounds of the window when the drag was started. If the window
  181. // started with restore bounds (snapped/maximized), those will be used
  182. // instead.
  183. gfx::Rect restore_bounds_for_gesture_;
  184. // Presentation time recorder for tab dragging in clamshell mode.
  185. std::unique_ptr<ui::PresentationTimeRecorder> tab_dragging_recorder_;
  186. // Used to determine if this has been deleted during a drag such as when a tab
  187. // gets dragged into another browser window.
  188. base::WeakPtrFactory<WorkspaceWindowResizer> weak_ptr_factory_{this};
  189. };
  190. } // namespace ash
  191. #endif // ASH_WM_WORKSPACE_WORKSPACE_WINDOW_RESIZER_H_