Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / workspace_window_resizer.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_WINDOW_RESIZER_H_
6 #define ASH_WM_WORKSPACE_WINDOW_RESIZER_H_
7
8 #include <vector>
9
10 #include "ash/wm/window_resizer.h"
11 #include "ash/wm/workspace/magnetism_matcher.h"
12 #include "ash/wm/workspace/snap_types.h"
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "ui/aura/window_tracker.h"
18
19 namespace ash {
20 namespace wm {
21 class WindowState;
22 }
23
24 namespace internal {
25
26 class DockedWindowLayoutManager;
27 class PhantomWindowController;
28 class SnapSizer;
29 class WindowSize;
30
31 // WindowResizer implementation for workspaces. This enforces that windows are
32 // not allowed to vertically move or resize outside of the work area. As windows
33 // are moved outside the work area they are shrunk. We remember the height of
34 // the window before it was moved so that if the window is again moved up we
35 // attempt to restore the old height.
36 class ASH_EXPORT WorkspaceWindowResizer : public WindowResizer {
37  public:
38   // When dragging an attached window this is the min size we'll make sure is
39   // visible. In the vertical direction we take the max of this and that from
40   // the delegate.
41   static const int kMinOnscreenSize;
42
43   // Min height we'll force on screen when dragging the caption.
44   // TODO: this should come from a property on the window.
45   static const int kMinOnscreenHeight;
46
47   // Snap region when dragging close to the edges. That is, as the window gets
48   // this close to an edge of the screen it snaps to the edge.
49   static const int kScreenEdgeInset;
50
51   // Distance in pixels that the cursor must move past an edge for a window
52   // to move or resize beyond that edge.
53   static const int kStickyDistancePixels;
54
55   virtual ~WorkspaceWindowResizer();
56
57   static WorkspaceWindowResizer* Create(
58       wm::WindowState* window_state,
59       const std::vector<aura::Window*>& attached_windows);
60
61   // WindowResizer:
62   virtual void Drag(const gfx::Point& location_in_parent,
63                     int event_flags) OVERRIDE;
64   virtual void CompleteDrag() OVERRIDE;
65   virtual void RevertDrag() OVERRIDE;
66
67  private:
68   WorkspaceWindowResizer(wm::WindowState* window_state,
69                          const std::vector<aura::Window*>& attached_windows);
70
71  private:
72   friend class WorkspaceWindowResizerTest;
73
74   // Lays out the attached windows. |bounds| is the bounds of the main window.
75   void LayoutAttachedWindows(gfx::Rect* bounds);
76
77   // Calculates the new sizes of the attached windows, given that the main
78   // window has been resized (along the primary axis) by |delta|.
79   // |available_size| is the maximum length of the space that the attached
80   // windows are allowed to occupy (ie: the distance between the right/bottom
81   // edge of the primary window and the right/bottom of the desktop area).
82   // Populates |sizes| with the desired sizes of the attached windows, and
83   // returns the number of pixels that couldn't be allocated to the attached
84   // windows (due to min/max size constraints).
85   // Note the return value can be positive or negative, a negative value
86   // indicating that that many pixels couldn't be removed from the attached
87   // windows.
88   int CalculateAttachedSizes(
89       int delta,
90       int available_size,
91       std::vector<int>* sizes) const;
92
93   // Divides |amount| evenly between |sizes|. If |amount| is negative it
94   // indicates how many pixels |sizes| should be shrunk by.
95   // Returns how many pixels failed to be allocated/removed from |sizes|.
96   int GrowFairly(int amount, std::vector<WindowSize>& sizes) const;
97
98   // Calculate the ratio of pixels that each WindowSize in |sizes| should
99   // receive when growing or shrinking.
100   void CalculateGrowthRatios(const std::vector<WindowSize*>& sizes,
101                              std::vector<float>* out_ratios) const;
102
103   // Adds a WindowSize to |sizes| for each attached window.
104   void CreateBucketsForAttached(std::vector<WindowSize>* sizes) const;
105
106   // If possible snaps the window to a neary window. Updates |bounds| if there
107   // was a close enough window.
108   void MagneticallySnapToOtherWindows(gfx::Rect* bounds);
109
110   // If possible snaps the resize to a neary window. Updates |bounds| if there
111   // was a close enough window.
112   void MagneticallySnapResizeToOtherWindows(gfx::Rect* bounds);
113
114   // Finds the neareset window to magentically snap to. Updates
115   // |magnetism_window_| and |magnetism_edge_| appropriately. |edges| is a
116   // bitmask of the MagnetismEdges to match again. Returns true if a match is
117   // found.
118   bool UpdateMagnetismWindow(const gfx::Rect& bounds, uint32 edges);
119
120   // Adjusts the bounds of the window: magnetically snapping, ensuring the
121   // window has enough on screen... |snap_size| is the distance from an edge of
122   // the work area before the window is snapped. A value of 0 results in no
123   // snapping.
124   void AdjustBoundsForMainWindow(int snap_size, gfx::Rect* bounds);
125
126   // Stick the window bounds to the work area during a move.
127   bool StickToWorkAreaOnMove(const gfx::Rect& work_area,
128                              int sticky_size,
129                              gfx::Rect* bounds) const;
130
131   // Stick the window bounds to the work area during a resize.
132   void StickToWorkAreaOnResize(const gfx::Rect& work_area,
133                                int sticky_size,
134                                gfx::Rect* bounds) const;
135
136   // Returns a coordinate along the primary axis. Used to share code for
137   // left/right multi window resize and top/bottom resize.
138   int PrimaryAxisSize(const gfx::Size& size) const;
139   int PrimaryAxisCoordinate(int x, int y) const;
140
141   // Updates the bounds of the phantom window for window snapping.
142   void UpdateSnapPhantomWindow(const gfx::Point& location,
143                                const gfx::Rect& bounds);
144
145   // Restacks the windows z-order position so that one of the windows is at the
146   // top of the z-order, and the rest directly underneath it.
147   void RestackWindows();
148
149   // Returns the SnapType for the specified point. SNAP_NONE is used if no
150   // snapping should be used.
151   SnapType GetSnapType(const gfx::Point& location) const;
152
153   // Returns true if |bounds_in_parent| are valid bounds for snapped show type
154   // |snapped_type|.
155   bool AreBoundsValidSnappedBounds(wm::WindowShowType snapped_type,
156                                    const gfx::Rect& bounds_in_parent) const;
157
158   // Docks or undocks the dragged window.
159   void SetDraggedWindowDocked(bool should_dock);
160
161   wm::WindowState* window_state() { return window_state_; }
162
163   const std::vector<aura::Window*> attached_windows_;
164
165   bool did_lock_cursor_;
166
167   // Set to true once Drag() is invoked and the bounds of the window change.
168   bool did_move_or_resize_;
169
170   // True if the window initially had |bounds_changed_by_user_| set in state.
171   bool initial_bounds_changed_by_user_;
172
173   // The initial size of each of the windows in |attached_windows_| along the
174   // primary axis.
175   std::vector<int> initial_size_;
176
177   // Sum of the minimum sizes of the attached windows.
178   int total_min_;
179
180   // Sum of the sizes in |initial_size_|.
181   int total_initial_size_;
182
183   // Gives a previews of where the the window will end up. Only used if there
184   // is a grid and the caption is being dragged.
185   scoped_ptr<PhantomWindowController> snap_phantom_window_controller_;
186
187   // Used to determine the target position of a snap.
188   scoped_ptr<SnapSizer> snap_sizer_;
189
190   // Last SnapType.
191   SnapType snap_type_;
192
193   // Number of mouse moves since the last bounds change. Only used for phantom
194   // placement to track when the mouse is moved while pushed against the edge of
195   // the screen.
196   int num_mouse_moves_since_bounds_change_;
197
198   // The mouse location passed to Drag().
199   gfx::Point last_mouse_location_;
200
201   // Window the drag has magnetically attached to.
202   aura::Window* magnetism_window_;
203
204   // Used to verify |magnetism_window_| is still valid.
205   aura::WindowTracker window_tracker_;
206
207   // If |magnetism_window_| is non-NULL this indicates how the two windows
208   // should attach.
209   MatchedEdge magnetism_edge_;
210
211   // Dock container window layout manager.
212   DockedWindowLayoutManager* dock_layout_;
213
214   // Used to determine if this has been deleted during a drag such as when a tab
215   // gets dragged into another browser window.
216   base::WeakPtrFactory<WorkspaceWindowResizer> weak_ptr_factory_;
217
218   // Current instance for use by the WorkspaceWindowResizerTest.
219   static WorkspaceWindowResizer* instance_;
220
221   DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowResizer);
222 };
223
224 }  // namespace internal
225 }  // namespace ash
226
227 #endif  // ASH_WM_WORKSPACE_WINDOW_RESIZER_H_