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.
5 #ifndef CHROME_BROWSER_UI_PANELS_PANEL_H_
6 #define CHROME_BROWSER_UI_PANELS_PANEL_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/command_updater.h"
15 #include "chrome/browser/command_updater_delegate.h"
16 #include "chrome/browser/sessions/session_id.h"
17 #include "chrome/browser/ui/panels/panel_constants.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "ui/base/base_window.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/rect.h"
26 class PanelCollection
;
30 class StackedPanelCollection
;
34 struct NativeWebKeyboardEvent
;
37 namespace extensions
{
39 class WindowController
;
42 // A platform independent implementation of ui::BaseWindow for Panels.
43 // This class gets the first crack at all the ui::BaseWindow calls for Panels
44 // and does one or more of the following:
45 // - Do nothing. The function is not relevant to Panels.
46 // - Do Panel specific platform independent processing and then invoke the
47 // function on the platform specific member. For example, restrict panel
48 // size to certain limits.
49 // - Invoke an appropriate PanelManager function to do stuff that might affect
50 // other Panels. For example deleting a panel would rearrange other panels.
51 class Panel
: public ui::BaseWindow
,
52 public CommandUpdaterDelegate
,
53 public content::NotificationObserver
{
56 // The panel is fully expanded with both title-bar and the client-area.
58 // The panel is shown with the title-bar only.
60 // The panel is shown with 3-pixel line.
64 // Controls how the attention should be drawn.
66 // Uses the panel attention. The panel's titlebar would be painted
67 // differently to attract the user's attention. This is the default mode.
68 USE_PANEL_ATTENTION
= 0x01,
69 // Uses the system attention. On Windows or Linux (depending on Window
70 // Manager), the app icon on taskbar will be flashed. On MacOS, the dock
71 // icon will jump once.
72 USE_SYSTEM_ATTENTION
= 0x02
77 // Returns the PanelManager associated with this panel.
78 PanelManager
* manager() const;
80 const std::string
& app_name() const { return app_name_
; }
81 const gfx::Image
& app_icon() const { return app_icon_
; }
82 const SessionID
& session_id() const { return session_id_
; }
83 extensions::WindowController
* extension_window_controller() const {
84 return extension_window_controller_
.get();
86 const std::string
extension_id() const;
88 CommandUpdater
* command_updater();
89 Profile
* profile() const;
91 const extensions::Extension
* GetExtension() const;
93 // Returns web contents of the panel, if any. There may be none if web
94 // contents have not been added to the panel yet.
95 content::WebContents
* GetWebContents() const;
97 void SetExpansionState(ExpansionState new_expansion_state
);
99 bool IsDrawingAttention() const;
101 // This function will only get called by PanelManager when full screen mode
102 // changes i.e it gets called when an app goes into full screen mode or when
103 // an app exits full screen mode. Panel should respond by making sure
104 // a) it does not go on top when some app enters full screen mode.
105 // b) it remains on top when an app exits full screen mode.
106 void FullScreenModeChanged(bool is_full_screen
);
108 int TitleOnlyHeight() const;
110 // Returns true if the panel can show minimize or restore button in its
111 // titlebar, depending on its state.
112 bool CanShowMinimizeButton() const;
113 bool CanShowRestoreButton() const;
115 // ui::BaseWindow overrides.
116 virtual bool IsActive() const OVERRIDE
;
117 virtual bool IsMaximized() const OVERRIDE
;
118 virtual bool IsMinimized() const OVERRIDE
;
119 virtual bool IsFullscreen() const OVERRIDE
;
120 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
121 virtual gfx::Rect
GetRestoredBounds() const OVERRIDE
;
122 virtual ui::WindowShowState
GetRestoredState() const OVERRIDE
;
123 virtual gfx::Rect
GetBounds() const OVERRIDE
;
124 virtual void Show() OVERRIDE
;
125 virtual void Hide() OVERRIDE
;
126 virtual void ShowInactive() OVERRIDE
;
127 virtual void Close() OVERRIDE
;
128 virtual void Activate() OVERRIDE
;
129 virtual void Deactivate() OVERRIDE
;
130 virtual void Maximize() OVERRIDE
;
131 virtual void Minimize() OVERRIDE
;
132 virtual void Restore() OVERRIDE
;
133 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
134 virtual void FlashFrame(bool flash
) OVERRIDE
;
135 virtual bool IsAlwaysOnTop() const OVERRIDE
;
136 virtual void SetAlwaysOnTop(bool on_top
) OVERRIDE
;
138 // Overridden from CommandUpdaterDelegate:
139 virtual void ExecuteCommandWithDisposition(
141 WindowOpenDisposition disposition
) OVERRIDE
;
143 // content::NotificationObserver overrides.
144 virtual void Observe(int type
,
145 const content::NotificationSource
& source
,
146 const content::NotificationDetails
& details
) OVERRIDE
;
148 // Construct a native panel implementation.
149 static NativePanel
* CreateNativePanel(Panel
* panel
,
150 const gfx::Rect
& bounds
,
153 NativePanel
* native_panel() const { return native_panel_
; }
155 // Invoked when the native panel has detected a mouse click on the
156 // panel's titlebar, minimize or restore buttons. Behavior of the
157 // click may be modified as indicated by |modifier|.
158 void OnTitlebarClicked(panel::ClickModifier modifier
);
159 void OnMinimizeButtonClicked(panel::ClickModifier modifier
);
160 void OnRestoreButtonClicked(panel::ClickModifier modifier
);
162 // Used on platforms where the panel cannot determine its window size
163 // until the window has been created. (e.g. GTK)
164 void OnWindowSizeAvailable();
166 // Asynchronous completion of panel close request.
167 void OnNativePanelClosed();
170 // * panel is newly created and has not been positioned yet.
171 // * panel is being closed asynchronously.
172 // Please use it with caution.
173 PanelCollection
* collection() const { return collection_
; }
175 // Sets the current panel collection that contains this panel.
176 void set_collection(PanelCollection
* new_collection
) {
177 collection_
= new_collection
;
180 StackedPanelCollection
* stack() const;
182 ExpansionState
expansion_state() const { return expansion_state_
; }
183 const gfx::Size
& min_size() const { return min_size_
; }
184 const gfx::Size
& max_size() const { return max_size_
; }
185 bool auto_resizable() const { return auto_resizable_
; }
187 bool in_preview_mode() const { return in_preview_mode_
; }
189 panel::Resizability
CanResizeByMouse() const;
191 AttentionMode
attention_mode() const { return attention_mode_
; }
192 void set_attention_mode(AttentionMode attention_mode
) {
193 attention_mode_
= attention_mode
;
196 // The full size is the size of the panel when it is detached or expanded
197 // in the docked collection and squeezing mode is not on.
198 gfx::Size
full_size() const { return full_size_
; }
199 void set_full_size(const gfx::Size
& size
) { full_size_
= size
; }
201 // Panel must be initialized to be "fully created" and ready for use.
202 // Only called by PanelManager.
203 bool initialized() const { return initialized_
; }
204 void Initialize(const GURL
& url
, const gfx::Rect
& bounds
, bool always_on_top
);
206 // This is different from BaseWindow::SetBounds():
207 // * SetPanelBounds() is only called by PanelManager to manage its position.
208 // * SetBounds() is called by the API to try to change the bounds, which may
209 // only change the size for Panel.
210 void SetPanelBounds(const gfx::Rect
& bounds
);
212 // Updates the panel bounds instantly without any animation.
213 void SetPanelBoundsInstantly(const gfx::Rect
& bounds
);
215 // Ensures that the panel's size does not exceed the work area by updating
216 // maximum and full size of the panel. This is called each time when display
217 // settings are changed. Note that bounds are not updated here and the call
218 // of setting bounds or refreshing layout should be called after this.
219 void LimitSizeToWorkArea(const gfx::Rect
& work_area
);
221 // Sets whether the panel will auto resize according to its content.
222 void SetAutoResizable(bool resizable
);
224 // Configures the web contents for auto resize, including configurations
225 // on the renderer and detecting renderer changes.
226 void EnableWebContentsAutoResize(content::WebContents
* web_contents
);
228 // Invoked when the preferred window size of the given panel might need to
229 // get changed due to the contents being auto-resized.
230 void OnContentsAutoResized(const gfx::Size
& new_content_size
);
232 // Resizes the panel and sets the origin. Invoked when the panel is resized
234 void OnWindowResizedByMouse(const gfx::Rect
& new_bounds
);
236 // Sets minimum and maximum size for the panel.
237 void SetSizeRange(const gfx::Size
& min_size
, const gfx::Size
& max_size
);
239 // Updates the maximum size of the panel so that it's never smaller than the
240 // panel's desired size. Note that even if the user resizes the panel smaller
241 // later, the increased maximum size will still be in effect. Since it's not
242 // possible currently to switch the panel back to autosizing from
243 // user-resizable, it should not be a problem.
244 void IncreaseMaxSize(const gfx::Size
& desired_panel_size
);
246 // Handles keyboard events coming back from the renderer.
247 void HandleKeyboardEvent(const content::NativeWebKeyboardEvent
& event
);
249 // Sets whether the panel is shown in preview mode. When the panel is
250 // being dragged, it is in preview mode.
251 void SetPreviewMode(bool in_preview_mode
);
253 // Sets whether the minimize or restore button, if any, are visible.
254 void UpdateMinimizeRestoreButtonVisibility();
256 // Changes the preferred size to acceptable based on min_size() and max_size()
257 gfx::Size
ClampSize(const gfx::Size
& size
) const;
259 // Called when the panel's active state changes.
260 // |active| is true if panel became active.
261 void OnActiveStateChanged(bool active
);
263 // Called when the panel starts/ends the user resizing.
264 void OnPanelStartUserResizing();
265 void OnPanelEndUserResizing();
267 // Gives beforeunload handlers the chance to cancel the close.
268 bool ShouldCloseWindow();
270 // Invoked when the window containing us is closing. Performs the necessary
272 void OnWindowClosing();
274 // Executes a command if it's enabled.
275 // Returns true if the command is executed.
276 bool ExecuteCommandIfEnabled(int id
);
278 // Gets the title of the window from the web contents.
279 base::string16
GetWindowTitle() const;
281 // Gets the Favicon of the web contents.
282 gfx::Image
GetCurrentPageIcon() const;
284 // Updates the title bar to display the current title and icon.
285 void UpdateTitleBar();
287 // Updates UI to reflect change in loading state.
288 void LoadingStateChanged(bool is_loading
);
290 // Updates UI to reflect that the web cotents receives the focus.
291 void WebContentsFocused(content::WebContents
* contents
);
293 // Moves the panel by delta instantly.
294 void MoveByInstantly(const gfx::Vector2d
& delta_origin
);
296 // Applies |corner_style| to the panel window.
297 void SetWindowCornerStyle(panel::CornerStyle corner_style
);
299 // Performs the system minimize for the panel, i.e. becoming iconic.
300 void MinimizeBySystem();
302 bool IsMinimizedBySystem() const;
304 // Returns true if the panel is shown in the active desktop. The user could
305 // create or use multiple desktops or workspaces.
306 bool IsShownOnActiveDesktop() const;
308 // Turns on/off the shadow effect around the window shape.
309 void ShowShadow(bool show
);
312 // Panel can only be created using PanelManager::CreatePanel() or subclass.
313 // |app_name| is the default title for Panels when the page content does not
314 // provide a title. For extensions, this is usually the application name
315 // generated from the extension id.
316 Panel(Profile
* profile
, const std::string
& app_name
,
317 const gfx::Size
& min_size
, const gfx::Size
& max_size
);
320 friend class PanelManager
;
321 friend class PanelBrowserTest
;
324 // Default maximum size is proportional to the work area.
326 // Custom maximum size is used when the panel is resized by the user.
330 void OnImageLoaded(const gfx::Image
& image
);
332 // Initialize state for all supported commands.
333 void InitCommandState();
335 // Configures the renderer for auto resize (if auto resize is enabled).
336 void ConfigureAutoResize(content::WebContents
* web_contents
);
338 // Load the app's image, firing a load state change when loaded.
339 void UpdateAppIcon();
341 // Prepares a title string for display (removes embedded newlines, etc).
342 static void FormatTitleForDisplay(base::string16
* title
);
344 // The application name that is also the name of the window when the
345 // page content does not provide a title.
346 // This name should be set when the panel is created.
347 const std::string app_name_
;
351 // Current collection of panels to which this panel belongs. This determines
352 // the panel's screen layout.
353 PanelCollection
* collection_
; // Owned by PanelManager.
357 // Stores the full size of the panel so we can restore it after it's
358 // been minimized or squeezed due to lack of space in the collection.
359 gfx::Size full_size_
;
361 // This is the minimum size that the panel can shrink to.
364 // This is the size beyond which the panel is not going to grow to accomodate
365 // the growing content and WebKit would add the scrollbars in such case.
368 MaxSizePolicy max_size_policy_
;
370 // True if this panel auto resizes based on content.
371 bool auto_resizable_
;
373 // True if this panel is in preview mode. When in preview mode, panel bounds
374 // should not be affected by layout refresh. This is currently used by drag
375 // controller to add a panel to the collection without causing its bounds to
377 bool in_preview_mode_
;
379 // Platform specifc implementation for panels. It'd be one of
380 // PanelGtk/PanelView/PanelCocoa.
381 NativePanel
* native_panel_
; // Weak, owns us.
383 AttentionMode attention_mode_
;
385 ExpansionState expansion_state_
;
387 // The CommandUpdater manages the window commands.
388 CommandUpdater command_updater_
;
390 content::NotificationRegistrar registrar_
;
391 const SessionID session_id_
;
392 scoped_ptr
<extensions::WindowController
> extension_window_controller_
;
393 scoped_ptr
<PanelHost
> panel_host_
;
395 // Icon showed in the task bar.
396 gfx::Image app_icon_
;
398 base::WeakPtrFactory
<Panel
> image_loader_ptr_factory_
;
400 DISALLOW_COPY_AND_ASSIGN(Panel
);
403 #endif // CHROME_BROWSER_UI_PANELS_PANEL_H_