views_delegate.h 7.3 KB

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