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
// Horizontal Layout Manager
// Lays out Widgets Horizontally in a Bounding Box
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::core::layout_manager::*;
use crate::core::point::{Point, Size};

pub struct HorizontalLayoutManager {
    container_widget_id: i32,
    padding: LayoutManagerPadding,
}

impl HorizontalLayoutManager {
    pub fn new(widget_id: i32, padding: LayoutManagerPadding) -> Self {
        Self {
            container_widget_id: widget_id,
            padding,
        }
    }
}

impl LayoutManager for HorizontalLayoutManager {
    fn do_layout(
        &mut self,
        origin: Point,
        size: Size,
        coordinates: LayoutManagerCoordinates,
    ) -> LayoutManagerCoordinates {
        let num_widgets = coordinates.widget_sizes.len() as i32;
        let width_per_widget = size.w / num_widgets;
        let mut widget_origins: Vec<Point> = vec![];
        let mut widget_sizes: Vec<Size> = vec![];
        let mut current_x: i32 = origin.x;
        let current_y: i32 = origin.y;

        for x in 0..num_widgets {
            if x == 0 {
                current_x = self.padding.left + origin.x;
            } else {
                current_x += width_per_widget + (self.padding.spacing / 2);
            }

            widget_origins.push(Point {
                x: current_x,
                y: current_y + self.padding.top,
            });

            if x == num_widgets - 1 {
                widget_sizes.push(Size {
                    w: width_per_widget
                        - (self.padding.left + self.padding.right)
                        - (self.padding.spacing / 2),
                    h: size.h - (self.padding.top + self.padding.bottom),
                });
            } else {
                widget_sizes.push(Size {
                    w: width_per_widget - (self.padding.spacing / 2),
                    h: size.h - (self.padding.top + self.padding.bottom),
                });
            }
        }

        LayoutManagerCoordinates {
            widget_origins: widget_origins.clone(),
            widget_sizes: widget_sizes.clone(),
            widget_positions: coordinates.widget_positions.clone(),
        }
    }

    fn adjust_layout(&mut self, coordinates: LayoutManagerPadding) {
        self.padding = coordinates.clone()
    }

    fn get_widget_id(&self) -> i32 {
        return self.container_widget_id;
    }
}