crabshell-ui 0.7.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
465
466
467
468
469
470
471
//! Multi-line text areas: lines, wrapping, moving by line and page, Enter
//! and Ctrl+Enter, scrolling the caret into view and input methods.

use crabshell_reactive::Signal;

use super::{Harness, capture_logs, node_at, rect_at};
use crate::{
  Clipboard, Column, Content, Key, KeyEvent, Modifiers, Point, PointerButton, Rect, Row,
  ScrollView, Style, TextArea, Window,
};

/// The default font's line height.
const LINE: f32 = 16.0;

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

fn typed(text: &str) -> KeyEvent {
  KeyEvent {
    text: Some(text.to_owned()),
    ..key(Key::Character(text.chars().next().unwrap_or(' ')))
  }
}

fn with(key_: Key, modifiers: Modifiers) -> KeyEvent {
  KeyEvent {
    modifiers,
    ..key(key_)
  }
}

const CTRL: Modifiers = Modifiers {
  ctrl: true,
  alt: false,
  shift: false,
  logo: false,
};

/// A 300 by 200 window with the keyboard and a focused area editing
/// `text`, 100 wide unless `area` says otherwise.
fn area(h: &mut Harness, text: Signal<String>, area: impl FnOnce(TextArea) -> TextArea) -> Window {
  let column = Column::new().child(area(
    TextArea::new(text)
      .focus(true)
      .style(Style::new().width(100.0)),
  ));
  let window = h.window(h.panel().child(column), 300.0, 200.0);
  window.keyboard_enter();
  h.frame(&window);
  window
}

/// The area's rectangle and its caret.
fn shown(window: &Window) -> (Rect, Rect) {
  let view = window.view();
  let node = node_at(view.root(), &[0, 0]);
  match node.content() {
    Content::Input(input) => (node.rect(), input.caret.expect("a caret")),
    _ => panic!("not an area"),
  }
}

/// Presses `key` and returns where the caret is in the text.
fn press(h: &mut Harness, window: &Window, event: KeyEvent) -> usize {
  window.key(&event);
  h.frame(window);
  window.field().expect("the focused area").cursor
}

#[test]
fn enter_breaks_a_line_and_the_area_is_as_tall_as_its_lines() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::new());
  let window = area(&mut h, text, |area| area);
  assert_eq!(shown(&window).0.height, LINE, "one line when empty");
  window.key(&typed("a"));
  window.key(&key(Key::Enter));
  window.key(&typed("b"));
  h.frame(&window);
  assert_eq!(text.get(), "a\nb");
  let (rect, caret) = shown(&window);
  assert_eq!(rect.height, 2.0 * LINE);
  assert_eq!(caret.y, rect.y + LINE, "the caret is on the second line");
  text.set(String::from("a\r\nb\nc"));
  h.frame(&window);
  assert_eq!(shown(&window).0.height, 3.0 * LINE);
  assert_eq!(window.field().expect("a field").text, "a\nb\nc");
}

#[test]
fn long_lines_wrap_at_the_width_unless_wrap_is_off() {
  let mut h = Harness::new();
  let words = "word ".repeat(20);
  let text = h.cx.signal(words.clone());
  let window = area(&mut h, text, |area| area);
  let (rect, _) = shown(&window);
  assert_eq!(rect.width, 100.0);
  assert!(rect.height > 3.0 * LINE, "{rect:?}");

  // In a row, which doesn't stretch it, it takes the default width.
  let text = h.cx.signal(words.clone());
  let row = Row::new().child(TextArea::new(text));
  let window = h.window(h.panel().child(row), 300.0, 200.0);
  assert_eq!(rect_at(&window, &[0, 0]).width, 200.0);

  let text = h.cx.signal(words);
  let window = area(&mut h, text, |area| area.wrap(false).style(Style::new()));
  let (rect, _) = shown(&window);
  assert_eq!(rect.height, LINE);
  assert!(rect.width > 200.0, "as wide as its line: {rect:?}");
}

#[test]
fn up_and_down_move_by_line_and_keep_the_caret_across() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("abcdef\nab\nabcdef"));
  let window = area(&mut h, text, |area| area);
  press(&mut h, &window, with(Key::Home, CTRL));
  assert_eq!(press(&mut h, &window, key(Key::End)), 6);
  assert_eq!(
    press(&mut h, &window, key(Key::Down)),
    9,
    "the short line's end"
  );
  assert_eq!(press(&mut h, &window, key(Key::Down)), 16, "back across");
  assert_eq!(press(&mut h, &window, key(Key::Up)), 9);
  assert_eq!(press(&mut h, &window, key(Key::Up)), 6, "still across");
  assert_eq!(
    press(&mut h, &window, key(Key::Up)),
    0,
    "above the first line"
  );
  // Shift selects as it goes, still across.
  let shift = Modifiers {
    shift: true,
    ..Modifiers::default()
  };
  press(&mut h, &window, with(Key::Down, shift));
  let state = window.field().expect("a field");
  assert_eq!((state.anchor, state.cursor), (0, 9));
}

#[test]
fn home_and_end_reach_the_ends_of_a_wrapped_line_and_ctrl_the_text() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("aaaa bbbb"));
  let window = area(&mut h, text, |area| area.style(Style::new().width(45.0)));
  let (rect, caret) = shown(&window);
  assert_eq!(rect.height, 2.0 * LINE, "one word a line");
  assert_eq!(caret.y, rect.y + LINE);
  assert_eq!(press(&mut h, &window, key(Key::Home)), 5);
  assert_eq!(press(&mut h, &window, key(Key::Up)), 0);
  // The space the line wraps at hangs past its end.
  assert_eq!(press(&mut h, &window, key(Key::End)), 4);
  let (rect, caret) = shown(&window);
  assert_eq!(caret.y, rect.y);
  assert!(caret.x > rect.x + 20.0, "{caret:?}");
  assert_eq!(press(&mut h, &window, with(Key::End, CTRL)), 9);
  assert_eq!(press(&mut h, &window, with(Key::Home, CTRL)), 0);

  // A word too long for a line breaks inside it: the end of the first
  // line is the start of the second, and the caret stays on the first.
  text.set("a".repeat(12));
  h.frame(&window);
  press(&mut h, &window, with(Key::Home, CTRL));
  let end = press(&mut h, &window, key(Key::End));
  assert!(end > 0 && end < 12, "{end}");
  let (rect, caret) = shown(&window);
  assert_eq!(caret.y, rect.y);
  // A click past the end of the first line puts it there too, unlike
  // Home on the second, which reaches the same byte.
  press(&mut h, &window, key(Key::Down));
  assert_eq!(press(&mut h, &window, key(Key::Home)), end);
  assert_eq!(shown(&window).1.y, rect.y + LINE);
  window.pointer_motion_at(Point::new(rect.right() - 1.0, rect.y + 4.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 0);
  window.pointer_button_at(PointerButton::Left, false, 10);
  h.frame(&window);
  assert_eq!(window.field().expect("a field").cursor, end);
  assert_eq!(shown(&window).1.y, rect.y);
}

#[test]
fn page_keys_move_ten_lines_or_a_scroll_view_high() {
  let mut h = Harness::new();
  let lines: Vec<String> = (0..30).map(|n| format!("{n:02}")).collect();
  let lines = lines.join("\n");
  let text = h.cx.signal(lines.clone());
  let window = area(&mut h, text, |area| area);
  press(&mut h, &window, with(Key::Home, CTRL));
  // Each line is three bytes with its break.
  assert_eq!(press(&mut h, &window, key(Key::PageDown)), 30);
  assert_eq!(press(&mut h, &window, key(Key::PageUp)), 0);

  let text = h.cx.signal(lines);
  let view = ScrollView::new(
    TextArea::new(text)
      .focus(true)
      .style(Style::new().width(100.0)),
  )
  .style(Style::new().height(3.0 * LINE));
  let window = h.window(h.panel().child(Column::new().child(view)), 300.0, 200.0);
  window.keyboard_enter();
  press(&mut h, &window, with(Key::Home, CTRL));
  assert_eq!(press(&mut h, &window, key(Key::PageDown)), 9);
}

#[test]
fn the_caret_leaving_a_scroll_view_scrolls_it_the_least_that_shows_it() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("1\n2\n3\n4\n5"));
  let offset = h.cx.signal(0.0_f32);
  let view = ScrollView::new(
    TextArea::new(text)
      .focus(true)
      .style(Style::new().width(100.0)),
  )
  .offset(offset)
  .style(Style::new().height(2.0 * LINE));
  let window = h.window(h.panel().child(Column::new().child(view)), 300.0, 200.0);
  window.keyboard_enter();
  window.key(&with(Key::Home, CTRL));
  h.frame(&window);
  assert_eq!(offset.get(), 0.0);
  window.key(&with(Key::End, CTRL));
  // One layout scrolls it, with no frame showing the caret outside.
  h.frame(&window);
  assert_eq!(offset.get(), 3.0 * LINE);
  let view = node_at(window.view().root(), &[0, 0]).rect();
  let caret = match node_at(window.view().root(), &[0, 0, 0]).content() {
    Content::Input(input) => input.caret.expect("a caret"),
    _ => panic!("not an area"),
  };
  assert_eq!(caret.bottom(), view.bottom());
  window.key(&key(Key::Up));
  h.frame(&window);
  assert_eq!(offset.get(), 3.0 * LINE, "still in view: nothing scrolls");
  // Up to the second line, which starts 16 down.
  window.key(&key(Key::Up));
  window.key(&key(Key::Up));
  h.frame(&window);
  assert_eq!(offset.get(), LINE);
  assert!(!window.needs_layout());
}

#[test]
fn ctrl_enter_runs_on_accept_and_a_panic_there_is_logged_with_its_path() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("a"));
  let window = area(&mut h, text, |area| {
    area.on_accept(|_, text| panic!("accept {text}"))
  });
  let logs = capture_logs(|| {
    window.key(&with(Key::Enter, CTRL));
  });
  assert!(
    logs.contains(
      "handler PanelWindow[DP-1]/Column/TextArea[0].on_accept panicked and is disabled: accept a"
    ),
    "{logs}"
  );
  window.key(&key(Key::Enter));
  assert_eq!(text.get(), "a\n", "the area still edits");
}

#[test]
fn input_methods_and_pastes_keep_line_breaks_and_dragging_selects_lines() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::new());
  let window = area(&mut h, text, |area| area);
  assert!(window.field().expect("a field").multiline);
  window.commit_text("a\nb");
  window.paste("\r\nc");
  h.frame(&window);
  assert_eq!(text.get(), "a\nb\nc");
  let (rect, _) = shown(&window);
  window.pointer_motion_at(Point::new(rect.x, rect.y + 2.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 0);
  window.pointer_motion_at(Point::new(rect.right(), rect.y + LINE + 2.0), 10);
  window.pointer_button_at(PointerButton::Left, false, 20);
  h.frame(&window);
  let copied = window.key(&KeyEvent {
    modifiers: CTRL,
    ..typed("c")
  });
  assert_eq!(copied, Some(Clipboard::Copy(String::from("a\nb"))));
  let selection = match node_at(window.view().root(), &[0, 0]).content() {
    Content::Input(input) => input.selection.to_vec(),
    _ => panic!("not an area"),
  };
  assert_eq!(selection.len(), 2, "{selection:?}");
}

#[test]
fn dropping_an_area_frees_what_it_made() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::new());
  let before = h.runtime.node_count();
  let window = area(&mut h, text, |area| area.on_accept(|_, _| {}));
  drop(window);
  assert_eq!(h.runtime.node_count(), before);
}

#[test]
fn other_moves_forget_the_end_of_a_wrapped_line_and_the_place_across() {
  let mut h = Harness::new();
  let text = h.cx.signal("a".repeat(12));
  let window = area(&mut h, text, |area| area.style(Style::new().width(45.0)));
  press(&mut h, &window, with(Key::Home, CTRL));
  let end = press(&mut h, &window, key(Key::End));
  press(&mut h, &window, key(Key::Right));
  // Back at the same byte by an arrow: the start of the second line.
  assert_eq!(press(&mut h, &window, key(Key::Left)), end);
  let (rect, caret) = shown(&window);
  assert_eq!(caret.y, rect.y + LINE);

  let text = h.cx.signal(String::from("abcdef\nab\nabcdef"));
  let window = area(&mut h, text, |area| area);
  press(&mut h, &window, with(Key::Home, CTRL));
  press(&mut h, &window, key(Key::End));
  assert_eq!(press(&mut h, &window, key(Key::Down)), 9);
  press(&mut h, &window, key(Key::Left));
  press(&mut h, &window, key(Key::Right));
  // From where the arrows left it, not from the first line's end.
  assert_eq!(press(&mut h, &window, key(Key::Down)), 12);

  // A click at the same byte forgets it too.
  press(&mut h, &window, with(Key::Home, CTRL));
  press(&mut h, &window, key(Key::End));
  assert_eq!(press(&mut h, &window, key(Key::Down)), 9);
  let (_, caret) = shown(&window);
  window.pointer_motion_at(Point::new(caret.x + 30.0, caret.y + 4.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 0);
  window.pointer_button_at(PointerButton::Left, false, 10);
  h.frame(&window);
  assert_eq!(window.field().expect("a field").cursor, 9);
  assert_eq!(press(&mut h, &window, key(Key::Down)), 12);
}

#[test]
fn focusing_a_field_below_a_scroll_views_end_scrolls_it_into_view() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("x"));
  let offset = h.cx.signal(0.0_f32);
  let content = Column::new()
    .child(crate::Icon::new("x").size(100))
    .child(TextArea::new(text));
  let view = ScrollView::new(content)
    .offset(offset)
    .style(Style::new().height(2.0 * LINE));
  let window = h.window(h.panel().child(Column::new().child(view)), 300.0, 200.0);
  window.keyboard_enter();
  window.key(&key(Key::Tab));
  h.frame(&window);
  // The area's one line ends 100 + 16 down, and the view is 32 high.
  assert_eq!(offset.get(), 100.0 + LINE - 2.0 * LINE);
}

#[test]
fn an_area_removed_after_an_edit_is_skipped_by_the_next_layout() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::new());
  let shown = h.cx.signal(true);
  let list = Column::new().children_keyed(
    move || if shown.get() { vec![()] } else { Vec::new() },
    |_| (),
    move |_, _| TextArea::new(text).focus(true),
  );
  let view = ScrollView::new(list).style(Style::new().height(2.0 * LINE));
  let window = h.window(h.panel().child(view), 300.0, 200.0);
  window.keyboard_enter();
  window.key(&typed("a"));
  shown.set(false);
  h.frame(&window);
  assert_eq!(window.focused(), None);
}

#[test]
fn tab_types_a_tab_and_shift_or_ctrl_tab_moves_the_focus() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("ab"));
  let before = h.cx.signal(String::new());
  let after = h.cx.signal(String::new());
  let column = Column::new()
    .child(crate::TextInput::new(before))
    .child(TextArea::new(text).focus(true))
    .child(crate::TextInput::new(after));
  let window = h.window(h.panel().child(column), 300.0, 200.0);
  window.keyboard_enter();
  h.frame(&window);
  window.key(&key(Key::Home));
  window.key(&key(Key::Tab));
  h.frame(&window);
  assert_eq!(text.get(), "\tab");
  // Drawn at four spaces' width.
  let caret = shown_at(&window, &[0, 1]).1;
  let field = window.field().expect("the area");
  assert_eq!(field.cursor, 1);
  let space = h.cx.measure_text("    ", crate::Font::DEFAULT).width;
  let area = rect_at(&window, &[0, 1]);
  assert!(
    (caret.x - area.x - space).abs() < 1.0,
    "{caret:?} in {area:?}, {space}"
  );
  let shift = Modifiers {
    shift: true,
    ..Modifiers::default()
  };
  window.key(&with(Key::Tab, shift));
  assert_eq!(
    window.focused().as_deref(),
    Some("PanelWindow[DP-1]/Column/TextInput[0]")
  );
  window.key(&key(Key::Tab));
  window.key(&with(Key::Tab, CTRL));
  assert_eq!(
    window.focused().as_deref(),
    Some("PanelWindow[DP-1]/Column/TextInput[2]")
  );
  assert_eq!(text.get(), "\tab");
  // A one-line field keeps Tab for the focus.
  window.key(&key(Key::Tab));
  assert_eq!(after.get(), "");
}

/// The rectangle and caret of the area at `path`.
fn shown_at(window: &Window, path: &[usize]) -> (Rect, Rect) {
  let view = window.view();
  let node = node_at(view.root(), path);
  match node.content() {
    Content::Input(input) => (node.rect(), input.caret.expect("a caret")),
    _ => panic!("not an area"),
  }
}

#[test]
fn a_triple_click_selects_a_line_and_a_drag_after_it_whole_lines() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("one two\nthree four\nfive"));
  let window = area(&mut h, text, |area| area);
  let (rect, _) = shown(&window);
  let line = |n: f32| Point::new(rect.x + 3.0, rect.y + LINE * n + 4.0);
  let copy = |window: &Window| match window.key(&KeyEvent {
    modifiers: CTRL,
    ..typed("c")
  }) {
    Some(Clipboard::Copy(text)) => text,
    _ => String::new(),
  };
  for (i, time) in [0, 100, 200].into_iter().enumerate() {
    window.pointer_motion_at(line(1.0), time);
    window.pointer_button_at(PointerButton::Left, true, time);
    if i < 2 {
      window.pointer_button_at(PointerButton::Left, false, time + 10);
    }
  }
  assert_eq!(copy(&window), "three four");
  window.pointer_motion_at(line(2.0), 250);
  assert_eq!(copy(&window), "three four\nfive");
  window.pointer_motion_at(line(0.0), 300);
  assert_eq!(copy(&window), "one two\nthree four");
  window.pointer_button_at(PointerButton::Left, false, 350);
  h.frame(&window);
  assert_eq!(copy(&window), "one two\nthree four");
}