Skip to main content

minifb_ui/
layout.rs

1/// Vertical stack layout helper.
2/// Call `next()` repeatedly to get positions for each child element.
3pub struct VStack {
4    x: usize,
5    y: usize,
6    spacing: usize,
7    count: usize,
8}
9
10impl VStack {
11    pub fn new(x: usize, y: usize, spacing: usize) -> Self {
12        Self { x, y, spacing, count: 0 }
13    }
14
15    /// Returns the (x, y) position for the next element, then advances by `height + spacing`.
16    pub fn next(&mut self, height: usize) -> (usize, usize) {
17        let pos = (self.x, self.y);
18        self.y += height + self.spacing;
19        self.count += 1;
20        pos
21    }
22
23    /// Returns the current y position without advancing
24    pub fn current_y(&self) -> usize {
25        self.y
26    }
27
28    /// Returns how many items have been placed
29    pub fn count(&self) -> usize {
30        self.count
31    }
32}
33
34/// Horizontal stack layout helper.
35/// Call `next()` repeatedly to get positions for each child element.
36pub struct HStack {
37    x: usize,
38    y: usize,
39    spacing: usize,
40    count: usize,
41}
42
43impl HStack {
44    pub fn new(x: usize, y: usize, spacing: usize) -> Self {
45        Self { x, y, spacing, count: 0 }
46    }
47
48    /// Returns the (x, y) position for the next element, then advances by `width + spacing`.
49    pub fn next(&mut self, width: usize) -> (usize, usize) {
50        let pos = (self.x, self.y);
51        self.x += width + self.spacing;
52        self.count += 1;
53        pos
54    }
55
56    /// Returns the current x position without advancing
57    pub fn current_x(&self) -> usize {
58        self.x
59    }
60
61    /// Returns how many items have been placed
62    pub fn count(&self) -> usize {
63        self.count
64    }
65}