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:
- blocks current thread;
- switches to the window’s original UI thread;
- runs the given
FnOnce
; - 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
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more