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
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
//! Selecting text with the pointer: dragging, double clicks and
//! Shift+click.

use crabshell_reactive::Signal;

use super::{Counter, rect_at};
use super::{Harness, node_at};
use crate::{
  Button, Clipboard, Column, Content, CursorShape, Icon, Key, KeyEvent, Modifiers, Point,
  PointerButton, Rect, Row, ScrollView, Span, Style, Text, TextInput, Window,
};

/// Ctrl+C, and what it copied.
fn copied(window: &Window) -> Option<String> {
  let ctrl_c = KeyEvent {
    key: Key::Character('c'),
    text: None,
    modifiers: Modifiers {
      ctrl: true,
      ..Modifiers::default()
    },
    repeat: false,
  };
  match window.key(&ctrl_c) {
    Some(Clipboard::Copy(text)) => Some(text),
    _ => None,
  }
}

/// A 200 by 30 window with the keyboard and one 100-pixel field.
fn field(h: &mut Harness, text: Signal<String>) -> Window {
  let row = Row::new().child(TextInput::new(text).style(Style::new().width(100.0)));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  window.keyboard_enter();
  window
}

/// A left click at `x`, 10 down, going down at `time` and up 10 ms later.
fn click(window: &Window, x: f32, time: u32) {
  window.pointer_motion_at(Point::new(x, 10.0), time);
  window.pointer_button_at(PointerButton::Left, true, time);
  window.pointer_button_at(PointerButton::Left, false, time + 10);
}

#[test]
fn dragging_in_a_field_selects_to_the_pointer_even_outside_it() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("abcdef"));
  let window = field(&mut h, text);
  window.pointer_motion_at(Point::new(1.0, 10.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 0);
  assert_eq!(copied(&window), None, "a press only places the caret");
  // Past the field's right end, outside it.
  window.pointer_motion_at(Point::new(190.0, 25.0), 20);
  h.frame(&window);
  assert_eq!(copied(&window).as_deref(), Some("abcdef"));
  // Back to the start: the selection follows the pointer.
  window.pointer_motion_at(Point::new(1.0, 10.0), 40);
  assert_eq!(copied(&window), None);
  window.pointer_motion_at(Point::new(190.0, 10.0), 60);
  window.pointer_button_at(PointerButton::Left, false, 80);
  // The release keeps the selection, and moving after it changes nothing.
  window.pointer_motion_at(Point::new(1.0, 10.0), 100);
  assert_eq!(copied(&window).as_deref(), Some("abcdef"));
}

#[test]
fn dragging_a_long_field_scrolls_its_text() {
  let mut h = Harness::new();
  let text = h.cx.signal("w".repeat(60));
  let window = field(&mut h, text);
  // The caret starts at the end, so the field shows the end first.
  click(&window, 1.0, 0);
  window.key(&KeyEvent {
    key: Key::Home,
    text: None,
    modifiers: Modifiers::default(),
    repeat: false,
  });
  h.frame(&window);
  window.pointer_motion_at(Point::new(1.0, 10.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 1000);
  // Each move past the end selects further and the layout scrolls the
  // text after it, as the caret goes.
  for step in 1..40 {
    window.pointer_motion_at(Point::new(150.0, 10.0), 1000 + step * 10);
    h.frame(&window);
  }
  window.pointer_button_at(PointerButton::Left, false, 1500);
  assert_eq!(copied(&window), Some("w".repeat(60)));
}

#[test]
fn a_double_click_selects_the_word_under_the_pointer() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("one two"));
  let window = field(&mut h, text);
  // Find a place in "two".
  let x = (1..100)
    .map(|x| x as f32)
    .find(|&x| {
      click(&window, x, 10_000 * x as u32);
      window.field().is_some_and(|field| field.cursor == 5)
    })
    .expect("a place inside the second word");
  click(&window, x, 0);
  click(&window, x, 200);
  assert_eq!(copied(&window).as_deref(), Some("two"));
  // Too slow for a double click: the second click places the caret.
  click(&window, x, 1000);
  click(&window, x, 1600);
  assert_eq!(copied(&window), None);
}

#[test]
fn shift_click_extends_the_selection() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("abcdef"));
  let window = field(&mut h, text);
  click(&window, 1.0, 0);
  window.set_modifiers(Modifiers {
    shift: true,
    ..Modifiers::default()
  });
  click(&window, 99.0, 1000);
  assert_eq!(copied(&window).as_deref(), Some("abcdef"));
  window.set_modifiers(Modifiers::default());
  click(&window, 99.0, 2000);
  assert_eq!(copied(&window), None, "a plain click drops it");
}

#[test]
fn dragging_a_field_in_a_scroll_view_selects_instead_of_scrolling() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("abcdef"));
  let offset = h.cx.signal(0.0_f32);
  let content = Column::new()
    .child(TextInput::new(text).style(Style::new().width(100.0)))
    .child(Icon::new("x").size(200));
  let view = ScrollView::new(content).offset(offset);
  let window = h.window(h.panel().child(view), 200.0, 30.0);
  window.keyboard_enter();
  let field = node_at(window.view().root(), &[0, 0, 0]).rect();
  window.pointer_motion_at(Point::new(field.x + 1.0, field.y + 5.0), 0);
  window.pointer_button_at(PointerButton::Left, true, 0);
  window.pointer_motion_at(Point::new(field.x + 1.0, field.y + 25.0), 50);
  window.pointer_motion_at(Point::new(150.0, 29.0), 100);
  window.pointer_button_at(PointerButton::Left, false, 150);
  h.frame(&window);
  assert_eq!(offset.get(), 0.0);
  assert_eq!(copied(&window).as_deref(), Some("abcdef"));
}

/// A 200 by 30 window with the keyboard and `child` in a row.
fn label(h: &mut Harness, child: impl crate::Widget) -> Window {
  let window = h.window(h.panel().child(Row::new().child(child)), 200.0, 30.0);
  window.keyboard_enter();
  window
}

/// Drags the left button from `from` to `to`.
fn drag(window: &Window, from: Point, to: Point, time: u32) {
  window.pointer_motion_at(from, time);
  window.pointer_button_at(PointerButton::Left, true, time);
  window.pointer_motion_at(to, time + 10);
  window.pointer_button_at(PointerButton::Left, false, time + 20);
}

/// The selection the text at `path` draws.
fn selection(window: &Window, path: &[usize]) -> Vec<Rect> {
  match node_at(window.view().root(), path).content() {
    Content::Text(text) => text.selection(),
    _ => panic!("not a text"),
  }
}

#[test]
fn a_selectable_text_selects_by_dragging_and_copies_while_focused() {
  let mut h = Harness::new();
  let window = label(&mut h, Text::new("hello world").selectable(true));
  window.pointer_motion(Point::new(5.0, 10.0));
  assert_eq!(window.cursor(), CursorShape::Text);
  drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
  h.frame(&window);
  assert_eq!(
    window.focused().as_deref(),
    Some("PanelWindow[DP-1]/Row/Text[0]")
  );
  assert_eq!(copied(&window).as_deref(), Some("hello world"));
  let text = rect_at(&window, &[0, 0]);
  let shown = selection(&window, &[0, 0]);
  assert_eq!(shown.len(), 1);
  assert!(shown[0].width > text.width / 2.0, "{shown:?} in {text:?}");
}

#[test]
fn a_text_selects_nothing_unless_it_is_selectable() {
  let mut h = Harness::new();
  let on = h.cx.signal(false);
  let clicks = Counter::default();
  let c = clicks.clone();
  let button =
    Button::new(Text::new("hello").selectable(move || on.get())).on_click(move |_| c.hit());
  let window = label(&mut h, button);
  drag(&window, Point::new(1.0, 10.0), Point::new(3.0, 10.0), 0);
  assert_eq!(clicks.get(), 1, "the press goes to the button");
  assert_eq!(window.focused(), None);
  // Selectable, the text is the deepest target and takes the press.
  on.set(true);
  h.frame(&window);
  drag(
    &window,
    Point::new(1.0, 10.0),
    Point::new(190.0, 10.0),
    1000,
  );
  assert_eq!(clicks.get(), 1);
  assert_eq!(copied(&window).as_deref(), Some("hello"));
  // Turned off, it drops its selection and the focus.
  on.set(false);
  h.frame(&window);
  assert_eq!(window.focused(), None);
  assert!(selection(&window, &[0, 0, 0]).is_empty());
}

#[test]
fn a_press_elsewhere_or_a_new_text_drops_the_selection() {
  let mut h = Harness::new();
  let words = h.cx.signal(String::from("hello world"));
  let row = Row::new()
    .child(Text::new(move || words.get()).selectable(true))
    .child(Icon::new("x").size(20));
  let window = label(&mut h, row);
  let icon = rect_at(&window, &[0, 0, 1]);
  drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
  assert_eq!(copied(&window).as_deref(), Some("hello world"));
  window.pointer_motion_at(Point::new(icon.x + 5.0, 10.0), 1000);
  window.pointer_button_at(PointerButton::Left, true, 1000);
  window.pointer_button_at(PointerButton::Left, false, 1010);
  h.frame(&window);
  assert_eq!(window.focused(), None);
  assert!(selection(&window, &[0, 0, 0]).is_empty());

  drag(
    &window,
    Point::new(1.0, 10.0),
    Point::new(190.0, 10.0),
    2000,
  );
  // Longer, so the old selection's bytes would still be there.
  words.set(String::from("bye bye bye bye"));
  h.frame(&window);
  assert_eq!(copied(&window), None);
}

#[test]
fn tab_skips_a_selectable_text() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::new());
  let row = Row::new()
    .child(Text::new("label").selectable(true))
    .child(TextInput::new(text));
  let window = label(&mut h, row);
  window.key(&KeyEvent {
    key: Key::Tab,
    text: None,
    modifiers: Modifiers::default(),
    repeat: false,
  });
  assert_eq!(
    window.focused().as_deref(),
    Some("PanelWindow[DP-1]/Row/Row[0]/TextInput[1]")
  );
}

#[test]
fn a_selection_runs_across_the_lines_of_a_text() {
  let mut h = Harness::new();
  let window = label(&mut h, Text::new("one\ntwo").max_lines(2));
  // Not selectable: nothing to show.
  assert!(selection(&window, &[0, 0]).is_empty());
  let window = label(&mut h, Text::new("one\ntwo").max_lines(2).selectable(true));
  let text = rect_at(&window, &[0, 0]);
  drag(
    &window,
    Point::new(text.x + 1.0, text.y + 2.0),
    Point::new(190.0, text.bottom() - 2.0),
    0,
  );
  h.frame(&window);
  assert_eq!(copied(&window).as_deref(), Some("one\ntwo"));
  let shown = selection(&window, &[0, 0]);
  assert_eq!(shown.len(), 2, "{shown:?}");
  assert!(shown[1].y > shown[0].y);
  // A double click on the second line selects its word.
  let second = Point::new(text.x + 5.0, text.bottom() - 4.0);
  for time in [5000, 5100] {
    window.pointer_motion_at(second, time);
    window.pointer_button_at(PointerButton::Left, true, time);
    window.pointer_button_at(PointerButton::Left, false, time + 10);
  }
  assert_eq!(copied(&window).as_deref(), Some("two"));
  assert_eq!(selection(&window, &[0, 0]).len(), 1, "the first line isn't");
}

#[test]
fn shift_click_after_new_text_extends_from_its_start() {
  let mut h = Harness::new();
  let words = h.cx.signal(String::from("hello world"));
  let window = label(&mut h, Text::new(move || words.get()).selectable(true));
  drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
  assert_eq!(copied(&window).as_deref(), Some("hello world"));
  words.set(String::from("bye bye bye bye"));
  h.frame(&window);
  // Shift and a press at the start: nothing from the old caret on.
  window.set_modifiers(Modifiers {
    shift: true,
    ..Modifiers::default()
  });
  click(&window, 1.0, 1000);
  assert_eq!(copied(&window), None);
}

#[test]
fn a_double_click_in_a_password_selects_all_of_it() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("ab cd"));
  let row = Row::new().child(
    TextInput::new(text)
      .password(true)
      .style(Style::new().width(100.0)),
  );
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  window.keyboard_enter();
  // Past the dots: the last word, were it shown.
  click(&window, 99.0, 0);
  click(&window, 99.0, 100);
  window.key(&KeyEvent {
    key: Key::Character('x'),
    text: Some(String::from("x")),
    modifiers: Modifiers::default(),
    repeat: false,
  });
  assert_eq!(text.get(), "x", "where its spaces are stays hidden");
}

#[test]
fn a_span_change_that_keeps_the_text_keeps_the_selection() {
  let mut h = Harness::new();
  let bold = h.cx.signal(false);
  let words = h.cx.signal(String::from("world"));
  let spans = move || {
    let word = Span::new(words.get());
    vec![
      Span::new("hello "),
      if bold.get() { word.bold() } else { word },
    ]
  };
  let window = label(&mut h, Text::rich(spans).selectable(true));
  drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
  assert_eq!(copied(&window).as_deref(), Some("hello world"));
  bold.set(true);
  h.frame(&window);
  assert_eq!(copied(&window).as_deref(), Some("hello world"));
  assert_eq!(selection(&window, &[0, 0]).len(), 1);
  // New text, longer, so the old bytes would still be there.
  words.set(String::from("there, everyone"));
  h.frame(&window);
  assert_eq!(copied(&window), None);
}

/// Left presses at `at`, `times.len()` of them, each up 10 ms after it
/// went down; the button stays down after the last when `hold`.
fn presses(window: &Window, at: Point, times: &[u32], hold: bool) {
  for (i, &time) in times.iter().enumerate() {
    window.pointer_motion_at(at, time);
    window.pointer_button_at(PointerButton::Left, true, time);
    if !(hold && i == times.len() - 1) {
      window.pointer_button_at(PointerButton::Left, false, time + 10);
    }
  }
}

/// An x in the 100-pixel field of `field` where a click puts the caret
/// at byte `at`.
fn x_of(window: &Window, at: usize) -> f32 {
  (1..100)
    .map(|x| x as f32)
    .find(|&x| {
      click(window, x, 10_000 * x as u32);
      window.field().is_some_and(|field| field.cursor == at)
    })
    .expect("a place for the byte")
}

#[test]
fn a_triple_click_selects_the_whole_field_or_text_line() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("one two three"));
  let window = field(&mut h, text);
  let x = x_of(&window, 5);
  presses(&window, Point::new(x, 10.0), &[1_000_000, 1_000_100], false);
  assert_eq!(copied(&window).as_deref(), Some("two"));
  presses(&window, Point::new(x, 10.0), &[1_000_200], false);
  assert_eq!(copied(&window).as_deref(), Some("one two three"));
  // A fourth press starts again: it places the caret.
  presses(&window, Point::new(x, 10.0), &[1_000_300], false);
  assert_eq!(copied(&window), None);
  // Too slow between the second and third: a single click.
  presses(
    &window,
    Point::new(x, 10.0),
    &[2_000_000, 2_000_100, 2_000_600],
    false,
  );
  assert_eq!(copied(&window), None);

  // A text on two lines selects the line, without its break.
  let window = label(
    &mut h,
    Text::new("one two\nthree four")
      .max_lines(2)
      .selectable(true),
  );
  let rect = rect_at(&window, &[0, 0]);
  let second = Point::new(rect.x + 3.0, rect.bottom() - 4.0);
  presses(&window, second, &[0, 100, 200], false);
  assert_eq!(copied(&window).as_deref(), Some("three four"));
}

#[test]
fn a_drag_after_a_double_click_extends_by_whole_words() {
  let mut h = Harness::new();
  let text = h.cx.signal(String::from("one two three"));
  let window = field(&mut h, text);
  let (two, three, one) = (x_of(&window, 5), x_of(&window, 10), x_of(&window, 1));
  presses(
    &window,
    Point::new(two, 10.0),
    &[1_000_000, 1_000_100],
    true,
  );
  window.pointer_motion_at(Point::new(three, 10.0), 1_000_150);
  assert_eq!(copied(&window).as_deref(), Some("two three"));
  // Back before the first word: it stays selected, from its end.
  window.pointer_motion_at(Point::new(one, 10.0), 1_000_200);
  assert_eq!(copied(&window).as_deref(), Some("one two"));
  window.pointer_button_at(PointerButton::Left, false, 1_000_250);
  // A plain drag after that goes by characters again.
  window.pointer_motion_at(Point::new(two, 10.0), 2_000_000);
  window.pointer_button_at(PointerButton::Left, true, 2_000_000);
  window.pointer_motion_at(Point::new(three, 10.0), 2_000_050);
  window.pointer_button_at(PointerButton::Left, false, 2_000_100);
  assert_eq!(copied(&window).as_deref(), Some("wo th"));
}

#[test]
fn an_empty_line_inside_a_selection_shows_half_an_em() {
  let mut h = Harness::new();
  let window = label(
    &mut h,
    Text::new("one\n\ntwo").max_lines(3).selectable(true),
  );
  let text = rect_at(&window, &[0, 0]);
  let line = text.height / 3.0;
  drag(
    &window,
    Point::new(text.x, text.y + 2.0),
    Point::new(text.x, text.y + line + 2.0),
    0,
  );
  h.frame(&window);
  assert_eq!(copied(&window).as_deref(), Some("one\n"));
  assert_eq!(
    selection(&window, &[0, 0]).len(),
    1,
    "its break isn't selected"
  );
  drag(
    &window,
    Point::new(text.x, text.y + 2.0),
    Point::new(190.0, text.bottom() - 2.0),
    1000,
  );
  h.frame(&window);
  assert_eq!(copied(&window).as_deref(), Some("one\n\ntwo"));
  let shown = selection(&window, &[0, 0]);
  assert_eq!(shown.len(), 3, "{shown:?}");
  let first = shown[0];
  assert_eq!(
    shown[1],
    Rect::new(first.x, first.bottom(), 6.5, first.height)
  );
}

#[test]
fn a_triple_click_ends_a_line_at_every_break_cosmic_text_makes() {
  let mut h = Harness::new();
  for (text, lines) in [
    ("one\r\ntwo", ["one", "two"]),
    ("one\rtwo", ["one", "two"]),
    ("one\n\rtwo", ["one", "two"]),
  ] {
    let window = h.window(
      h.panel().child(
        Row::new()
          .align(crate::Align::Start)
          .child(Text::new(text).max_lines(2).selectable(true)),
      ),
      200.0,
      60.0,
    );
    window.keyboard_enter();
    let rect = rect_at(&window, &[0, 0]);
    let first = Point::new(rect.x + 3.0, rect.y + 4.0);
    let second = Point::new(rect.x + 3.0, rect.bottom() - 4.0);
    presses(&window, first, &[0, 100, 200], false);
    assert_eq!(copied(&window).as_deref(), Some(lines[0]), "{text:?}");
    presses(&window, second, &[1000, 1100, 1200], false);
    assert_eq!(copied(&window).as_deref(), Some(lines[1]), "{text:?}");
  }
}