crabshell-ui 0.8.0

Widgets, styles, layout, text and text editing, focus, hit testing and invalidation for Crabshell
Documentation
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
//! What a screen reader is shown: roles, names, states, children, clicks
//! and the events heard while one listens.

use std::rc::Rc;
use std::task::Waker;

use super::{Counter, Harness, capture_logs};
use crate::{
  AccessEvent, AccessRole, Accessibility, Button, FloatingWindow, Icon, Key, KeyEvent, Lock,
  Modifiers, Output, Row, SessionLock, Size, Slider, Spacer, Style, Text, TextInput, Window,
};

fn tab() -> KeyEvent {
  KeyEvent {
    key: Key::Tab,
    text: None,
    modifiers: Modifiers::default(),
    repeat: false,
  }
}

/// A harness whose root context has the registry, as the shell's does.
fn harness() -> (Harness, Rc<Accessibility>) {
  let harness = Harness::new();
  let access = harness.cx.provide(Accessibility::default());
  (harness, access)
}

/// The counter bar's row: a button with a label, a spacer and a text.
fn counter(harness: &mut Harness, clicks: &Counter) -> Window {
  let count = harness.cx.signal(0);
  let clicks = clicks.clone();
  let panel = harness.panel().child(
    Row::new()
      .child(
        Button::new(Text::new(move || format!("clicked {} times", count.get()))).on_click(
          move |_| {
            clicks.hit();
            count.update(|n| *n += 1);
          },
        ),
      )
      .child(Spacer::new())
      .child(Text::new("hello")),
  );
  harness.window(panel, 300.0, 30.0)
}

fn take(access: &Accessibility) -> Vec<AccessEvent> {
  access.take_events(Waker::noop())
}

#[test]
fn a_counter_bar_shows_its_button_by_role_and_name() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let _window = counter(&mut harness, &clicks);
  let windows = access.windows();
  assert_eq!(windows.len(), 1);
  let frame = access.node(windows[0]).expect("the window");
  assert_eq!(frame.role, AccessRole::Frame);
  assert_eq!(frame.name, "crabshell");
  assert_eq!(frame.parent, None);
  let row = access.node(frame.children[0]).expect("the row");
  assert_eq!(row.role, AccessRole::Filler);
  assert_eq!(row.parent, Some(windows[0]));
  let roles: Vec<_> = row
    .children
    .iter()
    .map(|&id| access.node(id).expect("a child").role)
    .collect();
  assert_eq!(
    roles,
    [AccessRole::Button, AccessRole::Filler, AccessRole::Label]
  );
  let button = access.node(row.children[0]).expect("the button");
  assert_eq!(button.name, "clicked 0 times");
  assert_eq!(button.index, 0);
  assert!(button.focusable && button.enabled && button.shown && button.clickable);
  assert!(!button.focused);
  assert!(button.rect.width > 0.0);
  let label = access.node(row.children[2]).expect("the label");
  assert_eq!((label.name.as_str(), label.index), ("hello", 2));
  assert!(!label.clickable && !label.focusable);
}

#[test]
fn accessible_name_wins_over_the_text_and_follows_its_closure() {
  let (mut harness, access) = harness();
  let name = harness.cx.signal(String::from("volume"));
  let panel = harness.panel().child(
    Row::new()
      .child(Button::new(Icon::new("audio")).accessible_name(move || name.get()))
      .child(Button::new(Text::new("text")).accessible_name("named")),
  );
  let window = harness.window(panel, 300.0, 30.0);
  let frame = access.node(access.windows()[0]).expect("the window");
  let row = access.node(frame.children[0]).expect("the row");
  let names = |access: &Accessibility| -> Vec<String> {
    let row = access.node(row.children[0]).expect("a row").parent;
    let row = access.node(row.expect("a parent")).expect("the row");
    row
      .children
      .iter()
      .map(|&id| access.node(id).expect("a button").name)
      .collect()
  };
  assert_eq!(names(&access), ["volume", "named"]);
  // Heard only while a screen reader listens.
  assert!(take(&access).is_empty());
  access.listen(true);
  name.set(String::from("muted"));
  harness.frame(&window);
  assert_eq!(names(&access), ["muted", "named"]);
  assert_eq!(take(&access), [AccessEvent::NameChanged(row.children[0])]);
}

#[test]
fn a_label_change_renames_the_button_around_it() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let window = counter(&mut harness, &clicks);
  access.listen(true);
  let frame = access.node(access.windows()[0]).expect("the window");
  let row = access.node(frame.children[0]).expect("the row");
  let button = row.children[0];
  let label = access.node(button).expect("the button").children[0];
  assert!(access.click(button));
  harness.frame(&window);
  assert_eq!(clicks.get(), 1);
  assert_eq!(
    access.node(button).expect("the button").name,
    "clicked 1 times"
  );
  assert_eq!(
    take(&access),
    [
      AccessEvent::NameChanged(label),
      AccessEvent::NameChanged(button)
    ]
  );
}

#[test]
fn a_click_needs_an_enabled_shown_button() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let (on, shown) = (harness.cx.signal(true), harness.cx.signal(true));
  let hit = clicks.clone();
  let panel = harness.panel().child(
    Row::new()
      .child(
        Button::new(Text::new("a"))
          .enabled(move || on.get())
          .visible(move || shown.get())
          .on_click(move |_| hit.hit()),
      )
      .child(Text::new("b")),
  );
  let window = harness.window(panel, 300.0, 30.0);
  let frame = access.node(access.windows()[0]).expect("the window");
  let row = access.node(frame.children[0]).expect("the row");
  let (button, text) = (row.children[0], row.children[1]);
  assert!(!access.click(text), "a text has no action");
  on.set(false);
  harness.frame(&window);
  let state = access.node(button).expect("the button");
  assert!(!state.enabled && !state.focusable);
  assert!(!access.click(button));
  on.set(true);
  shown.set(false);
  harness.frame(&window);
  assert!(!access.click(button));
  // A hidden widget stays among its parent's children, and doesn't show.
  let parent = access.node(text).expect("the text").parent;
  let row = access.node(parent.expect("a parent")).expect("the row");
  assert_eq!(row.children, [button, text]);
  assert!(!access.node(button).expect("the button").shown);
  assert_eq!(clicks.get(), 0);
}

#[test]
fn showing_and_turning_a_button_on_are_heard() {
  let (mut harness, access) = harness();
  let (on, shown, label) = (
    harness.cx.signal(true),
    harness.cx.signal(true),
    harness.cx.signal(true),
  );
  let panel = harness.panel().child(
    Row::new().child(
      Button::new(
        Row::new()
          .child(Text::new("mute"))
          .child(Text::new("now").visible(move || label.get())),
      )
      .enabled(move || on.get())
      .visible(move || shown.get()),
    ),
  );
  let window = harness.window(panel, 300.0, 30.0);
  let row = access
    .node(access.windows()[0])
    .expect("the window")
    .children[0];
  let button = access.node(row).expect("the row").children[0];
  assert_eq!(access.node(button).expect("the button").name, "mute now");
  access.listen(true);
  on.set(false);
  harness.frame(&window);
  shown.set(false);
  harness.frame(&window);
  shown.set(true);
  harness.frame(&window);
  label.set(false);
  harness.frame(&window);
  let inner = access.node(button).expect("the button").children[0];
  let now = access.node(inner).expect("the labels").children[1];
  assert_eq!(
    take(&access),
    [
      AccessEvent::Enabled(button, false),
      AccessEvent::Shown(button, false),
      AccessEvent::Shown(button, true),
      AccessEvent::Shown(now, false),
      AccessEvent::NameChanged(button),
    ]
  );
  assert_eq!(access.node(button).expect("the button").name, "mute");
}

#[test]
fn keyed_children_coming_and_going_are_heard_once_laid_out() {
  let (mut harness, access) = harness();
  let items = harness.cx.signal(vec![1, 2]);
  let panel = harness.panel().child(Row::new().children_keyed(
    move || items.get(),
    |n| *n,
    |_, n| Text::new(move || n.get().to_string()),
  ));
  // Built while a screen reader listens: only the window is news.
  access.listen(true);
  let window = harness.window(panel, 300.0, 30.0);
  assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
  let row = access
    .node(access.windows()[0])
    .expect("the window")
    .children[0];
  let before = access.node(row).expect("the row").children;
  items.set(vec![2, 3]);
  harness.frame(&window);
  let after = access.node(row).expect("the row").children;
  assert_eq!(after[0], before[1]);
  assert_eq!(
    take(&access),
    [
      AccessEvent::Children {
        parent: row,
        child: before[0],
        index: 0,
        added: false
      },
      AccessEvent::Children {
        parent: row,
        child: after[1],
        index: 1,
        added: true
      },
    ]
  );
  drop(window);
  assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
}

#[test]
fn a_floating_window_title_and_a_placeholder_rename_them() {
  let (mut harness, access) = harness();
  let (title, shown) = (
    harness.cx.signal(String::from("About")),
    harness.cx.signal(true),
  );
  let placeholder = harness.cx.signal(String::from("search"));
  let window = Window::floating(
    &harness.cx,
    FloatingWindow::new()
      .title(move || title.get())
      .visible(move || shown.get())
      .child(
        TextInput::new(harness.cx.signal(String::new())).placeholder(move || placeholder.get()),
      ),
  );
  window.set_size(Size::new(200.0, 100.0));
  harness.frame(&window);
  let root = access.windows()[0];
  let field = access.node(root).expect("the window").children[0];
  assert_eq!(access.node(root).expect("the window").name, "About");
  access.listen(true);
  title.set(String::from("Settings"));
  placeholder.set(String::from("find"));
  harness.frame(&window);
  assert_eq!(
    take(&access),
    [
      AccessEvent::NameChanged(root),
      AccessEvent::NameChanged(field)
    ]
  );
  assert_eq!(access.node(field).expect("the field").name, "find");
  // Hidden, the window is unknown, and comes back with its parts.
  shown.set(false);
  harness.frame(&window);
  assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
  assert!(access.windows().is_empty());
  assert!(access.node(root).is_none() && access.node(field).is_none());
  shown.set(true);
  harness.frame(&window);
  assert_eq!(access.windows(), [root]);
}

#[test]
fn while_locked_only_the_lock_is_read_or_clicked() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let window = counter(&mut harness, &clicks);
  let panel = access.windows()[0];
  let button = access
    .node(access.node(panel).expect("the panel").children[0])
    .expect("the row")
    .children[0];
  access.listen(true);
  let lock = Lock::new(
    &harness.cx,
    SessionLock::new().surface(|_, _| Text::new("locked")),
  );
  let surface = lock.surface(&Output::new("DP-1")).expect("the lock lives");
  surface.set_size(Size::new(300.0, 30.0));
  harness.frame(&surface);
  assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
  let listed = access.windows();
  assert_eq!(listed.len(), 1);
  assert_eq!(
    access.node(listed[0]).expect("the lock").role,
    AccessRole::Frame
  );
  assert!(access.node(panel).is_none() && access.node(button).is_none());
  assert!(!access.click(button), "clicked behind the lock");
  harness.frame(&window);
  assert_eq!(clicks.get(), 0);
  drop(surface);
  assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
  assert_eq!(access.windows(), [panel]);
  assert!(access.click(button));
  assert_eq!(clicks.get(), 1);
}

#[test]
fn tab_and_the_keyboard_coming_and_going_are_heard() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let window = counter(&mut harness, &clicks);
  let root = access.windows()[0];
  let button = access
    .node(access.node(root).expect("the window").children[0])
    .expect("the row")
    .children[0];
  // Nothing is kept while nobody listens.
  window.keyboard_enter();
  window.key(&tab());
  assert!(take(&access).is_empty());
  window.keyboard_leave();
  access.listen(true);
  window.keyboard_enter();
  assert_eq!(
    take(&access),
    [
      AccessEvent::Active(root, true),
      AccessEvent::Focused(button, true)
    ]
  );
  assert!(access.node(button).expect("the button").focused);
  assert!(access.node(root).expect("the window").active);
  window.key(&tab());
  assert!(take(&access).is_empty(), "Tab stays on the only button");
  window.keyboard_leave();
  assert_eq!(
    take(&access),
    [
      AccessEvent::Focused(button, false),
      AccessEvent::Active(root, false)
    ]
  );
  // What nobody took goes when the screen reader does.
  window.keyboard_enter();
  access.listen(false);
  assert!(take(&access).is_empty());
}

#[test]
fn a_slider_is_one_object_that_takes_the_focus() {
  let (mut harness, access) = harness();
  let value = harness.cx.signal(0.5);
  let panel = harness.panel().child(
    Row::new()
      .child(Slider::new(value).accessible_name("brightness"))
      .child(TextInput::new(harness.cx.signal(String::new())).placeholder("search")),
  );
  let window = harness.window(panel, 300.0, 30.0);
  access.listen(true);
  let row = access
    .node(
      access
        .node(access.windows()[0])
        .expect("the window")
        .children[0],
    )
    .expect("the row");
  let slider = access.node(row.children[0]).expect("the slider");
  assert_eq!(slider.role, AccessRole::Slider);
  assert_eq!(slider.name, "brightness");
  assert!(slider.children.is_empty() && slider.focusable);
  let field = access.node(row.children[1]).expect("the field");
  assert_eq!(
    (field.role, field.name.as_str()),
    (AccessRole::Entry, "search")
  );
  assert!(field.editable && !field.multi_line);
  window.keyboard_enter();
  take(&access);
  window.key(&tab());
  assert_eq!(take(&access), [AccessEvent::Focused(row.children[0], true)]);
}

#[test]
fn a_window_leaves_the_list_when_dropped() {
  let (mut harness, access) = harness();
  let clicks = Counter::default();
  let window = counter(&mut harness, &clicks);
  let root = access.windows()[0];
  drop(window);
  assert!(access.windows().is_empty());
  assert!(access.node(root).is_none());
  assert!(!access.click(root));
}

#[test]
fn a_panicking_accessible_name_is_logged_with_its_path() {
  let (mut harness, _access) = harness();
  let logs = capture_logs(|| {
    let panel = harness.panel().child(
      Row::new().child(
        Button::new(Text::new("ok"))
          .style(Style::new())
          .accessible_name(|| -> String { panic!("no name") }),
      ),
    );
    let _window = harness.window(panel, 300.0, 30.0);
  });
  assert!(
    logs.contains(
      "PanelWindow[DP-1]/Row/Button[0].accessible_name panicked and is disabled: no name"
    ),
    "{logs}"
  );
}