Expand description
Sendable UI-thread command proxy for background work.
Background threads never borrow or mutate crate::Ui. Instead they hold
a cloneable, Send UiProxy and post TaskCommand values into a
bounded channel. The UI thread drains that channel inside its event
functions (wait_event, poll_event, dispatch_events,
run_until_closed), applies widget mutations through the same set_*
paths as direct calls, and surfaces task lifecycle notifications as
crate::UiEvent::Task values.
use native_gui_mock::MockBackend;
use native_gui_core::{Ui, UiEvent, WidgetKind};
let mut ui = Ui::new(MockBackend::default());
let label = ui.create(WidgetKind::Label, Some("idle"))?;
let proxy = ui.task_proxy();
let token = proxy.start_task();
let worker_proxy = proxy.clone();
std::thread::spawn(move || {
// Do the slow work here, then publish the result.
let _ = worker_proxy.set_text(label, "done");
let _ = worker_proxy.finish(&token);
});
// The UI thread applies queued commands while it waits for events.
while let Ok(event) = ui.wait_event() {
if matches!(event, UiEvent::Task(_)) {
break;
}
}
assert_eq!(ui.widget(label).and_then(|spec| spec.text.as_deref()), Some("done"));Guarantees:
- commands are applied in FIFO post order;
- the queue is bounded (see
DEFAULT_TASK_QUEUE_CAPACITY) and overflow is reported asTaskSendError::Fullinstead of blocking or growing without bound; - once the owning
crate::Uiis dropped, posts fail withTaskSendError::Closed, so stale workers stop deterministically; - drop contract: dropping a
Ui(or callingcrate::Ui::shutdown) closes the channel immediately. Commands already accepted but not yet drained are discarded unapplied — a post that returnedOk(())before teardown is accepted, not applied; callcrate::Ui::drain_task_commandsfirst, and use the count returned bycrate::Ui::shutdownto detect lost commits; - commands that target removed widgets are rejected (counted in
TaskDrainReport::rejected) and never panic; every rejected command also surfaces asTaskEvent::Aborted, so a teardown race cannot swallow a worker’s commit silently.
§Lossy vs. lossless backpressure
A bounded queue must distinguish what it may drop from what it must not:
| Class | Examples | Under pressure |
|---|---|---|
TaskClass::Lossy — coalescent telemetry | Progress, intermediate SetText/SetValue samples | sent with UiProxy::post; on TaskSendError::Full the worker skips the sample (the next one supersedes it) |
TaskClass::Lossless — essential lifecycle | Finished, Failed, and final transactional commits | sent with UiProxy::send_lossless, which blocks (with backoff) until the queue accepts the command, the UI is gone, or the caller’s timeout expires |
Dropping a lossless token strands the UI state machine (a progress bar
that never completes, a worker whose result never lands), so
TaskCommand::class names the default class of every command and
UiProxy::send_lossless is the guaranteed path. A lossless send never
waits forever: a worker that cannot make progress reports
TaskSendError::Full to its caller instead of hanging, and a worker
whose UI is gone stops on TaskSendError::Closed.
Structs§
- Task
Drain Report - Outcome of one
crate::Ui::drain_task_commandscall. - Task
Handle - Handle returned by
crate::Ui::spawn_task_with_callbackrepresenting an active background task. - TaskId
- Identifier for one background operation started through
UiProxy::start_task. - Task
Token - Cancellation handle shared between the UI thread and one worker.
- UiProxy
- Cloneable,
Send + Synchandle used by background threads to postTaskCommands to the UI thread.
Enums§
- Task
Abort Reason - Why a queued widget command never reached the shared model.
- Task
Class - Channel class of a
TaskCommandunder queue pressure. - Task
Command - A command posted from a worker thread to the UI thread.
- Task
Command Kind - The widget-targeting
TaskCommandfamily. - Task
Event - Lifecycle notification for one background task, delivered to the UI
thread as
crate::UiEvent::Task. - Task
Send Error - Why a
UiProxypost was not accepted.
Constants§
- DEFAULT_
TASK_ QUEUE_ CAPACITY - Default capacity of the UI command channel created by
crate::Ui::task_proxy.