app_window 0.3.5

Cross-platform window library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// SPDX-License-Identifier: MPL-2.0
//! Implements Win32 windows, message dispatch, surfaces, and alerts.

use crate::coordinates::{Position, Size};
use raw_window_handle::{
    RawDisplayHandle, RawWindowHandle, Win32WindowHandle, WindowsDisplayHandle,
};
use send_cells::send_cell::SendCell;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::fmt::Display;
use std::num::NonZero;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use std::sync::{LazyLock, Mutex};
use windows::Win32::Foundation::{
    ERROR_CLASS_ALREADY_EXISTS, GetLastError, HINSTANCE, HWND, LPARAM, LRESULT, RECT, WPARAM,
};
use windows::Win32::Graphics::Gdi::HBRUSH;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::HiDpi::GetDpiForWindow;
use windows::Win32::UI::WindowsAndMessaging::{
    CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetClientRect, GetMessageW,
    GetSystemMetrics, HWND_MESSAGE, IDC_ARROW, LoadCursorW, MSG, PostMessageW, PostQuitMessage,
    RegisterClassExW, SM_CXSCREEN, SM_CYSCREEN, SW_SHOWNORMAL, ShowWindow, TranslateMessage,
    WINDOW_EX_STYLE, WINDOW_STYLE, WM_DESTROY, WM_SIZE, WM_USER, WNDCLASSEXW, WS_OVERLAPPEDWINDOW,
    WS_POPUP,
};
use windows::core::{HSTRING, PCWSTR, w};

const WM_RUN_FUNCTION: u32 = WM_USER;

#[derive(Debug)]
pub struct FullscreenError;

impl Display for FullscreenError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "FullscreenError")
    }
}
impl std::error::Error for FullscreenError {}

fn main_thread_id() -> u32 {
    static mut MAIN_THREAD_ID: u32 = 0;
    #[used]
    #[allow(non_upper_case_globals)]
    #[unsafe(link_section = ".CRT$XCU")]
    static INIT_MAIN_THREAD_ID: unsafe fn() = {
        unsafe fn initer() {
            unsafe { MAIN_THREAD_ID = windows::Win32::System::Threading::GetCurrentThreadId() };
        }
        initer
    };

    unsafe { MAIN_THREAD_ID }
}

pub fn is_main_thread() -> bool {
    //windows does not have a clear concept of a main thread but allows any thread to be in charge
    //of a window.  However for compatibility we project a 'main thread-like' concept onto windows
    let current_id = unsafe { windows::Win32::System::Threading::GetCurrentThreadId() };
    current_id == main_thread_id()
}

struct WinClosure(Box<dyn FnOnce() + Send + 'static>);

// Window messages can be posted by code outside this process. Never put a Rust
// pointer in one: the receiver has no way to prove that an integer supplied in
// WPARAM points to a live allocation of the expected type. Keep ownership in a
// process-local table and send only a one-shot lookup token through Win32.
static NEXT_CLOSURE_ID: AtomicUsize = AtomicUsize::new(1);
static PENDING_CLOSURES: LazyLock<Mutex<HashMap<usize, WinClosure>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

fn store_closure(closure: WinClosure) -> usize {
    let mut closure = Some(closure);
    loop {
        let id = NEXT_CLOSURE_ID.fetch_add(1, Ordering::Relaxed);
        if id == 0 {
            // Zero is easy to generate accidentally and is also what Win32
            // callers conventionally use when a parameter is absent.
            continue;
        }

        let mut pending = PENDING_CLOSURES.lock().unwrap();
        if let std::collections::hash_map::Entry::Vacant(entry) = pending.entry(id) {
            entry.insert(closure.take().unwrap());
            return id;
        }
        // Atomic wraparound can collide with work that is still pending. Try
        // the next id rather than replacing (and dropping) live work.
    }
}

fn take_closure(id: usize) -> Option<WinClosure> {
    PENDING_CLOSURES.lock().unwrap().remove(&id)
}

#[derive(Default)]
struct HwndImp {
    size_notify: Option<Box<dyn Fn(Size)>>,
}
thread_local! {
    static HWND_IMPS: RefCell<HashMap<*mut c_void /* hwnd */, HwndImp>> = RefCell::new(HashMap::new());
}

//The message-only window that receives WM_RUN_FUNCTION.  We post closures to a window
//rather than via PostThreadMessageW because thread messages are silently discarded by
//modal message loops (window move/size, menus, dialogs), which would leak the closure
//and hang the awaiting future.
static DISPATCH_HWND: AtomicPtr<c_void> = AtomicPtr::new(std::ptr::null_mut());

extern "system" fn dispatch_window_proc(
    hwnd: HWND,
    msg: u32,
    w_param: WPARAM,
    l_param: LPARAM,
) -> LRESULT {
    match msg {
        WM_RUN_FUNCTION => {
            // Unknown ids include malformed, forged, duplicated, and replayed
            // messages. In every case the safe response is to ignore them.
            if let Some(winclosure) = take_closure(w_param.0) {
                winclosure.0();
            }
            LRESULT(0)
        }
        _ => unsafe { DefWindowProcW(hwnd, msg, w_param, l_param) },
    }
}

pub fn run_main_thread<F: FnOnce() + Send + 'static>(closure: F) {
    //create the message-only dispatch window; this also creates the message queue
    let instance = unsafe { GetModuleHandleW(PCWSTR::null()) }.expect("Can't get module");
    let class_name = w!("app_window_main_dispatch");
    let window_class = WNDCLASSEXW {
        cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
        style: Default::default(),
        lpfnWndProc: Some(dispatch_window_proc),
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: instance.into(),
        hIcon: Default::default(),
        hCursor: Default::default(),
        hbrBackground: HBRUSH::default(),
        lpszMenuName: PCWSTR::null(),
        lpszClassName: class_name,
        hIconSm: Default::default(),
    };
    let r = unsafe { RegisterClassExW(&window_class) };
    assert_ne!(r, 0, "failed to register dispatch class: {:?}", unsafe {
        GetLastError()
    });
    let dispatch_window = unsafe {
        CreateWindowExW(
            WINDOW_EX_STYLE(0),
            class_name,
            PCWSTR::null(),
            WINDOW_STYLE(0),
            0,
            0,
            0,
            0,
            Some(HWND_MESSAGE), //message-only window
            None,
            None,
            None,
        )
    }
    .expect("failed to create dispatch window");
    DISPATCH_HWND.store(dispatch_window.0, Ordering::Release);

    //Run the closure on a secondary thread, like other platforms.  Running it inline
    //would deadlock any closure that blocks on a future needing the main-thread
    //message loop (e.g. the documented `block_on(Window::default())` pattern),
    //since the loop below wouldn't be running yet.
    std::thread::spawn(closure);
    let mut message = MSG::default();
    loop {
        let message_ret = unsafe { GetMessageW(&mut message, None, 0, 0) };
        if message_ret.0 == 0 {
            break;
        } else if message_ret.0 == -1 {
            panic!("GetMessageW failed");
        }
        unsafe {
            //ms code seems to ignore this return value in practice
            //see https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage
            _ = TranslateMessage(&message);
            DispatchMessageW(&message);
        }
    }
    crate::application::IS_MAIN_THREAD_RUNNING.store(false, Ordering::Release);
}

pub fn on_main_thread<F: FnOnce() + Send + 'static>(closure: F) {
    let closure_id = store_closure(WinClosure(Box::new(closure)));
    //The dispatch window is created slightly after IS_MAIN_THREAD_RUNNING is set;
    //briefly wait for it rather than failing.
    let hwnd = loop {
        let ptr = DISPATCH_HWND.load(Ordering::Acquire);
        if !ptr.is_null() {
            break HWND(ptr);
        }
        std::thread::yield_now();
    };
    if let Err(error) =
        unsafe { PostMessageW(Some(hwnd), WM_RUN_FUNCTION, WPARAM(closure_id), LPARAM(0)) }
    {
        // A failed post did not transfer ownership to the window procedure.
        drop(take_closure(closure_id));
        panic!("PostMessageW failed: {error}");
    }
}

#[cfg(test)]
mod dispatch_tests {
    use super::{WinClosure, store_closure, take_closure};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};

    #[test]
    fn dispatch_tokens_are_one_shot_and_unknown_values_are_ignored() {
        assert!(take_closure(usize::MAX).is_none());

        let ran = Arc::new(AtomicBool::new(false));
        let ran_in_closure = ran.clone();
        let id = store_closure(WinClosure(Box::new(move || {
            ran_in_closure.store(true, Ordering::Relaxed);
        })));

        let closure = take_closure(id).expect("stored closure must be available");
        assert!(take_closure(id).is_none(), "a token cannot be replayed");
        closure.0();
        assert!(ran.load(Ordering::Relaxed));
    }
}

pub fn stop_main_thread() {
    // PostQuitMessage targets the calling thread. Route it through the dispatch
    // window so callers on worker threads stop the actual UI message loop.
    on_main_thread(|| unsafe { PostQuitMessage(0) });
}

pub async fn alert(message: String) {
    todo!("alert not yet implemented for Windows: {}", message)
}

#[derive(Debug)]
pub struct Window {
    hwnd: SendCell<HWND>,
}

unsafe impl Send for Window {}
unsafe impl Sync for Window {}

extern "system" fn window_proc(hwnd: HWND, msg: u32, w_param: WPARAM, l_param: LPARAM) -> LRESULT {
    logwise::log!("got Windows message {msg}", msg = msg);
    if crate::input::window_proc(hwnd, msg, w_param, l_param) == LRESULT(0) {
        return LRESULT(0);
    }

    match msg {
        m if m == WM_SIZE => {
            let width = (l_param.0 as u32 & 0xFFFF) as i32; // LOWORD(lParam)
            let height = ((l_param.0 as u32 >> 16) & 0xFFFF) as i32; // HIWORD(lParam)
            let size = Size::new(width as f64, height as f64);
            //take the callback out of the map before calling it: window procs are
            //re-entrant on the same thread, and a callback that sends a message
            //would otherwise hit a nested RefCell borrow (panic → abort).
            let notify =
                HWND_IMPS.with_borrow_mut(|c| c.entry(hwnd.0).or_default().size_notify.take());
            if let Some(f) = notify {
                f(size);
                HWND_IMPS.with_borrow_mut(|c| {
                    let entry = c.entry(hwnd.0).or_default();
                    //don't clobber a callback registered during the call
                    if entry.size_notify.is_none() {
                        entry.size_notify = Some(f);
                    }
                });
            }
            LRESULT(0)
        }
        m if m == WM_DESTROY => {
            //drop any registered callback so the entry can't fire for a recycled HWND
            HWND_IMPS.with_borrow_mut(|c| {
                c.remove(&hwnd.0);
            });
            unsafe { DefWindowProcW(hwnd, msg, w_param, l_param) }
        }
        _ => unsafe { DefWindowProcW(hwnd, msg, w_param, l_param) },
    }
}
fn create_window_impl(position: Position, size: Size, title: String, style: WINDOW_STYLE) -> HWND {
    let instance = unsafe { GetModuleHandleW(PCWSTR::null()) }.expect("Can't get module");
    let cursor =
        unsafe { LoadCursorW(Some(HINSTANCE::default()), IDC_ARROW) }.expect("Can't load cursor");
    let winstr: HSTRING = title.into();
    let class_name = w!("app_window_window");
    let window_class = WNDCLASSEXW {
        cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
        style: Default::default(),
        lpfnWndProc: Some(window_proc),
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: instance.into(),
        hIcon: Default::default(),
        hCursor: cursor,
        hbrBackground: HBRUSH::default(),
        lpszMenuName: PCWSTR::null(),
        lpszClassName: class_name,
        hIconSm: Default::default(),
    };
    let r = unsafe { RegisterClassExW(&window_class) };
    if r == 0 {
        //the class persists for the process lifetime, so a second window
        //re-registers it; that's fine
        let err = unsafe { GetLastError() };
        assert_eq!(
            err, ERROR_CLASS_ALREADY_EXISTS,
            "failed to register window class: {:?}",
            err
        );
    }

    let window = unsafe {
        CreateWindowExW(
            WINDOW_EX_STYLE(0), //style
            class_name,
            &winstr,
            style,
            position.x() as i32,
            position.y() as i32, //position
            size.width() as i32,
            size.height() as i32, //size
            None,                 //parent
            None,                 //menu
            None,                 //instance
            None,
        )
    }
    .expect("failed to create window");
    unsafe { _ = ShowWindow(window, SW_SHOWNORMAL) };
    window
}

impl Window {
    pub async fn new(position: Position, size: Size, title: String) -> Self {
        let window = crate::application::on_main_thread("Window::new".into(), move || {
            let window = create_window_impl(position, size, title, WS_OVERLAPPEDWINDOW);
            SendCell::new(window)
        })
        .await;

        Window { hwnd: window }
    }

    pub async fn default() -> Self {
        Self::new(
            Position::new(0.0, 0.0),
            Size::new(800.0, 600.0),
            "app_window".to_string(),
        )
        .await
    }

    pub async fn fullscreen(title: String) -> Result<Self, FullscreenError> {
        let size = Size::new(unsafe { GetSystemMetrics(SM_CXSCREEN) as f64 }, unsafe {
            GetSystemMetrics(SM_CYSCREEN) as f64
        });
        let window = crate::application::on_main_thread("Window::fullscreen".into(), move || {
            let window = create_window_impl(Position::new(0.0, 0.0), size, title, WS_POPUP);
            SendCell::new(window)
        })
        .await;

        Ok(Window { hwnd: window })
    }

    pub async fn surface(&self) -> Surface {
        let copy_hwnd = self.hwnd.copying();
        Surface { imp: copy_hwnd }
    }
}

impl Drop for Window {
    fn drop(&mut self) {
        let unsafe_hwnd = unsafe { *self.hwnd.get_unchecked() };
        let unsafe_port_hwnd = send_cells::unsafe_send_cell::UnsafeSendCell::new(unsafe_hwnd);
        logwise::log!("Destroying window");
        on_main_thread(move || {
            unsafe { DestroyWindow(*unsafe_port_hwnd.get()) }.expect("Can't close window");
        });
    }
}

#[derive(Debug)]
pub struct Surface {
    imp: SendCell<HWND>,
}

unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}

impl Surface {
    fn size_imp(hwnd: HWND) -> (Size, f64) {
        let mut rect = RECT::default();
        unsafe { GetClientRect(hwnd, &mut rect).expect("Can't get size") }
        let s = Size::new(rect.right as f64, rect.bottom as f64);
        let dpi = unsafe { GetDpiForWindow(hwnd) };
        let scale = dpi as f64 / 96.0;
        (s, scale)
    }
    pub async fn size_scale(&self) -> (Size, f64) {
        let send_hwnd = self.imp.copying();
        crate::application::on_main_thread("Surface::size_scale".into(), move || {
            Self::size_imp(*send_hwnd.get())
        })
        .await
    }
    pub fn size_main(&self) -> (Size, f64) {
        assert!(
            crate::application::is_main_thread(),
            "Call from main thread only"
        );
        Self::size_imp(*self.imp.get())
    }

    pub fn raw_window_handle(&self) -> RawWindowHandle {
        //should be fine since we're just reading the value
        let unsafe_hwnd: HWND = unsafe { *self.imp.get_unchecked() };
        RawWindowHandle::Win32(Win32WindowHandle::new(
            NonZero::new(unsafe_hwnd.0 as isize).expect("HWND is null"),
        ))
    }

    pub fn raw_display_handle(&self) -> RawDisplayHandle {
        RawDisplayHandle::Windows(WindowsDisplayHandle::new())
    }

    pub fn size_update<F: Fn(Size) + Send + 'static>(&mut self, _update: F) {
        let move_hwnd = self.imp.copying();
        on_main_thread(move || {
            let hwnd = move_hwnd.get();
            HWND_IMPS.with_borrow_mut(|c| {
                let entry = c.entry(hwnd.0).or_default();
                entry.size_notify = Some(Box::new(_update));
            });
        });
    }
}

impl Drop for Surface {
    fn drop(&mut self) {}
}