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
use egui::Rect;

use crate::Tree;

use super::{Behavior, DropContext, SimplifyAction, TileId, Tiles};

mod grid;
mod linear;
mod tabs;

pub use grid::{Grid, GridLayout};
pub use linear::{Linear, LinearDir, Shares};
pub use tabs::Tabs;

// ----------------------------------------------------------------------------

/// The layout type of a [`Container`].
///
/// This is used to describe a [`Container`], and to change it to a different layout type.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ContainerKind {
    /// Each child in an individual tab.
    #[default]
    Tabs,

    /// Left-to-right
    Horizontal,

    /// Top-down
    Vertical,

    /// In a grid, laied out row-wise, left-to-right, top-down.
    Grid,
}

impl ContainerKind {
    pub const ALL: [Self; 4] = [Self::Tabs, Self::Horizontal, Self::Vertical, Self::Grid];
}

// ----------------------------------------------------------------------------

/// A container of several [`super::Tile`]s.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Container {
    Tabs(Tabs),
    Linear(Linear),
    Grid(Grid),
}

impl From<Tabs> for Container {
    #[inline]
    fn from(tabs: Tabs) -> Self {
        Self::Tabs(tabs)
    }
}

impl From<Linear> for Container {
    #[inline]
    fn from(linear: Linear) -> Self {
        Self::Linear(linear)
    }
}

impl From<Grid> for Container {
    #[inline]
    fn from(grid: Grid) -> Self {
        Self::Grid(grid)
    }
}

impl Container {
    pub fn new(typ: ContainerKind, children: Vec<TileId>) -> Self {
        match typ {
            ContainerKind::Tabs => Self::new_tabs(children),
            ContainerKind::Horizontal => Self::new_horizontal(children),
            ContainerKind::Vertical => Self::new_vertical(children),
            ContainerKind::Grid => Self::new_grid(children),
        }
    }

    pub fn new_linear(dir: LinearDir, children: Vec<TileId>) -> Self {
        Self::Linear(Linear::new(dir, children))
    }

    pub fn new_horizontal(children: Vec<TileId>) -> Self {
        Self::new_linear(LinearDir::Horizontal, children)
    }

    pub fn new_vertical(children: Vec<TileId>) -> Self {
        Self::new_linear(LinearDir::Vertical, children)
    }

    pub fn new_tabs(children: Vec<TileId>) -> Self {
        Self::Tabs(Tabs::new(children))
    }

    pub fn new_grid(children: Vec<TileId>) -> Self {
        Self::Grid(Grid::new(children))
    }

    pub fn is_empty(&self) -> bool {
        self.num_children() == 0
    }

    pub fn num_children(&self) -> usize {
        match self {
            Container::Tabs(tabs) => tabs.children.len(),
            Container::Linear(linear) => linear.children.len(),
            Container::Grid(grid) => grid.num_children(),
        }
    }

    /// All the childrens of this container.
    pub fn children(&self) -> impl Iterator<Item = &TileId> {
        match self {
            Self::Tabs(tabs) => itertools::Either::Left(tabs.children.iter()),
            Self::Linear(linear) => itertools::Either::Left(linear.children.iter()),
            Self::Grid(grid) => itertools::Either::Right(grid.children()),
        }
    }

    /// All the active childrens of this container.
    ///
    /// For tabs, this is just the active tab.
    /// For other containers, it is all children.
    pub fn active_children(&self) -> impl Iterator<Item = &TileId> {
        match self {
            Self::Tabs(tabs) => {
                itertools::Either::Left(itertools::Either::Left(tabs.active.iter()))
            }
            Self::Linear(linear) => {
                itertools::Either::Left(itertools::Either::Right(linear.children.iter()))
            }
            Self::Grid(grid) => itertools::Either::Right(grid.children()),
        }
    }

    /// If we have exactly one child, return it
    pub fn only_child(&self) -> Option<TileId> {
        let mut only_child = None;
        for &child in self.children() {
            if only_child.is_none() {
                only_child = Some(child);
            } else {
                return None;
            }
        }
        only_child
    }

    pub fn children_vec(&self) -> Vec<TileId> {
        self.children().copied().collect()
    }

    pub fn has_child(&self, needle: TileId) -> bool {
        self.children().any(|&t| t == needle)
    }

    pub fn add_child(&mut self, child: TileId) {
        match self {
            Self::Tabs(tabs) => tabs.add_child(child),
            Self::Linear(linear) => linear.add_child(child),
            Self::Grid(grid) => grid.add_child(child),
        }
    }

    /// Iterate through all children in order, and keep only those for which the closure returns `true`.
    pub fn retain(&mut self, mut retain: impl FnMut(TileId) -> bool) {
        match self {
            Self::Tabs(tabs) => tabs.children.retain(|tile_id: &TileId| retain(*tile_id)),
            Self::Linear(linear) => linear.children.retain(|tile_id: &TileId| retain(*tile_id)),
            Self::Grid(grid) => grid.retain(retain),
        }
    }

    /// Returns child index, if found.
    pub fn remove_child(&mut self, child: TileId) -> Option<usize> {
        match self {
            Container::Tabs(tabs) => tabs.remove_child(child),
            Container::Linear(linear) => linear.remove_child(child),
            Container::Grid(grid) => grid.remove_child(child),
        }
    }

    pub fn kind(&self) -> ContainerKind {
        match self {
            Self::Tabs(_) => ContainerKind::Tabs,
            Self::Linear(linear) => match linear.dir {
                LinearDir::Horizontal => ContainerKind::Horizontal,
                LinearDir::Vertical => ContainerKind::Vertical,
            },
            Self::Grid(_) => ContainerKind::Grid,
        }
    }

    pub fn set_kind(&mut self, kind: ContainerKind) {
        if kind == self.kind() {
            return;
        }

        *self = match kind {
            ContainerKind::Tabs => Self::Tabs(Tabs::new(self.children_vec())),
            ContainerKind::Horizontal => {
                Self::Linear(Linear::new(LinearDir::Horizontal, self.children_vec()))
            }
            ContainerKind::Vertical => {
                Self::Linear(Linear::new(LinearDir::Vertical, self.children_vec()))
            }
            ContainerKind::Grid => Self::Grid(Grid::new(self.children_vec())),
        };
    }

    pub(super) fn simplify_children(&mut self, simplify: impl FnMut(TileId) -> SimplifyAction) {
        match self {
            Self::Tabs(tabs) => tabs.simplify_children(simplify),
            Self::Linear(linear) => linear.simplify_children(simplify),
            Self::Grid(grid) => grid.simplify_children(simplify),
        }
    }

    pub(super) fn layout<Pane>(
        &mut self,
        tiles: &mut Tiles<Pane>,
        style: &egui::Style,
        behavior: &mut dyn Behavior<Pane>,
        rect: Rect,
    ) {
        if self.is_empty() {
            return;
        }

        match self {
            Container::Tabs(tabs) => tabs.layout(tiles, style, behavior, rect),
            Container::Linear(linear) => {
                linear.layout(tiles, style, behavior, rect);
            }
            Container::Grid(grid) => grid.layout(tiles, style, behavior, rect),
        }
    }

    pub(super) fn ui<Pane>(
        &mut self,
        tree: &mut Tree<Pane>,
        behavior: &mut dyn Behavior<Pane>,
        drop_context: &mut DropContext,
        ui: &mut egui::Ui,
        rect: Rect,
        tile_id: TileId,
    ) {
        match self {
            Container::Tabs(tabs) => {
                tabs.ui(tree, behavior, drop_context, ui, rect, tile_id);
            }
            Container::Linear(linear) => {
                linear.ui(tree, behavior, drop_context, ui, tile_id);
            }
            Container::Grid(grid) => {
                grid.ui(tree, behavior, drop_context, ui, tile_id);
            }
        }
    }
}