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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
/// Runs `f` against the live widget at `widget_id` when it is a `T`.
///
/// List and combo-box item operations are *actions*, not state writes, so they
/// cannot travel through the property contract and need the concrete control. The
/// downcast is the capability layer's own helper — the same one the property hooks
/// use — so this adds no second type table (rule #67).
///
/// Returns `None` when the id addresses nothing or addresses a different kind,
/// which is what lets each caller report "this control has no items" instead of a
/// silent `true`.
#[cfg(not(alloc_frugal))]
fn with_typed_widget<T, R>(widget_id: ObjectId, f: impl FnOnce(&mut T) -> R) -> Option<R>
where
T: crate::widget::Widget + 'static,
{
crate::widget::runtime::with_widget_mut(widget_id, |widget| {
crate::widget::capability::widget_as_mut::<T>(widget).map(f)
})
.flatten()
}
macro_rules! impl_helpers {
() => {
fn poll_widget_trigger_event(&self) -> Option<WidgetTriggerEvent> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.widget_trigger_queue
.pop_front()
}
/// Pops the next triggered widget id.
///
/// Shares one queue with `poll_widget_trigger_event` — they are two views
/// of the same event stream — so both consume an event when they answer,
/// exactly as the platform backends do.
fn poll_widget_triggered(&self) -> Option<ObjectId> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.widget_trigger_queue
.pop_front()
.map(|event| event.widget_id)
}
fn inject_widget_trigger_event(
&self,
widget_id: ObjectId,
kind: WidgetTriggerKind,
) -> bool {
let mut state = self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
state.widget_trigger_queue.push_back(WidgetTriggerEvent { widget_id, kind });
true
}
/// Writes the control's label, repainting it.
///
/// Kinds disagree on the property's name — a `Button` exposes `text`, a
/// `Window`/`GroupBox` exposes `title`, a `StatusBar` exposes `message`.
/// Probing the known spellings keeps this accessor total without a `match`
/// on kind, which would be the kind of central table this refactor removes.
fn set_widget_text(&self, widget_id: ObjectId, text: &str) {
#[cfg(widgets_unstripped)]
{
let value = || crate::widget::capability::CapabilityValue::String(text.to_string());
let mut written =
Err(crate::widget::capability::CapabilityAccessError::UnknownWidget);
for property in LABEL_PROPERTY_NAMES {
match crate::widget::capability::write_widget_property_by_id(
widget_id,
property,
value(),
) {
Ok(()) => {
written = Ok(());
break;
}
Err(error) => written = Err(error),
}
}
if written.is_err() {
log::warn!(
"custom backend: widget {widget_id} exposes none of \
{LABEL_PROPERTY_NAMES:?}, so the label could not be set"
);
}
}
// A stripped profile has no property registry, so a label cannot be
// written there. Reporting that is more useful than silently doing
// nothing, and it matches the profile's whole purpose.
#[cfg(not(widgets_unstripped))]
{
let _ = (widget_id, text);
log::warn!(
"custom backend: labels are unavailable in this profile (no property \
registry is compiled in)"
);
}
}
/// Reads the control's label, trying each known spelling.
fn get_widget_text(&self, widget_id: ObjectId) -> String {
#[cfg(widgets_unstripped)]
{
for property in LABEL_PROPERTY_NAMES {
if let Ok(crate::widget::capability::CapabilityValue::String(text)) =
crate::widget::capability::read_widget_property_by_id(widget_id, property)
{
if !text.is_empty() {
return text;
}
}
}
}
#[cfg(not(widgets_unstripped))]
let _ = widget_id;
String::new()
}
fn set_widget_enabled(&self, widget_id: ObjectId, enabled: bool) {
// `widget::runtime` is compiled out of the alloc-frugal profile, which
// holds no widget objects by design. The host-side maps remain, so the
// accessors degrade rather than fail to compile.
#[cfg(not(alloc_frugal))]
{
self.with_live_widget(widget_id, |widget| widget.set_enabled(enabled));
crate::widget::runtime::request_repaint(widget_id);
}
#[cfg(alloc_frugal)]
let _ = (widget_id, enabled);
}
fn is_widget_enabled(&self, widget_id: ObjectId) -> bool {
#[cfg(not(alloc_frugal))]
{
return self
.with_live_widget(widget_id, |widget| widget.is_enabled())
.unwrap_or(false);
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
false
}
}
fn set_widget_visible(&self, widget_id: ObjectId, visible: bool) {
#[cfg(not(alloc_frugal))]
{
self.with_live_widget(widget_id, |widget| widget.set_visible(visible));
crate::widget::runtime::request_repaint(widget_id);
}
#[cfg(alloc_frugal)]
let _ = (widget_id, visible);
}
fn is_widget_visible(&self, widget_id: ObjectId) -> bool {
#[cfg(not(alloc_frugal))]
{
return self
.with_live_widget(widget_id, |widget| widget.is_visible())
.unwrap_or(false);
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
false
}
}
/// Shows the control; the id-addressed spelling of `set_widget_visible`.
fn show_widget(&self, widget_id: ObjectId) {
self.set_widget_visible(widget_id, true);
}
/// Hides the control.
fn hide_widget(&self, widget_id: ObjectId) {
self.set_widget_visible(widget_id, false);
}
/// Reads the control's rectangle from the widget registry.
///
/// The widget owns its geometry — the backend writes it into
/// `widget::runtime` whenever it moves one — so the answer comes from there
/// rather than from a backend-side copy that could disagree.
fn get_widget_geometry(&self, widget_id: ObjectId) -> Option<(i32, i32, u32, u32)> {
#[cfg(not(alloc_frugal))]
{
return crate::widget::runtime::geometry_of(widget_id)
.map(|rect| (rect.x, rect.y, rect.width, rect.height));
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
None
}
}
/// Appends an item to a combo box, repainting it.
fn combo_box_add_item(&self, widget_id: ObjectId, text: &str) -> bool {
#[cfg(not(alloc_frugal))]
{
let added =
with_typed_widget::<crate::widget::input_widgets::combobox::ComboBox, _>(
widget_id,
|combo| combo.add_item(text.to_string()),
)
.is_some();
if added {
crate::widget::runtime::request_repaint(widget_id);
}
return added;
}
#[cfg(alloc_frugal)]
{
let _ = (widget_id, text);
false
}
}
/// Removes every item from a combo box.
fn combo_box_clear_items(&self, widget_id: ObjectId) -> bool {
#[cfg(not(alloc_frugal))]
{
let cleared = with_typed_widget::<
crate::widget::input_widgets::combobox::ComboBox,
_,
>(widget_id, |combo| combo.clear())
.is_some();
if cleared {
crate::widget::runtime::request_repaint(widget_id);
}
return cleared;
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
false
}
}
/// Appends an item to a list box, repainting it.
fn list_box_add_item(&self, widget_id: ObjectId, text: &str) -> bool {
#[cfg(not(alloc_frugal))]
{
let added = with_typed_widget::<crate::widget::input_widgets::listbox::ListBox, _>(
widget_id,
|list| list.add_item(text.to_string()),
)
.is_some();
if added {
crate::widget::runtime::request_repaint(widget_id);
}
return added;
}
#[cfg(alloc_frugal)]
{
let _ = (widget_id, text);
false
}
}
/// Removes one item from a list box by index.
fn list_box_remove_item(&self, widget_id: ObjectId, index: usize) -> bool {
#[cfg(not(alloc_frugal))]
{
let removed =
with_typed_widget::<crate::widget::input_widgets::listbox::ListBox, _>(
widget_id,
|list| {
if index < list.count() {
list.remove_item(index);
true
} else {
false
}
},
)
.unwrap_or(false);
if removed {
crate::widget::runtime::request_repaint(widget_id);
}
return removed;
}
#[cfg(alloc_frugal)]
{
let _ = (widget_id, index);
false
}
}
/// Removes every item from a list box.
fn list_box_clear_items(&self, widget_id: ObjectId) -> bool {
#[cfg(not(alloc_frugal))]
{
let cleared =
with_typed_widget::<crate::widget::input_widgets::listbox::ListBox, _>(
widget_id,
|list| list.clear(),
)
.is_some();
if cleared {
crate::widget::runtime::request_repaint(widget_id);
}
return cleared;
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
false
}
}
/// Reads one list-box item's text, straight off the control.
fn list_box_item_text(&self, widget_id: ObjectId, index: usize) -> Option<String> {
#[cfg(not(alloc_frugal))]
{
return with_typed_widget::<crate::widget::input_widgets::listbox::ListBox, _>(
widget_id,
|list| list.items().get(index).cloned(),
)
.flatten();
}
#[cfg(alloc_frugal)]
{
let _ = (widget_id, index);
None
}
}
/// Reads one combo-box item's text, straight off the control.
fn combo_box_item_text(&self, widget_id: ObjectId, index: usize) -> Option<String> {
#[cfg(not(alloc_frugal))]
{
return with_typed_widget::<crate::widget::input_widgets::combobox::ComboBox, _>(
widget_id,
|combo| combo.items().get(index).cloned(),
)
.flatten();
}
#[cfg(alloc_frugal)]
{
let _ = (widget_id, index);
None
}
}
fn set_widget_geometry(
&self,
widget_id: ObjectId,
x: i32,
y: i32,
width: u32,
height: u32,
) {
#[cfg(not(alloc_frugal))]
{
if crate::widget::runtime::set_geometry(
widget_id,
crate::core::Rect::new(x, y, width, height),
) {
crate::widget::runtime::request_repaint(widget_id);
}
}
#[cfg(alloc_frugal)]
let _ = (widget_id, x, y, width, height);
}
/// Marks whether the mounted widget accepts IME input.
///
/// IME enablement is not a widget property (it describes how the host
/// routes composition events to the control), so it is kept in the
/// backend's own state rather than on the widget.
fn set_widget_ime_enabled(&self, widget_id: ObjectId, enabled: bool) -> bool {
#[cfg(not(alloc_frugal))]
if !crate::widget::runtime::is_mounted(widget_id) {
return false;
}
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.ime_enabled
.insert(widget_id, enabled);
true
}
fn is_widget_ime_enabled(&self, widget_id: ObjectId) -> bool {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.ime_enabled
.get(&widget_id)
.copied()
.unwrap_or(false)
}
fn set_widget_accessibility_name(&self, widget_id: ObjectId, name: &str) -> bool {
#[cfg(not(alloc_frugal))]
if !crate::widget::runtime::is_mounted(widget_id) {
return false;
}
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.accessibility_names
.insert(widget_id, name.to_string());
true
}
/// Reads the control's accessible name.
///
/// An explicit override wins; otherwise the control's own name is derived
/// from its label and kind, which is what the widget trait documents. The
/// old accessor returned an empty string unless a host had set one, so a
/// control with a perfectly good label reported no accessible name at all.
fn get_widget_accessibility_name(&self, widget_id: ObjectId) -> String {
let override_name = self
.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.accessibility_names
.get(&widget_id)
.cloned();
if let Some(name) = override_name {
return name;
}
#[cfg(not(alloc_frugal))]
{
return crate::widget::runtime::with_widget(widget_id, |widget| {
widget.accessible_name()
})
.unwrap_or_default();
}
#[cfg(alloc_frugal)]
{
let _ = widget_id;
String::new()
}
}
};
}