0

Revert "Add Cursor Visibility Management for Mouse-less Systems on Windows"

This reverts commit b4374976a5.

Reason for revert: Implicated in crbug.com/380703583

Original change's description:
> Add Cursor Visibility Management for Mouse-less Systems on Windows
>
> Currently the cursor is always considered visible by default at startup
> and manage the visibility based on related events. However, if the
> cursor is actually invisible at Chomium startup, Chromium will
> unexpectedly render mouse effects (e.g. hover). This might happens on
> systems where no mouse is installed or cursor is suppressed by the OS.
>
> As Windows provides some capabilities to read/monitor the cursor
> status, this CL:
>
> - Uses GetCursorInfo() to initialize the visibility at startup and
>   monitor the change via an event hook.
> - Ignores the ShowCursor/HideCursor requests if cursor is hidden by the
>   OS.
>
> Bug: 376174073
> Change-Id: Ic1be96ec77c834fb0cee064ea87c4f9340461b26
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5573002
> Reviewed-by: Robert Liao <robliao@chromium.org>
> Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
> Commit-Queue: Jiahe Zhang <jiahe.zhang@intel.com>
> Cr-Commit-Position: refs/heads/main@{#1384735}

Bug: 376174073,380703583
Change-Id: Id633ba9bf507371f4c9dfb187d3c7bd600e4d153
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6065956
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Owners-Override: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Robert Liao <robliao@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1391135}
This commit is contained in:
Robert Liao
2024-12-03 18:49:34 +00:00
committed by Chromium LUCI CQ
parent 8573308a26
commit afb3959f97
11 changed files with 2 additions and 145 deletions

@ -99,9 +99,6 @@ class AURA_EXPORT CursorClient {
// Returns the OS cursor size in DIP.
virtual gfx::Size GetSystemCursorSize() const = 0;
// Updates the system cursor visibility for testing purposes.
virtual void UpdateSystemCursorVisibilityForTest(bool visible) {}
protected:
virtual ~CursorClient() {}
};

@ -100,11 +100,4 @@ void DesktopNativeCursorManager::InitCursorSizeObserver(
NOTREACHED();
}
#if BUILDFLAG(IS_WIN)
void DesktopNativeCursorManager::InitSystemCursorVisibilityObserver(
wm::NativeCursorManagerDelegate* delegate) {
NOTREACHED();
}
#endif
} // namespace views

@ -45,11 +45,6 @@ class VIEWS_EXPORT DesktopNativeCursorManager : public wm::NativeCursorManager {
// Initialize the observer that will report system cursor size.
virtual void InitCursorSizeObserver(
wm::NativeCursorManagerDelegate* delegate);
#if BUILDFLAG(IS_WIN)
// Initialize the observer that will report system cursor visibility state.
virtual void InitSystemCursorVisibilityObserver(
wm::NativeCursorManagerDelegate* delegate);
#endif
private:
// Overridden from wm::NativeCursorManager:

@ -4,13 +4,8 @@
#include "ui/views/widget/desktop_aura/desktop_native_cursor_manager_win.h"
#include <windows.h>
#include <winuser.h>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/numerics/safe_conversions.h"
@ -23,51 +18,11 @@ namespace {
constexpr int kDefaultCursorSize = 32;
BASE_FEATURE(kUseCursorEventHook,
"UseCursorEventHook",
base::FEATURE_ENABLED_BY_DEFAULT);
HWINEVENTHOOK g_cursor_event_hook = nullptr;
raw_ptr<DesktopNativeCursorManagerWin> g_instance = nullptr;
raw_ptr<wm::NativeCursorManagerDelegate> g_delegate = nullptr;
bool IsSystemCursorVisible() {
CURSORINFO cursor_info;
cursor_info.cbSize = sizeof(cursor_info);
if (!GetCursorInfo(&cursor_info)) {
PLOG(ERROR) << "Unable to get cursor info. Error = " << GetLastError();
return false;
}
return cursor_info.flags == CURSOR_SHOWING;
}
void CALLBACK CursorEventProc(HWINEVENTHOOK hook,
DWORD event,
HWND hwnd,
LONG id_object,
LONG id_child,
DWORD id_event_thread,
DWORD time) {
if (hwnd == nullptr && id_object == OBJID_CURSOR &&
id_child == CHILDID_SELF) {
g_instance->OnSystemCursorVisibilityChanged(IsSystemCursorVisible());
}
}
} // namespace
DesktopNativeCursorManagerWin::DesktopNativeCursorManagerWin() = default;
DesktopNativeCursorManagerWin::~DesktopNativeCursorManagerWin() {
if (g_cursor_event_hook) {
UnhookWinEvent(g_cursor_event_hook);
g_cursor_event_hook = nullptr;
}
g_instance = nullptr;
g_delegate = nullptr;
}
DesktopNativeCursorManagerWin::~DesktopNativeCursorManagerWin() = default;
void DesktopNativeCursorManagerWin::SetSystemCursorSize(
wm::NativeCursorManagerDelegate* delegate) {
@ -112,38 +67,4 @@ void DesktopNativeCursorManagerWin::InitCursorSizeObserver(
SetSystemCursorSize(delegate);
}
void DesktopNativeCursorManagerWin::InitSystemCursorVisibilityObserver(
wm::NativeCursorManagerDelegate* delegate) {
if (!base::FeatureList::IsEnabled(kUseCursorEventHook)) {
return;
}
DCHECK(!g_instance);
g_instance = this;
DCHECK(!g_delegate);
g_delegate = delegate;
// Register for cursor show/hide events. WINEVENT_SKIPOWNPROCESS is set to
// skip the events generated by Chromium itself.
DCHECK(!g_cursor_event_hook);
g_cursor_event_hook = SetWinEventHook(
EVENT_OBJECT_SHOW, EVENT_OBJECT_HIDE, nullptr, CursorEventProc, 0, 0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
// Report the initial cursor visibility.
OnSystemCursorVisibilityChanged(IsSystemCursorVisible());
}
void DesktopNativeCursorManagerWin::OnSystemCursorVisibilityChanged(
bool visible) {
if (system_cursor_visible_ == visible) {
return;
}
system_cursor_visible_ = visible;
DCHECK(g_delegate);
g_delegate->CommitSystemCursorVisibility(visible);
}
} // namespace views

@ -32,9 +32,6 @@ class VIEWS_EXPORT DesktopNativeCursorManagerWin
void InitCursorSizeObserver(
wm::NativeCursorManagerDelegate* delegate) override;
void InitSystemCursorVisibilityObserver(
wm::NativeCursorManagerDelegate* delegate) override;
void OnSystemCursorVisibilityChanged(bool visible);
private:
// Retrieve and report the cursor size to cursor manager.
@ -43,9 +40,6 @@ class VIEWS_EXPORT DesktopNativeCursorManagerWin
base::win::RegKey hkcu_cursor_regkey_;
gfx::Size system_cursor_size_;
// Whether the cursor is visible from GetCursorInfo().
bool system_cursor_visible_ = true;
};
} // namespace views

@ -608,10 +608,6 @@ void DesktopNativeWidgetAura::InitNativeWidget(Widget::InitParams params) {
if (features::IsSystemCursorSizeSupported()) {
native_cursor_manager_->InitCursorSizeObserver(cursor_manager_);
}
#if BUILDFLAG(IS_WIN)
native_cursor_manager_->InitSystemCursorVisibilityObserver(
cursor_manager_);
#endif
}
aura::client::SetCursorClient(host_->window(), cursor_manager_);
}

@ -163,12 +163,6 @@ TEST_F(DesktopNativeWidgetAuraTest, MAYBE_GlobalCursorState) {
aura::client::CursorClient* cursor_client_b = aura::client::GetCursorClient(
widget_b.GetNativeView()->GetHost()->window());
// The cursor might be considered invisible after initialization on some
// machines (e.g. mouse-less) as |CursorClient|s read the cursor visibility
// from OS info. So force the cursor to be visible here.
cursor_client_a->UpdateSystemCursorVisibilityForTest(true);
cursor_client_b->UpdateSystemCursorVisibilityForTest(true);
// Verify the cursor can be locked using one client and unlocked using
// another.
EXPECT_FALSE(cursor_client_a->IsCursorLocked());

@ -270,28 +270,6 @@ gfx::Size CursorManager::GetSystemCursorSize() const {
return current_state_->system_cursor_size();
}
void CursorManager::UpdateSystemCursorVisibilityForTest(bool visible) {
CommitSystemCursorVisibility(visible);
}
void CursorManager::CommitSystemCursorVisibility(bool visible) {
if (visible == current_state_->visible()) {
return;
}
// Use lock to prevent ShowCursor/HideCursor when system cursor is invisible.
if (!visible) {
LockCursor();
} else {
UnlockCursor();
}
current_state_->SetVisible(visible);
observers_.Notify(
&aura::client::CursorClientObserver::OnCursorVisibilityChanged,
GetCursor().type() == ui::mojom::CursorType::kNone ? false : visible);
}
void CursorManager::CommitSystemCursorSize(
const gfx::Size& system_cursor_size) {
current_state_->set_system_cursor_size(system_cursor_size);

@ -69,7 +69,6 @@ class COMPONENT_EXPORT(UI_WM) CursorManager
bool ShouldHideCursorOnKeyEvent(const ui::KeyEvent& event) const override;
bool ShouldHideCursorOnTouchEvent(const ui::TouchEvent& event) const override;
gfx::Size GetSystemCursorSize() const override;
void UpdateSystemCursorVisibilityForTest(bool visible) override;
private:
// Overridden from NativeCursorManagerDelegate:
@ -78,7 +77,6 @@ class COMPONENT_EXPORT(UI_WM) CursorManager
void CommitCursorSize(ui::CursorSize cursor_size) override;
void CommitMouseEventsEnabled(bool enabled) override;
void CommitSystemCursorSize(const gfx::Size& cursor_size) override;
void CommitSystemCursorVisibility(bool visible) override;
void SetCursorImpl(gfx::NativeCursor cursor, bool forced);

@ -356,15 +356,7 @@ TEST_F(CursorManagerTest, TestCursorClientObserver) {
EXPECT_FALSE(observer_a.is_cursor_visible());
}
TEST_F(CursorManagerTest, SystemCursorVisibilityTest) {
// If the system cursor is invisible, ShowCursor() should not make the cursor
// visible.
cursor_manager_.UpdateSystemCursorVisibilityForTest(false);
cursor_manager_.ShowCursor();
EXPECT_FALSE(cursor_manager_.IsCursorVisible());
}
// This test validates that the cursor visibility state is restored when a
// This test validates that the cursor visiblity state is restored when a
// CursorManager instance is destroyed and recreated.
TEST(CursorManagerCreateDestroyTest, VisibilityTest) {
// This block ensures that the cursor is hidden when the CursorManager

@ -34,7 +34,6 @@ class COMPONENT_EXPORT(UI_WM) NativeCursorManagerDelegate {
virtual void CommitCursorSize(ui::CursorSize cursor_size) = 0;
virtual void CommitMouseEventsEnabled(bool enabled) = 0;
virtual void CommitSystemCursorSize(const gfx::Size& cursor_size) = 0;
virtual void CommitSystemCursorVisibility(bool visible) = 0;
};
} // namespace wm