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
use std::cell::{Cell, RefCell};
use std::sync::Arc;

use rect::Rect;
use point::Point;
use thickness::Thickness;
use traits::Place;
use widgets::{Widget, VerticalPlacement, HorizontalPlacement};

/// Describes the orientation of a StackLayout
#[derive(PartialEq, Copy, Clone)]
pub enum Orientation {
    Vertical,
    Horizontal,
}

/// StackLayout arranges it's child widgets into a single line. The children could be
/// stacked horizontal or vertical.
pub struct StackLayout {
    pub rect: Cell<Rect>,
    local_position: Cell<Point>,
    vertical_placement: Cell<VerticalPlacement>,
    horizontal_placement: Cell<HorizontalPlacement>,
    margin: Cell<Thickness>,
    children: RefCell<Vec<Arc<dyn Widget>>>,
    orientation: Cell<Orientation>,
    spacing: Cell<u32>,
}

impl StackLayout {
    /// Creates and returns a new StackLayout with the given orientation.
    pub fn new(orientation: Orientation) -> Arc<Self> {
        Arc::new(StackLayout {
            rect: Cell::new(Rect::default()),
            local_position: Cell::new(Point::new(0, 0)),
            vertical_placement: Cell::new(VerticalPlacement::Absolute),
            horizontal_placement: Cell::new(HorizontalPlacement::Absolute),
            margin: Cell::new(Thickness::default()),
            children: RefCell::new(vec![]),
            orientation: Cell::new(orientation),
            spacing: Cell::new(0),
        })
    }

    /// Borrow the arrangement orientation of stack layout.
    pub fn orientation(&self) -> &Cell<Orientation> {
        &self.orientation
    }

    /// Set the spacing between the children of stack layout.
    pub fn spacing(&self, spacing: u32) -> &Self {
        self.spacing.set(spacing);
        self
    }
}

impl Place for StackLayout {}

impl Widget for StackLayout {
    fn name(&self) -> &str {
        "StackLayout"
    }

    fn rect(&self) -> &Cell<Rect> {
        &self.rect
    }

    fn local_position(&self) -> &Cell<Point> {
        &self.local_position
    }

    fn vertical_placement(&self) -> &Cell<VerticalPlacement> {
        &self.vertical_placement
    }

    fn horizontal_placement(&self) -> &Cell<HorizontalPlacement> {
        &self.horizontal_placement
    }

    fn margin(&self) -> &Cell<Thickness> {
        &self.margin
    }

    fn children(&self) -> &RefCell<Vec<Arc<dyn Widget>>> {
        &self.children
    }

    fn arrange(&self) {
        let parent_rect = self.rect().get();

        let mut x_position = parent_rect.x;
        let mut y_position = parent_rect.y;

        for child in &*self.children().borrow_mut() {
            let child_position = child.local_position().get();
            let mut child_rect = child.rect().get();
            let child_margin = child.margin().get();

            match self.orientation().get() {
                Orientation::Horizontal => {
                    match child.vertical_placement().get() {
                        VerticalPlacement::Absolute => {
                            child_rect.y = parent_rect.y + child_position.y;
                        },
                        VerticalPlacement::Top => {
                            child_rect.y = parent_rect.y + child_margin.top as i32;
                        },
                        VerticalPlacement::Center => {
                            child_rect.y = parent_rect.y + parent_rect.height as i32 / 2
                                - child_rect.height as i32 / 2;
                        },
                        VerticalPlacement::Bottom => {
                            child_rect.y = parent_rect.y + parent_rect.height as i32
                                - child_rect.height as i32
                                - child_margin.bottom as i32;
                        },
                        VerticalPlacement::Stretch => {
                            child_rect.y = parent_rect.y;
                            child_rect.height = parent_rect.height;
                        }
                    }

                    child_rect.x = x_position + child_margin.left as i32;
                    x_position = child_rect.x + child_rect.width as i32 + child_margin.right as i32
                        + self.spacing.get() as i32;
                }
                Orientation::Vertical => {
                    match child.horizontal_placement().get() {
                        HorizontalPlacement::Absolute => {
                            child_rect.x = parent_rect.x + child_position.x;
                        },
                        HorizontalPlacement::Left => {
                            child_rect.x = parent_rect.x + child_margin.left as i32;
                        },
                        HorizontalPlacement::Center => {
                            child_rect.x = parent_rect.x + parent_rect.width as i32 / 2
                                - child_rect.width as i32 / 2;
                        },
                        HorizontalPlacement::Right => {
                            child_rect.x = parent_rect.x + parent_rect.width as i32
                                - child_rect.width as i32
                                - child_margin.right as i32;
                        },
                        HorizontalPlacement::Stretch => {
                            child_rect.x = parent_rect.x;
                            child_rect.width = parent_rect.width;
                        }
                    }

                    child_rect.y = y_position + child_margin.top as i32;
                    y_position = child_rect.y + child_rect.height as i32
                        + child_margin.bottom as i32
                        + self.spacing.get() as i32;
                }
            }

            child.rect().set(child_rect);
            child.arrange();
        }
    }
}