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/geometry/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(
183 aura::client::ActivationChangeObserver::ActivationReason reason
,
184 aura::Window
* gained_active
,
185 aura::Window
* lost_active
) override
;
188 class ShelfWindowObserver
;
189 friend class DockedWindowLayoutManagerTest
;
190 friend class DockedWindowResizerTest
;
192 // Width of the gap between the docked windows and a workspace.
193 static const int kMinDockGap
;
195 // Ideal (starting) width of the dock.
196 static const int kIdealWidth
;
198 // Returns the alignment of the docked windows other than the |child|.
199 DockedAlignment
CalculateAlignmentExcept(const aura::Window
* child
) const;
201 // Determines if the |alignment| is applicable taking into account
202 // the shelf alignment.
203 bool IsDockedAlignmentValid(DockedAlignment alignment
) const;
205 // Keep at most kMaxVisibleWindows visible in the dock and minimize the rest
206 // (except for |child|).
207 void MaybeMinimizeChildrenExcept(aura::Window
* child
);
209 // Minimize / restore window and relayout.
210 void MinimizeDockedWindow(wm::WindowState
* window_state
);
211 void RestoreDockedWindow(wm::WindowState
* window_state
);
213 // Record user-initiated |action| by |source| in UMA metrics.
214 void RecordUmaAction(DockedAction action
, DockedActionSource source
);
216 // Updates |docked_width_| and UMA histograms.
217 void UpdateDockedWidth(int width
);
219 // Updates docked layout state when a window gets inside the dock.
220 void OnDraggedWindowDocked(aura::Window
* window
);
222 // Updates docked layout state when a window gets outside the dock.
223 void OnDraggedWindowUndocked();
225 // Returns true if there are any windows currently docked.
226 bool IsAnyWindowDocked();
228 // Returns DOCKED_ALIGNMENT_LEFT if the |window|'s left edge is closer to
229 // the |dock_container_|'s left edge than the |window|'s right edge to
230 // the |dock_container_|'s right edge. Returns DOCKED_ALIGNMENT_RIGHT
232 DockedAlignment
GetEdgeNearestWindow(const aura::Window
* window
) const;
234 // Called whenever the window layout might change.
237 // Calculates target heights (and fills it in |visible_windows| array) such
238 // that the vertical space is fairly distributed among the windows taking
239 // into account their minimum and maximum size. Returns free vertical space
240 // (positive value) that remains after resizing all windows or deficit
241 // (negative value) if not all the windows fit.
242 int CalculateWindowHeightsAndRemainingRoom(
243 const gfx::Rect work_area
,
244 std::vector
<WindowWithHeight
>* visible_windows
);
246 // Calculate ideal width for the docked area. It will get used to adjust the
247 // dragged window or other windows as necessary.
248 int CalculateIdealWidth(const std::vector
<WindowWithHeight
>& visible_windows
);
250 // Fan out windows evenly distributing the overlap or remaining free space.
251 // Adjust the widths of the windows trying to make them all same. If this
252 // is not possible, center the windows in the docked area.
253 void FanOutChildren(const gfx::Rect
& work_area
,
254 int ideal_docked_width
,
256 std::vector
<WindowWithHeight
>* visible_windows
);
258 // Updates |docked_bounds_| and workspace insets when bounds of docked windows
259 // area change. Passing |reason| to observers allows selectively skipping
261 void UpdateDockBounds(DockedWindowLayoutManagerObserver::Reason reason
);
263 // Called whenever the window stacking order needs to be updated (e.g. focus
264 // changes or a window is moved).
265 void UpdateStacking(aura::Window
* active_window
);
267 // keyboard::KeyboardControllerObserver:
268 void OnKeyboardBoundsChanging(const gfx::Rect
& keyboard_bounds
) override
;
270 // Parent window associated with this layout manager.
271 aura::Window
* dock_container_
;
272 // Protect against recursive calls to Relayout().
275 // A window that is being dragged (whether docked or not).
276 // Windows are tracked by docked layout manager only if they are docked;
277 // however we need to know if a window is being dragged in order to avoid
278 // positioning it or even considering it for layout.
279 aura::Window
* dragged_window_
;
281 // True if the window being dragged is currently docked.
282 bool is_dragged_window_docked_
;
284 // Previously docked windows use a more relaxed dragging sorting algorithm
285 // that uses assumption that a window starts being dragged out of position
286 // that was previously established in Relayout. This allows easier reordering.
287 bool is_dragged_from_dock_
;
289 // The shelf to respond to alignment changes.
292 // Workspace controller that can be checked for fullscreen mode.
293 WorkspaceController
* workspace_controller_
;
294 // Tracks if any window in the same root window is in fullscreen mode.
296 // Current width of the dock.
299 // Last bounds that were sent to observers.
300 gfx::Rect docked_bounds_
;
302 // Target bounds of a docked window being dragged.
303 gfx::Rect dragged_bounds_
;
305 // Side of the screen that the dock is positioned at.
306 DockedAlignment alignment_
;
308 // The preferred alignment of the next window to be added to docked layout.
309 DockedAlignment preferred_alignment_
;
311 // The current event source
312 DockedActionSource event_source_
;
314 // The last active window. Used to maintain stacking order even if no windows
315 // are currently focused.
316 aura::Window
* last_active_window_
;
318 // Timestamp of the last user-initiated action that changed docked state.
319 // Used in UMA metrics.
320 base::Time last_action_time_
;
322 // Observes shelf for bounds changes.
323 scoped_ptr
<ShelfWindowObserver
> shelf_observer_
;
325 // Widget used to paint a background for the docked area.
326 scoped_ptr
<DockedBackgroundWidget
> background_widget_
;
328 // Observers of dock bounds changes.
329 base::ObserverList
<DockedWindowLayoutManagerObserver
> observer_list_
;
331 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManager
);
336 #endif // ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_