Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / wm / dock / docked_window_layout_manager.h
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_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_
6 #define ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_
7
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"
26
27 namespace aura {
28 class Window;
29 }
30
31 namespace gfx {
32 class Point;
33 }
34
35 namespace views {
36 class Widget;
37 }
38
39 namespace ash {
40 class DockedBackgroundWidget;
41 class DockedWindowLayoutManagerObserver;
42 class DockedWindowResizerTest;
43 class Shelf;
44 class ShelfLayoutManager;
45 class WorkspaceController;
46
47 struct WindowWithHeight {
48   explicit WindowWithHeight(aura::Window* window) :
49     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_;
54   int height_;
55 };
56
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.
61 //
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));
66 //
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 {
77  public:
78   // Maximum width of the docked windows area.
79   static const int kMaxDockWidth;
80
81   // Minimum width of the docked windows area.
82   static const int kMinDockWidth;
83
84   DockedWindowLayoutManager(aura::Window* dock_container,
85                             WorkspaceController* workspace_controller);
86   ~DockedWindowLayoutManager() override;
87
88   // Disconnects observers before container windows get destroyed.
89   void Shutdown();
90
91   // Management of the observer list.
92   virtual void AddObserver(DockedWindowLayoutManagerObserver* observer);
93   virtual void RemoveObserver(DockedWindowLayoutManagerObserver* observer);
94
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);
98
99   // Called by a DockedWindowResizer when a dragged window is docked.
100   void DockDraggedWindow(aura::Window* window);
101
102   // Called by a DockedWindowResizer when a dragged window is no longer docked.
103   void UndockDraggedWindow();
104
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);
109
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);
114
115   Shelf* shelf() { return shelf_; }
116   void SetShelf(Shelf* shelf);
117
118   // Calculates if a window is touching the screen edges and returns edge.
119   DockedAlignment GetAlignmentOfWindow(const aura::Window* window) const;
120
121   // Used to snap docked windows to the side of screen during drag.
122   DockedAlignment CalculateAlignment() const;
123
124   void set_preferred_alignment(DockedAlignment preferred_alignment) {
125     preferred_alignment_ = preferred_alignment;
126   }
127
128   void set_event_source(DockedActionSource event_source) {
129     event_source_ = event_source;
130   }
131
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);
135
136   aura::Window* dock_container() const { return dock_container_; }
137
138   // Returns current bounding rectangle of docked windows area.
139   const gfx::Rect& docked_bounds() const { return docked_bounds_; }
140
141   // Returns last known coordinates of |dragged_window_| after Relayout.
142   const gfx::Rect dragged_bounds() const { return dragged_bounds_;}
143
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_; }
146
147   // Updates docked layout when shelf bounds change.
148   void OnShelfBoundsChanged();
149
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;
159
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;
165
166   // ShelfLayoutManagerObserver:
167   void OnBackgroundUpdated(ShelfBackgroundType background_type,
168                            BackgroundAnimatorChangeType change_type) override;
169
170   // wm::WindowStateObserver:
171   void OnPreWindowStateTypeChange(wm::WindowState* window_state,
172                                   wm::WindowStateType old_type) override;
173
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;
180
181   // aura::client::ActivationChangeObserver:
182   void OnWindowActivated(aura::Window* gained_active,
183                          aura::Window* lost_active) override;
184
185  private:
186   class ShelfWindowObserver;
187   friend class DockedWindowLayoutManagerTest;
188   friend class DockedWindowResizerTest;
189
190   // Width of the gap between the docked windows and a workspace.
191   static const int kMinDockGap;
192
193   // Ideal (starting) width of the dock.
194   static const int kIdealWidth;
195
196   // Returns the alignment of the docked windows other than the |child|.
197   DockedAlignment CalculateAlignmentExcept(const aura::Window* child) const;
198
199   // Determines if the |alignment| is applicable taking into account
200   // the shelf alignment.
201   bool IsDockedAlignmentValid(DockedAlignment alignment) const;
202
203   // Keep at most kMaxVisibleWindows visible in the dock and minimize the rest
204   // (except for |child|).
205   void MaybeMinimizeChildrenExcept(aura::Window* child);
206
207   // Minimize / restore window and relayout.
208   void MinimizeDockedWindow(wm::WindowState* window_state);
209   void RestoreDockedWindow(wm::WindowState* window_state);
210
211   // Record user-initiated |action| by |source| in UMA metrics.
212   void RecordUmaAction(DockedAction action, DockedActionSource source);
213
214   // Updates |docked_width_| and UMA histograms.
215   void UpdateDockedWidth(int width);
216
217   // Updates docked layout state when a window gets inside the dock.
218   void OnDraggedWindowDocked(aura::Window* window);
219
220   // Updates docked layout state when a window gets outside the dock.
221   void OnDraggedWindowUndocked();
222
223   // Returns true if there are any windows currently docked.
224   bool IsAnyWindowDocked();
225
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
229   // otherwise.
230   DockedAlignment GetEdgeNearestWindow(const aura::Window* window) const;
231
232   // Called whenever the window layout might change.
233   void Relayout();
234
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);
243
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);
247
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,
253                       int available_room,
254                       std::vector<WindowWithHeight>* visible_windows);
255
256   // Updates |docked_bounds_| and workspace insets when bounds of docked windows
257   // area change. Passing |reason| to observers allows selectively skipping
258   // notifications.
259   void UpdateDockBounds(DockedWindowLayoutManagerObserver::Reason reason);
260
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);
264
265   // keyboard::KeyboardControllerObserver:
266   void OnKeyboardBoundsChanging(const gfx::Rect& keyboard_bounds) override;
267
268   // Parent window associated with this layout manager.
269   aura::Window* dock_container_;
270   // Protect against recursive calls to Relayout().
271   bool in_layout_;
272
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_;
278
279   // True if the window being dragged is currently docked.
280   bool is_dragged_window_docked_;
281
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_;
286
287   // The shelf to respond to alignment changes.
288   Shelf* shelf_;
289
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.
293   bool in_fullscreen_;
294   // Current width of the dock.
295   int docked_width_;
296
297   // Last bounds that were sent to observers.
298   gfx::Rect docked_bounds_;
299
300   // Target bounds of a docked window being dragged.
301   gfx::Rect dragged_bounds_;
302
303   // Side of the screen that the dock is positioned at.
304   DockedAlignment alignment_;
305
306   // The preferred alignment of the next window to be added to docked layout.
307   DockedAlignment preferred_alignment_;
308
309   // The current event source
310   DockedActionSource event_source_;
311
312   // The last active window. Used to maintain stacking order even if no windows
313   // are currently focused.
314   aura::Window* last_active_window_;
315
316   // Timestamp of the last user-initiated action that changed docked state.
317   // Used in UMA metrics.
318   base::Time last_action_time_;
319
320   // Observes shelf for bounds changes.
321   scoped_ptr<ShelfWindowObserver> shelf_observer_;
322
323   // Widget used to paint a background for the docked area.
324   scoped_ptr<DockedBackgroundWidget> background_widget_;
325
326   // Observers of dock bounds changes.
327   ObserverList<DockedWindowLayoutManagerObserver> observer_list_;
328
329   DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManager);
330 };
331
332 }  // namespace ash
333
334 #endif  // ASH_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_