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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use super::action::Actions;
use accesskit::{
Action, ActionRequest, CustomAction as AccessCustomAction, Node as AccessNode, NodeId, Toggled,
};
/// Configures a node added to a semantic tree.
#[derive(Debug)]
pub struct Node<'a, M = ()> {
/// The stable ID of the AccessKit node being configured.
id: NodeId,
/// The AccessKit node being configured.
inner: &'a mut AccessNode,
/// The node selected as semantic focus in the current publication.
focus: &'a mut Option<NodeId>,
/// Registry for actions supported by the current semantic tree.
actions: &'a mut Actions<M>,
}
impl<'a, M> Node<'a, M> {
/// Creates a semantic node builder for an AccessKit node.
pub(crate) fn new(
id: NodeId,
inner: &'a mut AccessNode,
focus: &'a mut Option<NodeId>,
actions: &'a mut Actions<M>,
) -> Self {
Self {
id,
inner,
focus,
actions,
}
}
/// Sets the node's accessible label.
///
/// # Examples
///
/// ```
/// # use molerat::{Molerat, accesskit::Role};
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.node("status", Role::Status).label("Application status");
/// # });
/// ```
pub fn label(self, label: impl Into<String>) -> Self {
self.inner.set_label(label);
self
}
/// Sets the node's value.
///
/// # Examples
///
/// ```
/// # use molerat::{Molerat, accesskit::Role};
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.node("status", Role::Status).value("Ready");
/// # });
/// ```
pub fn value(self, value: impl Into<String>) -> Self {
self.inner.set_value(value);
self
}
/// Sets the node's accessible description.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.button("save", "Save").description("Save current changes");
/// # });
/// ```
pub fn description(self, description: impl Into<String>) -> Self {
self.inner.set_description(description);
self
}
/// Sets whether the node is toggled.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.checkbox("auto-refresh", "Auto refresh", false).toggled(true);
/// # });
/// ```
pub fn toggled(self, toggled: bool) -> Self {
self.inner.set_toggled(Toggled::from(toggled));
self
}
/// Sets whether the node is selected.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// # ui.list_box("recipes", "Recipes", |ui| {
/// ui.list_box_option("ratatouille", "Ratatouille").selected(true);
/// # });
/// # });
/// ```
pub fn selected(self, selected: bool) -> Self {
self.inner.set_selected(selected);
self
}
/// Sets whether the node is disabled.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.button("save", "Save").disabled(true);
/// # });
/// ```
pub fn disabled(self, disabled: bool) -> Self {
if disabled {
self.inner.set_disabled();
} else {
self.inner.clear_disabled();
}
self
}
/// Sets whether the node is expanded.
///
/// # Examples
///
/// ```
/// # use molerat::{Molerat, accesskit::Role};
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.node("settings", Role::TreeItem).expanded(true);
/// # });
/// ```
pub fn expanded(self, expanded: bool) -> Self {
self.inner.set_expanded(expanded);
self
}
/// Sets whether the node and its descendants are hidden.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.group("advanced", "Advanced settings", |_| {}).hidden(true);
/// # });
/// ```
pub fn hidden(self, hidden: bool) -> Self {
if hidden {
self.inner.set_hidden();
} else {
self.inner.clear_hidden();
}
self
}
/// Sets whether the node has semantic focus.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.button("save", "Save").focused(true);
/// # });
/// ```
pub fn focused(self, focused: bool) -> Self {
if focused {
assert!(
self.focus.is_none() || *self.focus == Some(self.id),
"only one semantic node can be focused"
);
*self.focus = Some(self.id);
} else if *self.focus == Some(self.id) {
*self.focus = None;
}
self
}
/// Sets the node's current numeric value.
///
/// # Examples
///
/// ```
/// # use molerat::{Molerat, accesskit::Role};
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.node("cpu", Role::ProgressIndicator).numeric_value(42.0);
/// # });
/// ```
pub fn numeric_value(self, value: f64) -> Self {
self.inner.set_numeric_value(value);
self
}
/// Sets the minimum and maximum numeric values for the node.
///
/// # Examples
///
/// ```
/// # use molerat::{Molerat, accesskit::Role};
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.node("cpu", Role::ProgressIndicator)
/// .numeric_range(0.0, 100.0)
/// .numeric_value(42.0);
/// # });
/// ```
pub fn numeric_range(self, minimum: f64, maximum: f64) -> Self {
self.inner.set_min_numeric_value(minimum);
self.inner.set_max_numeric_value(maximum);
self
}
/// Registers a click handler and maps it to an application message.
///
/// This is a convenience wrapper around [`Node::on_action`] for [`Action::Click`].
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # enum Message { Save }
/// # let mut molerat = Molerat::<Message>::new("example");
/// # molerat.publish(|ui| {
/// ui.button("save", "Save").on_click(|| Message::Save);
/// # });
/// ```
pub fn on_click(self, handler: impl Fn() -> M + 'static) -> Self {
self.on_action(Action::Click, move |_| handler())
}
/// Registers a focus handler and maps it to an application message.
///
/// This is a convenience wrapper around [`Node::on_action`] for [`Action::Focus`].
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # enum Message { SelectRecipe }
/// # let mut molerat = Molerat::<Message>::new("example");
/// # molerat.publish(|ui| {
/// # ui.list_box("recipes", "Recipes", |ui| {
/// ui.list_box_option("ratatouille", "Ratatouille")
/// .on_focus(|| Message::SelectRecipe);
/// # });
/// # });
/// ```
pub fn on_focus(self, handler: impl Fn() -> M + 'static) -> Self {
self.on_action(Action::Focus, move |_| handler())
}
/// Registers an action supported by this node and maps it to an application message.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// use molerat::accesskit::Action;
/// # enum Message { Save }
/// # let mut molerat = Molerat::<Message>::new("example");
/// # molerat.publish(|ui| {
/// ui.button("save", "Save")
/// .on_action(Action::Click, |_| Message::Save);
/// # });
/// ```
pub fn on_action(
self,
action: Action,
handler: impl Fn(&ActionRequest) -> M + 'static,
) -> Self {
self.actions.register(self.id, action, handler);
self.inner.add_action(action);
self
}
/// Registers an application-defined action supported by this node.
///
/// `key` preserves the action's numeric AccessKit ID across publications, `description` is
/// exposed to consumers, and `handler` maps requests for the action to application messages.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// # enum Message { Refresh }
/// # let mut molerat = Molerat::<Message>::new("example");
/// # molerat.publish(|ui| {
/// ui.button("refresh", "Refresh")
/// .custom_action("force-refresh", "Force refresh", || Message::Refresh);
/// # });
/// ```
pub fn custom_action(
self,
key: impl Into<String>,
description: impl Into<String>,
handler: impl Fn() -> M + 'static,
) -> Self {
let id = self.actions.register_custom(self.id, key, handler);
self.inner.push_custom_action(AccessCustomAction {
id,
description: description.into(),
});
self
}
/// Applies raw AccessKit configuration to the node.
///
/// # Examples
///
/// ```
/// # use molerat::Molerat;
/// use molerat::accesskit::Live;
/// # let mut molerat: Molerat = Molerat::new("example");
/// # molerat.publish(|ui| {
/// ui.text("status", "Build complete")
/// .with_accesskit(|node| node.set_live(Live::Polite));
/// # });
/// ```
pub fn with_accesskit(self, configure: impl FnOnce(&mut AccessNode)) -> Self {
configure(self.inner);
self
}
}
#[cfg(test)]
mod tests {
use crate::Molerat;
use accesskit::{NodeId, Role, Toggled, TreeId, TreeInfo};
const ROOT_ID: NodeId = NodeId(0);
#[test]
fn publishes_a_generic_node() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.node("details", Role::Paragraph)
.label("Details")
.value("Three tasks are overdue")
.description("Task summary");
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
assert_eq!(update.tree, Some(TreeInfo::new(ROOT_ID)));
assert_eq!(update.tree_id, TreeId::ROOT);
assert_eq!(update.focus, ROOT_ID);
assert_eq!(update.nodes.len(), 2);
let (root_id, root) = &update.nodes[0];
let (paragraph_id, paragraph) = &update.nodes[1];
assert_eq!(*root_id, ROOT_ID);
assert_eq!(root.role(), Role::Window);
assert_eq!(root.label(), Some("example"));
assert_eq!(root.children(), [*paragraph_id]);
assert_eq!(paragraph.role(), Role::Paragraph);
assert_eq!(paragraph.label(), Some("Details"));
assert_eq!(paragraph.value(), Some("Three tasks are overdue"));
assert_eq!(paragraph.description(), Some("Task summary"));
}
#[test]
fn publishes_leaf_components() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.paragraph("details", "Details");
ui.text("status", "Ready");
ui.button("save", "Save");
ui.text_input("search", "Search", "molerat");
ui.checkbox("enabled", "Enabled", true);
ui.progress("download", "Download", 42.0, 0.0, 100.0);
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
assert_eq!(update.nodes[1].1.role(), Role::Paragraph);
assert_eq!(update.nodes[1].1.value(), Some("Details"));
assert_eq!(update.nodes[2].1.role(), Role::Label);
assert_eq!(update.nodes[2].1.value(), Some("Ready"));
assert_eq!(update.nodes[3].1.role(), Role::Button);
assert_eq!(update.nodes[3].1.label(), Some("Save"));
assert_eq!(update.nodes[4].1.role(), Role::TextInput);
assert_eq!(update.nodes[4].1.label(), Some("Search"));
assert_eq!(update.nodes[4].1.value(), Some("molerat"));
assert_eq!(update.nodes[5].1.role(), Role::CheckBox);
assert_eq!(update.nodes[5].1.toggled(), Some(Toggled::True));
assert_eq!(update.nodes[6].1.role(), Role::ProgressIndicator);
assert_eq!(update.nodes[6].1.numeric_value(), Some(42.0));
assert_eq!(update.nodes[6].1.min_numeric_value(), Some(0.0));
assert_eq!(update.nodes[6].1.max_numeric_value(), Some(100.0));
}
#[test]
fn publishes_live_status_and_alert_messages() {
use accesskit::Live;
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.status("status", "Build complete");
ui.alert("temperature", "CPU temperature is critically high");
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
let status = &update.nodes[1].1;
let alert = &update.nodes[2].1;
assert_eq!(status.role(), Role::Status);
assert_eq!(status.label(), Some("Build complete"));
assert_eq!(status.live(), Some(Live::Polite));
assert_eq!(alert.role(), Role::Alert);
assert_eq!(alert.label(), Some("CPU temperature is critically high"));
assert_eq!(alert.live(), Some(Live::Assertive));
}
#[test]
fn publishes_a_nested_list() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.group("main", "Main", |ui| {
ui.list("tasks", "Tasks", |ui| {
ui.list_item("first", "First task");
ui.list_item("second", "Second task");
});
});
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
let (root_id, root) = &update.nodes[0];
let (group_id, group) = &update.nodes[1];
let (list_id, list) = &update.nodes[2];
let (first_id, first) = &update.nodes[3];
let (second_id, second) = &update.nodes[4];
assert_eq!(*root_id, ROOT_ID);
assert_eq!(root.children(), [*group_id]);
assert_eq!(group.role(), Role::Group);
assert_eq!(group.label(), Some("Main"));
assert_eq!(group.children(), [*list_id]);
assert_eq!(list.role(), Role::List);
assert_eq!(list.label(), Some("Tasks"));
assert_eq!(list.children(), [*first_id, *second_id]);
assert_eq!(first.role(), Role::ListItem);
assert_eq!(first.label(), Some("First task"));
assert_eq!(second.role(), Role::ListItem);
assert_eq!(second.label(), Some("Second task"));
}
#[test]
fn publishes_a_selectable_list_with_position_metadata() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.list_box("recipes", "Recipes", |ui| {
ui.list_box_option("first", "Ratatouille");
ui.list_box_option("second", "Confit byaldi");
});
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
let list = &update.nodes[1].1;
let first = &update.nodes[2].1;
let second = &update.nodes[3].1;
assert_eq!(list.role(), Role::ListBox);
assert_eq!(list.size_of_set(), Some(2));
assert_eq!(first.role(), Role::ListBoxOption);
assert_eq!(first.position_in_set(), Some(0));
assert_eq!(second.role(), Role::ListBoxOption);
assert_eq!(second.position_in_set(), Some(1));
}
#[test]
fn publishes_a_table_with_structural_metadata() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.table("processes", "Processes", |ui| {
ui.row("headers", |ui| {
ui.column_header("name", "Name");
ui.column_header("cpu", "CPU");
});
ui.row("molerat", |ui| {
ui.row_header("name", "molerat");
ui.cell("cpu", "18 percent");
});
});
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
let table = &update.nodes[1].1;
let header_row = &update.nodes[2].1;
let name_header = &update.nodes[3].1;
let cpu_header = &update.nodes[4].1;
let process_row = &update.nodes[5].1;
let process_name = &update.nodes[6].1;
let process_cpu = &update.nodes[7].1;
assert_eq!(table.role(), Role::Table);
assert_eq!(table.label(), Some("Processes"));
assert_eq!(table.row_count(), Some(2));
assert_eq!(table.column_count(), Some(2));
assert_eq!(header_row.role(), Role::Row);
assert_eq!(header_row.row_index(), Some(0));
assert_eq!(name_header.role(), Role::ColumnHeader);
assert_eq!(name_header.column_index(), Some(0));
assert_eq!(cpu_header.role(), Role::ColumnHeader);
assert_eq!(cpu_header.column_index(), Some(1));
assert_eq!(process_row.role(), Role::Row);
assert_eq!(process_row.row_index(), Some(1));
assert_eq!(process_name.role(), Role::RowHeader);
assert_eq!(process_name.column_index(), Some(0));
assert_eq!(process_cpu.role(), Role::Cell);
assert_eq!(process_cpu.column_index(), Some(1));
}
#[test]
fn publishes_node_state_and_focus() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.list("tasks", "Tasks", |ui| {
ui.list_item("first", "First task")
.selected(true)
.disabled(true)
.expanded(false)
.focused(true);
ui.list_item("hidden", "Hidden task").hidden(true);
});
});
let Some(update) = molerat.last_update() else {
panic!("a tree update should have been published");
};
let (item_id, item) = &update.nodes[2];
assert_eq!(update.focus, *item_id);
assert_eq!(item.is_selected(), Some(true));
assert!(item.is_disabled());
assert_eq!(item.is_expanded(), Some(false));
assert!(!item.is_hidden());
assert!(update.nodes[3].1.is_hidden());
}
#[test]
#[should_panic(expected = "only one semantic node can be focused")]
fn rejects_multiple_focused_nodes() {
let mut molerat: Molerat = Molerat::new("example");
molerat.publish(|ui| {
ui.button("first", "First").focused(true);
ui.button("second", "Second").focused(true);
});
}
}