| 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/time/time.h" | |
| 20 #include "ui/aura/window_observer.h" | |
| 21 #include "ui/display/display_observer.h" | |
| 22 #include "ui/gfx/geometry/rect.h" | |
| 23 #include "ui/keyboard/keyboard_controller_observer.h" | |
| 24 #include "ui/wm/public/activation_change_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 aura::client::ActivationChangeObserver, | |
| 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 // aura::client::ActivationChangeObserver: | |
| 154 void OnWindowActivated(ActivationReason reason, | |
| 155 aura::Window* gained_active, | |
| 156 aura::Window* lost_active) override; | |
| 157 | |
| 158 // ShellObserver: | |
| 159 void OnShelfAlignmentChanged(WmWindow* root_window) override; | |
| 160 void OnFullscreenStateChanged(bool is_fullscreen, | |
| 161 WmWindow* root_window) override; | |
| 162 void OnOverviewModeStarting() override; | |
| 163 void OnOverviewModeEnded() override; | |
| 164 | |
| 165 private: | |
| 166 struct CompareMinimumHeight; | |
| 167 struct CompareWindowPos; | |
| 168 class ShelfWindowObserver; | |
| 169 struct WindowWithHeight; | |
| 170 | |
| 171 friend class DockedWindowLayoutManagerTest; | |
| 172 friend class DockedWindowResizerTest; | |
| 173 | |
| 174 // Width of the gap between the docked windows and a workspace. | |
| 175 static const int kMinDockGap; | |
| 176 | |
| 177 // Ideal (starting) width of the dock. | |
| 178 static const int kIdealWidth; | |
| 179 | |
| 180 // Returns the alignment of the docked windows other than the |child|. | |
| 181 DockedAlignment CalculateAlignmentExcept(const WmWindow* child) const; | |
| 182 | |
| 183 // Determines if the |alignment| is applicable taking into account | |
| 184 // the shelf alignment. | |
| 185 bool IsDockedAlignmentValid(DockedAlignment alignment) const; | |
| 186 | |
| 187 // Keep at most kMaxVisibleWindows visible in the dock and minimize the rest | |
| 188 // (except for |child|). | |
| 189 void MaybeMinimizeChildrenExcept(WmWindow* child); | |
| 190 | |
| 191 // Minimize / restore window and relayout. | |
| 192 void MinimizeDockedWindow(wm::WindowState* window_state); | |
| 193 void RestoreDockedWindow(wm::WindowState* window_state); | |
| 194 | |
| 195 // Record user-initiated |action| by |source| in UMA metrics. | |
| 196 void RecordUmaAction(DockedAction action, DockedActionSource source); | |
| 197 | |
| 198 // Updates |docked_width_| and UMA histograms. | |
| 199 void UpdateDockedWidth(int width); | |
| 200 | |
| 201 // Updates docked layout state when a window gets inside the dock. | |
| 202 void OnDraggedWindowDocked(WmWindow* window); | |
| 203 | |
| 204 // Updates docked layout state when a window gets outside the dock. | |
| 205 void OnDraggedWindowUndocked(); | |
| 206 | |
| 207 // Returns true if there are any windows currently docked. | |
| 208 bool IsAnyWindowDocked(); | |
| 209 | |
| 210 // Returns DOCKED_ALIGNMENT_LEFT if the |window|'s left edge is closer to | |
| 211 // the |dock_container_|'s left edge than the |window|'s right edge to | |
| 212 // the |dock_container_|'s right edge. Returns DOCKED_ALIGNMENT_RIGHT | |
| 213 // otherwise. | |
| 214 DockedAlignment GetEdgeNearestWindow(const WmWindow* window) const; | |
| 215 | |
| 216 // Called whenever the window layout might change. | |
| 217 void Relayout(); | |
| 218 | |
| 219 // Calculates target heights (and fills it in |visible_windows| array) such | |
| 220 // that the vertical space is fairly distributed among the windows taking | |
| 221 // into account their minimum and maximum size. Returns free vertical space | |
| 222 // (positive value) that remains after resizing all windows or deficit | |
| 223 // (negative value) if not all the windows fit. | |
| 224 int CalculateWindowHeightsAndRemainingRoom( | |
| 225 const gfx::Rect& work_area, | |
| 226 std::vector<WindowWithHeight>* visible_windows); | |
| 227 | |
| 228 // Calculate ideal width for the docked area. It will get used to adjust the | |
| 229 // dragged window or other windows as necessary. | |
| 230 int CalculateIdealWidth(const std::vector<WindowWithHeight>& visible_windows); | |
| 231 | |
| 232 // Fan out windows evenly distributing the overlap or remaining free space. | |
| 233 // Adjust the widths of the windows trying to make them all same. If this | |
| 234 // is not possible, center the windows in the docked area. | |
| 235 void FanOutChildren(const gfx::Rect& work_area, | |
| 236 int ideal_docked_width, | |
| 237 int available_room, | |
| 238 std::vector<WindowWithHeight>* visible_windows); | |
| 239 | |
| 240 // Updates |docked_bounds_| and workspace insets when bounds of docked windows | |
| 241 // area change. Passing |reason| to observers allows selectively skipping | |
| 242 // notifications. | |
| 243 void UpdateDockBounds(DockedWindowLayoutManagerObserver::Reason reason); | |
| 244 | |
| 245 // Called whenever the window stacking order needs to be updated (e.g. focus | |
| 246 // changes or a window is moved). | |
| 247 void UpdateStacking(WmWindow* active_window); | |
| 248 | |
| 249 // keyboard::KeyboardControllerObserver: | |
| 250 void OnKeyboardBoundsChanging(const gfx::Rect& keyboard_bounds) override; | |
| 251 void OnKeyboardClosed() override; | |
| 252 | |
| 253 // Parent window associated with this layout manager. | |
| 254 WmWindow* dock_container_; | |
| 255 | |
| 256 RootWindowController* root_window_controller_; | |
| 257 | |
| 258 // Protect against recursive calls to Relayout(). | |
| 259 bool in_layout_; | |
| 260 | |
| 261 // A window that is being dragged (whether docked or not). | |
| 262 // Windows are tracked by docked layout manager only if they are docked; | |
| 263 // however we need to know if a window is being dragged in order to avoid | |
| 264 // positioning it or even considering it for layout. | |
| 265 WmWindow* dragged_window_; | |
| 266 | |
| 267 // True if the window being dragged is currently docked. | |
| 268 bool is_dragged_window_docked_; | |
| 269 | |
| 270 // Previously docked windows use a more relaxed dragging sorting algorithm | |
| 271 // that uses assumption that a window starts being dragged out of position | |
| 272 // that was previously established in Relayout. This allows easier reordering. | |
| 273 bool is_dragged_from_dock_; | |
| 274 | |
| 275 // The shelf to respond to alignment changes. | |
| 276 WmShelf* shelf_; | |
| 277 | |
| 278 // Tracks if any window in the same root window is in fullscreen mode. | |
| 279 bool in_fullscreen_; | |
| 280 // Current width of the dock. | |
| 281 int docked_width_; | |
| 282 | |
| 283 // Last bounds that were sent to observers. | |
| 284 gfx::Rect docked_bounds_; | |
| 285 | |
| 286 // Target bounds of a docked window being dragged. | |
| 287 gfx::Rect dragged_bounds_; | |
| 288 | |
| 289 // True while in overview mode. | |
| 290 bool in_overview_; | |
| 291 | |
| 292 // Side of the screen that the dock is positioned at. | |
| 293 DockedAlignment alignment_; | |
| 294 | |
| 295 // The preferred alignment of the next window to be added to docked layout. | |
| 296 DockedAlignment preferred_alignment_; | |
| 297 | |
| 298 // The current event source | |
| 299 DockedActionSource event_source_; | |
| 300 | |
| 301 // The last active window. Used to maintain stacking order even if no windows | |
| 302 // are currently focused. | |
| 303 WmWindow* last_active_window_; | |
| 304 | |
| 305 // Timestamp of the last user-initiated action that changed docked state. | |
| 306 // Used in UMA metrics. | |
| 307 base::Time last_action_time_; | |
| 308 | |
| 309 // Observes shelf for bounds changes. | |
| 310 std::unique_ptr<ShelfWindowObserver> shelf_observer_; | |
| 311 | |
| 312 // Widget used to paint a background for the docked area. | |
| 313 std::unique_ptr<DockedBackgroundWidget> background_widget_; | |
| 314 | |
| 315 // Observers of dock bounds changes. | |
| 316 base::ObserverList<DockedWindowLayoutManagerObserver> observer_list_; | |
| 317 | |
| 318 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManager); | |
| 319 }; | |
| 320 | |
| 321 } // namespace ash | |
| 322 | |
| 323 #endif // ASH_COMMON_WM_DOCK_DOCKED_WINDOW_LAYOUT_MANAGER_H_ | |
| OLD | NEW |