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 UI_VIEWS_WIDGET_WIDGET_DELEGATE_H_
6 #define UI_VIEWS_WIDGET_WIDGET_DELEGATE_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "ui/accessibility/ax_enums.mojom-forward.h"
13 #include "ui/base/ui_base_types.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h"
16 
17 namespace gfx {
18 class ImageSkia;
19 class Rect;
20 }  // namespace gfx
21 
22 namespace views {
23 class BubbleDialogDelegateView;
24 class ClientView;
25 class DialogDelegate;
26 class NonClientFrameView;
27 class View;
28 
29 // Handles events on Widgets in context-specific ways.
30 class VIEWS_EXPORT WidgetDelegate {
31  public:
32   WidgetDelegate();
33 
34   // Sets the return value of CanActivate(). Default is true.
35   void SetCanActivate(bool can_activate);
36 
37   // Called whenever the widget's position changes.
38   virtual void OnWidgetMove();
39 
40   // Called with the display changes (color depth or resolution).
41   virtual void OnDisplayChanged();
42 
43   // Called when the work area (the desktop area minus task bars,
44   // menu bars, etc.) changes in size.
45   virtual void OnWorkAreaChanged();
46 
47   // Called when the widget's initialization is complete.
OnWidgetInitialized()48   virtual void OnWidgetInitialized() {}
49 
50   // Called when the window has been requested to close, after all other checks
51   // have run. Returns whether the window should be allowed to close (default is
52   // true).
53   //
54   // Can be used as an alternative to specifying a custom ClientView with
55   // the CanClose() method, or in widget types which do not support a
56   // ClientView.
57   virtual bool OnCloseRequested(Widget::ClosedReason close_reason);
58 
59   // Returns the view that should have the focus when the widget is shown.  If
60   // NULL no view is focused.
61   virtual View* GetInitiallyFocusedView();
62 
63   virtual BubbleDialogDelegateView* AsBubbleDialogDelegate();
64   virtual DialogDelegate* AsDialogDelegate();
65 
66   // Returns true if the window can be resized.
67   virtual bool CanResize() const;
68 
69   // Returns true if the window can be maximized.
70   virtual bool CanMaximize() const;
71 
72   // Returns true if the window can be minimized.
73   virtual bool CanMinimize() const;
74 
75   // Returns true if the window can be activated.
76   virtual bool CanActivate() const;
77 
78   // Returns the modal type that applies to the widget. Default is
79   // ui::MODAL_TYPE_NONE (not modal).
80   virtual ui::ModalType GetModalType() const;
81 
82   virtual ax::mojom::Role GetAccessibleWindowRole();
83 
84   // Returns the title to be read with screen readers.
85   virtual base::string16 GetAccessibleWindowTitle() const;
86 
87   // Returns the text to be displayed in the window title.
88   virtual base::string16 GetWindowTitle() const;
89 
90   // Returns true if the window should show a title in the title bar.
91   virtual bool ShouldShowWindowTitle() const;
92 
93   // Returns true if the title text should be centered. Default is false.
94   virtual bool ShouldCenterWindowTitleText() const;
95 
96   // Returns true if the window should show a close button in the title bar.
97   virtual bool ShouldShowCloseButton() const;
98 
99   // Returns the app icon for the window. On Windows, this is the ICON_BIG used
100   // in Alt-Tab list and Win7's taskbar.
101   virtual gfx::ImageSkia GetWindowAppIcon();
102 
103   // Returns the icon to be displayed in the window.
104   virtual gfx::ImageSkia GetWindowIcon();
105 
106   // Returns true if a window icon should be shown.
107   virtual bool ShouldShowWindowIcon() const;
108 
109   // Execute a command in the window's controller. Returns true if the command
110   // was handled, false if it was not.
111   virtual bool ExecuteWindowsCommand(int command_id);
112 
113   // Returns the window's name identifier. Used to identify this window for
114   // state restoration.
115   virtual std::string GetWindowName() const;
116 
117   // Saves the window's bounds and "show" state. By default this uses the
118   // process' local state keyed by window name (See GetWindowName above). This
119   // behavior can be overridden to provide additional functionality.
120   virtual void SaveWindowPlacement(const gfx::Rect& bounds,
121                                    ui::WindowShowState show_state);
122 
123   // Retrieves the window's bounds and "show" states.
124   // This behavior can be overridden to provide additional functionality.
125   virtual bool GetSavedWindowPlacement(const Widget* widget,
126                                        gfx::Rect* bounds,
127                                        ui::WindowShowState* show_state) const;
128 
129   // Returns true if the window's size should be restored. If this is false,
130   // only the window's origin is restored and the window is given its
131   // preferred size.
132   // Default is true.
133   virtual bool ShouldRestoreWindowSize() const;
134 
135   // Hooks for the end of the Widget/Window lifecycle. As of this writing, these
136   // callbacks happen like so:
137   //   1. Client code calls Widget::CloseWithReason()
138   //   2. WidgetDelegate::WindowWillClose() is called
139   //   3. NativeWidget teardown (maybe async) starts OR the operating system
140   //      abruptly closes the backing native window
141   //   4. WidgetDelegate::WindowClosing() is called
142   //   5. NativeWidget teardown completes, Widget teardown starts
143   //   6. WidgetDelegate::DeleteDelegate() is called
144   //   7. Widget teardown finishes, Widget is deleted
145   // At step 3, the "maybe async" is controlled by whether the close is done via
146   // Close() or CloseNow().
147   // Important note: for OS-initiated window closes, steps 1 and 2 don't happen
148   // - i.e, WindowWillClose() is never invoked.
WindowWillClose()149   virtual void WindowWillClose() {}
WindowClosing()150   virtual void WindowClosing() {}
DeleteDelegate()151   virtual void DeleteDelegate() {}
152 
153   // Called when the user begins/ends to change the bounds of the window.
OnWindowBeginUserBoundsChange()154   virtual void OnWindowBeginUserBoundsChange() {}
OnWindowEndUserBoundsChange()155   virtual void OnWindowEndUserBoundsChange() {}
156 
157   // Returns the Widget associated with this delegate.
158   virtual Widget* GetWidget() = 0;
159   virtual const Widget* GetWidget() const = 0;
160 
161   // Returns the View that is contained within this Widget.
162   virtual View* GetContentsView();
163 
164   // Called by the Widget to create the Client View used to host the contents
165   // of the widget.
166   virtual ClientView* CreateClientView(Widget* widget);
167 
168   // Called by the Widget to create the NonClient Frame View for this widget.
169   // Return NULL to use the default one.
170   virtual NonClientFrameView* CreateNonClientFrameView(Widget* widget);
171 
172   // Called by the Widget to create the overlay View for this widget. Return
173   // NULL for no overlay. The overlay View will fill the Widget and sit on top
174   // of the ClientView and NonClientFrameView (both visually and wrt click
175   // targeting).
176   virtual View* CreateOverlayView();
177 
178   // Returns true if the window can be notified with the work area change.
179   // Otherwise, the work area change for the top window will be processed by
180   // the default window manager. In some cases, like panel, we would like to
181   // manage the positions by ourselves.
182   virtual bool WillProcessWorkAreaChange() const;
183 
184   // Returns true if window has a hit-test mask.
185   virtual bool WidgetHasHitTestMask() const;
186 
187   // Provides the hit-test mask if HasHitTestMask above returns true.
188   virtual void GetWidgetHitTestMask(SkPath* mask) const;
189 
190   // Returns true if focus should advance to the top level widget when
191   // tab/shift-tab is hit and on the last/first focusable view. Default returns
192   // false, which means tab/shift-tab never advance to the top level Widget.
193   virtual bool ShouldAdvanceFocusToTopLevelWidget() const;
194 
195   // Returns true if event handling should descend into |child|.
196   // |location| is in terms of the Window.
197   virtual bool ShouldDescendIntoChildForEventHandling(
198       gfx::NativeView child,
199       const gfx::Point& location);
200 
201   // Populates |panes| with accessible panes in this window that can
202   // be cycled through with keyboard focus.
GetAccessiblePanes(std::vector<View * > * panes)203   virtual void GetAccessiblePanes(std::vector<View*>* panes) {}
204 
205  protected:
206   virtual ~WidgetDelegate();
207 
208  private:
209   friend class Widget;
210 
211   View* default_contents_view_ = nullptr;
212   bool can_activate_ = true;
213 
214   // Managed by Widget. Ensures |this| outlives its Widget.
215   bool can_delete_this_ = true;
216 
217   DISALLOW_COPY_AND_ASSIGN(WidgetDelegate);
218 };
219 
220 // A WidgetDelegate implementation that is-a View. Used to override GetWidget()
221 // to call View's GetWidget() for the common case where a WidgetDelegate
222 // implementation is-a View. Note that WidgetDelegateView is not owned by
223 // view's hierarchy and is expected to be deleted on DeleteDelegate call.
224 class VIEWS_EXPORT WidgetDelegateView : public WidgetDelegate, public View {
225  public:
226   METADATA_HEADER(WidgetDelegateView);
227 
228   WidgetDelegateView();
229   ~WidgetDelegateView() override;
230 
231   // WidgetDelegate:
232   void DeleteDelegate() override;
233   Widget* GetWidget() override;
234   const Widget* GetWidget() const override;
235   views::View* GetContentsView() override;
236 
237  private:
238   DISALLOW_COPY_AND_ASSIGN(WidgetDelegateView);
239 };
240 
241 }  // namespace views
242 
243 #endif  // UI_VIEWS_WIDGET_WIDGET_DELEGATE_H_
244