kbm: free the cursor while the window is dragged or resized

While the XCL window has the focus its cursor is clipped to the client centre (the mouse plays the right stick). WM_MOVE re-clipped it during a drag: the cursor teleported back to the (moving) centre mid-drag, breaking the drag. Track WM_ENTERSIZEMOVE/WM_EXITSIZEMOVE: release the clip for the whole drag/resize loop, restore it after. kbm tests pass (7).
This commit is contained in:
ryosen0
2026-11-23 22:05:50 +03:00
parent 229708a71f
commit a6e52be347
+26
View File
@@ -187,6 +187,8 @@ const WM_SETCURSOR: u32 = 0x0020;
const WM_SIZE: u32 = 0x0005;
const WM_MOVE: u32 = 0x0003;
const WM_LBUTTONDOWN: u32 = 0x0201;
const WM_ENTERSIZEMOVE: u32 = 0x0231;
const WM_EXITSIZEMOVE: u32 = 0x0232;
const RID_INPUT: u32 = 0x1000_0003;
const RIM_TYPEMOUSE: u32 = 0;
const MOUSE_MOVE_ABSOLUTE: u16 = 1;
@@ -207,6 +209,9 @@ const SC_D: (u32, i32) = (0x20, 0x44);
static WINDOW: AtomicIsize = AtomicIsize::new(0);
/// the cursor is held in the window (it has the focus)
static CAPTURED: AtomicBool = AtomicBool::new(false);
/// the user is dragging or resizing the window (WM_ENTERSIZEMOVE … WM_EXITSIZEMOVE):
/// the cursor must stay free, or the 1-pixel clip teleports it back mid-drag
static SIZING: AtomicBool = AtomicBool::new(false);
/// mouse counts not yet taken by `apply`
static DX: AtomicI32 = AtomicI32::new(0);
static DY: AtomicI32 = AtomicI32::new(0);
@@ -261,6 +266,14 @@ pub fn attach(hwnd: *mut c_void) {
}
unsafe fn hold_cursor(hwnd: *mut c_void, on: bool) {
// a drag/resize loop owns the cursor: clipping it to the window's centre would teleport
// it back on every WM_MOVE of the drag
if on && SIZING.load(Ordering::Acquire) {
if CAPTURED.swap(false, Ordering::AcqRel) {
ClipCursor(std::ptr::null());
}
return;
}
if !on {
if CAPTURED.swap(false, Ordering::AcqRel) {
ClipCursor(std::ptr::null());
@@ -311,6 +324,19 @@ pub unsafe fn on_message(hwnd: *mut c_void, msg: u32, wp: usize, lp: isize) -> b
}
false
}
WM_ENTERSIZEMOVE => {
SIZING.store(true, Ordering::Release);
hold_cursor(hwnd, false);
reset_mouse_state();
false
}
WM_EXITSIZEMOVE => {
SIZING.store(false, Ordering::Release);
if GetForegroundWindow() == hwnd {
hold_cursor(hwnd, true);
}
false
}
WM_LBUTTONDOWN | WM_SIZE | WM_MOVE => {
if GetForegroundWindow() == hwnd {
hold_cursor(hwnd, true);