Skip to main content

Module async_ui

Module async_ui 

Source
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:

§Lossy vs. lossless backpressure

A bounded queue must distinguish what it may drop from what it must not:

ClassExamplesUnder pressure
TaskClass::Lossy — coalescent telemetryProgress, intermediate SetText/SetValue samplessent with UiProxy::post; on TaskSendError::Full the worker skips the sample (the next one supersedes it)
TaskClass::Lossless — essential lifecycleFinished, Failed, and final transactional commitssent 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§

TaskDrainReport
Outcome of one crate::Ui::drain_task_commands call.
TaskHandle
Handle returned by crate::Ui::spawn_task_with_callback representing an active background task.
TaskId
Identifier for one background operation started through UiProxy::start_task.
TaskToken
Cancellation handle shared between the UI thread and one worker.
UiProxy
Cloneable, Send + Sync handle used by background threads to post TaskCommands to the UI thread.

Enums§

TaskAbortReason
Why a queued widget command never reached the shared model.
TaskClass
Channel class of a TaskCommand under queue pressure.
TaskCommand
A command posted from a worker thread to the UI thread.
TaskCommandKind
The widget-targeting TaskCommand family.
TaskEvent
Lifecycle notification for one background task, delivered to the UI thread as crate::UiEvent::Task.
TaskSendError
Why a UiProxy post was not accepted.

Constants§

DEFAULT_TASK_QUEUE_CAPACITY
Default capacity of the UI command channel created by crate::Ui::task_proxy.