Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / snap_sizer.h
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
5 #ifndef ASH_WM_WORKSPACE_SNAP_SIZER_H_
6 #define ASH_WM_WORKSPACE_SNAP_SIZER_H_
7
8 #include <vector>
9
10 #include "ash/ash_export.h"
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "ui/gfx/rect.h"
14
15 namespace ash {
16 namespace wm {
17 class WindowState;
18 }
19
20 namespace internal {
21
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 {
26  public:
27   enum Edge {
28     LEFT_EDGE,
29     RIGHT_EDGE
30   };
31
32   enum InputType {
33     TOUCH_MAXIMIZE_BUTTON_INPUT,
34     TOUCH_DRAG_INPUT,
35     OTHER_INPUT
36   };
37
38   // Snapping is made easier with a touch drag when using touchscreen.
39   static const int kScreenEdgeInsetForTouchDrag;
40
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,
46             Edge edge,
47             InputType input_type);
48   virtual ~SnapSizer();
49
50   // Snaps a window left or right.
51   static void SnapWindow(wm::WindowState* window_state, Edge edge);
52
53   // Snaps |window_| to the target bounds.
54   void SnapWindowToTargetBounds();
55
56   // Updates the target bounds based on a mouse move.
57   void Update(const gfx::Point& location);
58
59   // Bounds to position the window at.
60   const gfx::Rect& target_bounds() const { return target_bounds_; }
61
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);
65
66   // Set the snap sizer to the button press default size and prevent resizing.
67   void SelectDefaultSizeAndDisableResize();
68
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;
72
73   // Returns true when snapping sequence is at its last (docking) step.
74   bool end_of_sequence() const { return end_of_sequence_; }
75
76  private:
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;
82
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
85   // to |size_index_|.
86   void ChangeBounds(int x, int delta);
87
88   // Returns the target bounds based on the edge and |size_index_|.
89   gfx::Rect GetTargetBounds() const;
90
91   // Returns true if the specified point is along the edge of the screen.
92   bool AlongEdge(int x) const;
93
94   // WindowState of the window being snapped.
95   wm::WindowState* window_state_;
96
97   const Edge edge_;
98
99   // Current target bounds for the snap.
100   gfx::Rect target_bounds_;
101
102   // Time Update() was last invoked.
103   base::TimeTicks time_last_update_;
104
105   // Index into |kSizes| that dictates the width of the screen the target
106   // bounds should get.
107   int size_index_;
108
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_;
112
113   // If set, |size_index_| will get ignored and the single button default
114   // setting will be used instead.
115   bool resize_disabled_;
116
117   // Number of times Update() has been invoked since last ChangeBounds().
118   int num_moves_since_adjust_;
119
120   // X-coordinate the last time ChangeBounds() was invoked.
121   int last_adjust_x_;
122
123   // X-coordinate last supplied to Update().
124   int last_update_x_;
125
126   // Initial x-coordinate.
127   const int start_x_;
128
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_;
133
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_;
137
138   DISALLOW_COPY_AND_ASSIGN(SnapSizer);
139 };
140
141 }  // namespace internal
142 }  // namespace ash
143
144 #endif  // ASH_WM_WORKSPACE_SNAP_SIZER_H_