Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-window

Source of release 0.6.4

0.6.4 / src / window / windows / cursor.lucb

#==============================================================================================
#
#   cursor - The Win32 client-area cursor
#
#   DESCRIPTION:
#       WM_SETCURSOR owns cursor restoration inside HTCLIENT. Windows continues to select title-
#       bar and frame cursors. Shared system resources are not destroyed.
#
#==============================================================================================

extern func SetCursor(cursor: void*?) -> void*?
extern func GetCursorPos(point: WinPoint*) -> i32
extern func GetCapture() -> void*?
extern func WindowFromPoint(point: WinPoint) -> void*?

## The system cursor for `cursor`; none for `hidden`, which SetCursor shows as nothing.
func win_cursor(cursor: input.Cursor) -> void*?!:
    if cursor == input.Cursor.hidden:
        return none
    var resource: usize = 32512
    match cursor:
        .text: resource = 32513
        .pointer: resource = 32649
        .resize_horizontal: resource = 32644
        .resize_vertical: resource = 32645
        .grab: resource = 32646
        .grabbing: resource = 32646
        .not_allowed: resource = 32648
        .crosshair: resource = 32515
        _: _ = ()
    return LoadCursorW(none, (u16*)resource) else error(failed, "Windows could not load a system cursor")

func win_set_cursor(state: State*, cursor: input.Cursor) -> !:
    if (state.native_cursor != none or cursor == input.Cursor.hidden) and state.cursor == cursor: return
    if state.native_cursor == none or state.cursor != cursor:
        state.native_cursor = try win_cursor(cursor)
        state.cursor = cursor
    let native = required(state.native)
    var point: WinPoint
    if GetCapture() == native:
        _ = SetCursor(state.native_cursor)
    elif GetCursorPos(&point) != 0 and WindowFromPoint(point) == native:
        _ = ScreenToClient(native, &point)
        var bounds: WinRect
        if GetClientRect(native, &bounds) != 0 and point.x >= 0 and point.y >= 0 and point.x < bounds.right and point.y < bounds.bottom:
            _ = SetCursor(state.native_cursor)