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