From f7d1f46c1f68d17b90e0dd21a8613ea9e2f20190 Mon Sep 17 00:00:00 2001 From: pierre Date: Wed, 27 Jan 2021 16:32:05 +0100 Subject: [PATCH] app: make cursor support more robust on Windows Post a dedicated message upon Window.SetCursor calls. Make sure that the cursor is only changed if the cursor is in the window. Signed-off-by: pierre --- app/internal/window/os_windows.go | 22 +++++++++++++++++++--- app/internal/windows/windows.go | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/internal/window/os_windows.go b/app/internal/window/os_windows.go index ae29073f..4014d5f1 100644 --- a/app/internal/window/os_windows.go +++ b/app/internal/window/os_windows.go @@ -46,7 +46,11 @@ type window struct { height int stage system.Stage pointerBtns pointer.Buttons - cursor syscall.Handle + + // cursorIn tracks whether the cursor was inside the window according + // to the most recent WM_SETCURSOR. + cursorIn bool + cursor syscall.Handle mu sync.Mutex animating bool @@ -56,7 +60,10 @@ type window struct { opts *Options } -const _WM_REDRAW = windows.WM_USER + 0 +const ( + _WM_REDRAW = windows.WM_USER + iota + _WM_CURSOR +) type gpuAPI struct { priority int @@ -309,7 +316,13 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr } } case windows.WM_SETCURSOR: - windows.SetCursor(w.cursor) + w.cursorIn = (lParam & 0xffff) == windows.HTCLIENT + fallthrough + case _WM_CURSOR: + if w.cursorIn { + windows.SetCursor(w.cursor) + return 1 + } } return windows.DefWindowProc(hwnd, msg, wParam, lParam) @@ -571,6 +584,9 @@ func (w *window) SetCursor(name pointer.CursorName) { c = resources.cursor } w.cursor = c + if err := windows.PostMessage(w.hwnd, _WM_CURSOR, 0, 0); err != nil { + panic(err) + } } func loadCursor(name pointer.CursorName) (syscall.Handle, error) { diff --git a/app/internal/windows/windows.go b/app/internal/windows/windows.go index d35f05a9..54e9e387 100644 --- a/app/internal/windows/windows.go +++ b/app/internal/windows/windows.go @@ -61,6 +61,8 @@ const ( CW_USEDEFAULT = -2147483648 + HTCLIENT = 1 + IDC_ARROW = 32512 IDC_IBEAM = 32513 IDC_HAND = 32649