Skip to main content

UiProxy

Struct UiProxy 

Source
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

Source

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.

Source

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 parked wait_event returns promptly);
  • Err(TaskSendError::Closed) — the owning Ui is gone; stop the worker;
  • Err(TaskSendError::Full) — timeout elapsed 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 { .. })));
Source

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.

Source

pub fn capacity(&self) -> usize

The bounded capacity of this proxy’s command queue.

Source

pub fn set_text( &self, widget_id: WidgetId, text: impl Into<String>, ) -> Result<(), TaskSendError>

Source

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.

Source

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.

Source

pub fn set_value( &self, widget_id: WidgetId, value: i32, ) -> Result<(), TaskSendError>

Source

pub fn set_checked( &self, widget_id: WidgetId, checked: bool, ) -> Result<(), TaskSendError>

Source

pub fn set_enabled( &self, widget_id: WidgetId, enabled: bool, ) -> Result<(), TaskSendError>

Source

pub fn set_visible( &self, widget_id: WidgetId, visible: bool, ) -> Result<(), TaskSendError>

Source

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.

Source

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.

Source

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.

Source

pub fn finish_lossless( &self, token: &TaskToken, timeout: Duration, ) -> Result<(), TaskSendError>

Source

pub fn fail_lossless( &self, token: &TaskToken, message: impl Into<String>, timeout: Duration, ) -> Result<(), TaskSendError>

Trait Implementations§

Source§

impl Clone for UiProxy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UiProxy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for UiProxy

§

impl !UnwindSafe for UiProxy

§

impl Forget for UiProxy

§

impl Freeze for UiProxy
where *const ArcInner<UiProxyInner> is !null: Freeze, PhantomData<ArcInner<UiProxyInner>>: Freeze,

§

impl Move for UiProxy

§

impl Send for UiProxy
where Arc<UiProxyInner>: Send,

§

impl Sync for UiProxy
where Arc<UiProxyInner>: Sync,

§

impl Unpin for UiProxy
where Arc<UiProxyInner>: Unpin,

§

impl UnsafeUnpin for UiProxy
where *const ArcInner<UiProxyInner> is !null: UnsafeUnpin, PhantomData<ArcInner<UiProxyInner>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.