microsoft-webui-desktop 0.0.30

Configurable native desktop SDK for WebUI applications
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! The native window procedure, message pump, and custom-frame geometry.

use crate::{DesktopEvent, EventResponse};
use anyhow::Result;
use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Controller;
use windows::core::Error as WindowsError;
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, RECT, SIZE, WPARAM};
use windows::Win32::Graphics::Gdi;
use windows::Win32::UI::HiDpi;
use windows::Win32::UI::WindowsAndMessaging::{self, MSG};

use super::command::execute_window_command;
use super::event::{logical_dimension, physical_to_logical, size_event_transition};
use super::nonclient::{initialize_frame, non_client_calc_size, non_client_hit_test, redraw_frame};
use super::state::{save_window_state, set_window_state, with_window_state, FrameState};
use super::webview::mirror_event;
#[cfg(feature = "application-ipc")]
use super::IPC_WAKE_MESSAGE;
use super::{WAKE_MESSAGE, WINDOW_ID};

/// Pump native messages until the window closes.
pub(super) fn message_loop() -> Result<()> {
    let mut msg = MSG::default();
    loop {
        // SAFETY: `msg` is valid writable storage for the retrieved message.
        let result = unsafe { WindowsAndMessaging::GetMessageW(&mut msg, None, 0, 0).0 };
        match result {
            -1 => return Err(WindowsError::from_thread().into()),
            0 => return Ok(()),
            // SAFETY: `msg` was filled by a successful `GetMessageW` call.
            _ => unsafe {
                let _ = WindowsAndMessaging::TranslateMessage(&msg);
                WindowsAndMessaging::DispatchMessageW(&msg);
            },
        }
    }
}

/// Native window procedure for the WebUI desktop window.
pub(super) extern "system" fn window_proc(
    hwnd: HWND,
    msg: u32,
    w_param: WPARAM,
    l_param: LPARAM,
) -> LRESULT {
    match msg {
        WindowsAndMessaging::WM_NCCREATE => {
            if let Err(error) = initialize_frame(hwnd, l_param) {
                eprintln!(
                    "WebUI: failed to initialize the native window frame: {error}; help: verify the native window creation parameters"
                );
                return LRESULT(0);
            }
            // SAFETY: Frame configuration is installed before default creation.
            unsafe { WindowsAndMessaging::DefWindowProcW(hwnd, msg, w_param, l_param) }
        }
        super::APP_WAKE_MESSAGE => {
            let tasks = super::state::with_window_state_result(hwnd, |state| {
                state.application_tasks.clone()
            });
            if let Some(tasks) = tasks {
                tasks.drain(w_param.0);
            }
            LRESULT(0)
        }
        #[cfg(feature = "application-ipc")]
        IPC_WAKE_MESSAGE => {
            let ipc = super::state::with_window_state_result(hwnd, |state| state.ipc.clone());
            // Release the native state borrow before polling COM completions.
            if let Some(ipc) = ipc {
                ipc.drain(w_param.0);
            }
            LRESULT(0)
        }
        #[cfg(feature = "application-ipc")]
        WindowsAndMessaging::WM_TIMER => {
            let ipc = super::state::with_window_state_result(hwnd, |state| state.ipc.clone());
            if let Some(ipc) = ipc {
                ipc.expire_hello(w_param.0);
            }
            LRESULT(0)
        }
        WAKE_MESSAGE => {
            drain_commands(hwnd);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_NCCALCSIZE => non_client_calc_size(hwnd, msg, w_param, l_param),
        WindowsAndMessaging::WM_NCHITTEST => non_client_hit_test(hwnd, msg, w_param, l_param),
        WindowsAndMessaging::WM_DWMCOMPOSITIONCHANGED => {
            refresh_frame(hwnd);
            redraw_frame(hwnd);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_DPICHANGED => {
            apply_suggested_rect(hwnd, l_param);
            refresh_frame(hwnd);
            dispatch_scale_changed(hwnd, w_param);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_ACTIVATE => {
            refresh_frame(hwnd);
            dispatch_activation(hwnd, w_param);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_SETTINGCHANGE => {
            refresh_frame(hwnd);
            dispatch_theme_changed(hwnd);
            // SAFETY: Setting changes must still reach default processing.
            unsafe { WindowsAndMessaging::DefWindowProcW(hwnd, msg, w_param, l_param) }
        }
        WindowsAndMessaging::WM_GETMINMAXINFO => {
            apply_size_limits(hwnd, l_param);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_SIZE => {
            dispatch_size_changed(hwnd, w_param);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_MOVE => {
            dispatch_moved(hwnd);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_CLOSE => {
            close_window(hwnd);
            LRESULT(0)
        }
        WindowsAndMessaging::WM_ERASEBKGND => erase_background(hwnd, w_param),
        WindowsAndMessaging::WM_DESTROY => {
            destroy_window(hwnd);
            LRESULT(0)
        }
        // SAFETY: Unhandled messages are delegated to the system procedure.
        _ => unsafe { WindowsAndMessaging::DefWindowProcW(hwnd, msg, w_param, l_param) },
    }
}

/// Run every command queued from other threads.
fn drain_commands(hwnd: HWND) {
    let commands =
        super::state::with_window_state_result(hwnd, |state| state.window_handle.drain_commands())
            .unwrap_or_default();
    for command in commands {
        execute_window_command(hwnd, command);
    }
}

/// Adopt the rectangle Windows suggests for a new DPI.
fn apply_suggested_rect(hwnd: HWND, l_param: LPARAM) {
    if l_param.0 == 0 {
        return;
    }
    // SAFETY: `WM_DPICHANGED` supplies a valid `RECT` for this message.
    let rect = unsafe { *(l_param.0 as *const RECT) };
    // SAFETY: `hwnd` is a live window and the call only repositions it.
    unsafe {
        let _ = WindowsAndMessaging::SetWindowPos(
            hwnd,
            None,
            rect.left,
            rect.top,
            rect.right.saturating_sub(rect.left),
            rect.bottom.saturating_sub(rect.top),
            WindowsAndMessaging::SWP_NOZORDER | WindowsAndMessaging::SWP_NOACTIVATE,
        );
    }
}

/// Publish a scale-factor change derived from the new DPI.
fn dispatch_scale_changed(hwnd: HWND, w_param: WPARAM) {
    let dpi = u16::try_from((w_param.0 >> 16) & 0xffff).unwrap_or(96);
    let event = DesktopEvent::ScaleFactorChanged {
        scale: f64::from(dpi) / 96.0,
    };
    publish(hwnd, &event);
}

/// Publish focus gain or loss.
fn dispatch_activation(hwnd: HWND, w_param: WPARAM) {
    let event = if w_param.0 & 0xffff == 0 {
        DesktopEvent::WindowBlurred {
            window_id: WINDOW_ID,
        }
    } else {
        DesktopEvent::WindowFocused {
            window_id: WINDOW_ID,
        }
    };
    publish(hwnd, &event);
}

/// Publish a theme change after a system settings broadcast.
fn dispatch_theme_changed(hwnd: HWND) {
    let event = DesktopEvent::ThemeChanged {
        dark: system_dark(),
    };
    publish(hwnd, &event);
}

/// Report whether apps should currently use a dark theme.
pub(super) fn system_dark() -> bool {
    // SAFETY: `GetSysColor` reads a process-wide system color value.
    let color = unsafe { Gdi::GetSysColor(Gdi::COLOR_WINDOW) };
    let red = u32::from(u8::try_from(color & 0xff).unwrap_or(0));
    let green = u32::from(u8::try_from((color >> 8) & 0xff).unwrap_or(0));
    let blue = u32::from(u8::try_from((color >> 16) & 0xff).unwrap_or(0));
    (red * 299 + green * 587 + blue * 114) / 1000 < 128
}

/// Constrain interactive resizing to the manifest's size limits.
fn apply_size_limits(hwnd: HWND, l_param: LPARAM) {
    if l_param.0 == 0 {
        return;
    }
    with_window_state(hwnd, |state| {
        // SAFETY: `WM_GETMINMAXINFO` supplies a valid, writable `MINMAXINFO`
        // that stays alive for the duration of this message.
        let info = unsafe { &mut *(l_param.0 as *mut WindowsAndMessaging::MINMAXINFO) };
        if let Some(value) = state.options.min_width.and_then(|v| i32::try_from(v).ok()) {
            info.ptMinTrackSize.x = value;
        }
        if let Some(value) = state.options.min_height.and_then(|v| i32::try_from(v).ok()) {
            info.ptMinTrackSize.y = value;
        }
        if let Some(value) = state.options.max_width.and_then(|v| i32::try_from(v).ok()) {
            info.ptMaxTrackSize.x = value;
        }
        if let Some(value) = state.options.max_height.and_then(|v| i32::try_from(v).ok()) {
            info.ptMaxTrackSize.y = value;
        }
    });
}

/// Resize the WebView2 surface and publish the matching lifecycle event.
fn dispatch_size_changed(hwnd: HWND, w_param: WPARAM) {
    with_window_state(hwnd, |state| {
        if let Err(error) = resize_content(hwnd, state) {
            eprintln!("WebUI: failed to resize native web content: {error}");
        }
        let size = get_window_size(state.content);
        let size_code = u32::try_from(w_param.0).unwrap_or(WindowsAndMessaging::SIZE_RESTORED);
        let (current, transition) = size_event_transition(state.window_state.get(), size_code);
        state.window_state.set(current);
        if let Some(event) = transition {
            emit(state, &event);
        }
        let dpi = window_dpi(hwnd);
        let event = DesktopEvent::WindowResized {
            window_id: WINDOW_ID,
            width: logical_dimension(size.cx, dpi),
            height: logical_dimension(size.cy, dpi),
        };
        emit(state, &event);
        save_window_state(hwnd, state);
    });
}

pub(super) fn refresh_frame(hwnd: HWND) {
    with_window_state(hwnd, |state| {
        if matches!(state.options.titlebar, crate::TitlebarStyle::None) {
            if let Err(error) = super::create::extend_frameless_frame(hwnd) {
                eprintln!("WebUI: failed to refresh the frameless DWM border: {error}");
            }
        }
        if let Err(error) = resize_content(hwnd, state) {
            eprintln!("WebUI: failed to refresh native caption controls: {error}");
        }
    });
}

fn resize_content(hwnd: HWND, state: &FrameState) -> Result<()> {
    if state.app_window.refresh(hwnd)? {
        state.app_window.publish_metrics(&state.webview)?;
    }
    set_controller_bounds(&state.controller, state.content)
}

/// Publish a move event and persist the new position.
fn dispatch_moved(hwnd: HWND) {
    with_window_state(hwnd, |state| {
        let mut rect = RECT::default();
        // SAFETY: `hwnd` is a live window and `rect` is writable storage.
        if unsafe { WindowsAndMessaging::GetWindowRect(hwnd, &mut rect) }.is_err() {
            return;
        }
        let dpi = window_dpi(hwnd);
        emit(
            state,
            &DesktopEvent::WindowMoved {
                window_id: WINDOW_ID,
                x: physical_to_logical(rect.left, dpi),
                y: physical_to_logical(rect.top, dpi),
            },
        );
        save_window_state(hwnd, state);
    });
}

/// Offer the close request to handlers before destroying the window.
fn close_window(hwnd: HWND) {
    let prevented = super::state::with_window_state_result(hwnd, |state| {
        let event = DesktopEvent::WindowCloseRequested {
            window_id: WINDOW_ID,
        };
        let prevented = state.events.dispatch(&event) == EventResponse::PreventDefault;
        mirror_event(&state.webview, &event);
        prevented
    })
    .unwrap_or(false);
    if prevented {
        return;
    }
    // SAFETY: `hwnd` is a live window and destruction is the normal close path.
    unsafe {
        let _ = WindowsAndMessaging::DestroyWindow(hwnd);
    }
}

/// Paint the configured background so there is no flash before first paint.
fn erase_background(hwnd: HWND, w_param: WPARAM) -> LRESULT {
    let Some(color) =
        super::state::with_window_state_result(hwnd, |state| state.options.background).flatten()
    else {
        return LRESULT(0);
    };
    let rgb = u32::from(color.r) | (u32::from(color.g) << 8) | (u32::from(color.b) << 16);
    let hdc = Gdi::HDC(w_param.0 as *mut std::ffi::c_void);
    let mut rect = RECT::default();
    // SAFETY: `hwnd` is a live window and `rect` is writable storage.
    if unsafe { WindowsAndMessaging::GetClientRect(hwnd, &mut rect) }.is_err() {
        return LRESULT(0);
    }
    // SAFETY: Windows supplies the device context in `w_param` for this
    // message, `rect` is initialized, and the brush is deleted before return.
    unsafe {
        let brush = Gdi::CreateSolidBrush(windows::Win32::Foundation::COLORREF(rgb));
        let _ = Gdi::FillRect(hdc, &rect, brush);
        let _ = Gdi::DeleteObject(brush.into());
    }
    LRESULT(1)
}

/// Publish the final close event and release the frame state.
fn destroy_window(hwnd: HWND) {
    #[cfg(feature = "application-ipc")]
    {
        let ipc = super::state::with_window_state_result(hwnd, |state| state.ipc.clone());
        if let Some(ipc) = ipc {
            ipc.close();
        }
    }
    with_window_state(hwnd, |state| {
        emit(
            state,
            &DesktopEvent::WindowClosed {
                window_id: WINDOW_ID,
            },
        );
    });
    drop(set_window_state(hwnd, None));
    // SAFETY: Posting quit from WM_DESTROY ends this thread's message loop.
    unsafe { WindowsAndMessaging::PostQuitMessage(0) };
}

/// Dispatch an event to handlers and mirror it into web content.
pub(super) fn emit(state: &FrameState, event: &DesktopEvent) {
    let _ = state.events.dispatch(event);
    mirror_event(&state.webview, event);
}

/// Dispatch an event when the frame state is installed.
pub(super) fn publish(hwnd: HWND, event: &DesktopEvent) {
    with_window_state(hwnd, |state| emit(state, event));
}

/// Match the WebView2 surface to the window's client area.
pub(super) fn set_controller_bounds(
    controller: &ICoreWebView2Controller,
    hwnd: HWND,
) -> Result<()> {
    let size = get_window_size(hwnd);
    // SAFETY: `controller` is a live WebView2 controller for this window.
    unsafe {
        controller.SetBounds(RECT {
            left: 0,
            top: 0,
            right: size.cx,
            bottom: size.cy,
        })?;
    }
    Ok(())
}

/// Return the window's current DPI for physical-to-logical conversion.
///
/// `GetDpiForWindow` returns 0 for an invalid window; callers treat 0 as the
/// 96-DPI baseline rather than dividing by zero.
fn window_dpi(hwnd: HWND) -> u32 {
    // SAFETY: `hwnd` is a live window and `GetDpiForWindow` only reads its
    // DPI awareness context.
    unsafe { HiDpi::GetDpiForWindow(hwnd) }
}

/// Return the window's client size in physical pixels.
pub(super) fn get_window_size(hwnd: HWND) -> SIZE {
    let mut client_rect = RECT::default();
    // SAFETY: `hwnd` is a live window and `client_rect` is writable storage.
    let _ = unsafe { WindowsAndMessaging::GetClientRect(hwnd, &mut client_rect) };
    SIZE {
        cx: client_rect.right.saturating_sub(client_rect.left),
        cy: client_rect.bottom.saturating_sub(client_rect.top),
    }
}