canvist_canvas 0.2.2

Headless interaction core for the canvist canvas: camera, spatial index, hit testing, selection, transform handles, snapping, and tools
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
//! Auto-layout: where a frame's flow children go. The scene stores the
//! intent ([`AutoLayout`], [`LayoutChild`]); positions are computed here and
//! never stored, so peers never write conflicting results.

use canvist_scene::Align;
use canvist_scene::AutoLayout;
use canvist_scene::Direction;
use canvist_scene::LayoutChild;
use canvist_scene::Sizing;
use kurbo::Point;
use kurbo::Vec2;

/// One child as auto-layout sees it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FlowItem {
	/// The child's own (or intrinsic) size.
	pub size: Vec2,
	/// How it sizes itself in the flow.
	pub child: LayoutChild,
}

/// Where one child goes, in the frame's coordinates.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Placed {
	/// The top-left corner.
	pub origin: Point,
	/// The size, after fill and stretch.
	pub size: Vec2,
}

/// The result of laying out a frame.
#[derive(Clone, Debug, PartialEq)]
pub struct FlowResult {
	/// One placement per item, in order. Absolute items keep their size and
	/// get `None`: they keep their own transform.
	pub placed: Vec<Option<Placed>>,
	/// The size the frame takes after hugging its content.
	pub frame: Vec2,
}

/// Lay out `items` in a frame of `frame` size with `layout`. `hug` says per
/// axis (x, y) whether the frame shrinks to its content.
#[must_use]
pub fn flow(layout: &AutoLayout, frame: Vec2, hug: (bool, bool), items: &[FlowItem]) -> FlowResult {
	let axes = Axes::new(layout.direction);
	let padding = layout.padding;
	let inset = Vec2::new(padding.left + padding.right, padding.top + padding.bottom);
	let inner = frame - inset;

	let flow: Vec<usize> = (0..items.len())
		.filter(|&index| !items[index].child.absolute)
		.collect();
	let lines = if layout.wrap {
		wrap_lines(layout, &axes, axes.main(inner), items, &flow)
	} else {
		vec![flow]
	};

	let mut placed = vec![None; items.len()];
	let mut cross_cursor = 0.0;
	let mut content = Vec2::ZERO;

	for line in &lines {
		let measured = place_line(layout, &axes, inner, items, line);
		let line_cross = measured.cross;

		for (index, mut item) in measured.items {
			item.origin += Vec2::new(padding.left, padding.top) + axes.vec(0.0, cross_cursor);
			placed[index] = Some(item);
		}

		content = axes.vec(
			axes.main(content).max(measured.main),
			cross_cursor + line_cross,
		);
		cross_cursor += line_cross + layout.gap;
	}

	let hugged = Vec2::new(
		if hug.0 { content.x + inset.x } else { frame.x },
		if hug.1 { content.y + inset.y } else { frame.y },
	);

	FlowResult {
		placed,
		frame: hugged,
	}
}

/// Main and cross axis helpers for one direction.
struct Axes {
	row: bool,
}

impl Axes {
	fn new(direction: Direction) -> Self {
		Self {
			row: direction == Direction::Row,
		}
	}

	fn main(&self, value: Vec2) -> f64 {
		if self.row { value.x } else { value.y }
	}

	fn cross(&self, value: Vec2) -> f64 {
		if self.row { value.y } else { value.x }
	}

	fn vec(&self, main: f64, cross: f64) -> Vec2 {
		if self.row {
			Vec2::new(main, cross)
		} else {
			Vec2::new(cross, main)
		}
	}

	fn main_sizing(&self, child: LayoutChild) -> Sizing {
		if self.row { child.width } else { child.height }
	}

	fn cross_sizing(&self, child: LayoutChild) -> Sizing {
		if self.row { child.height } else { child.width }
	}
}

/// Break flow items into lines no longer than `limit` on the main axis.
fn wrap_lines(
	layout: &AutoLayout,
	axes: &Axes,
	limit: f64,
	items: &[FlowItem],
	flow: &[usize],
) -> Vec<Vec<usize>> {
	let mut lines: Vec<Vec<usize>> = Vec::new();
	let mut used = 0.0;

	for &index in flow {
		let length = axes.main(items[index].size);
		let fits = lines
			.last()
			.is_some_and(|line| !line.is_empty() && used + layout.gap + length <= limit);

		if fits {
			used += layout.gap + length;

			if let Some(line) = lines.last_mut() {
				line.push(index);
			}
		} else {
			used = length;
			lines.push(vec![index]);
		}
	}

	lines
}

/// One laid-out line, before it is moved to its cross position.
struct Line {
	items: Vec<(usize, Placed)>,
	main: f64,
	cross: f64,
}

/// Size and place one line's items along the main axis, and align each on
/// the cross axis.
fn place_line(
	layout: &AutoLayout,
	axes: &Axes,
	inner: Vec2,
	items: &[FlowItem],
	line: &[usize],
) -> Line {
	let gaps = layout.gap * line.len().saturating_sub(1) as f64;
	let fill_length = fill_length(axes, axes.main(inner) - gaps, items, line);

	let sizes: Vec<Vec2> = line
		.iter()
		.map(|&index| item_size(layout, axes, inner, items[index], fill_length))
		.collect();
	let total = sizes.iter().map(|size| axes.main(*size)).sum::<f64>() + gaps;
	let cross = sizes
		.iter()
		.map(|size| axes.cross(*size))
		.fold(0.0, f64::max);
	let (mut cursor, spacing) = distribute(
		layout.align,
		axes.main(inner),
		total,
		line.len(),
		layout.gap,
	);
	let mut placed = Vec::with_capacity(line.len());

	for (&index, size) in line.iter().zip(sizes) {
		let offset = align_offset(
			layout.cross_align,
			axes.cross(inner).max(cross),
			axes.cross(size),
		);
		placed.push((
			index,
			Placed {
				origin: axes.vec(cursor, offset).to_point(),
				size,
			},
		));
		cursor += axes.main(size) + spacing;
	}

	Line {
		items: placed,
		main: total,
		cross,
	}
}

/// The main-axis length each fill child gets: an equal share of `room`
/// after the fixed-size children.
fn fill_length(axes: &Axes, room: f64, items: &[FlowItem], line: &[usize]) -> f64 {
	let (fills, fixed): (Vec<usize>, Vec<usize>) = line
		.iter()
		.partition(|&&index| axes.main_sizing(items[index].child) == Sizing::Fill);

	if fills.is_empty() {
		return 0.0;
	}

	let fixed_length: f64 = fixed
		.iter()
		.map(|&index| axes.main(items[index].size))
		.sum();
	((room - fixed_length) / fills.len() as f64).max(0.0)
}

fn item_size(
	layout: &AutoLayout,
	axes: &Axes,
	inner: Vec2,
	item: FlowItem,
	fill_length: f64,
) -> Vec2 {
	let main = if axes.main_sizing(item.child) == Sizing::Fill {
		fill_length
	} else {
		axes.main(item.size)
	};

	let stretch =
		layout.cross_align == Align::Stretch || axes.cross_sizing(item.child) == Sizing::Fill;
	let cross = if stretch {
		axes.cross(inner)
	} else {
		axes.cross(item.size)
	};

	axes.vec(main, cross.max(0.0))
}

/// The starting offset and the space between items on the main axis.
fn distribute(align: Align, room: f64, used: f64, count: usize, gap: f64) -> (f64, f64) {
	let free = room - used;

	match align {
		Align::Center => (free / 2.0, gap),
		Align::End => (free, gap),

		Align::SpaceBetween if count > 1 => (0.0, gap + free.max(0.0) / (count - 1) as f64),
		_ => (0.0, gap),
	}
}

fn align_offset(align: Align, room: f64, size: f64) -> f64 {
	match align {
		Align::Center => (room - size) / 2.0,
		Align::End => room - size,
		_ => 0.0,
	}
}

#[cfg(test)]
mod tests {
	use canvist_scene::Padding;

	use super::*;

	fn item(width: f64, height: f64) -> FlowItem {
		FlowItem {
			size: Vec2::new(width, height),
			child: LayoutChild::default(),
		}
	}

	fn origins(result: &FlowResult) -> Vec<(f64, f64)> {
		result
			.placed
			.iter()
			.flatten()
			.map(|placed| (placed.origin.x, placed.origin.y))
			.collect()
	}

	/// A short child's size.
	const SHORT: Vec2 = Vec2::new(20.0, 10.0);
	/// A taller, wider child's size.
	const TALL: Vec2 = Vec2::new(30.0, 40.0);

	/// A short child and a taller, wider one.
	fn pair() -> [FlowItem; 2] {
		[item(SHORT.x, SHORT.y), item(TALL.x, TALL.y)]
	}

	#[test]
	fn a_row_places_children_with_gaps_and_padding() {
		/// Wider and taller than the children, so cross-centring shows.
		const FRAME: Vec2 = Vec2::new(200.0, 50.0);
		/// Padding on every side.
		const PADDING: f64 = 5.0;

		let layout = AutoLayout {
			gap: 10.0,
			padding: Padding::all(PADDING),
			cross_align: Align::Center,
			..AutoLayout::default()
		};
		let result = flow(&layout, FRAME, (false, false), &pair());

		assert_eq!(origins(&result), [(5.0, 20.0), (35.0, 5.0)]);
	}

	#[test]
	fn fill_children_share_the_remaining_space() {
		/// A column with 100 units of height to share.
		const FRAME: Vec2 = Vec2::new(50.0, 100.0);
		/// The fixed child's height.
		const FIXED: f64 = 20.0;

		let layout = AutoLayout {
			direction: Direction::Column,
			gap: 4.0,
			..AutoLayout::default()
		};
		let mut fill = item(10.0, 0.0);
		fill.child.height = Sizing::Fill;
		let result = flow(
			&layout,
			FRAME,
			(false, false),
			&[item(10.0, FIXED), fill, fill],
		);
		let heights: Vec<f64> = result
			.placed
			.iter()
			.flatten()
			.map(|placed| placed.size.y)
			.collect();

		assert_eq!(heights, [20.0, 36.0, 36.0]);
	}

	#[test]
	fn hugging_frames_shrink_to_their_content() {
		/// Far larger than the children; hugging ignores it.
		const FRAME: Vec2 = Vec2::new(500.0, 500.0);
		/// Padding on every side.
		const PADDING: f64 = 8.0;

		let layout = AutoLayout {
			gap: 10.0,
			padding: Padding::all(PADDING),
			..AutoLayout::default()
		};
		let result = flow(&layout, FRAME, (true, true), &pair());

		assert_eq!(result.frame, Vec2::new(76.0, 56.0));
	}

	#[test]
	fn wrapping_starts_new_lines() {
		let layout = AutoLayout {
			gap: 10.0,
			wrap: true,
			..AutoLayout::default()
		};
		let items = [item(40.0, 10.0), item(40.0, 20.0), item(40.0, 10.0)];
		let result = flow(&layout, Vec2::new(100.0, 100.0), (false, false), &items);

		assert_eq!(origins(&result), [(0.0, 0.0), (50.0, 0.0), (0.0, 30.0)]);
	}

	#[test]
	fn space_between_spreads_children() {
		let layout = AutoLayout {
			align: Align::SpaceBetween,
			..AutoLayout::default()
		};
		let result = flow(
			&layout,
			Vec2::new(100.0, 10.0),
			(false, false),
			&[item(10.0, 10.0), item(10.0, 10.0), item(10.0, 10.0)],
		);

		assert_eq!(origins(&result), [(0.0, 0.0), (45.0, 0.0), (90.0, 0.0)]);
	}
}