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