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.
5 #ifndef ASH_WM_WORKSPACE_SNAP_SIZER_H_
6 #define ASH_WM_WORKSPACE_SNAP_SIZER_H_
10 #include "ash/ash_export.h"
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "ui/gfx/rect.h"
22 // SnapSizer is responsible for determining the resulting bounds of a window
23 // that is being snapped to the left or right side of the screen.
24 // The bounds used in this class are in the container's coordinates.
25 class ASH_EXPORT SnapSizer {
33 TOUCH_MAXIMIZE_BUTTON_INPUT,
38 // Snapping is made easier with a touch drag when using touchscreen.
39 static const int kScreenEdgeInsetForTouchDrag;
41 // Set |input_type| to |TOUCH_MAXIMIZE_BUTTON_INPUT| when called by a touch
42 // operation by the maximize button. This will allow the user to snap resize
43 // the window beginning close to the border.
44 SnapSizer(wm::WindowState* window_state,
45 const gfx::Point& start,
47 InputType input_type);
50 // Snaps a window left or right.
51 static void SnapWindow(wm::WindowState* window_state, Edge edge);
53 // Snaps |window_| to the target bounds.
54 void SnapWindowToTargetBounds();
56 // Updates the target bounds based on a mouse move.
57 void Update(const gfx::Point& location);
59 // Bounds to position the window at.
60 const gfx::Rect& target_bounds() const { return target_bounds_; }
62 // Returns the appropriate snap bounds (e.g. if a window is already snapped,
63 // then it returns the next snap-bounds).
64 gfx::Rect GetSnapBounds(const gfx::Rect& bounds);
66 // Set the snap sizer to the button press default size and prevent resizing.
67 void SelectDefaultSizeAndDisableResize();
69 // Returns the target bounds based on the edge and the provided |size_index|.
70 // For unit test purposes this function is not private.
71 gfx::Rect GetTargetBoundsForSize(size_t size_index) const;
73 // Returns true when snapping sequence is at its last (docking) step.
74 bool end_of_sequence() const { return end_of_sequence_; }
77 // Calculates the amount to increment by. This returns one of -1, 0 or 1 and
78 // is intended to by applied to |size_index_|. |x| is the current
79 // x-coordinate, and |reference_x| is used to determine whether to increase
80 // or decrease the position. It's one of |last_adjust_x_| or |last_update_x_|.
81 int CalculateIncrement(int x, int reference_x) const;
83 // Changes the bounds. |x| is the current x-coordinate and |delta| the amount
84 // to increase by. |delta| comes from CalculateIncrement() and is applied
86 void ChangeBounds(int x, int delta);
88 // Returns the target bounds based on the edge and |size_index_|.
89 gfx::Rect GetTargetBounds() const;
91 // Returns true if the specified point is along the edge of the screen.
92 bool AlongEdge(int x) const;
94 // WindowState of the window being snapped.
95 wm::WindowState* window_state_;
99 // Current target bounds for the snap.
100 gfx::Rect target_bounds_;
102 // Time Update() was last invoked.
103 base::TimeTicks time_last_update_;
105 // Index into |kSizes| that dictates the width of the screen the target
106 // bounds should get.
109 // Set to true when an attempt is made to increment |size_index_| past
110 // the size of |usable_width_|.
111 bool end_of_sequence_;
113 // If set, |size_index_| will get ignored and the single button default
114 // setting will be used instead.
115 bool resize_disabled_;
117 // Number of times Update() has been invoked since last ChangeBounds().
118 int num_moves_since_adjust_;
120 // X-coordinate the last time ChangeBounds() was invoked.
123 // X-coordinate last supplied to Update().
126 // Initial x-coordinate.
129 // |TOUCH_MAXIMIZE_BUTTON_INPUT| if the snap sizer was created through a
130 // touch & drag operation of the maximizer button. It changes the behavior of
131 // the drag / resize behavior when the dragging starts close to the border.
132 const InputType input_type_;
134 // A list of usable window widths for size. This gets created when the
135 // sizer gets created.
136 const std::vector<int> usable_width_;
138 DISALLOW_COPY_AND_ASSIGN(SnapSizer);
141 } // namespace internal
144 #endif // ASH_WM_WORKSPACE_SNAP_SIZER_H_