crabshell-ui 0.4.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
//! Layout: rows, columns, stacks, spacers, buttons and the window itself.
//! Icons have exact sizes, so these tests can check exact rectangles.

use super::{Harness, node_at, rect_at};
use crate::text::TextNode;
use crate::{
  Align, Button, Column, Content, Fit, Icon, Image, Justify, Rect, Row, Spacer, Stack, Style, Text,
};

#[test]
fn the_window_child_fills_the_window() {
  let mut h = Harness::new();
  let window = h.window(h.panel().child(Row::new()), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[]), Rect::new(0.0, 0.0, 200.0, 30.0));
  assert_eq!(rect_at(&window, &[0]), Rect::new(0.0, 0.0, 200.0, 30.0));
}

#[test]
fn content_too_wide_overflows_inside_the_window_child() {
  let mut h = Harness::new();
  let row = Row::new()
    .child(Icon::new("a").size(150))
    .child(Spacer::new())
    .child(Icon::new("b").size(100));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  // The row stays the window's size instead of growing to its content.
  assert_eq!(rect_at(&window, &[0]), Rect::new(0.0, 0.0, 200.0, 30.0));
  assert_eq!(rect_at(&window, &[0, 1]).width, 0.0);
  assert_eq!(rect_at(&window, &[0, 2]).right(), 250.0);
}

#[test]
fn a_row_places_children_with_padding_gap_and_a_spacer() {
  let mut h = Harness::new();
  let row = Row::new()
    .padding(4, 8)
    .gap(6)
    .child(Icon::new("a").size(10))
    .child(Icon::new("b").size(12))
    .child(Spacer::new())
    .child(Icon::new("c").size(16));
  let window = h.window(h.panel().child(row), 200.0, 30.0);

  // Children stretch to the row's 22-pixel content height by default.
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(8.0, 4.0, 10.0, 22.0));
  assert_eq!(rect_at(&window, &[0, 1]), Rect::new(24.0, 4.0, 12.0, 22.0));
  // 184 wide inside the padding, minus 38 of icons and 18 of gaps.
  assert_eq!(rect_at(&window, &[0, 2]), Rect::new(42.0, 4.0, 128.0, 22.0));
  assert_eq!(rect_at(&window, &[0, 3]), Rect::new(176.0, 4.0, 16.0, 22.0));
}

#[test]
fn a_stretched_icon_is_drawn_centered_at_its_size() {
  let mut h = Harness::new();
  let row = Row::new().padding(4, 8).child(Icon::new("a").size(10));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  let view = window.view();
  let Content::Icon(icon) = node_at(view.root(), &[0, 0]).content() else {
    panic!("an icon node has icon content");
  };
  assert_eq!(icon.rect, Rect::new(8.0, 10.0, 10.0, 10.0));
}

#[test]
fn align_and_justify_place_children() {
  let mut h = Harness::new();
  let row = Row::new()
    .padding(4, 8)
    .gap(6)
    .align(Align::Center)
    .justify(Justify::Center)
    .child(Icon::new("a").size(10))
    .child(Icon::new("b").size(12));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  // (184 - 28) / 2 = 78 from the content edge; (22 - 10) / 2 = 6 down.
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(86.0, 10.0, 10.0, 10.0));
  assert_eq!(rect_at(&window, &[0, 1]), Rect::new(102.0, 9.0, 12.0, 12.0));

  let mut h = Harness::new();
  let row = Row::new()
    .align(Align::End)
    .justify(Justify::SpaceBetween)
    .child(Icon::new("a").size(10))
    .child(Icon::new("b").size(12));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(0.0, 20.0, 10.0, 10.0));
  assert_eq!(
    rect_at(&window, &[0, 1]),
    Rect::new(188.0, 18.0, 12.0, 12.0)
  );
}

#[test]
fn a_column_stacks_children_downward() {
  let mut h = Harness::new();
  let column = Column::new()
    .gap(2)
    .padding(1, 3)
    .child(Icon::new("a").size(10))
    .child(Spacer::new())
    .child(Icon::new("b").size(12));
  let window = h.window(h.panel().child(column), 40.0, 100.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(3.0, 1.0, 34.0, 10.0));
  assert_eq!(rect_at(&window, &[0, 1]), Rect::new(3.0, 13.0, 34.0, 72.0));
  assert_eq!(rect_at(&window, &[0, 2]), Rect::new(3.0, 87.0, 34.0, 12.0));
}

#[test]
fn a_stack_puts_children_in_one_cell_sized_to_the_largest() {
  let mut h = Harness::new();
  let row = Row::new().align(Align::Start).child(
    Stack::new()
      .child(Icon::new("a").size(10))
      .child(Icon::new("b").size(20)),
  );
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(0.0, 0.0, 20.0, 20.0));
  assert_eq!(
    rect_at(&window, &[0, 0, 0]),
    Rect::new(0.0, 0.0, 20.0, 20.0)
  );
  assert_eq!(
    rect_at(&window, &[0, 0, 1]),
    Rect::new(0.0, 0.0, 20.0, 20.0)
  );
}

#[test]
fn a_button_centers_its_child_inside_its_padding() {
  let mut h = Harness::new();
  let row = Row::new()
    .padding(4, 8)
    .child(Button::new(Icon::new("a").size(10)).style(Style::new().padding(2, 8)));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(8.0, 4.0, 26.0, 22.0));
  assert_eq!(
    rect_at(&window, &[0, 0, 0]),
    Rect::new(16.0, 10.0, 10.0, 10.0)
  );
}

#[test]
fn row_padding_wins_over_style_padding() {
  let mut h = Harness::new();
  let row = Row::new()
    .style(Style::new().padding(1, 1))
    .padding(4, 8)
    .child(Icon::new("a").size(10));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(8.0, 4.0, 10.0, 22.0));

  let mut h = Harness::new();
  let row = Row::new()
    .style(Style::new().padding(1, 2))
    .child(Icon::new("a").size(10));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(2.0, 1.0, 10.0, 28.0));
}

#[test]
fn the_window_style_padding_insets_its_child() {
  let mut h = Harness::new();
  let panel = h
    .panel()
    .style(Style::new().padding(3, 5))
    .child(Row::new());
  let window = h.window(panel, 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0]), Rect::new(5.0, 3.0, 190.0, 24.0));
}

#[test]
fn each_side_of_a_style_pads_by_its_own_amount() {
  let mut h = Harness::new();
  let row = Row::new()
    .style(
      Style::new()
        .padding(2, 8)
        .padding_left(12)
        .padding_bottom(6),
    )
    .child(Icon::new("a").size(10))
    .child(Spacer::new())
    .child(Icon::new("b").size(10));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  // 30 high less 2 above and 6 below; 12 in on the left, 8 on the right.
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(12.0, 2.0, 10.0, 22.0));
  assert_eq!(rect_at(&window, &[0, 2]), Rect::new(182.0, 2.0, 10.0, 22.0));

  // A button is its child and each side's padding, and centers nothing
  // past them.
  let mut h = Harness::new();
  let button = Button::new(Icon::new("a").size(10))
    .style(Style::new().padding_top(1).padding_right(3).padding_left(7));
  let row = Row::new().align(Align::Start).child(button);
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(0.0, 0.0, 20.0, 11.0));
  assert_eq!(
    rect_at(&window, &[0, 0, 0]),
    Rect::new(7.0, 1.0, 10.0, 10.0)
  );
}

#[test]
fn a_rows_own_padding_wins_over_each_side_of_its_style() {
  let mut h = Harness::new();
  let row = Row::new()
    .style(Style::new().padding_top(9).padding_left(9))
    .padding(4, 8)
    .child(Icon::new("a").size(10));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(8.0, 4.0, 10.0, 22.0));
}

#[test]
fn text_is_as_wide_as_its_glyphs_and_one_line_high() {
  let mut h = Harness::new();
  let row = Row::new()
    .align(Align::Start)
    .child(Text::new("hello"))
    .child(Text::new("hello world"));
  let window = h.window(h.panel().child(row), 400.0, 30.0);
  let short = rect_at(&window, &[0, 0]);
  let long = rect_at(&window, &[0, 1]);
  assert!(short.width > 10.0, "{short:?}");
  assert!(long.width > short.width + 10.0, "{short:?} {long:?}");
  // The default font is 13 pixels with a line height of round(13 * 1.2).
  assert_eq!(short.height, 16.0);
  assert_eq!(long.height, 16.0);
  assert_eq!(long.x, short.right());
}

#[test]
fn a_text_measures_with_the_buffer_it_is_drawn_from() {
  let mut h = Harness::new();
  let row = Row::new()
    .align(Align::Start)
    .child(Text::new("hello world"));
  let window = h.window(h.panel().child(row), 400.0, 30.0);
  let view = window.view();
  let node = node_at(view.root(), &[0, 0]);
  let Content::Text(text) = node.content() else {
    panic!("a text node has text content");
  };
  let runs: Vec<_> = text.runs().collect();
  assert_eq!(runs.len(), 1);
  assert_eq!(node.rect().width, runs[0].line_w.ceil());
  assert_eq!(text.content(), "hello world");
}

#[test]
fn stretched_text_is_centered_vertically() {
  let mut h = Harness::new();
  let row = Row::new().padding(4, 8).child(Text::new("hi"));
  let window = h.window(h.panel().child(row), 200.0, 30.0);
  let view = window.view();
  let node = node_at(view.root(), &[0, 0]);
  assert_eq!(node.rect().height, 22.0);
  let Content::Text(text) = node.content() else {
    panic!("a text node has text content");
  };
  // A 16-pixel line in a 22-pixel box starting at y = 4.
  assert_eq!(text.origin().y, 7.0);
  assert_eq!(text.origin().x, 8.0);
}

#[test]
fn ellipsis_lets_text_shrink_to_fit() {
  let long = "a sentence much too long for the narrow window it sits in";
  let mut h = Harness::new();
  let row = Row::new().child(Text::new(long));
  let window = h.window(h.panel().child(row), 400.0, 30.0);
  let natural = rect_at(&window, &[0, 0]).width;
  assert!(natural > 120.0, "{natural}");

  let mut h = Harness::new();
  let row = Row::new().child(Text::new(long).ellipsis(true));
  let window = h.window(h.panel().child(row), 120.0, 30.0);
  let view = window.view();
  let node = node_at(view.root(), &[0, 0]);
  assert!(node.rect().width <= 120.0, "{:?}", node.rect());
  let Content::Text(text) = node.content() else {
    panic!("a text node has text content");
  };
  let line = text.runs().next().expect("one line");
  assert!(line.line_w <= 120.0, "{}", line.line_w);
  assert!(line.glyphs.len() < long.chars().count());
  // The line ends in the font's own "…" glyph.
  let mut dots = TextNode::new();
  dots.set_content(String::from("\u{2026}"));
  dots.prepare(crate::Font::DEFAULT, &mut h.text);
  let dots = dots.runs().next().expect("a line").glyphs[0].glyph_id;
  assert_ne!(dots, 0);
  assert_eq!(line.glyphs.last().map(|glyph| glyph.glyph_id), Some(dots));

  // Without an ellipsis the text keeps its width and overflows.
  let mut h = Harness::new();
  let row = Row::new().child(Text::new(long));
  let window = h.window(h.panel().child(row), 120.0, 30.0);
  assert_eq!(rect_at(&window, &[0, 0]).width, natural);
}

#[test]
fn max_lines_wraps_up_to_that_many_lines() {
  let long = "a sentence much too long for the narrow window it sits in";
  let mut h = Harness::new();
  let column = Column::new()
    .child(Text::new(long).max_lines(2))
    .child(Text::new(long).max_lines(20))
    .child(Text::new(long));
  let window = h.window(h.panel().child(column), 120.0, 400.0);
  let two = rect_at(&window, &[0, 0]);
  let many = rect_at(&window, &[0, 1]);
  assert_eq!(two.height, 32.0);
  assert!(two.width <= 120.0, "{two:?}");
  assert!(many.height > 32.0 && many.height % 16.0 == 0.0, "{many:?}");
  assert_eq!(rect_at(&window, &[0, 2]).height, 16.0);
}

#[test]
fn a_word_too_long_for_the_line_breaks_inside_it() {
  let word = "Supercalifragilisticexpialidocious-and-then-some";
  let mut h = Harness::new();
  let column = Column::new().child(Text::new(word).max_lines(3));
  let window = h.window(h.panel().child(column), 100.0, 200.0);
  let view = window.view();
  let node = node_at(view.root(), &[0, 0]);
  let Content::Text(text) = node.content() else {
    panic!("a text node has text content");
  };
  let widths: Vec<f32> = text.runs().map(|run| run.line_w).collect();
  assert!(widths.len() > 1, "{widths:?}");
  assert!(widths.iter().all(|&width| width <= 100.0), "{widths:?}");
}

#[test]
fn text_takes_the_nearest_font_and_color() {
  use crate::{Color, Font};

  let mut h = Harness::new();
  let panel = h
    .panel()
    .style(
      Style::new()
        .font(Font::new("sans-serif", 20.0))
        .color(Color::rgb(1, 2, 3)),
    )
    .child(
      Row::new()
        .align(Align::Start)
        .child(Text::new("inherits"))
        .child(
          Text::new("own").style(
            Style::new()
              .font(Font::new("sans-serif", 10.0))
              .color(Color::rgb(4, 5, 6)),
          ),
        )
        .child(Button::new(Text::new("in a button"))),
    );
  let window = h.window(panel, 400.0, 40.0);
  assert_eq!(rect_at(&window, &[0, 0]).height, 24.0);
  assert_eq!(rect_at(&window, &[0, 1]).height, 12.0);
  let view = window.view();
  let color = |path: &[usize]| match node_at(view.root(), path).content() {
    Content::Text(text) => text.color(),
    _ => panic!("not a text node"),
  };
  assert_eq!(color(&[0, 0]), Color::rgb(1, 2, 3));
  assert_eq!(color(&[0, 1]), Color::rgb(4, 5, 6));
  assert_eq!(color(&[0, 2, 0]), Color::rgb(1, 2, 3));
}

#[test]
fn text_defaults_to_black() {
  let mut h = Harness::new();
  let window = h.window(h.panel().child(Text::new("x")), 100.0, 30.0);
  let view = window.view();
  let Content::Text(text) = node_at(view.root(), &[0]).content() else {
    panic!("a text node has text content");
  };
  assert_eq!(text.color(), crate::Color::rgb(0, 0, 0));
}

#[test]
fn bounds_cover_a_node_and_everything_it_paints() {
  let mut h = Harness::new();
  // The icon overflows its 16-pixel box by 4 above and below.
  let row = Row::new()
    .padding(10, 4)
    .child(Icon::new("x").size(24))
    .child(Text::new("text"));
  let window = h.window(h.panel().child(row), 100.0, 36.0);
  let view = window.view();
  let icon = node_at(view.root(), &[0, 0]);
  assert_eq!(icon.rect(), Rect::new(4.0, 10.0, 24.0, 16.0));
  assert_eq!(icon.bounds(), Rect::new(4.0, 6.0, 24.0, 24.0));
  let row = node_at(view.root(), &[0]);
  assert!(
    row.bounds().y <= 6.0 && row.bounds().bottom() >= 30.0,
    "{:?}",
    row.bounds()
  );
  // Text bounds reach past the line box by the ink allowance.
  let text = node_at(view.root(), &[0, 1]);
  assert!(
    text.bounds().y < text.rect().y,
    "{:?} {:?}",
    text.bounds(),
    text.rect()
  );
}

#[test]
fn a_clipping_widget_cuts_its_childrens_reach_to_its_box() {
  let mut h = Harness::new();
  let clip = h.cx.signal(true);
  // A 20-pixel icon in a 10-pixel-tall window overflows the row.
  let row = Row::new()
    .align(Align::Start)
    .style(move || Style::new().clip(clip.get()))
    .child(Icon::new("x").size(20));
  let window = h.window(h.panel().child(row), 100.0, 10.0);
  let bounds = || super::node_at(window.view().root(), &[0]).bounds();
  assert_eq!(bounds(), Rect::new(0.0, 0.0, 100.0, 10.0));

  // Turning the clip off is a layout change that lets the icon reach out.
  clip.set(false);
  h.runtime.flush();
  assert!(window.needs_layout());
  h.frame(&window);
  assert_eq!(bounds(), Rect::new(0.0, 0.0, 100.0, 20.0));
}

#[test]
fn an_image_is_its_size_and_a_new_picture_only_repaints() {
  let mut h = Harness::new();
  let path = h.cx.signal(String::from("a.png"));
  let wide = h.cx.signal(false);
  let row = Row::new()
    .align(Align::Start)
    .child(Image::new("default.png"))
    .child(
      Image::new(move || std::path::PathBuf::from(path.get()))
        .width(move || if wide.get() { 50 } else { 30 })
        .height(20)
        .fit(Fit::Cover),
    );
  let window = h.window(h.panel().child(row), 200.0, 100.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(0.0, 0.0, 64.0, 64.0));
  assert_eq!(rect_at(&window, &[0, 1]), Rect::new(64.0, 0.0, 30.0, 20.0));
  match node_at(window.view().root(), &[0, 1]).content() {
    Content::Image(image) => {
      assert_eq!(image.path, std::path::Path::new("a.png"));
      assert_eq!(image.rect, Rect::new(64.0, 0.0, 30.0, 20.0));
      assert_eq!(image.fit, Fit::Cover);
    }
    _ => panic!("not an image"),
  }

  window.take_damage();
  path.set(String::from("b.png"));
  h.runtime.flush();
  assert!(!window.needs_layout());
  assert_eq!(window.take_damage(), vec![Rect::new(64.0, 0.0, 30.0, 20.0)]);

  wide.set(true);
  h.runtime.flush();
  assert!(window.needs_layout());
  h.frame(&window);
  assert_eq!(rect_at(&window, &[0, 1]).width, 50.0);
}

#[test]
fn a_style_size_wins_over_the_content_and_changing_it_lays_out_again() {
  let mut h = Harness::new();
  let tall = h.cx.signal(false);
  let row = Row::new()
    .align(Align::Start)
    .child(
      Stack::new()
        .style(move || {
          Style::new()
            .width(50.0)
            .height(if tall.get() { 25.0 } else { 20.0 })
        })
        .child(Icon::new("x").size(10)),
    )
    // Smaller than its 30-pixel icon, which overflows.
    .child(
      Stack::new()
        .style(Style::new().width(10.0).height(10.0))
        .child(Icon::new("x").size(30)),
    );
  let window = h.window(h.panel().child(row), 200.0, 100.0);
  assert_eq!(rect_at(&window, &[0, 0]), Rect::new(0.0, 0.0, 50.0, 20.0));
  assert_eq!(rect_at(&window, &[0, 1]), Rect::new(50.0, 0.0, 10.0, 10.0));
  tall.set(true);
  h.runtime.flush();
  assert!(window.needs_layout());
  h.frame(&window);
  assert_eq!(rect_at(&window, &[0, 0]).height, 25.0);
}