palette_tray.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2016 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 ASH_SYSTEM_PALETTE_PALETTE_TRAY_H_
  5. #define ASH_SYSTEM_PALETTE_PALETTE_TRAY_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/display/window_tree_host_manager.h"
  9. #include "ash/public/cpp/projector/projector_session.h"
  10. #include "ash/public/cpp/session/session_observer.h"
  11. #include "ash/shell_observer.h"
  12. #include "ash/system/palette/palette_tool_manager.h"
  13. #include "ash/system/palette/stylus_battery_delegate.h"
  14. #include "ash/system/tray/tray_background_view.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/scoped_observation.h"
  17. #include "ui/events/devices/input_device_event_observer.h"
  18. class PrefChangeRegistrar;
  19. class PrefRegistrySimple;
  20. class PrefService;
  21. namespace gfx {
  22. class Point;
  23. }
  24. namespace views {
  25. class ImageView;
  26. }
  27. namespace ash {
  28. class PaletteTrayTestApi;
  29. class PaletteToolManager;
  30. class PaletteWelcomeBubble;
  31. class TrayBubbleWrapper;
  32. // The PaletteTray shows the palette in the bottom area of the screen. This
  33. // class also controls the lifetime for all of the tools available in the
  34. // palette. PaletteTray has one instance per-display. It is only made visible if
  35. // the display has stylus hardware.
  36. class ASH_EXPORT PaletteTray : public TrayBackgroundView,
  37. public SessionObserver,
  38. public ShellObserver,
  39. public WindowTreeHostManager::Observer,
  40. public PaletteToolManager::Delegate,
  41. public ui::InputDeviceEventObserver,
  42. public ProjectorSessionObserver {
  43. public:
  44. explicit PaletteTray(Shelf* shelf);
  45. PaletteTray(const PaletteTray&) = delete;
  46. PaletteTray& operator=(const PaletteTray&) = delete;
  47. ~PaletteTray() override;
  48. static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
  49. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  50. // Returns true if the palette tray contains the given point. This is useful
  51. // for determining if an event should be propagated through to the palette.
  52. bool ContainsPointInScreen(const gfx::Point& point);
  53. // Returns true if the palette should be visible in the UI. This happens when
  54. // there is a stylus display and the user has not disabled it in settings.
  55. // This can be overridden by passing switches.
  56. bool ShouldShowPalette() const;
  57. // Handles stylus events to show the welcome bubble on first usage.
  58. void OnStylusEvent(const ui::TouchEvent& event);
  59. // SessionObserver:
  60. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  61. void OnSessionStateChanged(session_manager::SessionState state) override;
  62. // ShellObserver:
  63. void OnLockStateChanged(bool locked) override;
  64. void OnShellInitialized() override;
  65. // WindowTreeHostManager::Observer:
  66. void OnDisplayConfigurationChanged() override;
  67. // TrayBackgroundView:
  68. void ClickedOutsideBubble() override;
  69. void OnThemeChanged() override;
  70. std::u16string GetAccessibleNameForTray() override;
  71. void HandleLocaleChange() override;
  72. void HideBubbleWithView(const TrayBubbleView* bubble_view) override;
  73. void AnchorUpdated() override;
  74. void Initialize() override;
  75. bool PerformAction(const ui::Event& event) override;
  76. void CloseBubble() override;
  77. void ShowBubble() override;
  78. TrayBubbleView* GetBubbleView() override;
  79. views::Widget* GetBubbleWidget() const override;
  80. const char* GetClassName() const override;
  81. // PaletteToolManager::Delegate:
  82. void HidePalette() override;
  83. void HidePaletteImmediately() override;
  84. void RecordPaletteOptionsUsage(PaletteTrayOptions option,
  85. PaletteInvocationMethod method) override;
  86. void RecordPaletteModeCancellation(PaletteModeCancelType type) override;
  87. // ProjectorSessionObserver:
  88. void OnProjectorSessionActiveStateChanged(bool active) override;
  89. private:
  90. friend class PaletteTrayTestApi;
  91. // ui::InputDeviceObserver:
  92. void OnInputDeviceConfigurationChanged(uint8_t input_device_types) override;
  93. void OnStylusStateChanged(ui::StylusState stylus_state) override;
  94. void OnTouchDeviceAssociationChanged() override;
  95. // TrayBubbleView::Delegate:
  96. void BubbleViewDestroyed() override;
  97. std::u16string GetAccessibleNameForBubble() override;
  98. bool ShouldEnableExtraKeyboardAccessibility() override;
  99. void HideBubble(const TrayBubbleView* bubble_view) override;
  100. // PaletteToolManager::Delegate:
  101. void OnActiveToolChanged() override;
  102. aura::Window* GetWindow() override;
  103. // Returns true if we're on a display with a stylus or on every
  104. // display if requested from the command line.
  105. bool ShouldShowOnDisplay();
  106. // Returns true if our widget is on an internal display.
  107. bool IsWidgetOnInternalDisplay();
  108. // Initializes with Shell's local state and starts to observe it.
  109. void InitializeWithLocalState();
  110. // Updates the tray icon from the palette tool manager.
  111. void UpdateTrayIcon();
  112. // Sets the icon to visible if the palette can be used.
  113. void UpdateIconVisibility();
  114. // Called when the palette enabled pref has changed.
  115. void OnPaletteEnabledPrefChanged();
  116. // Called when the has seen stylus pref has changed.
  117. void OnHasSeenStylusPrefChanged();
  118. // Deactivates the active tool. Returns false if there was no active tool.
  119. bool DeactivateActiveTool();
  120. // Helper method which returns true if the device has seen a stylus event
  121. // previously, or if the device has an internal stylus.
  122. bool HasSeenStylus();
  123. // Have the palette act as though it is on a display with a stylus for
  124. // testing purposes.
  125. void SetDisplayHasStylusForTesting();
  126. std::unique_ptr<PaletteToolManager> palette_tool_manager_;
  127. std::unique_ptr<PaletteWelcomeBubble> welcome_bubble_;
  128. std::unique_ptr<TrayBubbleWrapper> bubble_;
  129. // A Shell pre-target handler that notifies PaletteTray of stylus events.
  130. std::unique_ptr<ui::EventHandler> stylus_event_handler_;
  131. PrefService* local_state_ = nullptr; // Not owned.
  132. PrefService* active_user_pref_service_ = nullptr; // Not owned.
  133. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_local_;
  134. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_user_;
  135. // Weak pointer, will be parented by TrayContainer for its lifetime.
  136. views::ImageView* icon_;
  137. // Cached palette pref value.
  138. bool is_palette_enabled_ = true;
  139. // True when the palette tray should not be visible, regardless of palette
  140. // pref values.
  141. bool is_palette_visibility_paused_ = false;
  142. // Whether the palette should behave as though its display has a stylus.
  143. bool display_has_stylus_for_testing_ = false;
  144. // Used to indicate whether the palette bubble is automatically opened by a
  145. // stylus eject event.
  146. bool is_bubble_auto_opened_ = false;
  147. // Number of actions in pen palette bubble.
  148. int num_actions_in_bubble_ = 0;
  149. base::ScopedObservation<ProjectorSession, ProjectorSessionObserver>
  150. projector_session_observation_{this};
  151. ScopedSessionObserver scoped_session_observer_;
  152. base::WeakPtrFactory<PaletteTray> weak_factory_{this};
  153. };
  154. } // namespace ash
  155. #endif // ASH_SYSTEM_PALETTE_PALETTE_TRAY_H_