touch_ui_controller.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 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_BASE_POINTER_TOUCH_UI_CONTROLLER_H_
  5. #define UI_BASE_POINTER_TOUCH_UI_CONTROLLER_H_
  6. #include <memory>
  7. #include "base/callback_list.h"
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #if BUILDFLAG(IS_WIN)
  12. namespace gfx {
  13. class SingletonHwndObserver;
  14. }
  15. #endif
  16. namespace ui {
  17. // Central controller to handle touch UI modes.
  18. class COMPONENT_EXPORT(UI_BASE) TouchUiController {
  19. public:
  20. using CallbackList = base::RepeatingClosureList;
  21. enum class TouchUiState {
  22. kDisabled,
  23. kAuto,
  24. kEnabled,
  25. };
  26. class COMPONENT_EXPORT(UI_BASE) TouchUiScoperForTesting {
  27. public:
  28. explicit TouchUiScoperForTesting(bool enabled,
  29. TouchUiController* controller = Get());
  30. TouchUiScoperForTesting(const TouchUiScoperForTesting&) = delete;
  31. TouchUiScoperForTesting& operator=(const TouchUiScoperForTesting&) = delete;
  32. ~TouchUiScoperForTesting();
  33. // Update the current touch mode state but still roll back to the
  34. // original state at destruction. Allows a test to change the mode
  35. // multiple times without creating multiple instances.
  36. void UpdateState(bool enabled);
  37. private:
  38. const raw_ptr<TouchUiController> controller_;
  39. const TouchUiState old_state_;
  40. };
  41. static TouchUiController* Get();
  42. explicit TouchUiController(TouchUiState touch_ui_state = TouchUiState::kAuto);
  43. TouchUiController(const TouchUiController&) = delete;
  44. TouchUiController& operator=(const TouchUiController&) = delete;
  45. ~TouchUiController();
  46. bool touch_ui() const {
  47. return (touch_ui_state_ == TouchUiState::kEnabled) ||
  48. ((touch_ui_state_ == TouchUiState::kAuto) && tablet_mode_);
  49. }
  50. base::CallbackListSubscription RegisterCallback(
  51. const base::RepeatingClosure& closure);
  52. void OnTabletModeToggled(bool enabled);
  53. private:
  54. TouchUiState SetTouchUiState(TouchUiState touch_ui_state);
  55. void TouchUiChanged();
  56. bool tablet_mode_ = false;
  57. TouchUiState touch_ui_state_;
  58. #if BUILDFLAG(IS_WIN)
  59. std::unique_ptr<gfx::SingletonHwndObserver> singleton_hwnd_observer_;
  60. #endif
  61. CallbackList callback_list_;
  62. };
  63. } // namespace ui
  64. #endif // UI_BASE_POINTER_TOUCH_UI_CONTROLLER_H_