OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 CHROME_BROWSER_UI_PANELS_PANEL_MANAGER_H_ | |
6 #define CHROME_BROWSER_UI_PANELS_PANEL_MANAGER_H_ | |
7 | |
8 #include <list> | |
9 #include <memory> | |
10 #include <vector> | |
11 | |
12 #include "base/lazy_instance.h" | |
13 #include "base/macros.h" | |
14 #include "chrome/browser/ui/panels/display_settings_provider.h" | |
15 #include "chrome/browser/ui/panels/panel.h" | |
16 #include "chrome/browser/ui/panels/panel_collection.h" | |
17 #include "chrome/browser/ui/panels/panel_constants.h" | |
18 #include "ui/gfx/geometry/rect.h" | |
19 | |
20 class DetachedPanelCollection; | |
21 class DockedPanelCollection; | |
22 class GURL; | |
23 class PanelDragController; | |
24 class PanelResizeController; | |
25 class PanelMouseWatcher; | |
26 class StackedPanelCollection; | |
27 | |
28 namespace content { | |
29 class SiteInstance; | |
30 } | |
31 | |
32 // This class manages a set of panels. | |
33 class PanelManager : public DisplaySettingsProvider::DisplayObserver, | |
34 public DisplaySettingsProvider::FullScreenObserver { | |
35 public: | |
36 typedef std::list<StackedPanelCollection*> Stacks; | |
37 | |
38 enum CreateMode { | |
39 CREATE_AS_DOCKED, // Creates a docked panel. The default. | |
40 CREATE_AS_DETACHED // Creates a detached panel. | |
41 }; | |
42 | |
43 // Returns a single instance. | |
44 static PanelManager* GetInstance(); | |
45 | |
46 // Tells PanelManager to use |provider| for testing purpose. This has to be | |
47 // called before GetInstance. | |
48 static void SetDisplaySettingsProviderForTesting( | |
49 DisplaySettingsProvider* provider); | |
50 | |
51 // Returns true if panels should be used for the extension. | |
52 static bool ShouldUsePanels(const std::string& extension_id); | |
53 | |
54 // Returns true if panel stacking support is enabled. | |
55 static bool IsPanelStackingEnabled(); | |
56 | |
57 // Returns true if a panel can be system-minimized by the desktop | |
58 // environment. Some desktop environment, like Unity, does not trigger the | |
59 // "window-state-event" which prevents the minimize and unminimize from | |
60 // working. | |
61 static bool CanUseSystemMinimize(); | |
62 | |
63 // Returns the default top-left position for a detached panel. | |
64 gfx::Point GetDefaultDetachedPanelOrigin(); | |
65 | |
66 // Creates a panel and returns it. The panel might be queued for display | |
67 // later. | |
68 // |app_name| is the default title for Panels when the page content does not | |
69 // provide a title. For extensions, this is usually the application name | |
70 // generated from the extension id. | |
71 // |source_site_instance| indicates the SiteInstance that requested to create | |
72 // this panel. | |
73 // |requested_bounds| is the desired bounds for the panel, but actual | |
74 // bounds may differ after panel layout depending on create |mode|. | |
75 // |mode| indicates whether panel should be created as docked or detached. | |
76 Panel* CreatePanel(const std::string& app_name, | |
77 Profile* profile, | |
78 const GURL& url, | |
79 content::SiteInstance* source_site_instance, | |
80 const gfx::Rect& requested_bounds, | |
81 CreateMode mode); | |
82 | |
83 // Close all panels (asynchronous). Panels will be removed after closing. | |
84 void CloseAll(); | |
85 | |
86 // Asynchronous confirmation of panel having been closed. | |
87 void OnPanelClosed(Panel* panel); | |
88 | |
89 // Creates a StackedPanelCollection and returns it. | |
90 StackedPanelCollection* CreateStack(); | |
91 | |
92 // Deletes |stack|. The stack must be empty at the time of deletion. | |
93 void RemoveStack(StackedPanelCollection* stack); | |
94 | |
95 // Returns the maximum size that panel can be auto-resized or resized by the | |
96 // API. | |
97 int GetMaxPanelWidth(const gfx::Rect& work_area) const; | |
98 int GetMaxPanelHeight(const gfx::Rect& work_area) const; | |
99 | |
100 // Drags the given panel. | |
101 // |mouse_location| is in screen coordinate system. | |
102 void StartDragging(Panel* panel, const gfx::Point& mouse_location); | |
103 void Drag(const gfx::Point& mouse_location); | |
104 void EndDragging(bool cancelled); | |
105 | |
106 // Resizes the given panel. | |
107 // |mouse_location| is in screen coordinate system. | |
108 void StartResizingByMouse(Panel* panel, const gfx::Point& mouse_location, | |
109 int component); | |
110 void ResizeByMouse(const gfx::Point& mouse_location); | |
111 void EndResizingByMouse(bool cancelled); | |
112 | |
113 // Invoked when a panel's expansion state changes. | |
114 void OnPanelExpansionStateChanged(Panel* panel); | |
115 | |
116 // Moves the |panel| to a different collection. | |
117 void MovePanelToCollection(Panel* panel, | |
118 PanelCollection* target_collection, | |
119 PanelCollection::PositioningMask positioning_mask); | |
120 | |
121 // Returns true if we should bring up the titlebars, given the current mouse | |
122 // point. | |
123 bool ShouldBringUpTitlebars(int mouse_x, int mouse_y) const; | |
124 | |
125 // Brings up or down the titlebars for all minimized panels. | |
126 void BringUpOrDownTitlebars(bool bring_up); | |
127 | |
128 std::vector<Panel*> GetDetachedAndStackedPanels() const; | |
129 | |
130 int num_panels() const; | |
131 std::vector<Panel*> panels() const; | |
132 | |
133 const Stacks& stacks() const { return stacks_; } | |
134 int num_stacks() const { return stacks_.size(); } | |
135 | |
136 PanelDragController* drag_controller() const { | |
137 return drag_controller_.get(); | |
138 } | |
139 | |
140 #ifdef UNIT_TEST | |
141 PanelResizeController* resize_controller() const { | |
142 return resize_controller_.get(); | |
143 } | |
144 #endif | |
145 | |
146 DisplaySettingsProvider* display_settings_provider() const { | |
147 return display_settings_provider_.get(); | |
148 } | |
149 | |
150 PanelMouseWatcher* mouse_watcher() const { | |
151 return panel_mouse_watcher_.get(); | |
152 } | |
153 | |
154 DetachedPanelCollection* detached_collection() const { | |
155 return detached_collection_.get(); | |
156 } | |
157 | |
158 DockedPanelCollection* docked_collection() const { | |
159 return docked_collection_.get(); | |
160 } | |
161 | |
162 // Reduces time interval in tests to shorten test run time. | |
163 // Wrapper should be used around all time intervals in panels code. | |
164 static inline double AdjustTimeInterval(double interval) { | |
165 if (shorten_time_intervals_) | |
166 return interval / 500.0; | |
167 else | |
168 return interval; | |
169 } | |
170 | |
171 | |
172 bool auto_sizing_enabled() const { | |
173 return auto_sizing_enabled_; | |
174 } | |
175 | |
176 // Called from native level when panel animation ends. | |
177 void OnPanelAnimationEnded(Panel* panel); | |
178 | |
179 #ifdef UNIT_TEST | |
180 static void shorten_time_intervals_for_testing() { | |
181 shorten_time_intervals_ = true; | |
182 } | |
183 | |
184 void set_display_settings_provider( | |
185 DisplaySettingsProvider* display_settings_provider) { | |
186 display_settings_provider_.reset(display_settings_provider); | |
187 } | |
188 | |
189 void enable_auto_sizing(bool enabled) { | |
190 auto_sizing_enabled_ = enabled; | |
191 } | |
192 | |
193 void SetMouseWatcherForTesting(PanelMouseWatcher* watcher) { | |
194 SetMouseWatcher(watcher); | |
195 } | |
196 #endif | |
197 | |
198 private: | |
199 friend struct base::DefaultLazyInstanceTraits<PanelManager>; | |
200 | |
201 PanelManager(); | |
202 virtual ~PanelManager(); | |
203 | |
204 void Initialize(DisplaySettingsProvider* provider); | |
205 | |
206 // Overridden from DisplaySettingsProvider::DisplayObserver: | |
207 void OnDisplayChanged() override; | |
208 | |
209 // Overridden from DisplaySettingsProvider::FullScreenObserver: | |
210 void OnFullScreenModeChanged(bool is_full_screen) override; | |
211 | |
212 // Returns the collection to which a new panel should add. The new panel | |
213 // is expected to be created with |bounds| and |mode|. The size of |bounds| | |
214 // could be used to determine which collection is more appropriate to have | |
215 // the new panel. Upon return, |positioning_mask| contains the required mask | |
216 // to be applied when the new panel is being added to the collection. | |
217 PanelCollection* GetCollectionForNewPanel( | |
218 Panel* new_panel, | |
219 const gfx::Rect& bounds, | |
220 CreateMode mode, | |
221 PanelCollection::PositioningMask* positioning_mask); | |
222 | |
223 // Tests may want to use a mock panel mouse watcher. | |
224 void SetMouseWatcher(PanelMouseWatcher* watcher); | |
225 | |
226 // Tests may want to shorten time intervals to reduce running time. | |
227 static bool shorten_time_intervals_; | |
228 | |
229 std::unique_ptr<DetachedPanelCollection> detached_collection_; | |
230 std::unique_ptr<DockedPanelCollection> docked_collection_; | |
231 Stacks stacks_; | |
232 | |
233 std::unique_ptr<PanelDragController> drag_controller_; | |
234 std::unique_ptr<PanelResizeController> resize_controller_; | |
235 | |
236 // Use a mouse watcher to know when to bring up titlebars to "peek" at | |
237 // minimized panels. Mouse movement is only tracked when there is a minimized | |
238 // panel. | |
239 std::unique_ptr<PanelMouseWatcher> panel_mouse_watcher_; | |
240 | |
241 std::unique_ptr<DisplaySettingsProvider> display_settings_provider_; | |
242 | |
243 // Whether or not bounds will be updated when the preferred content size is | |
244 // changed. The testing code could set this flag to false so that other tests | |
245 // will not be affected. | |
246 bool auto_sizing_enabled_; | |
247 | |
248 DISALLOW_COPY_AND_ASSIGN(PanelManager); | |
249 }; | |
250 | |
251 #endif // CHROME_BROWSER_UI_PANELS_PANEL_MANAGER_H_ | |
OLD | NEW |