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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Stack layouting like Gtk's VBox/HBox
use std::collections::VecDeque;

use pugl_sys::*;

use crate::layout::*;
use crate::ui;
use crate::widget::*;

pub type Spacing = f64;

pub enum StackDirection {
    Front,
    Back
}

#[derive(Clone, Copy, Default, Debug)]
pub struct HorizontalLayouter;

#[derive(Clone, Copy, Default, Debug)]
pub struct VerticalLayouter;

#[derive(Default)]
pub struct Spacer {
    stub: WidgetStub,
    width_expandable: bool,
    height_expandable: bool
}

impl Widget for Spacer {
    fn stub (&self) -> &WidgetStub {
        &self.stub
    }
    fn stub_mut (&mut self) -> &mut WidgetStub {
        &mut self.stub
    }
    fn width_expandable(&self) -> bool { self.width_expandable }
    fn height_expandable(&self) -> bool { self.height_expandable }
}

impl Spacer {
    pub(crate) fn set_expandable(&mut self, (we, he): (bool, bool)) {
        self.width_expandable = we;
        self.height_expandable = he;
    }
}


struct StackLayoutData {
    padding: Spacing,
    spacing: Spacing,
    subnodes: VecDeque<Id>,
}

impl Default for StackLayoutData {
    fn default() -> StackLayoutData {
        StackLayoutData {
            padding: 0.0,
            spacing: 5.0,
            subnodes: VecDeque::new(),
        }
    }
}

impl StackLayoutData {
    fn pack(&mut self, subnode_id: Id, target: StackDirection) {
        match target {
            StackDirection::Back  => self.subnodes.push_back(subnode_id),
            StackDirection::Front => self.subnodes.push_front(subnode_id)
        };
    }
}


pub struct HorizontalLayouterImpl {
    d: StackLayoutData
}

impl HorizontalLayouterImpl {
    pub fn set_spacing(&mut self, s: Spacing) -> &mut HorizontalLayouterImpl {
        self.d.spacing = s;
        self
    }
    pub fn set_padding(&mut self, s: Spacing) -> &mut HorizontalLayouterImpl {
        self.d.padding = s;
        self
    }
}

impl Default for HorizontalLayouterImpl {
    fn default() -> HorizontalLayouterImpl {
        HorizontalLayouterImpl {
            d: StackLayoutData::default()
        }
    }
}

impl LayouterImpl for HorizontalLayouterImpl {
    fn apply_layouts(&self, widgets: &mut Vec<Box<dyn Widget>>, children: &[ui::WidgetNode],
                   orig_pos: Coord, size_avail: Size) {
        let sized_widgets = self.d.subnodes.iter().fold (0, | acc, sn | {
            if widgets[children[*sn].id].sized_width() {
                acc + 1
            } else {
                acc
            }
        });

        let width_avail = size_avail.w - self.d.spacing * (sized_widgets - 1) as f64  - 2.*self.d.padding;
        let height_avail = size_avail.h - 2.*self.d.padding;
        let (expanders, width_avail) = self.d.subnodes.iter().fold((0, width_avail), |(exp, wa), sn| {
            let wgt = &widgets[children[*sn].id];
            (if wgt.width_expandable() { exp + 1 } else { exp },  wa - wgt.size().w)
        });
        let expand_each = width_avail / expanders as f64;

        let mut pos = orig_pos + Coord { x: self.d.padding, y: self.d.padding };
        for sn in self.d.subnodes.iter() {
            let (width, sized_width) = {
                let widget = &mut widgets[children[*sn].id];

                if widget.width_expandable() {
                    widget.expand_width(expand_each);
                }
                if widget.height_expandable() {
                    widget.set_height(height_avail);
                }
                widget.set_pos(&pos);
                (widget.size().w, widget.sized_width())
            };
            children[*sn].apply_sizes(widgets, pos);
            if width > 0.0 {
                pos += Coord { x: width, y: 0.0 };
            }
            if sized_width {
                pos += Coord { x: self.d.spacing, y: 0.0 };
            }
        }
    }

    fn calc_size(&self, widgets: &mut Vec<Box<dyn Widget>>, children: &[ui::WidgetNode]) -> Size {
        let mut need = Size::default();
        need.w += self.d.padding;
        for subnode in self.d.subnodes.iter() {

            let size = children[*subnode].calc_widget_sizes(widgets);
            need.w += size.w;
            if size.h > need.h {
                need.h = size.h;
            }
            need.w += self.d.spacing;
        }
        need.w += self.d.padding - self.d.spacing;
        need.h += 2.*self.d.padding;

        need
    }
}

impl HorizontalLayouterImpl {
    fn pack(&mut self, subnode_id: Id, target: StackDirection) { self.d.pack(subnode_id, target) }
}

impl Layouter for HorizontalLayouter {
    type Target = StackDirection;
    type Implementor = HorizontalLayouterImpl;

    fn new_implementor() -> Box<dyn LayouterImpl> {
        Box::new(HorizontalLayouterImpl::default())
    }
    fn pack(&mut self, layout_impl: &mut Self::Implementor, subnode_id: Id, target: Self::Target) {
        layout_impl.pack(subnode_id, target);
    }
    fn expandable() -> (bool, bool) {
        (true, false)
    }
}


pub struct VerticalLayouterImpl {
    d: StackLayoutData
}

impl VerticalLayouterImpl {
    pub fn set_spacing(&mut self, s: Spacing) -> &mut VerticalLayouterImpl {
        self.d.spacing = s;
        self
    }
    pub fn set_padding(&mut self, s: Spacing) -> &mut VerticalLayouterImpl {
        self.d.padding = s;
        self
    }
}

impl Default for VerticalLayouterImpl {
    fn default() -> VerticalLayouterImpl {
        VerticalLayouterImpl {
            d: StackLayoutData::default()
        }
    }
}

impl LayouterImpl for VerticalLayouterImpl {
    fn apply_layouts(&self, widgets: &mut Vec<Box<dyn Widget>>, children: &[ui::WidgetNode],
                   orig_pos: Coord, size_avail: Size) {
        let sized_widgets = self.d.subnodes.iter().fold (0, | acc, sn | {
            if widgets[children[*sn].id].sized_height() {
                acc + 1
            } else {
                acc
            }
        });

        let height_avail = size_avail.h - self.d.spacing * (sized_widgets - 1) as f64 - 2.*self.d.padding;
        let width_avail = size_avail.w - 2.*self.d.padding;
        let (expanders, height_avail) = self.d.subnodes.iter().fold((0, height_avail), |(exp, wa), sn| {
            let wgt = &widgets[children[*sn].id];
            (if wgt.height_expandable() { exp + 1 } else { exp },  wa - wgt.size().h)
        });
        let expand_each = height_avail / expanders as f64;

        let mut pos = orig_pos + Coord { x: self.d.padding, y: self.d.padding };
        for sn in self.d.subnodes.iter() {
            let (height, sized_height)  = {
                let widget = &mut widgets[children[*sn].id];
                if widget.height_expandable() {
                    widget.expand_height(expand_each);
                }
                if widget.width_expandable() {
                    widget.set_width(width_avail);
                }
                widget.set_pos (&pos);
                (widget.size().h, widget.sized_height())
            };
            children[*sn].apply_sizes(widgets, pos);
            if height > 0.0 {
                pos += Coord { x: 0.0, y: height };
            }
            if sized_height {
                pos += Coord { x: 0.0, y:  self.d.spacing };
            }
        }

    }

    fn calc_size(&self, widgets: &mut Vec<Box<dyn Widget>>, children: &[ui::WidgetNode]) -> Size {
        let mut need = Size::default();
        need.h += self.d.padding;
        for subnode in self.d.subnodes.iter() {

            let size = children[*subnode].calc_widget_sizes(widgets);
            need.h += size.h;
            if size.w > need.w {
                need.w = size.w;
            }
            need.h += self.d.spacing
        }
        need.w += 2.*self.d.padding;
        need.h += self.d.padding - self.d.spacing;

        need
    }
}

impl VerticalLayouterImpl {
    fn pack(&mut self, subnode_id: Id, target: StackDirection) { self.d.pack(subnode_id, target) }
}

impl Layouter for VerticalLayouter {
    type Target = StackDirection;
    type Implementor = VerticalLayouterImpl;

    fn new_implementor() -> Box<dyn LayouterImpl> {
        Box::new(VerticalLayouterImpl::default())
    }
    fn pack(&mut self, layout_impl: &mut Self::Implementor, subnode_id: Id, target: Self::Target) {
        layout_impl.pack(subnode_id, target);
    }
    fn expandable() -> (bool, bool) {
        (false, true)
    }
}