Struct winsafe::gui::WindowModal[][src]

pub struct WindowModal { /* fields omitted */ }
Expand description

An user modal window, which can handle events. Can be programmatically created or load a dialog resource from a .res file.

Implements Parent trait.

Implementations

Instantiates a new WindowModal object, to be created with HWND::CreateWindowEx.

Instantiates a new WindowModal object, to be loaded from a dialog resource with HWND::GetDlgItem.

Returns the underlying handle for this control.

Note that the handle is initially null, receiving an actual value only after the control is created.

Exposes the window events.

Panics

Panics if the window is already created. Events must be set before window creation.

If you perform a very long task in the UI thread, the UI freezes until the task is complete – this may cause the impression that your application crashed. That’s why long tasks are performed in parallel threads. However, at some point you’ll want to update the UI to reflect the task progress, but if you update the UI from another thread (different from the original UI thread), the UI may deadlock, and you application crashes.

The run_ui_thread method allows UI updates by running a closure synchronously in the window’s original UI thread.

This is what this run_ui_thread does, step-by-step:

  1. blocks current thread;
  2. switches to the window’s original UI thread;
  3. runs the given FnOnce;
  4. switches back to the first thread, which is then unblocked.

When working in a parallel thread, you must call run_ui_thread to update the UI.

Examples

The example below shows the event of a button click which starts a long task in a parallel thread. As it progresses, the status is printed at the windows’s titlebar.

use winsafe::{gui, BoxResult, GetCurrentThreadId, Sleep};

let wnd: gui::WindowMain; // initialized somewhere
let btn: gui::Button;

btn.on().bn_clicked({
    let wnd = wnd.clone();
    move || -> BoxResult<()> {
        println!("Click event at {:#x}", GetCurrentThreadId());

        std::thread::spawn({
            let wnd = wnd.clone();
            move || {
                println!("Parallel task starts at {:#x}", GetCurrentThreadId());
                Sleep(2000);

                wnd.run_ui_thread({
                    let wnd = wnd.clone();
                    move || -> BoxResult<()> {
                        println!("Updating UI at {:#x}", GetCurrentThreadId());
                        wnd.hwnd().SetWindowText("Status... 50%")?;
                        Ok(())
                    }
                });

                println!("Parallel task keeps going at {:#x}", GetCurrentThreadId());
                Sleep(2000);

                wnd.run_ui_thread({
                    let wnd = wnd.clone();
                    move || -> BoxResult<()> {
                        println!("Updating UI at {:#x}", GetCurrentThreadId());
                        wnd.hwnd().SetWindowText("Status... 100%")?;
                        Ok(())
                    }
                });
            }
        });

        Ok(())
    }
});

Physically creates the window, then runs the modal loop. This method will block until the window is closed.

Panics

Panics if the window is already created.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns a reference to the Any trait, allowing downcasting.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.