pub struct UiProxy { /* private fields */ }Expand description
Cloneable, Send + Sync handle used by background threads to post
TaskCommands to the UI thread.
Obtained from crate::Ui::task_proxy. Clones share one bounded queue
and one task-ID counter. Every successful post invokes the backend’s wake
hook (when the backend has one) so a blocked wait_event returns promptly
and the UI thread can drain the queue.
Implementations§
Source§impl UiProxy
impl UiProxy
Sourcepub fn post(&self, command: TaskCommand) -> Result<(), TaskSendError>
pub fn post(&self, command: TaskCommand) -> Result<(), TaskSendError>
Posts a command to the UI thread (lossy path).
Returns TaskSendError::Full when the bounded queue is full and
TaskSendError::Closed after the owning Ui was dropped. This is
the right path for TaskClass::Lossy telemetry: on Full, skip the
sample — the next one supersedes it. Essential lifecycle tokens use
UiProxy::send_lossless instead.
Sourcepub fn send_lossless(
&self,
command: TaskCommand,
timeout: Duration,
) -> Result<(), TaskSendError>
pub fn send_lossless( &self, command: TaskCommand, timeout: Duration, ) -> Result<(), TaskSendError>
Posts a command losslessly: waits (with backoff) until the bounded queue has room.
This is the guaranteed path for TaskClass::Lossless commands
(Finished, Failed, final transactional commits). UiProxy::post
is the lossy counterpart for coalescent telemetry.
The wait is bounded by timeout, so a stalled UI thread can never
hang the worker forever:
Ok(())— the command is queued and will be applied in FIFO order (the backend wake hook is signalled, so a parkedwait_eventreturns promptly);Err(TaskSendError::Closed)— the owningUiis gone; stop the worker;Err(TaskSendError::Full)—timeoutelapsed while the queue stayed full. The command was not queued, so the caller decides what to do (surface the error, retry with a longer timeout, or degrade explicitly); it is never reported as success and never silently dropped.
use native_gui_mock::MockBackend;
use native_gui_core::{TaskEvent, Ui, UiEvent};
use std::time::Duration;
let mut ui = Ui::new(MockBackend::default());
let proxy = ui.task_proxy();
let token = proxy.start_task();
let worker_proxy = proxy.clone();
let worker = std::thread::spawn(move || {
// ... do the slow work ...
worker_proxy
.finish_lossless(&token, Duration::from_secs(5))
.expect("the completion token must reach the UI thread");
});
worker.join().expect("worker thread");
ui.drain_task_commands();
let event = ui.poll_event().expect("poll").expect("lifecycle event");
assert!(matches!(event, UiEvent::Task(TaskEvent::Finished { .. })));Sourcepub fn start_task(&self) -> TaskToken
pub fn start_task(&self) -> TaskToken
Starts a new background operation and returns its cancellation token.
IDs are unique per proxy; clone a token to the worker and keep one
clone on the UI thread for TaskToken::cancel.
Sourcepub fn set_text(
&self,
widget_id: WidgetId,
text: impl Into<String>,
) -> Result<(), TaskSendError>
pub fn set_text( &self, widget_id: WidgetId, text: impl Into<String>, ) -> Result<(), TaskSendError>
Posts TaskCommand::SetText.
Sourcepub fn set_data_list_items(
&self,
widget_id: WidgetId,
items: impl IntoIterator<Item = String>,
) -> Result<(), TaskSendError>
pub fn set_data_list_items( &self, widget_id: WidgetId, items: impl IntoIterator<Item = String>, ) -> Result<(), TaskSendError>
Posts a collection replacement for a list widget.
Sourcepub fn set_data_grid_rows(
&self,
widget_id: WidgetId,
rows: impl IntoIterator<Item = Vec<String>>,
) -> Result<(), TaskSendError>
pub fn set_data_grid_rows( &self, widget_id: WidgetId, rows: impl IntoIterator<Item = Vec<String>>, ) -> Result<(), TaskSendError>
Posts a row-major collection replacement for a data grid.
Sourcepub fn set_value(
&self,
widget_id: WidgetId,
value: i32,
) -> Result<(), TaskSendError>
pub fn set_value( &self, widget_id: WidgetId, value: i32, ) -> Result<(), TaskSendError>
Posts TaskCommand::SetValue.
Sourcepub fn set_checked(
&self,
widget_id: WidgetId,
checked: bool,
) -> Result<(), TaskSendError>
pub fn set_checked( &self, widget_id: WidgetId, checked: bool, ) -> Result<(), TaskSendError>
Posts TaskCommand::SetChecked.
Sourcepub fn set_enabled(
&self,
widget_id: WidgetId,
enabled: bool,
) -> Result<(), TaskSendError>
pub fn set_enabled( &self, widget_id: WidgetId, enabled: bool, ) -> Result<(), TaskSendError>
Posts TaskCommand::SetEnabled.
Sourcepub fn set_visible(
&self,
widget_id: WidgetId,
visible: bool,
) -> Result<(), TaskSendError>
pub fn set_visible( &self, widget_id: WidgetId, visible: bool, ) -> Result<(), TaskSendError>
Posts TaskCommand::SetVisible.
Sourcepub fn progress(
&self,
token: &TaskToken,
value: f64,
) -> Result<(), TaskSendError>
pub fn progress( &self, token: &TaskToken, value: f64, ) -> Result<(), TaskSendError>
Posts TaskCommand::Progress for the given task (lossy sample).
Progress is coalescent telemetry: on TaskSendError::Full skip the
sample — the next one supersedes it. The completion token still
arrives through UiProxy::finish_lossless.
Sourcepub fn finish(&self, token: &TaskToken) -> Result<(), TaskSendError>
pub fn finish(&self, token: &TaskToken) -> Result<(), TaskSendError>
Posts TaskCommand::Finished for the given task (lossy path).
Prefer UiProxy::finish_lossless in real workers: a completion
token dropped at queue capacity can strand the UI state machine.
Sourcepub fn fail(
&self,
token: &TaskToken,
message: impl Into<String>,
) -> Result<(), TaskSendError>
pub fn fail( &self, token: &TaskToken, message: impl Into<String>, ) -> Result<(), TaskSendError>
Posts TaskCommand::Failed for the given task (lossy path).
Prefer UiProxy::fail_lossless in real workers: an error token
dropped at queue capacity can strand the UI state machine.
Sourcepub fn finish_lossless(
&self,
token: &TaskToken,
timeout: Duration,
) -> Result<(), TaskSendError>
pub fn finish_lossless( &self, token: &TaskToken, timeout: Duration, ) -> Result<(), TaskSendError>
Posts TaskCommand::Finished losslessly (see
UiProxy::send_lossless).
Sourcepub fn fail_lossless(
&self,
token: &TaskToken,
message: impl Into<String>,
timeout: Duration,
) -> Result<(), TaskSendError>
pub fn fail_lossless( &self, token: &TaskToken, message: impl Into<String>, timeout: Duration, ) -> Result<(), TaskSendError>
Posts TaskCommand::Failed losslessly (see
UiProxy::send_lossless).