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.
5 #ifndef ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_
6 #define ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_
8 #include "ash/ash_export.h"
9 #include "ash/shelf/shelf_layout_manager_observer.h"
10 #include "ash/shell_observer.h"
11 #include "ash/snap_to_pixel_layout_manager.h"
12 #include "ash/wm/dock/dock_types.h"
13 #include "ash/wm/dock/docked_window_layout_manager_observer.h"
14 #include "ash/wm/window_state_observer.h"
15 #include "base/basictypes.h"
16 #include "base/compiler_specific.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/time/time.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_observer.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/keyboard/keyboard_controller_observer.h"
25 #include "ui/wm/public/activation_change_observer.h"
40 class DockedBackgroundWidget
;
41 class DockedWindowLayoutManagerObserver
;
42 class DockedWindowResizerTest
;
44 class ShelfLayoutManager
;
45 class WorkspaceController
;
47 struct WindowWithHeight
{
48 explicit WindowWithHeight(aura::Window
* window
) :
50 height_(window
->bounds().height()) { }
51 aura::Window
* window() { return window_
; }
52 const aura::Window
* window() const { return window_
; }
53 aura::Window
* window_
;
57 // DockedWindowLayoutManager is responsible for organizing windows when they are
58 // docked to the side of a screen. It is associated with a specific container
59 // window (i.e. kShellWindowId_DockContainer) and controls the layout of any
60 // windows added to that container.
62 // The constructor takes a |dock_container| argument which is expected to set
63 // its layout manager to this instance, e.g.:
64 // dock_container->SetLayoutManager(
65 // new DockedWindowLayoutManager(dock_container));
67 // TODO(varkha): extend BaseLayoutManager instead of LayoutManager to inherit
68 // common functionality.
69 class ASH_EXPORT DockedWindowLayoutManager
70 : public SnapToPixelLayoutManager
,
71 public ash::ShellObserver
,
72 public aura::WindowObserver
,
73 public aura::client::ActivationChangeObserver
,
74 public keyboard::KeyboardControllerObserver
,
75 public ShelfLayoutManagerObserver
,
76 public wm::WindowStateObserver
{
78 // Maximum width of the docked windows area.
79 static const int kMaxDockWidth
;
81 // Minimum width of the docked windows area.
82 static const int kMinDockWidth
;
84 DockedWindowLayoutManager(aura::Window
* dock_container
,
85 WorkspaceController
* workspace_controller
);
86 ~DockedWindowLayoutManager() override
;
88 // Disconnects observers before container windows get destroyed.
91 // Management of the observer list.
92 virtual void AddObserver(DockedWindowLayoutManagerObserver
* observer
);
93 virtual void RemoveObserver(DockedWindowLayoutManagerObserver
* observer
);
95 // Called by a DockedWindowResizer to update which window is being dragged.
96 // Starts observing the window unless it is a child.
97 void StartDragging(aura::Window
* window
);
99 // Called by a DockedWindowResizer when a dragged window is docked.
100 void DockDraggedWindow(aura::Window
* window
);
102 // Called by a DockedWindowResizer when a dragged window is no longer docked.
103 void UndockDraggedWindow();
105 // Called by a DockedWindowResizer when a window is no longer being dragged.
106 // Stops observing the window unless it is a child.
107 // Records |action| by |source| in UMA.
108 void FinishDragging(DockedAction action
, DockedActionSource source
);
110 // Checks the rules and possibly updates the docked layout to match
111 // the |alignment|. May not apply the |alignment| when
112 // the current shelf alignment conflicts. Never clears the |alignment_|.
113 void MaybeSetDesiredDockedAlignment(DockedAlignment alignment
);
115 Shelf
* shelf() { return shelf_
; }
116 void SetShelf(Shelf
* shelf
);
118 // Calculates if a window is touching the screen edges and returns edge.
119 DockedAlignment
GetAlignmentOfWindow(const aura::Window
* window
) const;
121 // Used to snap docked windows to the side of screen during drag.
122 DockedAlignment
CalculateAlignment() const;
124 void set_preferred_alignment(DockedAlignment preferred_alignment
) {
125 preferred_alignment_
= preferred_alignment
;
128 void set_event_source(DockedActionSource event_source
) {
129 event_source_
= event_source
;
132 // Returns true when a window can be docked. Windows cannot be docked at the
133 // edge used by the shelf or the edge opposite from existing dock.
134 bool CanDockWindow(aura::Window
* window
, DockedAlignment desired_alignment
);
136 aura::Window
* dock_container() const { return dock_container_
; }
138 // Returns current bounding rectangle of docked windows area.
139 const gfx::Rect
& docked_bounds() const { return docked_bounds_
; }
141 // Returns last known coordinates of |dragged_window_| after Relayout.
142 const gfx::Rect
dragged_bounds() const { return dragged_bounds_
;}
144 // Returns true if currently dragged window is docked at the screen edge.
145 bool is_dragged_window_docked() const { return is_dragged_window_docked_
; }
147 // Updates docked layout when shelf bounds change.
148 void OnShelfBoundsChanged();
150 // SnapLayoutManager:
151 void OnWindowResized() override
;
152 void OnWindowAddedToLayout(aura::Window
* child
) override
;
153 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
154 void OnWindowRemovedFromLayout(aura::Window
* child
) override
;
155 void OnChildWindowVisibilityChanged(aura::Window
* child
,
156 bool visibile
) override
;
157 void SetChildBounds(aura::Window
* child
,
158 const gfx::Rect
& requested_bounds
) override
;
160 // ash::ShellObserver:
161 void OnDisplayWorkAreaInsetsChanged() override
;
162 void OnFullscreenStateChanged(bool is_fullscreen
,
163 aura::Window
* root_window
) override
;
164 void OnShelfAlignmentChanged(aura::Window
* root_window
) override
;
166 // ShelfLayoutManagerObserver:
167 void OnBackgroundUpdated(ShelfBackgroundType background_type
,
168 BackgroundAnimatorChangeType change_type
) override
;
170 // wm::WindowStateObserver:
171 void OnPreWindowStateTypeChange(wm::WindowState
* window_state
,
172 wm::WindowStateType old_type
) override
;
174 // aura::WindowObserver:
175 void OnWindowBoundsChanged(aura::Window
* window
,
176 const gfx::Rect
& old_bounds
,
177 const gfx::Rect
& new_bounds
) override
;
178 void OnWindowVisibilityChanging(aura::Window
* window
, bool visible
) override
;
179 void OnWindowDestroying(aura::Window
* window
) override
;
181 // aura::client::ActivationChangeObserver:
182 void OnWindowActivated(aura::Window
* gained_active
,
183 aura::Window
* lost_active
) override
;
186 class ShelfWindowObserver
;
187 friend class DockedWindowLayoutManagerTest
;
188 friend class DockedWindowResizerTest
;
190 // Width of the gap between the docked windows and a workspace.
191 static const int kMinDockGap
;
193 // Ideal (starting) width of the dock.
194 static const int kIdealWidth
;
196 // Returns the alignment of the docked windows other than the |child|.
197 DockedAlignment
CalculateAlignmentExcept(const aura::Window
* child
) const;
199 // Determines if the |alignment| is applicable taking into account
200 // the shelf alignment.
201 bool IsDockedAlignmentValid(DockedAlignment alignment
) const;
203 // Keep at most kMaxVisibleWindows visible in the dock and minimize the rest
204 // (except for |child|).
205 void MaybeMinimizeChildrenExcept(aura::Window
* child
);
207 // Minimize / restore window and relayout.
208 void MinimizeDockedWindow(wm::WindowState
* window_state
);
209 void RestoreDockedWindow(wm::WindowState
* window_state
);
211 // Record user-initiated |action| by |source| in UMA metrics.
212 void RecordUmaAction(DockedAction action
, DockedActionSource source
);
214 // Updates |docked_width_| and UMA histograms.
215 void UpdateDockedWidth(int width
);
217 // Updates docked layout state when a window gets inside the dock.
218 void OnDraggedWindowDocked(aura::Window
* window
);
220 // Updates docked layout state when a window gets outside the dock.
221 void OnDraggedWindowUndocked();
223 // Returns true if there are any windows currently docked.
224 bool IsAnyWindowDocked();
226 // Returns DOCKED_ALIGNMENT_LEFT if the |window|'s left edge is closer to
227 // the |dock_container_|'s left edge than the |window|'s right edge to
228 // the |dock_container_|'s right edge. Returns DOCKED_ALIGNMENT_RIGHT
230 DockedAlignment
GetEdgeNearestWindow(const aura::Window
* window
) const;
232 // Called whenever the window layout might change.
235 // Calculates target heights (and fills it in |visible_windows| array) such
236 // that the vertical space is fairly distributed among the windows taking
237 // into account their minimum and maximum size. Returns free vertical space
238 // (positive value) that remains after resizing all windows or deficit
239 // (negative value) if not all the windows fit.
240 int CalculateWindowHeightsAndRemainingRoom(
241 const gfx::Rect work_area
,
242 std::vector
<WindowWithHeight
>* visible_windows
);
244 // Calculate ideal width for the docked area. It will get used to adjust the
245 // dragged window or other windows as necessary.
246 int CalculateIdealWidth(const std::vector
<WindowWithHeight
>& visible_windows
);
248 // Fan out windows evenly distributing the overlap or remaining free space.
249 // Adjust the widths of the windows trying to make them all same. If this
250 // is not possible, center the windows in the docked area.
251 void FanOutChildren(const gfx::Rect
& work_area
,
252 int ideal_docked_width
,
254 std::vector
<WindowWithHeight
>* visible_windows
);
256 // Updates |docked_bounds_| and workspace insets when bounds of docked windows
257 // area change. Passing |reason| to observers allows selectively skipping
259 void UpdateDockBounds(DockedWindowLayoutManagerObserver::Reason reason
);
261 // Called whenever the window stacking order needs to be updated (e.g. focus
262 // changes or a window is moved).
263 void UpdateStacking(aura::Window
* active_window
);
265 // keyboard::KeyboardControllerObserver:
266 void OnKeyboardBoundsChanging(const gfx::Rect
& keyboard_bounds
) override
;
268 // Parent window associated with this layout manager.
269 aura::Window
* dock_container_
;
270 // Protect against recursive calls to Relayout().
273 // A window that is being dragged (whether docked or not).
274 // Windows are tracked by docked layout manager only if they are docked;
275 // however we need to know if a window is being dragged in order to avoid
276 // positioning it or even considering it for layout.
277 aura::Window
* dragged_window_
;
279 // True if the window being dragged is currently docked.
280 bool is_dragged_window_docked_
;
282 // Previously docked windows use a more relaxed dragging sorting algorithm
283 // that uses assumption that a window starts being dragged out of position
284 // that was previously established in Relayout. This allows easier reordering.
285 bool is_dragged_from_dock_
;
287 // The shelf to respond to alignment changes.
290 // Workspace controller that can be checked for fullscreen mode.
291 WorkspaceController
* workspace_controller_
;
292 // Tracks if any window in the same root window is in fullscreen mode.
294 // Current width of the dock.
297 // Last bounds that were sent to observers.
298 gfx::Rect docked_bounds_
;
300 // Target bounds of a docked window being dragged.
301 gfx::Rect dragged_bounds_
;
303 // Side of the screen that the dock is positioned at.
304 DockedAlignment alignment_
;
306 // The preferred alignment of the next window to be added to docked layout.
307 DockedAlignment preferred_alignment_
;
309 // The current event source
310 DockedActionSource event_source_
;
312 // The last active window. Used to maintain stacking order even if no windows
313 // are currently focused.
314 aura::Window
* last_active_window_
;
316 // Timestamp of the last user-initiated action that changed docked state.
317 // Used in UMA metrics.
318 base::Time last_action_time_
;
320 // Observes shelf for bounds changes.
321 scoped_ptr
<ShelfWindowObserver
> shelf_observer_
;
323 // Widget used to paint a background for the docked area.
324 scoped_ptr
<DockedBackgroundWidget
> background_widget_
;
326 // Observers of dock bounds changes.
327 ObserverList
<DockedWindowLayoutManagerObserver
> observer_list_
;
329 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManager
);
334 #endif // ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_