MCPcopy Create free account
hub / github.com/audulus/rui / stack_layout

Function stack_layout

src/views/stack_layout.rs:11–44  ·  view source on GitHub ↗

1-D stack layout to make the algorithm clear. Returns length used to express the layout. If there are any flexible items, will return `total`, since the flexible items will expand to fill the available space.

(
    total: f32,
    sizes: &[StackItem],
    intervals: &mut [(f32, f32)],
    flex_length: &mut f32,
)

Source from the content-addressed store, hash-verified

9/// flexible items, will return `total`, since the flexible items
10/// will expand to fill the available space.
11pub fn stack_layout(
12 total: f32,
13 sizes: &[StackItem],
14 intervals: &mut [(f32, f32)],
15 flex_length: &mut f32,
16) -> f32 {
17 assert_eq!(sizes.len(), intervals.len());
18
19 // Count the number of flexible items and total of fixed sizes.
20 let mut flex_count = 0;
21 let mut sizes_sum = 0.0;
22 for sz in sizes {
23 match sz {
24 StackItem::Flexible => flex_count += 1,
25 StackItem::Fixed(s) => sizes_sum += s,
26 }
27 }
28
29 // length of flexible items is remaining size divided equally
30 *flex_length = (total - sizes_sum) / (flex_count as f32);
31
32 let mut x = 0.0;
33 for i in 0..sizes.len() {
34 let sz = match sizes[i] {
35 StackItem::Flexible => *flex_length,
36 StackItem::Fixed(s) => s,
37 };
38
39 intervals[i] = (x, x + sz);
40 x += sz;
41 }
42
43 x
44}
45
46#[cfg(test)]
47mod tests {

Callers 2

test_layout_basicFunction · 0.85
layoutMethod · 0.85

Calls 1

lenMethod · 0.80

Tested by 1

test_layout_basicFunction · 0.68