Skip to main content

rs_teststand_sys/window/
dismiss.rs

1//! Closes a dialog that is holding up the process.
2//!
3//! The engine raises some dialogs before a caller can configure anything away.
4//! The clearest case is the warning that sequence files were left unreleased
5//! when the engine was last destroyed: it appears while the engine object is
6//! being created, so it is already on screen by the time the first line of
7//! caller code runs, and no station option suppresses it. On a station with
8//! somebody sitting at it that is a click. On a headless host it is a hang that
9//! ends in the engine being killed and reinitialised.
10//!
11//! Closing is done with a posted `WM_CLOSE`, never a synthesised click. Posting
12//! returns immediately, so the thread doing the closing cannot itself be stuck
13//! behind the modal loop, and the message goes to the window rather than to
14//! whatever happens to be under the cursor. A dialog whose only button is *OK*
15//! treats closing as that button.
16//!
17//! This is a blunt instrument by nature: it answers a question without reading
18//! it. That is why it is never the default, why the text is captured first so
19//! the caller can log what was dismissed, and why it is scoped to a short window
20//! around a call known to raise one of these.
21
22use windows::Win32::Foundation::{HWND, LPARAM, WPARAM};
23use windows::Win32::UI::WindowsAndMessaging::{IsWindow, PostMessageW, WM_CLOSE};
24
25use super::dialog::{DialogInfo, describe, first_visible_dialog};
26
27/// What happened to a dialog that was dismissed.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Dismissed {
30    /// What the dialog was showing, read before it was asked to close.
31    pub dialog: DialogInfo,
32    /// Whether the window was gone when it was checked again.
33    ///
34    /// `false` is not necessarily a failure: a dialog can take a moment to
35    /// unwind, and a modal loop that is not processing messages yet will not
36    /// see the request until it starts. It does mean the caller should not
37    /// assume the way is clear.
38    pub closed: bool,
39}
40
41/// Asks the first visible dialog owned by this process to close.
42///
43/// Returns `None` when there is nothing to close.
44#[must_use]
45pub fn dismiss_blocking_dialog() -> Option<Dismissed> {
46    let handle = first_visible_dialog()?;
47    // Read it before closing: afterwards there is nothing left to read, and a
48    // caller that cannot say what it dismissed cannot diagnose a wrong one.
49    let dialog = describe(handle);
50    Some(Dismissed {
51        dialog,
52        closed: close(handle),
53    })
54}
55
56/// How long to wait for a window to go away, in total.
57const CONFIRM_ATTEMPTS: u8 = 20;
58/// Gap between checks.
59const CONFIRM_INTERVAL: core::time::Duration = core::time::Duration::from_millis(25);
60
61/// Posts `WM_CLOSE` and reports whether the window went away.
62fn close(handle: HWND) -> bool {
63    // SAFETY: `handle` comes from the OS enumeration. Posting never blocks, so
64    // a failure here means the window had already gone, which is the outcome
65    // being asked for anyway.
66    if unsafe { PostMessageW(Some(handle), WM_CLOSE, WPARAM(0), LPARAM(0)) }.is_err() {
67        return true;
68    }
69
70    for _ in 0..CONFIRM_ATTEMPTS {
71        std::thread::sleep(CONFIRM_INTERVAL);
72        // SAFETY: reading whether a handle still names a window is valid even
73        // once it does not.
74        if !unsafe { IsWindow(Some(handle)) }.as_bool() {
75            return true;
76        }
77    }
78    false
79}