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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use super::internal;
use crate::prelude::*;

/// Modifiers for changing the layout properties of a view.
pub trait LayoutModifiers: internal::Modifiable {
    modifier!(
        /// Sets the layout type of the view.
        ///
        /// The layout type controls how a parent will position any children which have `PositionType::ParentDirected`.
        /// Accepts any value, or lens to a target, with a type which can be converted into `LayoutType`.
        ///
        /// There are three variants:
        /// - `LayoutType::Row` - Parent will stack its children horizontally.
        /// - `LayoutType::Column` - (default) Parent will stack its children vertically.
        /// - `LayoutType::Grid` - The position of children is determine by the grid properties.
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// #[derive(Lens, Model, Setter)]
        /// pub struct AppData {
        ///     layout_type: LayoutType,
        /// }
        ///
        /// # AppData {
        /// #   layout_type: LayoutType::Row,
        /// # }.build(cx);
        ///
        /// Element::new(cx).layout_type(LayoutType::Row);  // Value of type `LayoutType`.
        /// Element::new(cx).layout_type(AppData::layout_type); // Lens to target of type `LayoutType`.
        /// ```
        layout_type,
        LayoutType,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the position type of the view.
        ///
        /// The position type determines how a child will be positioned within a parent.
        ///
        /// - `PositionType::ParentDirected` - The child will be positioned relative to its siblings in a stack
        /// (if parent layout type is `Row` or `Column`), or relative to its grid position (if parent layout type is `Grid`).
        /// - `PositionType::SelfDirected` - The child will be positioned relative to the top-left corner of its parents bounding box
        /// and will ignore its siblings or grid position. This is approximately equivalent to absolute positioning.
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// Element::new(cx).position_type(PositionType::SelfDirected);
        /// ```
        position_type,
        PositionType,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space on the left side of the view.
        ///
        /// The left space, along with the right space, determines the horizontal position of a view.
        ///
        /// - `Units::Pixels(...)` - The left space will be a fixed number of points. This will scale with the DPI of the target display.
        /// - `Units::Percentage(...)` - The left space will be a proportion of the parent width.
        /// - `Units::Stretch(...)` - The left space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
        /// - `Units::Auto` - The left space will be determined by the parent `child-left`, see [`child_left`](crate::prelude::LayoutModifiers::left).
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// Element::new(cx).left(Units::Pixels(100.0));
        /// ```
        left,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space on the right side of the view.
        ///
        /// The right space, along with the left space, determines the horizontal position of a view.
        ///
        /// - `Units::Pixels(...)` - The right space will be a fixed number of points. This will scale with the DPI of the target display.
        /// - `Units::Percentage(...)` - The right space will be a proportion of the parent width.
        /// - `Units::Stretch(...)` - The right space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
        /// - `Units::Auto` - The right space will be determined by the parent `child-left`, see [`child_left`](crate::prelude::LayoutModifiers::left).
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// Element::new(cx).right(Units::Pixels(100.0));
        /// ```
        right,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space on the top side of the view.
        ///
        /// The top space, along with the bottom space, determines the vertical position of a view.
        ///
        /// - `Units::Pixels(...)` - The top space will be a fixed number of points. This will scale with the DPI of the target display.
        /// - `Units::Percentage(...)` - The top space will be a proportion of the parent width.
        /// - `Units::Stretch(...)` - The top space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
        /// - `Units::Auto` - The top space will be determined by the parent `child-left`, see [`child_left`](crate::prelude::LayoutModifiers::left).
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// Element::new(cx).top(Units::Pixels(100.0));
        /// ```
        top,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space on the bottom side of the view.
        ///
        /// The bottom space, along with the top space, determines the vertical position of a view.
        ///
        /// - `Units::Pixels(...)` - The bottom space will be a fixed number of points. This will scale with the DPI of the target display.
        /// - `Units::Percentage(...)` - The bottom space will be a proportion of the parent width.
        /// - `Units::Stretch(...)` - The bottom space will be a ratio of the remaining free space, see [`Units`](crate::prelude::Units).
        /// - `Units::Auto` - The bottom space will be determined by the parent `child-left`, see [`child_left`](crate::prelude::LayoutModifiers::left).
        ///
        /// # Example
        /// ```
        /// # use vizia_core::prelude::*;
        /// # let cx = &mut Context::default();
        /// Element::new(cx).bottom(Units::Pixels(100.0));
        /// ```
        bottom,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the space for all sides of the view.
    fn space<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, |cx, v| {
                let value = v.get(cx).into();
                cx.style.left.insert(cx.current, value);
                cx.style.right.insert(cx.current, value);
                cx.style.top.insert(cx.current, value);
                cx.style.bottom.insert(cx.current, value);

                cx.style.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the width of the view.
        width,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the height of the view.
        height,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the width and height of the view.
    fn size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.width.insert(cx.current, value);
                cx.style.height.insert(cx.current, value);

                cx.style.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the space between the left side of the view and the left side of its children.
        ///
        /// Applies only to child views which have a `left` property set to `Auto`.
        child_left,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space between the right side of the view and the right side of its children.
        ///
        /// Applies only to child views which have a `right` property set to `Auto`.
        child_right,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space between the top side of the view and the top side of its children.
        ///
        /// Applies only to child views which have a `top` property set to `Auto`.
        child_top,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space between the bottom side of the view and the bottom side of its children.
        ///
        /// Applies only to child views which have a `bottom` property set to `Auto`.
        child_bottom,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the space between the vew and its children.
    ///
    /// The child_space works by overriding the `Auto` space properties of its children.
    fn child_space<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.child_left.insert(cx.current, value);
                cx.style.child_right.insert(cx.current, value);
                cx.style.child_top.insert(cx.current, value);
                cx.style.child_bottom.insert(cx.current, value);

                cx.style.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the space between the views children in a vertical stack.
        row_between,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the space between the views children in a horizontal stack.
        col_between,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the minimum width of the view.
        min_width,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the minimum height of the view.
        min_height,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the minimum width and minimum height of the view.
    fn min_size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.min_width.insert(cx.current, value);
                cx.style.min_height.insert(cx.current, value);

                cx.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the maximum width of the view.
        max_width,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the maximum height of the view.
        max_height,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the maximum width and maximum height of the view.
    fn max_size<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(entity, |cx| {
            value.set_or_bind(cx, current, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.max_width.insert(cx.current, value);
                cx.style.max_height.insert(cx.current, value);

                cx.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the minimum left space of the view.
        min_left,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the minimum right space of the view.
        min_right,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the minimum top space of the view.
        min_top,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the minimum bottom space of the view.
        min_bottom,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the minimum space for all sides of the view.
    fn min_space<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.min_left.insert(cx.current, value);
                cx.style.min_right.insert(cx.current, value);
                cx.style.min_top.insert(cx.current, value);
                cx.style.min_bottom.insert(cx.current, value);

                cx.style.needs_relayout();
            });
        });

        self
    }

    modifier!(
        /// Sets the maximum left space of the view.
        max_left,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the maximum right space of the view.
        max_right,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the maximum top space of the view.
        max_top,
        Units,
        SystemFlags::RELAYOUT
    );

    modifier!(
        /// Sets the maximum bottom space of the view.
        max_bottom,
        Units,
        SystemFlags::RELAYOUT
    );

    /// Sets the maximum space for all sides of the view.
    fn max_space<U: Into<Units>>(mut self, value: impl Res<U>) -> Self {
        let entity = self.entity();
        let current = self.current();
        self.context().with_current(current, |cx| {
            value.set_or_bind(cx, entity, move |cx, v| {
                let value = v.get(cx).into();
                cx.style.max_left.insert(cx.current, value);
                cx.style.max_right.insert(cx.current, value);
                cx.style.max_top.insert(cx.current, value);
                cx.style.max_bottom.insert(cx.current, value);

                cx.style.needs_relayout();
            });
        });

        self
    }
}

impl<'a, V: View> LayoutModifiers for Handle<'a, V> {}