linux_ui.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2013 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_LINUX_LINUX_UI_H_
  5. #define UI_LINUX_LINUX_UI_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/command_line.h"
  11. #include "base/component_export.h"
  12. #include "base/containers/flat_map.h"
  13. #include "base/observer_list.h"
  14. #include "build/buildflag.h"
  15. #include "build/chromecast_buildflags.h"
  16. #include "printing/buildflags/buildflags.h"
  17. // The main entrypoint into Linux toolkit specific code. GTK/QT code should only
  18. // be executed behind this interface.
  19. using SkColor = uint32_t;
  20. namespace aura {
  21. class Window;
  22. }
  23. namespace base {
  24. class TimeDelta;
  25. }
  26. namespace gfx {
  27. struct FontRenderParams;
  28. class Image;
  29. class Size;
  30. } // namespace gfx
  31. namespace printing {
  32. class PrintingContextLinux;
  33. class PrintDialogLinuxInterface;
  34. } // namespace printing
  35. namespace ui {
  36. class CursorThemeManagerObserver;
  37. class DeviceScaleFactorObserver;
  38. class Event;
  39. class LinuxInputMethodContext;
  40. class LinuxInputMethodContextDelegate;
  41. class NativeTheme;
  42. class NavButtonProvider;
  43. class SelectFileDialog;
  44. class SelectFilePolicy;
  45. class TextEditCommandAuraLinux;
  46. class WindowButtonOrderObserver;
  47. class WindowFrameProvider;
  48. // Adapter class with targets to render like different toolkits. Set by any
  49. // project that wants to do linux desktop native rendering.
  50. class COMPONENT_EXPORT(LINUX_UI) LinuxUi {
  51. public:
  52. using UseSystemThemeCallback =
  53. base::RepeatingCallback<bool(aura::Window* window)>;
  54. // Describes the window management actions that could be taken in response to
  55. // a middle click in the non client area.
  56. enum class WindowFrameAction {
  57. kNone,
  58. kLower,
  59. kMinimize,
  60. kToggleMaximize,
  61. kMenu,
  62. };
  63. // The types of clicks that might invoke a WindowFrameAction.
  64. enum class WindowFrameActionSource {
  65. kDoubleClick,
  66. kMiddleClick,
  67. kRightClick,
  68. };
  69. LinuxUi(const LinuxUi&) = delete;
  70. LinuxUi& operator=(const LinuxUi&) = delete;
  71. virtual ~LinuxUi();
  72. // Sets the dynamically loaded singleton that draws the desktop native UI.
  73. // Returns the old instance if any.
  74. static std::unique_ptr<LinuxUi> SetInstance(
  75. std::unique_ptr<LinuxUi> instance);
  76. // Returns a LinuxUI instance for the toolkit used in the user's desktop
  77. // environment.
  78. //
  79. // Can return NULL, in case no toolkit has been set. (For example, if we're
  80. // running with the "--ash" flag.)
  81. static LinuxUi* instance();
  82. // Notifies the observer about changes about how window buttons should be
  83. // laid out.
  84. void AddWindowButtonOrderObserver(WindowButtonOrderObserver* observer);
  85. // Removes the observer from the LinuxUI's list.
  86. void RemoveWindowButtonOrderObserver(WindowButtonOrderObserver* observer);
  87. // Registers |observer| to be notified about changes to the device
  88. // scale factor.
  89. void AddDeviceScaleFactorObserver(DeviceScaleFactorObserver* observer);
  90. // Unregisters |observer| from receiving changes to the device scale
  91. // factor.
  92. void RemoveDeviceScaleFactorObserver(DeviceScaleFactorObserver* observer);
  93. // Adds |observer| and makes initial OnCursorThemNameChanged() and/or
  94. // OnCursorThemeSizeChanged() calls if the respective settings were set.
  95. void AddCursorThemeObserver(CursorThemeManagerObserver* observer);
  96. void RemoveCursorThemeObserver(CursorThemeManagerObserver* observer);
  97. // Returns the NativeTheme that reflects the theme used by `window`.
  98. ui::NativeTheme* GetNativeTheme(aura::Window* window) const;
  99. // Returns the classic or system NativeTheme depending on `use_system_theme`.
  100. virtual ui::NativeTheme* GetNativeTheme(bool use_system_theme) const = 0;
  101. // Sets a callback that determines whether to use the system theme.
  102. void SetUseSystemThemeCallback(UseSystemThemeCallback callback);
  103. // Returns whether we should be using the native theme provided by this
  104. // object by default.
  105. bool GetDefaultUsesSystemTheme() const;
  106. // Returns true on success. If false is returned, this instance shouldn't
  107. // be used and the behavior of all functions is undefined.
  108. [[nodiscard]] virtual bool Initialize() = 0;
  109. virtual bool GetColor(int id,
  110. SkColor* color,
  111. bool use_custom_frame) const = 0;
  112. virtual bool GetDisplayProperty(int id, int* result) const = 0;
  113. // Returns the preferences that we pass to Blink.
  114. virtual SkColor GetFocusRingColor() const = 0;
  115. virtual SkColor GetActiveSelectionBgColor() const = 0;
  116. virtual SkColor GetActiveSelectionFgColor() const = 0;
  117. virtual SkColor GetInactiveSelectionBgColor() const = 0;
  118. virtual SkColor GetInactiveSelectionFgColor() const = 0;
  119. virtual base::TimeDelta GetCursorBlinkInterval() const = 0;
  120. // Returns the icon for a given content type from the icon theme.
  121. // TODO(davidben): Add an observer for the theme changing, so we can drop the
  122. // caches.
  123. virtual gfx::Image GetIconForContentType(const std::string& content_type,
  124. int size,
  125. float scale) const = 0;
  126. // What action we should take when the user clicks on the non-client area.
  127. // |source| describes the type of click.
  128. virtual WindowFrameAction GetWindowFrameAction(
  129. WindowFrameActionSource source) = 0;
  130. // Determines the device scale factor of the primary screen.
  131. virtual float GetDeviceScaleFactor() const = 0;
  132. // Only used on GTK to indicate if the dark GTK theme variant is
  133. // preferred.
  134. virtual bool PreferDarkTheme() const = 0;
  135. // Returns a new NavButtonProvider, or nullptr if the underlying
  136. // toolkit does not support drawing client-side navigation buttons.
  137. virtual std::unique_ptr<NavButtonProvider> CreateNavButtonProvider() = 0;
  138. // Returns a WindowFrameProvider, or nullptr if the underlying toolkit does
  139. // not support drawing client-side window decorations. |solid_frame| indicates
  140. // if transparency is unsupported and the frame should be rendered opaque.
  141. // The returned object is not owned by the caller and will remain alive until
  142. // the process ends.
  143. virtual WindowFrameProvider* GetWindowFrameProvider(bool solid_frame) = 0;
  144. // Returns a map of KeyboardEvent code to KeyboardEvent key values.
  145. virtual base::flat_map<std::string, std::string> GetKeyboardLayoutMap() = 0;
  146. #if BUILDFLAG(ENABLE_PRINTING)
  147. virtual printing::PrintDialogLinuxInterface* CreatePrintDialog(
  148. printing::PrintingContextLinux* context) = 0;
  149. virtual gfx::Size GetPdfPaperSize(
  150. printing::PrintingContextLinux* context) = 0;
  151. #endif
  152. // Returns a native file selection dialog. `listener` is of type
  153. // SelectFileDialog::Listener. TODO(thomasanderson): Move
  154. // SelectFileDialog::Listener to SelectFileDialogListener so that it can be
  155. // forward declared.
  156. virtual SelectFileDialog* CreateSelectFileDialog(
  157. void* listener,
  158. std::unique_ptr<SelectFilePolicy> policy) const = 0;
  159. // Returns the prefererd theme name for cursor loading.
  160. virtual std::string GetCursorThemeName() = 0;
  161. // Returns the preferred size for cursor bitmaps. A value of 64 indicates
  162. // that 64x64 px bitmaps are preferred.
  163. virtual int GetCursorThemeSize() = 0;
  164. // Returns a platform specific input method context.
  165. virtual std::unique_ptr<LinuxInputMethodContext> CreateInputMethodContext(
  166. LinuxInputMethodContextDelegate* delegate) const = 0;
  167. // Matches a key event against the users' platform specific key bindings,
  168. // false will be returned if the key event doesn't correspond to a predefined
  169. // key binding. Edit commands matched with |event| will be stored in
  170. // |edit_commands|, if |edit_commands| is non-nullptr.
  171. virtual bool GetTextEditCommandsForEvent(
  172. const ui::Event& event,
  173. std::vector<TextEditCommandAuraLinux>* commands) = 0;
  174. // Returns the default font rendering settings.
  175. virtual gfx::FontRenderParams GetDefaultFontRenderParams() const = 0;
  176. // Returns details about the default UI font. |style_out| holds a bitfield of
  177. // gfx::Font::Style values.
  178. virtual void GetDefaultFontDescription(
  179. std::string* family_out,
  180. int* size_pixels_out,
  181. int* style_out,
  182. int* weight_out,
  183. gfx::FontRenderParams* params_out) const = 0;
  184. // Indicates if animations are enabled by the toolkit.
  185. virtual bool AnimationsEnabled() const = 0;
  186. protected:
  187. struct CmdLineArgs {
  188. CmdLineArgs();
  189. CmdLineArgs(CmdLineArgs&&);
  190. CmdLineArgs& operator=(CmdLineArgs&&);
  191. ~CmdLineArgs();
  192. // `argc` is modified by toolkits, so store it explicitly.
  193. int argc = 0;
  194. // Contains C-strings that point into `args`. `argv.size()` >= `argc`.
  195. std::vector<char*> argv;
  196. // `argv` concatenated with NUL characters.
  197. std::vector<char> args;
  198. };
  199. LinuxUi();
  200. static CmdLineArgs CopyCmdLine(const base::CommandLine& command_line);
  201. const base::ObserverList<WindowButtonOrderObserver>::Unchecked&
  202. window_button_order_observer_list() const {
  203. return window_button_order_observer_list_;
  204. }
  205. const base::ObserverList<DeviceScaleFactorObserver>::Unchecked&
  206. device_scale_factor_observer_list() const {
  207. return device_scale_factor_observer_list_;
  208. }
  209. const base::ObserverList<CursorThemeManagerObserver>&
  210. cursor_theme_observers() {
  211. return cursor_theme_observer_list_;
  212. }
  213. virtual ui::NativeTheme* GetNativeThemeImpl() const = 0;
  214. private:
  215. // Used to determine whether the system theme should be used for a window. If
  216. // no override is provided or the callback returns true, LinuxUI will default
  217. // to GetNativeTheme().
  218. UseSystemThemeCallback use_system_theme_callback_;
  219. // Objects to notify when the window frame button order changes.
  220. base::ObserverList<WindowButtonOrderObserver>::Unchecked
  221. window_button_order_observer_list_;
  222. // Objects to notify when the device scale factor changes.
  223. base::ObserverList<DeviceScaleFactorObserver>::Unchecked
  224. device_scale_factor_observer_list_;
  225. // Objects to notify when the cursor theme or size changes.
  226. base::ObserverList<CursorThemeManagerObserver> cursor_theme_observer_list_;
  227. };
  228. } // namespace ui
  229. #endif // UI_LINUX_LINUX_UI_H_