1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use RefCell;
use Rc;
type Listener<T> = ;
/// A reactive signal that notifies listeners when its value changes.
///
/// # Bridging signals to the UI thread (`async` feature)
///
/// `Signal` is UI-thread-only (`Rc` internals, not `Send`) and listeners run
/// synchronously inside [`Signal::set`]/[`Signal::update`]. To drive widgets
/// from a signal, bridge the listener into the bounded UI command queue
/// instead of touching widgets directly:
///
/// 1. Create a `UiProxy` with `Ui::task_proxy` before wiring the listener.
/// 2. Subscribe once on the UI thread; the listener converts the new value
/// into proxy posts (`set_text`, `set_value`, `set_checked`, `set_enabled`,
/// `set_visible`, `progress`). A `UiProxy` is `Send` and bounded, so the
/// same wiring works when the signal is updated on the UI thread and the
/// commands are applied on the next event-loop turn.
/// 3. Drain the queue from the event loop: [`Ui::wait_event`](crate::Ui::wait_event),
/// [`Ui::poll_event`](crate::Ui::poll_event),
/// [`Ui::dispatch_events`](crate::Ui::dispatch_events), and
/// `run_until_closed` apply posted commands in FIFO order on the UI thread
/// before returning backend events.
///
/// Posts that arrive after teardown, or when the bounded queue is full,
/// report `TaskSendError` instead of blocking or
/// panicking, so a listener can degrade gracefully. Background threads cannot
/// hold a `Signal`: send raw values through the proxy (or a channel) and
/// update the signal on the UI thread, where its listeners run.
///
/// ```rust
/// use native_gui_mock::MockBackend;
/// use native_gui_core::state::Signal;
/// use native_gui_core::{Ui, WidgetKind};
///
/// # fn main() -> native_gui_core::GuiResult<()> {
/// # #[cfg(feature = "async")]
/// # {
/// let mut ui = Ui::new(MockBackend::default());
/// let label = ui.create(WidgetKind::Label, Some("0"))?;
/// let proxy = ui.task_proxy();
///
/// let counter = Signal::new(0i32);
/// counter.subscribe(move |value| {
/// let _ = proxy.set_text(label, value.to_string());
/// });
///
/// counter.set(1);
/// // The queue drains on the next event-loop turn (here: a dispatch pass).
/// ui.dispatch_events(|_| {})?;
/// assert_eq!(
/// ui.widget(label).and_then(|spec| spec.text.clone()),
/// Some("1".to_owned())
/// );
/// # }
/// # Ok(())
/// # }
/// ```