OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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_COMMON_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_ | |
6 #define ASH_WM_COMMON_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "ash/ash_export.h" | |
11 #include "ash/wm/common/dock/dock_types.h" | |
12 #include "ash/wm/common/dock/docked_window_layout_manager_observer.h" | |
13 #include "ash/wm/common/window_state_observer.h" | |
14 #include "ash/wm/common/wm_activation_observer.h" | |
15 #include "ash/wm/common/wm_root_window_controller_observer.h" | |
16 #include "ash/wm/common/wm_snap_to_pixel_layout_manager.h" | |
17 #include "ash/wm/common/wm_window_observer.h" | |
18 #include "base/compiler_specific.h" | |
19 #include "base/macros.h" | |
20 #include "base/observer_list.h" | |
21 #include "base/time/time.h" | |
22 #include "ui/gfx/geometry/rect.h" | |
23 #include "ui/keyboard/keyboard_controller_observer.h" | |
24 | |
25 namespace ash { | |
26 class DockedBackgroundWidget; | |
27 class DockedWindowLayoutManagerObserver; | |
28 class DockedWindowResizerTest; | |
29 class Shelf; | |
30 | |
31 namespace wm { | |
32 class WmRootWindowController; | |
33 class WmShelf; | |
34 } | |
35 | |
36 // DockedWindowLayoutManager is responsible for organizing windows when they are | |
37 // docked to the side of a screen. It is associated with a specific container | |
38 // window (i.e. kShellWindowId_DockedContainer) and controls the layout of any | |
39 // windows added to that container. | |
40 // | |
41 // The constructor takes a |dock_container| argument which is expected to set | |
42 // its layout manager to this instance, e.g.: | |
43 // dock_container->SetLayoutManager( | |
44 // new DockedWindowLayoutManager(dock_container)); | |
45 // | |
46 // TODO(varkha): extend BaseLayoutManager instead of LayoutManager to inherit | |
47 // common functionality. | |
48 class ASH_EXPORT DockedWindowLayoutManager | |
49 : public wm::WmSnapToPixelLayoutManager, | |
50 public wm::WmRootWindowControllerObserver, | |
51 public wm::WmWindowObserver, | |
52 public wm::WmActivationObserver, | |
53 public keyboard::KeyboardControllerObserver, | |
54 public wm::WindowStateObserver { | |
55 public: | |
56 // Maximum width of the docked windows area. | |
57 static const int kMaxDockWidth; | |
58 | |
59 // Minimum width of the docked windows area. | |
60 static const int kMinDockWidth; | |
61 | |
62 explicit DockedWindowLayoutManager(wm::WmWindow* dock_container); | |
63 ~DockedWindowLayoutManager() override; | |
64 | |
65 // Returns the DockedWindowLayoutManager in the specified hierarchy. This | |
66 // searches from the root of |window|. | |
67 static DockedWindowLayoutManager* Get(wm::WmWindow* window); | |
68 | |
69 // Disconnects observers before container windows get destroyed. | |
70 void Shutdown(); | |
71 | |
72 // Management of the observer list. | |
73 virtual void AddObserver(DockedWindowLayoutManagerObserver* observer); | |
74 virtual void RemoveObserver(DockedWindowLayoutManagerObserver* observer); | |
75 | |
76 // Called by a DockedWindowResizer to update which window is being dragged. | |
77 // Starts observing the window unless it is a child. | |
78 void StartDragging(wm::WmWindow* window); | |
79 | |
80 // Called by a DockedWindowResizer when a dragged window is docked. | |
81 void DockDraggedWindow(wm::WmWindow* window); | |
82 | |
83 // Called by a DockedWindowResizer when a dragged window is no longer docked. | |
84 void UndockDraggedWindow(); | |
85 | |
86 // Called by a DockedWindowResizer when a window is no longer being dragged. | |
87 // Stops observing the window unless it is a child. | |
88 // Records |action| by |source| in UMA. | |
89 void FinishDragging(DockedAction action, DockedActionSource source); | |
90 | |
91 // Checks the rules and possibly updates the docked layout to match | |
92 // the |alignment|. May not apply the |alignment| when | |
93 // the current shelf alignment conflicts. Never clears the |alignment_|. | |
94 void MaybeSetDesiredDockedAlignment(DockedAlignment alignment); | |
95 | |
96 wm::WmShelf* shelf() { return shelf_; } | |
97 void SetShelf(wm::WmShelf* shelf); | |
98 | |
99 // Calculates if a window is touching the screen edges and returns edge. | |
100 DockedAlignment GetAlignmentOfWindow(const wm::WmWindow* window) const; | |
101 | |
102 // Used to snap docked windows to the side of screen during drag. | |
103 DockedAlignment CalculateAlignment() const; | |
104 | |
105 void set_preferred_alignment(DockedAlignment preferred_alignment) { | |
106 preferred_alignment_ = preferred_alignment; | |
107 } | |
108 | |
109 void set_event_source(DockedActionSource event_source) { | |
110 event_source_ = event_source; | |
111 } | |
112 | |
113 // Returns true when a window can be docked. Windows cannot be docked at the | |
114 // edge used by the shelf or the edge opposite from existing dock. | |
115 bool CanDockWindow(wm::WmWindow* window, DockedAlignment desired_alignment); | |
116 | |
117 wm::WmWindow* dock_container() const { return dock_container_; } | |
118 | |
119 // Returns current bounding rectangle of docked windows area. | |
120 const gfx::Rect& docked_bounds() const { return docked_bounds_; } | |
121 | |
122 // Returns last known coordinates of |dragged_window_| after Relayout. | |
123 const gfx::Rect dragged_bounds() const { return dragged_bounds_; } | |
124 | |
125 // Returns true if currently dragged window is docked at the screen edge. | |
126 bool is_dragged_window_docked() const { return is_dragged_window_docked_; } | |
127 | |
128 // Updates docked layout when shelf bounds change. | |
129 void OnShelfBoundsChanged(); | |
130 | |
131 // SnapLayoutManager: | |
132 void OnWindowResized() override; | |
133 void OnWindowAddedToLayout(wm::WmWindow* child) override; | |
134 void OnWillRemoveWindowFromLayout(wm::WmWindow* child) override {} | |
135 void OnWindowRemovedFromLayout(wm::WmWindow* child) override; | |
136 void OnChildWindowVisibilityChanged(wm::WmWindow* child, | |
137 bool visibile) override; | |
138 void SetChildBounds(wm::WmWindow* child, | |
139 const gfx::Rect& requested_bounds) override; | |
140 | |
141 // wm::WmRootWindowControllerObserver: | |
142 void OnWorkAreaChanged() override; | |
143 void OnFullscreenStateChanged(bool is_fullscreen) override; | |
144 void OnShelfAlignmentChanged() override; | |
145 | |
146 // wm::WindowStateObserver: | |
147 void OnPreWindowStateTypeChange(wm::WindowState* window_state, | |
148 wm::WindowStateType old_type) override; | |
149 | |
150 // wm::WmWindowObserver: | |
151 void OnWindowBoundsChanged(wm::WmWindow* window, | |
152 const gfx::Rect& old_bounds, | |
153 const gfx::Rect& new_bounds) override; | |
154 void OnWindowVisibilityChanging(wm::WmWindow* window, bool visible) override; | |
155 void OnWindowDestroying(wm::WmWindow* window) override; | |
156 | |
157 // wm::WmActivationObserver: | |
158 void OnWindowActivated(wm::WmWindow* gained_active, | |
159 wm::WmWindow* lost_active) override; | |
160 | |
161 private: | |
162 struct CompareMinimumHeight; | |
163 struct CompareWindowPos; | |
164 class ShelfWindowObserver; | |
165 struct WindowWithHeight; | |
166 | |
167 friend class DockedWindowLayoutManagerTest; | |
168 friend class DockedWindowResizerTest; | |
169 | |
170 // Width of the gap between the docked windows and a workspace. | |
171 static const int kMinDockGap; | |
172 | |
173 // Ideal (starting) width of the dock. | |
174 static const int kIdealWidth; | |
175 | |
176 // Returns the alignment of the docked windows other than the |child|. | |
177 DockedAlignment CalculateAlignmentExcept(const wm::WmWindow* child) const; | |
178 | |
179 // Determines if the |alignment| is applicable taking into account | |
180 // the shelf alignment. | |
181 bool IsDockedAlignmentValid(DockedAlignment alignment) const; | |
182 | |
183 // Keep at most kMaxVisibleWindows visible in the dock and minimize the rest | |
184 // (except for |child|). | |
185 void MaybeMinimizeChildrenExcept(wm::WmWindow* child); | |
186 | |
187 // Minimize / restore window and relayout. | |
188 void MinimizeDockedWindow(wm::WindowState* window_state); | |
189 void RestoreDockedWindow(wm::WindowState* window_state); | |
190 | |
191 // Record user-initiated |action| by |source| in UMA metrics. | |
192 void RecordUmaAction(DockedAction action, DockedActionSource source); | |
193 | |
194 // Updates |docked_width_| and UMA histograms. | |
195 void UpdateDockedWidth(int width); | |
196 | |
197 // Updates docked layout state when a window gets inside the dock. | |
198 void OnDraggedWindowDocked(wm::WmWindow* window); | |
199 | |
200 // Updates docked layout state when a window gets outside the dock. | |
201 void OnDraggedWindowUndocked(); | |
202 | |
203 // Returns true if there are any windows currently docked. | |
204 bool IsAnyWindowDocked(); | |
205 | |
206 // Returns DOCKED_ALIGNMENT_LEFT if the |window|'s left edge is closer to | |
207 // the |dock_container_|'s left edge than the |window|'s right edge to | |
208 // the |dock_container_|'s right edge. Returns DOCKED_ALIGNMENT_RIGHT | |
209 // otherwise. | |
210 DockedAlignment GetEdgeNearestWindow(const wm::WmWindow* window) const; | |
211 | |
212 // Called whenever the window layout might change. | |
213 void Relayout(); | |
214 | |
215 // Calculates target heights (and fills it in |visible_windows| array) such | |
216 // that the vertical space is fairly distributed among the windows taking | |
217 // into account their minimum and maximum size. Returns free vertical space | |
218 // (positive value) that remains after resizing all windows or deficit | |
219 // (negative value) if not all the windows fit. | |
220 int CalculateWindowHeightsAndRemainingRoom( | |
221 const gfx::Rect& work_area, | |
222 std::vector<WindowWithHeight>* visible_windows); | |
223 | |
224 // Calculate ideal width for the docked area. It will get used to adjust the | |
225 // dragged window or other windows as necessary. | |
226 int CalculateIdealWidth(const std::vector<WindowWithHeight>& visible_windows); | |
227 | |
228 // Fan out windows evenly distributing the overlap or remaining free space. | |
229 // Adjust the widths of the windows trying to make them all same. If this | |
230 // is not possible, center the windows in the docked area. | |
231 void FanOutChildren(const gfx::Rect& work_area, | |
232 int ideal_docked_width, | |
233 int available_room, | |
234 std::vector<WindowWithHeight>* visible_windows); | |
235 | |
236 // Updates |docked_bounds_| and workspace insets when bounds of docked windows | |
237 // area change. Passing |reason| to observers allows selectively skipping | |
238 // notifications. | |
239 void UpdateDockBounds(DockedWindowLayoutManagerObserver::Reason reason); | |
240 | |
241 // Called whenever the window stacking order needs to be updated (e.g. focus | |
242 // changes or a window is moved). | |
243 void UpdateStacking(wm::WmWindow* active_window); | |
244 | |
245 // keyboard::KeyboardControllerObserver: | |
246 void OnKeyboardBoundsChanging(const gfx::Rect& keyboard_bounds) override; | |
247 | |
248 // Parent window associated with this layout manager. | |
249 wm::WmWindow* dock_container_; | |
250 | |
251 wm::WmRootWindowController* root_window_controller_; | |
252 | |
253 // Protect against recursive calls to Relayout(). | |
254 bool in_layout_; | |
255 | |
256 // A window that is being dragged (whether docked or not). | |
257 // Windows are tracked by docked layout manager only if they are docked; | |
258 // however we need to know if a window is being dragged in order to avoid | |
259 // positioning it or even considering it for layout. | |
260 wm::WmWindow* dragged_window_; | |
261 | |
262 // True if the window being dragged is currently docked. | |
263 bool is_dragged_window_docked_; | |
264 | |
265 // Previously docked windows use a more relaxed dragging sorting algorithm | |
266 // that uses assumption that a window starts being dragged out of position | |
267 // that was previously established in Relayout. This allows easier reordering. | |
268 bool is_dragged_from_dock_; | |
269 | |
270 // The shelf to respond to alignment changes. | |
271 wm::WmShelf* shelf_; | |
272 | |
273 // Tracks if any window in the same root window is in fullscreen mode. | |
274 bool in_fullscreen_; | |
275 // Current width of the dock. | |
276 int docked_width_; | |
277 | |
278 // Last bounds that were sent to observers. | |
279 gfx::Rect docked_bounds_; | |
280 | |
281 // Target bounds of a docked window being dragged. | |
282 gfx::Rect dragged_bounds_; | |
283 | |
284 // Side of the screen that the dock is positioned at. | |
285 DockedAlignment alignment_; | |
286 | |
287 // The preferred alignment of the next window to be added to docked layout. | |
288 DockedAlignment preferred_alignment_; | |
289 | |
290 // The current event source | |
291 DockedActionSource event_source_; | |
292 | |
293 // The last active window. Used to maintain stacking order even if no windows | |
294 // are currently focused. | |
295 wm::WmWindow* last_active_window_; | |
296 | |
297 // Timestamp of the last user-initiated action that changed docked state. | |
298 // Used in UMA metrics. | |
299 base::Time last_action_time_; | |
300 | |
301 // Observes shelf for bounds changes. | |
302 std::unique_ptr<ShelfWindowObserver> shelf_observer_; | |
303 | |
304 // Widget used to paint a background for the docked area. | |
305 std::unique_ptr<DockedBackgroundWidget> background_widget_; | |
306 | |
307 // Observers of dock bounds changes. | |
308 base::ObserverList<DockedWindowLayoutManagerObserver> observer_list_; | |
309 | |
310 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManager); | |
311 }; | |
312 | |
313 } // namespace ash | |
314 | |
315 #endif // ASH_WM_COMMON_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_ | |
OLD | NEW |