window_resizer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_WINDOW_RESIZER_H_
  5. #define ASH_WM_WINDOW_RESIZER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/wm/drag_details.h"
  9. #include "ash/wm/window_state.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "ui/wm/public/window_move_client.h"
  12. namespace aura {
  13. class Window;
  14. }
  15. namespace gfx {
  16. class Rect;
  17. }
  18. namespace ui {
  19. class GestureEvent;
  20. }
  21. namespace ash {
  22. class PresentationTimeRecorder;
  23. // WindowResizer is used by ToplevelWindowEventFilter to handle dragging, moving
  24. // or resizing a window. All coordinates passed to this are in the parent
  25. // windows coordinates.
  26. class ASH_EXPORT WindowResizer {
  27. public:
  28. // Constants to identify the type of resize.
  29. static const int kBoundsChange_None;
  30. static const int kBoundsChange_Repositions;
  31. static const int kBoundsChange_Resizes;
  32. // Used to indicate which direction the resize occurs in.
  33. static const int kBoundsChangeDirection_None;
  34. static const int kBoundsChangeDirection_Horizontal;
  35. static const int kBoundsChangeDirection_Vertical;
  36. explicit WindowResizer(WindowState* window_state);
  37. WindowResizer(const WindowResizer&) = delete;
  38. WindowResizer& operator=(const WindowResizer&) = delete;
  39. virtual ~WindowResizer();
  40. // Returns a bitmask of the kBoundsChange_ values.
  41. static int GetBoundsChangeForWindowComponent(int component);
  42. // Returns a bitmask of the kBoundsChange_ values.
  43. static int GetPositionChangeDirectionForWindowComponent(int window_component);
  44. // Invoked to drag/move/resize the window. |location| is in the coordinates
  45. // of the window supplied to the constructor. |event_flags| is the event
  46. // flags from the event.
  47. virtual void Drag(const gfx::PointF& location, int event_flags) = 0;
  48. // Invoked to complete the drag.
  49. virtual void CompleteDrag() = 0;
  50. // Reverts the drag.
  51. virtual void RevertDrag() = 0;
  52. // Flings or Swipes to end the drag.
  53. virtual void FlingOrSwipe(ui::GestureEvent* event) = 0;
  54. // Returns the target window the resizer was created for.
  55. aura::Window* GetTarget() const;
  56. // See comment for |DragDetails::initial_location_in_parent|.
  57. const gfx::PointF& GetInitialLocation() const {
  58. return window_state_->drag_details()->initial_location_in_parent;
  59. }
  60. // Drag parameters established when drag starts.
  61. const DragDetails& details() const { return *window_state_->drag_details(); }
  62. protected:
  63. gfx::Rect CalculateBoundsForDrag(const gfx::PointF& location);
  64. // Call during an active resize to change the bounds of the window. This
  65. // should not be called as the result of a revert.
  66. void SetBoundsDuringResize(const gfx::Rect& bounds);
  67. void SetPresentationTimeRecorder(
  68. std::unique_ptr<PresentationTimeRecorder> recorder);
  69. // WindowState of the drag target.
  70. WindowState* window_state_;
  71. private:
  72. // In case of touch resizing, adjusts deltas so that the border is positioned
  73. // just under the touch point.
  74. void AdjustDeltaForTouchResize(int* delta_x, int* delta_y);
  75. // Returns the new origin of the window. |delta_x| and |delta_y| are the
  76. // difference between the current location and the initial location.
  77. // |event_location| is the current location of the mouse or touch event.
  78. gfx::Point GetOriginForDrag(int delta_x,
  79. int delta_y,
  80. const gfx::PointF& event_location);
  81. // Returns the size of the window for the drag.
  82. gfx::Size GetSizeForDrag(int* delta_x, int* delta_y);
  83. // Returns the width of the window.
  84. int GetWidthForDrag(int min_width, int* delta_x);
  85. // Returns the height of the drag.
  86. int GetHeightForDrag(int min_height, int* delta_y);
  87. // Updates |new_bounds| to adhere to the aspect ratio.
  88. void CalculateBoundsWithAspectRatio(float aspect_ratio,
  89. gfx::Rect* new_bounds);
  90. std::unique_ptr<PresentationTimeRecorder> recorder_;
  91. base::WeakPtrFactory<WindowResizer> weak_ptr_factory_{this};
  92. };
  93. // Creates a WindowResizer for |window|. Returns a unique_ptr with null if
  94. // |window| should not be resized nor dragged.
  95. ASH_EXPORT std::unique_ptr<WindowResizer> CreateWindowResizer(
  96. aura::Window* window,
  97. const gfx::PointF& point_in_parent,
  98. int window_component,
  99. ::wm::WindowMoveSource source);
  100. } // namespace ash
  101. #endif // ASH_WM_WINDOW_RESIZER_H_