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_VIEWS_DELEGATE_H_
6 #define UI_VIEWS_VIEWS_DELEGATE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #if defined(OS_WIN)
13 #include <windows.h>
14 #endif
15 
16 #include "base/callback.h"
17 #include "base/location.h"
18 #include "base/macros.h"
19 #include "base/strings/string16.h"
20 #include "build/build_config.h"
21 #include "ui/base/ui_base_types.h"
22 #include "ui/gfx/native_widget_types.h"
23 #include "ui/views/buildflags.h"
24 #include "ui/views/views_export.h"
25 #include "ui/views/widget/widget.h"
26 
27 namespace gfx {
28 class ImageSkia;
29 class Rect;
30 }  // namespace gfx
31 
32 namespace ui {
33 #if defined(OS_MACOSX)
34 class ContextFactory;
35 #endif
36 class TouchEditingControllerFactory;
37 }  // namespace ui
38 
39 namespace views {
40 
41 class NativeWidget;
42 class NonClientFrameView;
43 class Widget;
44 
45 #if defined(USE_AURA)
46 class TouchSelectionMenuRunnerViews;
47 #endif
48 
49 namespace internal {
50 class NativeWidgetDelegate;
51 }
52 
53 // ViewsDelegate is an interface implemented by an object using the views
54 // framework. It is used to obtain various high level application utilities
55 // and perform some actions such as window placement saving.
56 //
57 // The embedding app must set the ViewsDelegate instance by instantiating an
58 // implementation of ViewsDelegate (the constructor will set the instance).
59 class VIEWS_EXPORT ViewsDelegate {
60  public:
61   using NativeWidgetFactory =
62       base::RepeatingCallback<NativeWidget*(const Widget::InitParams&,
63                                             internal::NativeWidgetDelegate*)>;
64 #if defined(OS_WIN)
65   enum AppbarAutohideEdge {
66     EDGE_TOP = 1 << 0,
67     EDGE_LEFT = 1 << 1,
68     EDGE_BOTTOM = 1 << 2,
69     EDGE_RIGHT = 1 << 3,
70   };
71 #endif
72 
73   enum class ProcessMenuAcceleratorResult {
74     // The accelerator was handled while the menu was showing. No further action
75     // is needed and the menu should be kept open.
76     LEAVE_MENU_OPEN,
77 
78     // The accelerator was not handled. The menu should be closed and event
79     // handling should stop for this event.
80     CLOSE_MENU,
81   };
82 
83   virtual ~ViewsDelegate();
84 
85   // Returns the ViewsDelegate instance.  This should never return non-null
86   // unless the binary has not yet initialized the delegate, so callers should
87   // not generally null-check.
88   static ViewsDelegate* GetInstance();
89 
90   // Call this method to set a factory callback that will be used to construct
91   // NativeWidget implementations overriding the platform defaults.
set_native_widget_factory(NativeWidgetFactory factory)92   void set_native_widget_factory(NativeWidgetFactory factory) {
93     native_widget_factory_ = std::move(factory);
94   }
native_widget_factory()95   const NativeWidgetFactory& native_widget_factory() const {
96     return native_widget_factory_;
97   }
98 
99   // Saves the position, size and "show" state for the window with the
100   // specified name.
101   virtual void SaveWindowPlacement(const Widget* widget,
102                                    const std::string& window_name,
103                                    const gfx::Rect& bounds,
104                                    ui::WindowShowState show_state);
105 
106   // Retrieves the saved position and size and "show" state for the window with
107   // the specified name.
108   virtual bool GetSavedWindowPlacement(const Widget* widget,
109                                        const std::string& window_name,
110                                        gfx::Rect* bounds,
111                                        ui::WindowShowState* show_state) const;
112 
113   // For accessibility, notify the delegate that a menu item was focused
114   // so that alternate feedback (speech / magnified text) can be provided.
115   virtual void NotifyMenuItemFocused(const base::string16& menu_name,
116                                      const base::string16& menu_item_name,
117                                      int item_index,
118                                      int item_count,
119                                      bool has_submenu);
120 
121   // If |accelerator| can be processed while a menu is showing, it will be
122   // processed now and LEAVE_MENU_OPEN is returned. Otherwise, |accelerator|
123   // will be reposted for processing later after the menu closes and CLOSE_MENU
124   // will be returned.
125   virtual ProcessMenuAcceleratorResult ProcessAcceleratorWhileMenuShowing(
126       const ui::Accelerator& accelerator);
127 
128 #if defined(OS_WIN)
129   // Retrieves the default window icon to use for windows if none is specified.
130   virtual HICON GetDefaultWindowIcon() const;
131   // Retrieves the small window icon to use for windows if none is specified.
132   virtual HICON GetSmallWindowIcon() const;
133   // Returns true if the window passed in is in the Windows 8 metro
134   // environment.
135   virtual bool IsWindowInMetro(gfx::NativeWindow window) const;
136 #elif (defined(OS_LINUX) || defined(OS_BSD)) && BUILDFLAG(ENABLE_DESKTOP_AURA)
137   virtual gfx::ImageSkia* GetDefaultWindowIcon() const;
138 #endif
139 
140   // Creates a default NonClientFrameView to be used for windows that don't
141   // specify their own. If this function returns NULL, the
142   // views::CustomFrameView type will be used.
143   virtual NonClientFrameView* CreateDefaultNonClientFrameView(Widget* widget);
144 
145   // AddRef/ReleaseRef are invoked while a menu is visible. They are used to
146   // ensure we don't attempt to exit while a menu is showing.
147   virtual void AddRef();
148   virtual void ReleaseRef();
149   // Returns true if the application is shutting down. AddRef/Release should not
150   // be called in this situation.
151   virtual bool IsShuttingDown() const;
152 
153   // Gives the platform a chance to modify the properties of a Widget.
154   virtual void OnBeforeWidgetInit(Widget::InitParams* params,
155                                   internal::NativeWidgetDelegate* delegate);
156 
157   // Returns true if the operating system's window manager will always provide a
158   // title bar with caption buttons (ignoring the setting to
159   // |remove_standard_frame| in InitParams). If |maximized|, this applies to
160   // maximized windows; otherwise to restored windows.
161   virtual bool WindowManagerProvidesTitleBar(bool maximized);
162 
163 #if defined(OS_MACOSX)
164   // Returns the context factory for new windows.
165   virtual ui::ContextFactory* GetContextFactory();
166 #endif
167 
168   // Returns the user-visible name of the application.
169   virtual std::string GetApplicationName();
170 
171 #if defined(OS_WIN)
172   // Starts a query for the appbar autohide edges of the specified monitor and
173   // returns the current value.  If the query finds the edges have changed from
174   // the current value, |callback| is subsequently invoked.  If the edges have
175   // not changed, |callback| is never run.
176   //
177   // The return value is a bitmask of AppbarAutohideEdge.
178   virtual int GetAppbarAutohideEdges(HMONITOR monitor,
179                                      base::OnceClosure callback);
180 #endif
181 
182  protected:
183   ViewsDelegate();
184 
185 #if defined(USE_AURA)
186   void SetTouchSelectionMenuRunner(
187       std::unique_ptr<TouchSelectionMenuRunnerViews> menu_runner);
188 #endif
189 
190  private:
191   std::unique_ptr<ui::TouchEditingControllerFactory>
192       editing_controller_factory_;
193 
194 #if defined(USE_AURA)
195   std::unique_ptr<TouchSelectionMenuRunnerViews> touch_selection_menu_runner_;
196 #endif
197 
198   NativeWidgetFactory native_widget_factory_;
199 
200   DISALLOW_COPY_AND_ASSIGN(ViewsDelegate);
201 };
202 
203 }  // namespace views
204 
205 #endif  // UI_VIEWS_VIEWS_DELEGATE_H_
206