OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_OVERVIEW_WINDOW_GRID_H_ | |
6 #define ASH_COMMON_WM_OVERVIEW_WINDOW_GRID_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <memory> | |
11 #include <set> | |
12 #include <vector> | |
13 | |
14 #include "ash/common/wm/overview/window_selector.h" | |
15 #include "ash/common/wm/window_state_observer.h" | |
16 #include "base/macros.h" | |
17 #include "base/scoped_observer.h" | |
18 #include "ui/aura/window_observer.h" | |
19 | |
20 namespace views { | |
21 class Widget; | |
22 } | |
23 | |
24 namespace wm { | |
25 class Shadow; | |
26 } | |
27 | |
28 namespace ash { | |
29 | |
30 class WindowSelectorItem; | |
31 | |
32 // Represents a grid of windows in the Overview Mode in a particular root | |
33 // window, and manages a selection widget that can be moved with the arrow keys. | |
34 // The idea behind the movement strategy is that it should be possible to access | |
35 // any window pressing a given arrow key repeatedly. | |
36 // +-------+ +-------+ +-------+ | |
37 // | 0 | | 1 | | 2 | | |
38 // +-------+ +-------+ +-------+ | |
39 // +-------+ +-------+ +-------+ | |
40 // | 3 | | 4 | | 5 | | |
41 // +-------+ +-------+ +-------+ | |
42 // +-------+ | |
43 // | 6 | | |
44 // +-------+ | |
45 // Example sequences: | |
46 // - Going right to left | |
47 // 0, 1, 2, 3, 4, 5, 6 | |
48 // The selector is switched to the next window grid (if available) or wrapped if | |
49 // it reaches the end of its movement sequence. | |
50 class ASH_EXPORT WindowGrid : public aura::WindowObserver, | |
51 public wm::WindowStateObserver { | |
52 public: | |
53 WindowGrid(WmWindow* root_window, | |
54 const std::vector<WmWindow*>& window_list, | |
55 WindowSelector* window_selector); | |
56 ~WindowGrid() override; | |
57 | |
58 // Exits overview mode, fading out the |shield_widget_| if necessary. | |
59 void Shutdown(); | |
60 | |
61 // Prepares the windows in this grid for overview. This will restore all | |
62 // minimized windows and ensure they are visible. | |
63 void PrepareForOverview(); | |
64 | |
65 // Positions all the windows in rows of equal height scaling each window to | |
66 // fit that height. | |
67 // Layout is done in 2 stages maintaining fixed MRU ordering. | |
68 // 1. Optimal height is determined. In this stage |height| is bisected to find | |
69 // maximum height which still allows all the windows to fit. | |
70 // 2. Row widths are balanced. In this stage the available width is reduced | |
71 // until some windows are no longer fitting or until the difference between | |
72 // the narrowest and the widest rows starts growing. | |
73 // Overall this achieves the goals of maximum size for previews (or maximum | |
74 // row height which is equivalent assuming fixed height), balanced rows and | |
75 // minimal wasted space. | |
76 // Optionally animates the windows to their targets when |animate| is true. | |
77 void PositionWindows(bool animate); | |
78 | |
79 // Updates |selected_index_| according to the specified |direction| and calls | |
80 // MoveSelectionWidget(). Returns |true| if the new selection index is out of | |
81 // this window grid bounds. | |
82 bool Move(WindowSelector::Direction direction, bool animate); | |
83 | |
84 // Returns the target selected window, or NULL if there is none selected. | |
85 WindowSelectorItem* SelectedWindow() const; | |
86 | |
87 // Returns true if a window is contained in any of the WindowSelectorItems | |
88 // this grid owns. | |
89 bool Contains(const WmWindow* window) const; | |
90 | |
91 // Dims the items whose titles do not contain |pattern| and prevents their | |
92 // selection. The pattern has its accents removed and is converted to | |
93 // lowercase in a l10n sensitive context. | |
94 // If |pattern| is empty, no item is dimmed. | |
95 void FilterItems(const base::string16& pattern); | |
96 | |
97 // Called when |window| is about to get closed. If the |window| is currently | |
98 // selected the implementation fades out |selection_widget_| to transparent | |
99 // opacity, effectively hiding the selector widget. | |
100 void WindowClosing(WindowSelectorItem* window); | |
101 | |
102 // Returns true if the grid has no more windows. | |
103 bool empty() const { return window_list_.empty(); } | |
104 | |
105 // Returns how many window selector items are in the grid. | |
106 size_t size() const { return window_list_.size(); } | |
107 | |
108 // Returns true if the selection widget is active. | |
109 bool is_selecting() const { return selection_widget_ != nullptr; } | |
110 | |
111 // Returns the root window in which the grid displays the windows. | |
112 const WmWindow* root_window() const { return root_window_; } | |
113 | |
114 const std::vector<std::unique_ptr<WindowSelectorItem>>& window_list() const { | |
115 return window_list_; | |
116 } | |
117 | |
118 // aura::WindowObserver: | |
119 void OnWindowDestroying(aura::Window* window) override; | |
120 // TODO(flackr): Handle window bounds changed in WindowSelectorItem. | |
121 void OnWindowBoundsChanged(aura::Window* window, | |
122 const gfx::Rect& old_bounds, | |
123 const gfx::Rect& new_bounds) override; | |
124 | |
125 // wm::WindowStateObserver: | |
126 void OnPostWindowStateTypeChange(wm::WindowState* window_state, | |
127 wm::WindowStateType old_type) override; | |
128 | |
129 private: | |
130 friend class WindowSelectorTest; | |
131 | |
132 // Initializes the screen shield widget. | |
133 void InitShieldWidget(); | |
134 | |
135 // Internal function to initialize the selection widget. | |
136 void InitSelectionWidget(WindowSelector::Direction direction); | |
137 | |
138 // Moves the selection widget to the specified |direction|. | |
139 void MoveSelectionWidget(WindowSelector::Direction direction, | |
140 bool recreate_selection_widget, | |
141 bool out_of_bounds, | |
142 bool animate); | |
143 | |
144 // Moves the selection widget to the targeted window. | |
145 void MoveSelectionWidgetToTarget(bool animate); | |
146 | |
147 // Attempts to fit all |rects| inside |bounds|. The method ensures that | |
148 // the |rects| vector has appropriate size and populates it with the values | |
149 // placing Rects next to each other left-to-right in rows of equal |height|. | |
150 // While fitting |rects| several metrics are collected that can be used by the | |
151 // caller. |max_bottom| specifies the bottom that the rects are extending to. | |
152 // |min_right| and |max_right| report the right bound of the narrowest and the | |
153 // widest rows respectively. In-values of the |max_bottom|, |min_right| and | |
154 // |max_right| parameters are ignored and their values are always initialized | |
155 // inside this method. Returns true on success and false otherwise. | |
156 bool FitWindowRectsInBounds(const gfx::Rect& bounds, | |
157 int height, | |
158 std::vector<gfx::Rect>* rects, | |
159 int* max_bottom, | |
160 int* min_right, | |
161 int* max_right); | |
162 | |
163 // Root window the grid is in. | |
164 WmWindow* root_window_; | |
165 | |
166 // Pointer to the window selector that spawned this grid. | |
167 WindowSelector* window_selector_; | |
168 | |
169 // Vector containing all the windows in this grid. | |
170 std::vector<std::unique_ptr<WindowSelectorItem>> window_list_; | |
171 | |
172 ScopedObserver<aura::Window, WindowGrid> window_observer_; | |
173 ScopedObserver<wm::WindowState, WindowGrid> window_state_observer_; | |
174 | |
175 // Widget that darkens the screen background. | |
176 std::unique_ptr<views::Widget> shield_widget_; | |
177 | |
178 // Widget that indicates to the user which is the selected window. | |
179 std::unique_ptr<views::Widget> selection_widget_; | |
180 | |
181 // Shadow around the selector. | |
182 std::unique_ptr<::wm::Shadow> selector_shadow_; | |
183 | |
184 // Current selected window position. | |
185 size_t selected_index_; | |
186 | |
187 // Number of columns in the grid. | |
188 size_t num_columns_; | |
189 | |
190 // True only after all windows have been prepared for overview. | |
191 bool prepared_for_overview_; | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(WindowGrid); | |
194 }; | |
195 | |
196 } // namespace ash | |
197 | |
198 #endif // ASH_COMMON_WM_OVERVIEW_WINDOW_GRID_H_ | |
OLD | NEW |