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
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use crate::{Element, Renderer};
use derive_setters::Setters;
use iced_core::event::{self, Event};
use iced_core::widget::{Operation, Tree};
use iced_core::{
    layout, mouse, overlay, renderer, Clipboard, Layout, Length, Padding, Rectangle, Shell, Vector,
    Widget,
};

/// Responsively generates rows and columns of widgets based on its dimensions.
#[derive(Setters)]
#[must_use]
pub struct FlexRow<'a, Message> {
    #[setters(skip)]
    children: Vec<Element<'a, Message>>,
    /// Sets the padding around the widget.
    #[setters(into)]
    padding: Padding,
    /// Sets the space between each column of items.
    column_spacing: u16,
    /// Sets the space between each item in a row.
    row_spacing: u16,
    /// Sets the width.
    width: Length,
    /// Sets minimum width of items that grow.
    #[setters(into)]
    min_item_width: Option<f32>,
    /// Sets the max width
    max_width: f32,
    /// Defines how content will be aligned horizontally.
    #[setters(skip)]
    align_items: Option<taffy::AlignItems>,
    /// Defines how content will be aligned vertically.
    #[setters(skip)]
    justify_items: Option<taffy::AlignItems>,
    /// Defines how the content will be justified.
    #[setters(into)]
    justify_content: Option<crate::widget::JustifyContent>,
}

impl<'a, Message> FlexRow<'a, Message> {
    pub(crate) const fn new(children: Vec<Element<'a, Message>>) -> Self {
        Self {
            children,
            padding: Padding::ZERO,
            column_spacing: 4,
            row_spacing: 4,
            width: Length::Shrink,
            min_item_width: None,
            max_width: f32::INFINITY,
            align_items: None,
            justify_items: None,
            justify_content: None,
        }
    }

    /// Defines how content will be aligned horizontally.
    pub fn align_items(mut self, alignment: iced::Alignment) -> Self {
        self.align_items = Some(match alignment {
            iced::Alignment::Center => taffy::AlignItems::Center,
            iced::Alignment::Start => taffy::AlignItems::Start,
            iced::Alignment::End => taffy::AlignItems::End,
        });
        self
    }

    /// Defines how content will be aligned vertically.
    pub fn justify_items(mut self, alignment: iced::Alignment) -> Self {
        self.justify_items = Some(match alignment {
            iced::Alignment::Center => taffy::AlignItems::Center,
            iced::Alignment::Start => taffy::AlignItems::Start,
            iced::Alignment::End => taffy::AlignItems::End,
        });
        self
    }

    /// Sets the space between each column and row.
    pub const fn spacing(mut self, spacing: u16) -> Self {
        self.column_spacing = spacing;
        self.row_spacing = spacing;
        self
    }
}

impl<'a, Message: 'static + Clone> Widget<Message, crate::Theme, Renderer>
    for FlexRow<'a, Message>
{
    fn children(&self) -> Vec<Tree> {
        self.children.iter().map(Tree::new).collect()
    }

    fn diff(&mut self, tree: &mut Tree) {
        tree.diff_children(self.children.as_mut_slice());
    }

    fn size(&self) -> iced_core::Size<Length> {
        iced_core::Size::new(self.width, Length::Shrink)
    }

    fn layout(
        &self,
        tree: &mut Tree,
        renderer: &Renderer,
        limits: &layout::Limits,
    ) -> layout::Node {
        let size = self.size();
        let limits = limits
            .max_width(self.max_width)
            .width(size.width)
            .height(size.height);

        super::layout::resolve(
            renderer,
            &limits,
            &self.children,
            self.padding,
            f32::from(self.column_spacing),
            f32::from(self.row_spacing),
            self.min_item_width,
            self.align_items,
            self.justify_items,
            self.justify_content,
            &mut tree.children,
        )
    }

    fn operate(
        &self,
        tree: &mut Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        operation: &mut dyn Operation<()>,
    ) {
        operation.container(None, layout.bounds(), &mut |operation| {
            self.children
                .iter()
                .zip(&mut tree.children)
                .zip(layout.children())
                .for_each(|((child, state), layout)| {
                    child
                        .as_widget()
                        .operate(state, layout, renderer, operation);
                });
        });
    }

    fn on_event(
        &mut self,
        tree: &mut Tree,
        event: Event,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        renderer: &Renderer,
        clipboard: &mut dyn Clipboard,
        shell: &mut Shell<'_, Message>,
        viewport: &Rectangle,
    ) -> event::Status {
        self.children
            .iter_mut()
            .zip(&mut tree.children)
            .zip(layout.children())
            .map(|((child, state), layout)| {
                child.as_widget_mut().on_event(
                    state,
                    event.clone(),
                    layout,
                    cursor,
                    renderer,
                    clipboard,
                    shell,
                    viewport,
                )
            })
            .fold(event::Status::Ignored, event::Status::merge)
    }

    fn mouse_interaction(
        &self,
        tree: &Tree,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        viewport: &Rectangle,
        renderer: &Renderer,
    ) -> mouse::Interaction {
        self.children
            .iter()
            .zip(&tree.children)
            .zip(layout.children())
            .map(|((child, state), layout)| {
                child
                    .as_widget()
                    .mouse_interaction(state, layout, cursor, viewport, renderer)
            })
            .max()
            .unwrap_or_default()
    }

    fn draw(
        &self,
        tree: &Tree,
        renderer: &mut Renderer,
        theme: &crate::Theme,
        style: &renderer::Style,
        layout: Layout<'_>,
        cursor: mouse::Cursor,
        viewport: &Rectangle,
    ) {
        for ((child, state), layout) in self
            .children
            .iter()
            .zip(&tree.children)
            .zip(layout.children())
        {
            child
                .as_widget()
                .draw(state, renderer, theme, style, layout, cursor, viewport);
        }
    }

    fn overlay<'b>(
        &'b mut self,
        tree: &'b mut Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        translation: Vector,
    ) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> {
        overlay::from_children(&mut self.children, tree, layout, renderer, translation)
    }

    #[cfg(feature = "a11y")]
    /// get the a11y nodes for the widget
    fn a11y_nodes(
        &self,
        layout: Layout<'_>,
        state: &Tree,
        p: mouse::Cursor,
    ) -> iced_accessibility::A11yTree {
        use iced_accessibility::A11yTree;
        A11yTree::join(
            self.children
                .iter()
                .zip(layout.children())
                .zip(state.children.iter())
                .map(|((c, c_layout), state)| c.as_widget().a11y_nodes(c_layout, state, p)),
        )
    }

    fn drag_destinations(
        &self,
        state: &Tree,
        layout: Layout<'_>,
        renderer: &Renderer,
        dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles,
    ) {
        for ((e, layout), state) in self
            .children
            .iter()
            .zip(layout.children())
            .zip(state.children.iter())
        {
            e.as_widget()
                .drag_destinations(state, layout, renderer, dnd_rectangles);
        }
    }
}

impl<'a, Message: 'static + Clone> From<FlexRow<'a, Message>> for Element<'a, Message> {
    fn from(flex_row: FlexRow<'a, Message>) -> Self {
        Self::new(flex_row)
    }
}