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_WINDOW_RESIZER_H_
6 #define ASH_WM_WORKSPACE_WINDOW_RESIZER_H_
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"
26 class DockedWindowLayoutManager;
27 class PhantomWindowController;
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 {
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
41 static const int kMinOnscreenSize;
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;
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;
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;
55 virtual ~WorkspaceWindowResizer();
57 static WorkspaceWindowResizer* Create(
59 const gfx::Point& location_in_parent,
61 aura::client::WindowMoveSource source,
62 const std::vector<aura::Window*>& attached_windows);
65 virtual void Drag(const gfx::Point& location_in_parent,
66 int event_flags) OVERRIDE;
67 virtual void CompleteDrag(int event_flags) OVERRIDE;
68 virtual void RevertDrag() OVERRIDE;
69 virtual aura::Window* GetTarget() OVERRIDE;
70 virtual const gfx::Point& GetInitialLocation() const OVERRIDE;
73 WorkspaceWindowResizer(const Details& details,
74 const std::vector<aura::Window*>& attached_windows);
77 FRIEND_TEST_ALL_PREFIXES(WorkspaceWindowResizerTest, CancelSnapPhantom);
78 FRIEND_TEST_ALL_PREFIXES(WorkspaceWindowResizerTest, PhantomSnapMaxSize);
79 FRIEND_TEST_ALL_PREFIXES(WorkspaceWindowResizerTest, PhantomWindowShow);
81 // Returns the final bounds to place the window at. This differs from
82 // the current when snapping.
83 gfx::Rect GetFinalBounds(const gfx::Rect& bounds) const;
85 // Lays out the attached windows. |bounds| is the bounds of the main window.
86 void LayoutAttachedWindows(gfx::Rect* bounds);
88 // Calculates the new sizes of the attached windows, given that the main
89 // window has been resized (along the primary axis) by |delta|.
90 // |available_size| is the maximum length of the space that the attached
91 // windows are allowed to occupy (ie: the distance between the right/bottom
92 // edge of the primary window and the right/bottom of the desktop area).
93 // Populates |sizes| with the desired sizes of the attached windows, and
94 // returns the number of pixels that couldn't be allocated to the attached
95 // windows (due to min/max size constraints).
96 // Note the return value can be positive or negative, a negative value
97 // indicating that that many pixels couldn't be removed from the attached
99 int CalculateAttachedSizes(
102 std::vector<int>* sizes) const;
104 // Divides |amount| evenly between |sizes|. If |amount| is negative it
105 // indicates how many pixels |sizes| should be shrunk by.
106 // Returns how many pixels failed to be allocated/removed from |sizes|.
107 int GrowFairly(int amount, std::vector<WindowSize>& sizes) const;
109 // Calculate the ratio of pixels that each WindowSize in |sizes| should
110 // receive when growing or shrinking.
111 void CalculateGrowthRatios(const std::vector<WindowSize*>& sizes,
112 std::vector<float>* out_ratios) const;
114 // Adds a WindowSize to |sizes| for each attached window.
115 void CreateBucketsForAttached(std::vector<WindowSize>* sizes) const;
117 // If possible snaps the window to a neary window. Updates |bounds| if there
118 // was a close enough window.
119 void MagneticallySnapToOtherWindows(gfx::Rect* bounds);
121 // If possible snaps the resize to a neary window. Updates |bounds| if there
122 // was a close enough window.
123 void MagneticallySnapResizeToOtherWindows(gfx::Rect* bounds);
125 // Finds the neareset window to magentically snap to. Updates
126 // |magnetism_window_| and |magnetism_edge_| appropriately. |edges| is a
127 // bitmask of the MagnetismEdges to match again. Returns true if a match is
129 bool UpdateMagnetismWindow(const gfx::Rect& bounds, uint32 edges);
131 // Adjusts the bounds of the window: magnetically snapping, ensuring the
132 // window has enough on screen... |snap_size| is the distance from an edge of
133 // the work area before the window is snapped. A value of 0 results in no
135 void AdjustBoundsForMainWindow(int snap_size, gfx::Rect* bounds);
137 // Stick the window bounds to the work area during a move.
138 bool StickToWorkAreaOnMove(const gfx::Rect& work_area,
140 gfx::Rect* bounds) const;
142 // Stick the window bounds to the work area during a resize.
143 void StickToWorkAreaOnResize(const gfx::Rect& work_area,
145 gfx::Rect* bounds) const;
147 // Returns a coordinate along the primary axis. Used to share code for
148 // left/right multi window resize and top/bottom resize.
149 int PrimaryAxisSize(const gfx::Size& size) const;
150 int PrimaryAxisCoordinate(int x, int y) const;
152 // Updates the bounds of the phantom window for window snapping.
153 void UpdateSnapPhantomWindow(const gfx::Point& location,
154 const gfx::Rect& bounds);
156 // Restacks the windows z-order position so that one of the windows is at the
157 // top of the z-order, and the rest directly underneath it.
158 void RestackWindows();
160 // Returns the SnapType for the specified point. SNAP_NONE is used if no
161 // snapping should be used.
162 SnapType GetSnapType(const gfx::Point& location) const;
164 // Docks the dragged window if |should_dock| and the window can be docked.
165 // Undocks the window if |should_dock| is false.
166 void SetDraggedWindowDocked(bool should_dock);
168 aura::Window* window() const { return details_.window; }
170 wm::WindowState* window_state() { return details_.window_state; }
172 const Details details_;
174 const std::vector<aura::Window*> attached_windows_;
176 // Set to true once Drag() is invoked and the bounds of the window change.
177 bool did_move_or_resize_;
179 // The initial size of each of the windows in |attached_windows_| along the
181 std::vector<int> initial_size_;
183 // Sum of the minimum sizes of the attached windows.
186 // Sum of the sizes in |initial_size_|.
187 int total_initial_size_;
189 // Gives a previews of where the the window will end up. Only used if there
190 // is a grid and the caption is being dragged.
191 scoped_ptr<PhantomWindowController> snap_phantom_window_controller_;
193 // Used to determine the target position of a snap.
194 scoped_ptr<SnapSizer> snap_sizer_;
199 // Number of mouse moves since the last bounds change. Only used for phantom
200 // placement to track when the mouse is moved while pushed against the edge of
202 int num_mouse_moves_since_bounds_change_;
204 // The mouse location passed to Drag().
205 gfx::Point last_mouse_location_;
207 // Window the drag has magnetically attached to.
208 aura::Window* magnetism_window_;
210 // Used to verify |magnetism_window_| is still valid.
211 aura::WindowTracker window_tracker_;
213 // If |magnetism_window_| is non-NULL this indicates how the two windows
215 MatchedEdge magnetism_edge_;
217 // Dock container window layout manager.
218 DockedWindowLayoutManager* dock_layout_;
220 // Used to determine if this has been deleted during a drag such as when a tab
221 // gets dragged into another browser window.
222 base::WeakPtrFactory<WorkspaceWindowResizer> weak_ptr_factory_;
224 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowResizer);
227 } // namespace internal
230 #endif // ASH_WM_WORKSPACE_WINDOW_RESIZER_H_