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
//! A simple horizontal or vertical layout with padding and child spacing.

use std::collections::HashMap;

use crate::{scalar, Scalar, Dimensions, Context, LayoutChildrenArgs, MinimumSizeArgs};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Axis {
    Horizontal,
    Vertical,
}

#[derive(Debug, Clone)]
pub struct Settings {
    pub axis: Axis,
    pub padding: Scalar,
    pub child_spacing: Scalar,
}

pub fn minimum_size<C: Context>(args: &MinimumSizeArgs<'_, C>, settings: &Settings) -> Dimensions {
    let Settings { axis, padding, child_spacing } = *settings;
    
    let mut minimum_size = Dimensions::zero();
    let num_children = crate::visible_children(args.slots, args.slot_id).count();
    let child_spacing = if num_children > 0 {
        child_spacing * ((num_children - 1) as Scalar)
    } else {
        scalar::ZERO
    };
    match axis {
        Axis::Horizontal => {
            minimum_size.width += padding * scalar::TWO + child_spacing;
        }
        Axis::Vertical => {
            minimum_size.height += padding * scalar::TWO + child_spacing;
        }
    }
    for child_id in crate::visible_children(args.slots, args.slot_id) {
        let child_minimum_size = args.minimum_size_cache[&child_id];
        match axis {
            Axis::Horizontal => {
                minimum_size.width += child_minimum_size.width;
                minimum_size.height = minimum_size.height.max(child_minimum_size.height);
            }
            Axis::Vertical => {
                minimum_size.width = minimum_size.width.max(child_minimum_size.width);
                minimum_size.height += child_minimum_size.height;
            }
        }
    }
    match axis {
        Axis::Horizontal => {
            minimum_size.height += padding * scalar::TWO;
        }
        Axis::Vertical => {
            minimum_size.width += padding * scalar::TWO;
        }
    }
    minimum_size
}

pub fn layout_children<C: Context>(args: &mut LayoutChildrenArgs<'_, C>, settings: &Settings) {
    let Settings { axis, padding, child_spacing } = *settings;
    
    let bounds = args.slots.get(args.slot_id).bounds;
    let children = crate::visible_children(args.slots, args.slot_id).collect::<Vec<_>>();
    let num_children = children.len();
    let size = match axis {
        Axis::Horizontal => bounds.size.width,
        Axis::Vertical => bounds.size.height,
    };
    let available_size = size - padding * scalar::TWO - child_spacing * ((num_children - 1) as Scalar);

    let mut irregular_sizes = HashMap::new();
    let mut num_regular_children = num_children;
    let mut reserved_size = scalar::ZERO;

    let mut expand_children = Vec::new();
    // Children not set as `expand` will always be their minimum size.
    for child_id in children.iter().rev() {
        let child = args.slots.get(*child_id);
        let child_minimum_size = args.minimum_size_cache[child_id];
        let (axis_expand, minimum_size) = match axis {
            Axis::Horizontal => (child.info.expand_x, child_minimum_size.width),
            Axis::Vertical => (child.info.expand_y, child_minimum_size.height),
        };
        if !axis_expand {
            irregular_sizes.insert(*child_id, minimum_size);
            num_regular_children -= 1;
            reserved_size += minimum_size;
        } else {
            expand_children.push((*child_id, minimum_size));
        }
    }

    // Children set as `expand` will be at least their minimum size or at most the "regular" size.
    // (The leftover size that is distributed equally (for now) between children marked `expand`.)
    // We need to figure out which children will be irregularly-sized.
    //
    // It makes sense to check this on children ordered from largest to smallest minimum sizes.
    expand_children.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
    for (child_id, minimum_size) in expand_children.into_iter().rev() {
        let regular_size = if num_regular_children > 0 {
            (available_size - reserved_size) / num_regular_children as Scalar
        } else {
            scalar::ZERO
        };
        if minimum_size > regular_size {
            irregular_sizes.insert(child_id, minimum_size);
            num_regular_children -= 1;
            reserved_size += minimum_size;
        }
    }

    let regular_size = if num_regular_children > 0 {
        (available_size - reserved_size) / num_regular_children as Scalar
    } else {
        scalar::ZERO
    };
    let mut offset = padding;
    for (i, child_id) in children.iter().enumerate() {
        let child = args.slots.get_mut(*child_id);
        let other_expand = match axis {
            Axis::Horizontal => child.info.expand_y,
            Axis::Vertical => child.info.expand_x,
        };
        let child_size = irregular_sizes
            .get(child_id)
            .cloned()
            .unwrap_or(regular_size);
        let child_minimum_size = args.minimum_size_cache[child_id];
        match axis {
            Axis::Horizontal => {
                child.bounds.x = offset;
                child.bounds.y = padding;
                child.bounds.size.width = child_size.max(child_minimum_size.width);
                child.bounds.size.height = if other_expand {
                    (bounds.size.height - (padding * scalar::TWO)).max(child_minimum_size.height)
                } else {
                    child_minimum_size.height
                };
                offset += child.bounds.size.width;
            }
            Axis::Vertical => {
                child.bounds.x = padding;
                child.bounds.y = offset;
                child.bounds.size.width = if other_expand {
                    (bounds.size.width - (padding * scalar::TWO)).max(child_minimum_size.width)
                } else {
                    child_minimum_size.width
                };
                child.bounds.size.height = child_size.max(child_minimum_size.height);
                offset += child.bounds.size.height;
            }
        }
        if i < children.len() - 1 {
            offset += child_spacing;
        }
    }
}