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
use crate::widget;
use alloc::boxed::Box;
use dynamic_cast::impl_supports_interfaces;
use tuifw_screen_base::{Rect, Vector, Thickness, Point};
use tuifw_window::{Event, Layout, RenderPort, Widget, WidgetData, Window, WindowTree, App};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Dock { Left, Top, Right, Bottom }

#[derive(Clone)]
struct DockLayout {
    dock: Option<Dock>,
}

impl Layout for DockLayout { }

widget! {
    #[widget(DockPanelWidget)]
    pub struct DockPanel { }
}

impl DockPanel {
    pub fn dock(tree: &WindowTree, window: Window) -> Option<Dock> {
        window.layout::<DockLayout>(tree).and_then(|x| x.dock)
    }

    pub fn set_dock(tree: &mut WindowTree, window: Window, value: Option<Dock>) {
        window.set_layout(tree, Some(Box::new(DockLayout { dock: value })));
    }
}

#[derive(Clone, Default)]
pub struct DockPanelWidget;

impl_supports_interfaces!(DockPanelWidget);

impl Widget for DockPanelWidget {
    fn new(&self) -> Box<dyn WidgetData> {
        Box::new(DockPanel { })
    }

    fn clone_data(
        &self,
        tree: &mut WindowTree,
        source: Window,
        dest: Window,
        clone_window: Box<dyn Fn(&WindowTree, Window) -> Window>,
    ) {
        DockPanel::clone(tree, source, dest, clone_window);
    }

    fn render(
        &self,
        _tree: &WindowTree,
        _window: Window,
        _rp: &mut RenderPort,
        _app: &mut dyn App,
    ) { }

    fn measure(
        &self,
        tree: &mut WindowTree,
        window: Window,
        mut available_width: Option<i16>,
        mut available_height: Option<i16>,
        app: &mut dyn App,
    ) -> Vector {
        if let Some(first_child) = window.first_child(tree) {
            let mut size = Vector::null();
            let mut docked = Thickness::all(0);
            let mut child = first_child;
            loop {
                let dock = DockPanel::dock(tree, child);
                match dock {
                    None => { },
                    Some(Dock::Left) => {
                        child.measure(tree, None, available_height, app);
                        if let Some(available_width) = available_width.as_mut() {
                            *available_width =
                                (*available_width as u16).saturating_sub(child.desired_size(tree).x as u16) as i16;
                        }
                        size = size.max(Vector { x: 0, y: child.desired_size(tree).y });
                        let docked_child = Thickness::new(i32::from(child.desired_size(tree).x), 0, 0, 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Top) => {
                        child.measure(tree, available_width, None, app);
                        if let Some(available_height) = available_height.as_mut() {
                            *available_height =
                                (*available_height as u16).saturating_sub(child.desired_size(tree).y as u16) as i16;
                        }
                        size = size.max(Vector { x: child.desired_size(tree).x, y: 0 });
                        let docked_child = Thickness::new(0, i32::from(child.desired_size(tree).y), 0, 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Right) => {
                        child.measure(tree, None, available_height, app);
                        if let Some(available_width) = available_width.as_mut() {
                            *available_width =
                                (*available_width as u16).saturating_sub(child.desired_size(tree).x as u16) as i16;
                        }
                        size = size.max(Vector { x: 0, y: child.desired_size(tree).y });
                        let docked_child = Thickness::new(0, 0, i32::from(child.desired_size(tree).x), 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Bottom) => {
                        child.measure(tree, available_width, None, app);
                        if let Some(available_height) = available_height.as_mut() {
                            *available_height =
                                (*available_height as u16).saturating_sub(child.desired_size(tree).y as u16) as i16;
                        }
                        size = size.max(Vector { x: child.desired_size(tree).x, y: 0 });
                        let docked_child = Thickness::new(0, 0, 0, i32::from(child.desired_size(tree).y));
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                }
                child = child.next(tree);
                if child == first_child { break; }
            }
            let mut child = first_child;
            loop {
                let dock = DockPanel::dock(tree, child);
                if dock.is_none() {
                    child.measure(tree, available_width, available_height, app);
                    size = size.max(child.desired_size(tree));
                }
                child = child.next(tree);
                if child == first_child { break; }
            }
            docked.expand_rect_size(size)
        } else {
            Vector::null()
        }
    }

    fn arrange(
        &self,
        tree: &mut WindowTree,
        window: Window,
        final_inner_bounds: Rect,
        app: &mut dyn App,
    ) -> Vector {
        if let Some(first_child) = window.first_child(tree) {
            let mut size = Vector::null();
            let mut docked = Thickness::all(0);
            let mut child = first_child;
            loop {
                let bounds = docked.shrink_rect(final_inner_bounds);
                let dock = DockPanel::dock(tree, child);
                match dock {
                    None => { },
                    Some(Dock::Left) => {
                        child.arrange(
                            tree,
                            Rect { tl: bounds.tl, size: Vector { x: child.desired_size(tree).x, y: bounds.h() } },
                            app
                        );
                        size = size.max(Vector { x: 0, y: child.desired_size(tree).y });
                        let docked_child = Thickness::new(i32::from(child.desired_size(tree).x), 0, 0, 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Top) => {
                        child.arrange(
                            tree,
                            Rect { tl: bounds.tl, size: Vector { x: bounds.w(), y: child.desired_size(tree).y } },
                            app
                        );
                        size = size.max(Vector { x: child.desired_size(tree).x, y: 0 });
                        let docked_child = Thickness::new(0, i32::from(child.desired_size(tree).y), 0, 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Right) => {
                        child.arrange(
                            tree,
                            Rect::from_tl_br(
                                Point {
                                    x: bounds.r().wrapping_sub(child.desired_size(tree).x),
                                    y: bounds.t()
                                },
                                bounds.br()
                            ),
                            app
                        );
                        size = size.max(Vector { x: 0, y: child.desired_size(tree).y });
                        let docked_child = Thickness::new(0, 0, i32::from(child.desired_size(tree).x), 0);
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                    Some(Dock::Bottom) => {
                        child.arrange(
                            tree,
                            Rect::from_tl_br(
                                Point {
                                    x: bounds.l(),
                                    y: bounds.b().wrapping_sub(child.desired_size(tree).y)
                                },
                                bounds.br()
                            ),
                            app
                        );
                        size = size.max(Vector { x: child.desired_size(tree).x, y: 0 });
                        let docked_child = Thickness::new(0, 0, 0, i32::from(child.desired_size(tree).y));
                        docked += docked_child;
                        size = docked_child.shrink_rect_size(size);
                    },
                }
                child = child.next(tree);
                if child == first_child { break; }
            }
            let bounds = docked.shrink_rect(final_inner_bounds);
            let mut child = first_child;
            loop {
                let dock = DockPanel::dock(tree, child);
                if dock.is_none() {
                    child.arrange(tree, bounds, app);
                    size = size.max(child.render_bounds(tree).size);
                }
                child = child.next(tree);
                if child == first_child { break; }
            }
            docked.expand_rect_size(size)
        } else {
            Vector::null()
        }
    }

    fn update(
        &self,
        _tree: &mut WindowTree,
        _window: Window,
        _event: Event,
        _event_source: Window,
        _app: &mut dyn App,
    ) -> bool {
        false
    }
}