#ifndef UI_BASE_POINTER_TOUCH_UI_CONTROLLER_H_
#define UI_BASE_POINTER_TOUCH_UI_CONTROLLER_H_
#include <memory>
#include "base/callback_list.h"
#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/blink_buildflags.h"
#include "build/build_config.h"
#if BUILDFLAG(USE_BLINK)
#include "ui/base/pointer/pointer_device.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/callback_list.h"
#endif
namespace ui {
class COMPONENT_EXPORT(UI_BASE) TouchUiController {
public:
using TouchModeCallbackList = base::RepeatingClosureList;
#if BUILDFLAG(IS_WIN)
using TabletModeCallbackList = base::RepeatingClosureList;
#endif
enum class TouchUiState {
kDisabled,
kAuto,
kEnabled,
};
enum class PostureMode {
kTablet = 0,
kDesktop = 1,
kMaxValue = kDesktop,
};
class COMPONENT_EXPORT(UI_BASE) TouchUiScoperForTesting {
public:
explicit TouchUiScoperForTesting(bool touch_ui_enabled,
bool tablet_mode_enabled = false,
TouchUiController* controller = Get());
TouchUiScoperForTesting(const TouchUiScoperForTesting&) = delete;
TouchUiScoperForTesting& operator=(const TouchUiScoperForTesting&) = delete;
~TouchUiScoperForTesting();
void UpdateState(bool enabled);
void UpdateTabletMode(bool enabled);
private:
const raw_ptr<TouchUiController> controller_;
const TouchUiState old_ui_state_;
const bool old_tablet_mode_;
};
static TouchUiController* Get();
explicit TouchUiController(TouchUiState touch_ui_state = TouchUiState::kAuto);
TouchUiController(const TouchUiController&) = delete;
TouchUiController& operator=(const TouchUiController&) = delete;
virtual ~TouchUiController();
bool touch_ui() const {
return (touch_ui_state_ == TouchUiState::kEnabled) ||
((touch_ui_state_ == TouchUiState::kAuto) && tablet_mode_);
}
#if BUILDFLAG(IS_WIN)
bool tablet_mode() const { return tablet_mode_; }
#endif
base::CallbackListSubscription RegisterCallback(
const base::RepeatingClosure& closure);
#if BUILDFLAG(IS_WIN)
base::CallbackListSubscription RegisterTabletModeCallback(
const base::RepeatingClosure& closure);
#endif
void OnTabletModeToggled(bool enabled);
#if BUILDFLAG(IS_WIN)
void RefreshTabletMode();
#endif
#if BUILDFLAG(USE_BLINK)
void OnPointerDeviceConnected(PointerDevice::Key key);
void OnPointerDeviceDisconnected(PointerDevice::Key key);
#endif
protected:
TouchUiState SetTouchUiState(TouchUiState touch_ui_state);
bool SetTabletMode(bool enable_tablet_mode);
#if BUILDFLAG(USE_BLINK)
virtual int MaxTouchPoints() const;
virtual std::optional<PointerDevice> GetPointerDevice(
PointerDevice::Key key) const;
virtual std::vector<PointerDevice> GetPointerDevices() const;
const std::vector<PointerDevice>& GetLastKnownPointerDevicesForTesting()
const;
#endif
private:
void TouchUiChanged();
#if BUILDFLAG(IS_WIN)
void TabletModeChanged();
void SetInitialTabletMode(bool enabled);
#endif
TouchUiState touch_ui_state_;
bool tablet_mode_ = false;
#if BUILDFLAG(USE_BLINK)
void OnInitializePointerDevices();
std::vector<PointerDevice> last_known_pointer_devices_;
#endif
#if BUILDFLAG(IS_WIN)
base::CallbackListSubscription hwnd_subscription_;
TabletModeCallbackList tablet_mode_callback_list_;
#endif
TouchModeCallbackList touch_mode_callback_list_;
base::WeakPtrFactory<TouchUiController> weak_factory_{this};
};
}
#endif