plat/win_api.rs (Windows)#
Source: lib/plat/win_api.rs
win_api -- the window-indexed platform API on Windows: windows (records), input, the event
wait, presentation, cursors [src/plat/win/win_window.s]
(popups: win_popup.rs; clipboard, IME, drops: win_clip.rs; file dialog: win_filedlg.rs;
pens: win_pen.rs; fingers: win_touch.rs; shell, theme, home, drives, spawn_self: win_shell.rs)
win_create(title, w_dip, h_dip, flags, owner, x, y) -> index / -1
owner PW_NOOWNER: a top-level window (WS_OVERLAPPEDWINDOW, preferably index 0); else
a window owned by window `owner`: WS_POPUP | WS_CAPTION | WS_SYSMENU (+ WS_THICKFRAME
with PWF_RESIZABLE), WS_EX_TOOLWINDOW for PWF_TOOL (no taskbar button, not in
Alt+Tab), WS_EX_NOACTIVATE for PWF_NOACTIVATE. Created hidden; x, y = client origin
(PW_DEFAULT: system default / owner client origin + 32 px). Without PWF_SYSFRAME the
caption is removed (the library draws the frame; DWM keeps shadow and corners).
win_destroy(w) win_show(w, PWS_HIDE / SHOW / SHOWNA)
win_activate(w) win_enable(w, on)
win_set_rect(w, x, y, cw, ch) client origin (screen px) and size; PW_KEEP
win_get_rect(w) -> [x, y, w, h] client origin + client size (None: no window)
win_work_area(w) -> [x, y, w, h] work area of the window's monitor
win_set_title(w, utf8) win_state(w) -> WST_* (WST_ACTIVE: the app is active)
win_cmd(w, WCMD_*) win_begin_move(w) win_begin_resize(w, edges)
(L7) WCMD_FULLSCREEN: without caption and border over the
window's monitor (WS_POPUP; the placement and the styles
are kept), WCMD_WINDOWED (or MAXIMIZE / RESTORE): back as
it was; win_state says WST_FULLSCREEN meanwhile
win_size(w) -> (w, h) win_dpi(w) -> 96 = 100 %
win_set_min_size(w, w_dip, h_dip) (also before the window exists)
win_present(w, frame, rects) rects x, y, w, h; None = all. The window keeps a copy
of the presented pixels (a DIB section the rectangles
are copied into and BitBlt from): WM_PAINT repaints from
it.
win_native(w) -> NativeWindow (gpu) HWND + HINSTANCE for a GPU surface (lib/gpu); such
a window is never given to win_present: without a kept
copy WM_PAINT paints nothing and the swapchain's images
stay on screen. The UI's windows draw on both: their
swapchain lives on a child (win_gpuout.rs)
win_set_cursor(w, shape) CUR_* or a custom cursor CUR_USER0..CUR_TABLE-1
cursor_create(px, w, h, hot_x, hot_y, dpi) -> CUR_USER0 + k / -1 a cursor from
premultiplied ARGB pixels (rows of w * 4 bytes,
1..CUR_MAX_PX; CreateIconIndirect)
cursor_destroy(id) cursor_dpi(dpi) -> the dpi to draw cursors for (the
window's own on Windows)
win_hit_hook(f) f(w, x, y) -> HB_* flags of the UI at a client point
(WM_NCHITTEST, WM_MOUSEACTIVATE)
win_move_hook(f) f(w, &mut [x, y, w, h]) may snap the client rectangle
(screen px) the user drags window w to (WM_MOVING)
wait(deadline_us) -> queued events 0 = poll, u64::MAX = forever, else plat::time_us()
set_refresh(f) f() redraws during the modal move / size loop
win_test_send(w, msg, wp, lp) (test aid) SendMessageW to window w (input, hit test,
activation, size-move messages: no pointers); more test
aids in win_test.rs (win_test, file_dialog_autoclose)
(the single-window window_create / present / ... are in pwin0.rs)
Library frame (no PWF_SYSFRAME, AF_CUSTOM_TITLEBAR for the main window): the caption is
removed (WM_NCCALCSIZE) while the resize borders, shadow and rounded corners stay;
WM_NCHITTEST asks the UI hit boxes (hit hook): HB_DRAG moves the window, HB_MAX gives snap
layouts (main window), the caption buttons are drawn by the UI.
Every event carries its window (Event::win). The window procedure never acts on input: it
only records events into the ring (evq.rs); it re-enters (SendMessage, SetWindowPos), so
the window of the events it pushes is saved and restored.
The owner keeps looking active while one of its own windows is active: WM_NCACTIVATE (FALSE)
is drawn active unless the whole app loses the activation (WM_ACTIVATEAPP).
Tool windows are not activated by clicks (WM_MOUSEACTIVATE -> MA_NOACTIVATE) unless the
click hits a text box: the keyboard stays with the main window.
State: everything lives on the UI thread (the thread that creates the windows runs their
window procedure); the records are Cells, so the re-entrant window procedure and the
callbacks (refresh, hit hook) never hold a reference across a Win32 call.
struct NativeWindow(gpu) the native handles of a platform window for a GPU surface (VK_KHR_win32_surface)hwnd: isizehinstance: isize
fn win_native(w: i32) -> Option<NativeWindow>(gpu) HWND and HINSTANCE of platform window w (None: no such window). Its pixels then come from the GPU: do not call win_present for it -- until a frame is presented WM_PAINT has no kept copy to paint (WM_ERASEBKGND erases nothing, the class has no brush).fn win_create(title: &str, w_dip: i32, h_dip: i32, flags: u32, owner: i32, x: i32, y: i32) -> i32a hidden window (see the file header) -> index / -1 [plat_win_create]fn key_char_test(hkl: isize, vk: u32, mods: u8) -> (u32, bool)(test aid, plattest --test keychar) the character layout hkl (one of key_layouts_test) gives key vk with the modifiers MOD_SHIFT, MOD_CTRL, MOD_ALT, MOD_ALTGR (the right Alt: Ctrl + Alt as Windows makes it), MOD_CAPS -> (character, dead key), as a key event of that layout says it; the thread's keyboard state is not touchedfn key_layouts_test() -> Vec<isize>(test aid) the keyboard layouts loaded for the user (GetKeyboardLayoutList), HKLs whose low word is the language (0x0407 German, 0x0409 English (US)) and high word the layoutfn win_hit_hook(f: Option<fn(i32, i32, i32) -> u32>)f(w, x, y) -> HB_* flags at a client point (None = no hit tests)fn win_move_hook(f: Option<fn(i32, &mut [i32; 4])>)f(w, &mut [x, y, w, h]) -> the client rectangle (screen px) the user drags window w to, as the UI wants it (snapped); called inside the modal move loop (None = no hook)fn set_refresh(f: Option<fn()>)f() redraws during the modal move / size loopfn wait_refresh(deadline_us: u64, refresh: &mut dyn FnMut()) -> u32wait() with a redraw closure for the modal move / size loop: refresh() runs (instead of the set_refresh hook) whenever the window must redraw before the loop returns [main agent]fn wait(deadline_us: u64) -> u32pump the OS until an event is queued or the deadline (plat::time_us; 0 = poll without blocking, u64::MAX = forever) passes -> events queued (wake() makes it return at once). (G7a) While pads are watched, XInput is polled when its time comes (pad.rs pad_wait)fn win_present(w: i32, f: &Frame, rects: Option<&[[i32; 4]]>)show the frame's dirty rectangles (x, y, w, h; None = the whole frame); the window keeps a copy of the pixels (WM_PAINT repaints from it; jfgui_WINPRESENT=dibits: a plain buffer and SetDIBitsToDevice instead of the DIB section) [plat_win_present]fn win_destroy(w: i32)close the window (w = 0: ends the message loop) [plat_win_destroy]fn win_show(w: i32, mode: u32)PWS_HIDE / PWS_SHOW (activates) / PWS_SHOWNAfn win_activate(w: i32)make the window the active one (keyboard focus)fn win_enable(w: i32, on: bool)a disabled window takes no input (modal dialogs disable the windows below them)fn win_set_rect(w: i32, x: i32, y: i32, cw: i32, ch: i32)client origin (screen px) and client size; PW_KEEP keeps a fieldfn win_get_rect(w: i32) -> Option<[i32; 4]>client origin (screen px) and client size (minimized: where it was); None: no windowfn win_work_area(w: i32) -> Option<[i32; 4]>work area (without the taskbar) of the monitor the window is on: x, y, w, h (screen px)fn win_set_title(w: i32, title: &str)fn win_state(w: i32) -> u32WST_* bits (WST_ACTIVE: one of the app's windows is active)fn win_cmd(w: i32, cmd: u32)WCMD_MINIMIZE / MAXIMIZE / RESTORE / CLOSE (asks, like the system menu does) / FULLSCREEN / WINDOWEDfn win_begin_move(w: i32)let the system move the window (call on a button press)fn win_begin_resize(w: i32, edges: u32)let the system resize the window from an edge (EDGE_* bits)fn win_size(w: i32) -> (i32, i32)client width, height in px ((0, 0) without a window)fn win_dpi(w: i32) -> i3296 = 100 %fn win_set_min_size(w: i32, w_dip: i32, h_dip: i32)minimum client size (also before the window exists)fn win_test_send(w: i32, msg: u32, wp: usize, lp: isize) -> isize(test aid) SendMessageW to window w -> its result (0 without the window or for a message that could carry a pointer)fn win_set_cursor(w: i32, shape: u32)CUR_* shape while the pointer is over window wfn cursor_probe(shape: u32, dpi: i32) -> (u64, i32)(tests) the cursor Windows shows for shape in a window of this dpi: its HCURSOR and, for a drawn one, its width in px (0: a system cursor or cursor_fallback's)fn cursor_create(px: &[u8], w: i32, h: i32, hot_x: i32, hot_y: i32, _dpi: i32) -> i32a cursor from w x h premultiplied 0xAARRGGBB pixels (rows of w * 4 bytes), hot spot in px: a 32-bit top-down DIB section with straight alpha + an empty AND mask -> CreateIconIndirect; Windows shows it at its size (dpi is not needed here) -> CUR_USER0 + k / -1fn cursor_destroy(id: i32)free a custom cursor; windows showing it fall back to the arrow (their shape is forgotten, so the next win_set_cursor applies even with the same id)fn cursor_dpi(dpi: i32) -> i32the dpi to draw a cursor for a window of this dpi (Windows: the window's own)