iced_rizzen 0.14.0

Extra widgets for official releases of iced GUI library.
Documentation
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
// This file is part of `iced_rizzen` project. For the terms of use, please see the file
// called `LICENSE-BSD-3-Clause` at the top level of the `iced_rizzen` project's Git repository.

//! The content structs and builder structs for the [`CellGrid`] widget.

#[doc(inline)]
#[allow(unused_imports)]
use super::{
    cellgrid::CellGrid,
    style,
    types::{ContentSize, Portion, PortionProperties, Property, PropertyMinimal},
};
use crate::core::{Size, renderer};
use iced_widget::{
    Space,
    core::{Element, Point},
};

//
// ----- Content
//

/// The content with layout data for the [`CellGrid`] widget. Consists of a
/// single [`Vec<Element>`] representing the cells' child widgets, four
/// [`Vec<bool>] representing the active state of the cells within the four
/// regions, and a reference to [`ContentLayout`] struct containing the layout
/// data of the content.
///
/// The preferred way is to create the [`Content`] struct within the
/// application's `view()` function by using the content builder strut
/// [`ContentBuilder`] and finally calling the `.into()`.
pub struct Content<'a, Message, Theme, Renderer>
where
    Renderer: renderer::Renderer,
    Theme: style::Catalog,
{
    // Content
    /// In the widget struct all the [`Element`]s MUST be placed into a single
    /// vector [`Vec<Element<'a, Message, Theme, Renderer>>`], due to an issue
    /// within the `Widget::diff()` function where the mutable referenced
    /// `Element` vector can't be constructed correctly in order for it to be
    /// passed as a parameter to `Tree::diff_children()`.
    pub elements: Vec<Element<'a, Message, Theme, Renderer>>,
    /// The up left region active vector.
    pub active_up_left: Vec<bool>,
    /// The up right region active vector.
    pub active_up_right: Vec<bool>,
    /// The down left region active vector.
    pub active_down_left: Vec<bool>,
    /// The down right region active vector.
    pub active_down_right: Vec<bool>,

    // Layout
    /// The layout of the cells portions in the four areas.
    pub layout: &'a ContentLayout,
}

//
// ----- ContentLayout
//

/// The layout data of the content for [`Content`] struct. Consists of the four
/// portions' ranges, the four regions' cell counts, the region indices into the
/// [`Vec<Element<'a, Message, Theme, Renderer>>`], the four properties
/// portions, size information, and scrolling offset.
///
/// The preferred way is to create the [`ContentLayout`] struct within the
/// application's state "new" function by using the the content layout
/// builder strut [`ContentLayoutBuilder`] and finally calling the
/// [`ContentLayoutBuilder::build()`] function.
pub struct ContentLayout {
    // Regions
    /// The columns range for the optional left portion. Includes hidden columns.
    pub range_left: Option<usize>, // Just end, as start = 0
    /// The columns range for the right portion. Includes hidden columns.
    ///
    /// Note: There can be zero columns of data, thus is an [`Option`].
    pub range_right: Option<(usize, usize)>,
    /// The rows range for the optional up portion. Includes hidden rows.
    pub range_up: Option<usize>, // Just end, as start = 0
    /// The rows range for the down portion. Includes hidden rows.
    ///
    /// Note: There can be zero rows of data, thus is an [`Option`].
    pub range_down: Option<(usize, usize)>,
    /// Up left region cell count (includes the labels), minimum of 1.
    pub cells_up_left: usize,
    /// Up right region cell count (includes the labels), minimum of 0.
    pub cells_up_right: usize,
    /// Down left region cell count (includes the labels), minimum of 0.
    pub cells_down_left: usize,
    /// Down right region cell count, minimum of 0.
    pub cells_down_right: usize,
    /// Maximum number of [`Element<'a, Message, Theme, Renderer>`]s.
    pub cells_total: usize,
    /// Index to up right region in [`Vec<Element<'a, Message, Theme, Renderer>>`].
    /// Index of 0 indicates there are no elements.
    pub index_up_right: usize,
    /// Index to down left region in [`Vec<Element<'a, Message, Theme, Renderer>>`].
    /// Index of 0 indicates there are no elements.
    pub index_down_left: usize,
    /// Index to down right region in
    /// [`Vec<Element<'a, Message, Theme, Renderer>>`].
    /// Index of 0 indicates there are no elements .
    pub index_down_right: usize,

    // Layout
    /// Properties of the left columns.
    pub properties_left: PortionProperties,
    /// Properties of the up rows.
    pub properties_up: PortionProperties,
    /// Properties of the right columns.
    pub properties_right: PortionProperties,
    /// Properties of the down rows.
    pub properties_down: PortionProperties,
    /// The total size of the content including hidden columns and rows.
    pub size_total: ContentSize,
    /// The size of the visible content.
    pub size: ContentSize,
    /// The total size of the scrollable visible content.
    pub size_scrolling: ContentSize,
    /// The initial scrolled offset.
    pub scrolled_offset: Option<Point>,
}

impl ContentLayout {
    /// Partially update the layout; either the columns or the rows.
    ///
    /// Warning: Ensure the properties has been updated before calling this
    /// function.
    ///
    /// The vector references are all pointing to vectors stored in the
    /// application state. The parameters `horizontal` and `vertical` are the
    /// page breaks. The parameter `portion` indicates the portion where the
    /// change took place.
    pub fn update(
        &mut self,
        columns: &Vec<PropertyMinimal>,
        rows: &Vec<PropertyMinimal>,
        horizontal: &Vec<f32>,
        vertical: &Vec<f32>,
        portion: &Portion,
    ) {
        match portion {
            Portion::Left | Portion::Right => {
                let (size_total, size, size_scrolling, properties_left, properties_right) =
                    build_columns(columns, &self.range_left, &self.range_right, horizontal); //&self.page);
                self.size_total.pixels.width = size_total.pixels.width;
                self.size.pixels.width = size.pixels.width;
                self.size_scrolling.pixels.width = size_scrolling.pixels.width;
                self.properties_left = properties_left;
                self.properties_right = properties_right;
            }
            Portion::Up | Portion::Down => {
                let (size_total, size, size_scrolling, properties_up, properties_down) =
                    build_rows(rows, &self.range_up, &self.range_down, vertical); //&self.page);
                self.size_total.pixels.height = size_total.pixels.height;
                self.size.pixels.height = size.pixels.height;
                self.size_scrolling.pixels.height = size_scrolling.pixels.height;
                self.properties_up = properties_up;
                self.properties_down = properties_down;
            }
        }
    }
}

fn get_portion(
    size_total: &mut f32,
    size: &mut f32,
    total: &mut usize,
    visible: &mut usize,
    minimal: &Vec<PropertyMinimal>,
    end: usize,
) -> (Vec<Property>, f32) {
    let mut properties = Vec::new();
    let mut size_portion = 0.0;
    while *total <= end {
        let offset = *size;
        let width = minimal[*total].size;
        *size_total += width;
        if !minimal[*total].hidden {
            *size += width;
            size_portion += width;
            properties.push(Property {
                offset,
                index: *total,
                size: width,
                range: minimal[*total].range,
            });
            *visible += 1;
        }
        *total += 1;
    }
    (properties, size_portion)
}

fn get_page_breaks(
    properties: &Vec<Property>,
    size_portion: f32,
    page_breaks: &Vec<f32>,
) -> Vec<f32> {
    let mut breaks = Vec::new();
    let end = properties[0].offset + size_portion;
    for page in page_breaks {
        if *page >= properties[0].offset && *page < end {
            breaks.push(*page);
        }
    }
    breaks.shrink_to(breaks.len());
    breaks
}

/// Builds the two column portions.
///
/// The vector references are all pointing to vectors stored in the
/// application state.
pub fn build_columns(
    columns: &Vec<PropertyMinimal>,
    range_left: &Option<usize>,
    range_right: &Option<(usize, usize)>,
    page_breaks: &Vec<f32>,
) -> (
    ContentSize,
    ContentSize,
    ContentSize,
    PortionProperties,
    PortionProperties,
) {
    let mut size_total = 0.0; // Includes hidden columns and rows.
    let mut size = 0.0; // Excludes hidden columns and rows.
    let mut total = 0;
    let mut visible = 0;
    let mut properties_left = PortionProperties::default();
    let mut properties_right = PortionProperties::default();
    if page_breaks.is_empty() {
        if let Some(end) = range_left {
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                columns,
                *end,
            );
            properties_left = PortionProperties {
                size: size_portion,
                properties,
                pages: Vec::with_capacity(0),
            };
        }
        if let Some((start, end)) = range_right {
            while total < *start {
                let width = columns[total].size;
                size_total += width;
                if !columns[total].hidden {
                    size += width;
                    visible += 1;
                }
                total += 1;
            }
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                columns,
                *end,
            );
            properties_right = PortionProperties {
                size: size_portion,
                properties,
                pages: Vec::with_capacity(0),
            };
        }
    } else {
        if let Some(end) = range_left {
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                columns,
                *end,
            );
            let pages = if !properties.is_empty() {
                get_page_breaks(&properties, size_portion, &page_breaks)
            } else {
                Vec::with_capacity(0)
            };
            properties_left = PortionProperties {
                size: size_portion,
                properties,
                pages,
            };
        }
        if let Some((start, end)) = range_right {
            while total < *start {
                let width = columns[total].size;
                size_total += width;
                if !columns[total].hidden {
                    size += width;
                    visible += 1;
                }
                total += 1;
            }
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                columns,
                *end,
            );
            let pages = if !properties.is_empty() {
                get_page_breaks(&properties, size_portion, &page_breaks)
            } else {
                Vec::with_capacity(0)
            };
            properties_right = PortionProperties {
                size: size_portion,
                properties,
                pages,
            };
        }
    }
    let size_scrolling = ContentSize {
        pixels: Size {
            height: 0.0,
            width: size - properties_left.size,
        },
        columns: visible - properties_left.len(),
        rows: 0,
    };
    (
        ContentSize {
            pixels: Size {
                width: size_total,
                height: 0.0,
            },
            columns: total,
            rows: 0,
        },
        ContentSize {
            pixels: Size {
                width: size,
                height: 0.0,
            },
            columns: visible,
            rows: 0,
        },
        size_scrolling,
        properties_left,
        properties_right,
    )
}

/// Builds the two rows portions
///
/// The vector references are all pointing to vectors stored in the
/// application state.
pub fn build_rows(
    rows: &Vec<PropertyMinimal>,
    range_up: &Option<usize>,
    range_down: &Option<(usize, usize)>,
    page_breaks: &Vec<f32>,
) -> (
    ContentSize,
    ContentSize,
    ContentSize,
    PortionProperties,
    PortionProperties,
) {
    let mut size_total = 0.0; // Includes hidden columns and rows.
    let mut size = 0.0; // Excludes hidden columns and rows.
    let mut total = 0;
    let mut visible = 0;
    let mut properties_up = PortionProperties::default();
    let mut properties_down = PortionProperties::default();
    if page_breaks.is_empty() {
        if let Some(end) = range_up {
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                rows,
                *end,
            );
            properties_up = PortionProperties {
                size: size_portion,
                properties,
                pages: Vec::with_capacity(0),
            };
        }
        if let Some((start, end)) = range_down {
            while total < *start {
                let height = rows[total].size;
                size_total += height;
                if !rows[total].hidden {
                    size += height;
                    visible += 1;
                }
                total += 1;
            }
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                rows,
                *end,
            );
            properties_down = PortionProperties {
                size: size_portion,
                properties,
                pages: Vec::with_capacity(0),
            };
        }
    } else {
        if let Some(end) = range_up {
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                rows,
                *end,
            );
            let pages = if !properties.is_empty() {
                get_page_breaks(&properties, size_portion, &page_breaks)
            } else {
                Vec::with_capacity(0)
            };
            properties_up = PortionProperties {
                size: size_portion,
                properties,
                pages,
            };
        }
        if let Some((start, end)) = range_down {
            while total < *start {
                let height = rows[total].size;
                size_total += height;
                if !rows[total].hidden {
                    size += height;
                    visible += 1;
                }
                total += 1;
            }
            let (properties, size_portion) = get_portion(
                &mut size_total,
                &mut size,
                &mut total,
                &mut visible,
                rows,
                *end,
            );
            let pages = if !properties.is_empty() {
                get_page_breaks(&properties, size_portion, &page_breaks)
            } else {
                Vec::with_capacity(0)
            };
            properties_down = PortionProperties {
                size: size_portion,
                properties,
                pages,
            };
        }
    }
    let size_scrolling = ContentSize {
        pixels: Size {
            height: size - properties_up.size,
            width: 0.0,
        },
        columns: 0,
        rows: visible - properties_up.len(),
    };
    (
        ContentSize {
            pixels: Size {
                width: 0.0,
                height: size_total,
            },
            columns: 0,
            rows: total,
        },
        ContentSize {
            pixels: Size {
                width: 0.0,
                height: size,
            },
            columns: 0,
            rows: visible,
        },
        size_scrolling,
        properties_up,
        properties_down,
    )
}

//
// ----- ContentLayoutBuilder
//

/// The builder struct for [`ContentLayout`].
pub struct ContentLayoutBuilder {
    range_left: Option<usize>, // Just end, as start = 0
    range_up: Option<usize>,   // Just end, as start = 0
    range_right: Option<(usize, usize)>,
    range_down: Option<(usize, usize)>,
    scrolled_offset: Option<Point>,
}

impl ContentLayoutBuilder {
    /// Creates a new builder struct for [`ContentLayout`].
    pub fn new() -> Self {
        Self {
            range_left: None,
            range_up: None,
            range_right: None,
            range_down: None,
            scrolled_offset: None,
        }
    }

    /// Specify the columns range for the left portion.
    ///
    /// The range always starts at 0 for left columns.
    pub fn set_left_range(mut self, end: usize) -> Self {
        self.range_left = Some(end);
        self
    }

    /// Specify the rows range for the up portion.
    ///
    /// The range always starts at 0 for up rows.
    pub fn set_up_range(mut self, end: usize) -> Self {
        self.range_up = Some(end);
        self
    }

    /// Specify the columns range for the right portion. If necessary swap `start`
    /// and `end`.
    ///
    /// At build time, if the right range end is less or equal to the left range
    /// end, then the right range is set to `None`.
    pub fn set_right_range(mut self, mut start: usize, mut end: usize) -> Self {
        if end < start {
            let t = start;
            start = end;
            end = t;
        }
        self.range_right = Some((start, end));
        self
    }

    /// Specify the rows range for the down portion. If necessary swap `start` and
    /// `end`.
    ///
    /// At build time, if the down range end is less or equal to the up range end,
    /// then the down range is set to `None`.
    pub fn set_down_range(mut self, mut start: usize, mut end: usize) -> Self {
        if end < start {
            let t = start;
            start = end;
            end = t;
        }
        self.range_down = Some((start, end));
        self
    }

    /// Sets the initial scrolled offset.
    pub fn set_scrolled_offset(mut self, offset: Point) -> Self {
        self.scrolled_offset = Some(offset);
        self
    }

    /// Build the layout of the columns and rows for the for regions.
    pub fn build(
        mut self,
        columns: &Vec<PropertyMinimal>,
        rows: &Vec<PropertyMinimal>,
        horizontal: &Vec<f32>,
        vertical: &Vec<f32>,
    ) -> ContentLayout {
        // Ensure portion ranges are valid.
        if let Some(range) = self.range_left {
            self.range_left = Some(range.min(columns.len()));
        }
        if let Some(range) = self.range_up {
            self.range_up = Some(range.min(columns.len()));
        }
        if let Some(range) = self.range_right {
            self.range_right = Some((range.0.min(columns.len()), range.1.min(columns.len())));
        }
        if let Some(range) = self.range_down {
            self.range_down = Some((range.0.min(columns.len()), range.1.min(columns.len())));
        }
        if let Some(frozen) = self.range_left
            && let Some(scroll) = self.range_right
        {
            if scroll.1 <= frozen {
                self.range_right = None;
            } else if scroll.0 <= frozen {
                self.range_right = Some((frozen + 1, scroll.1));
            }
        }
        if let Some(frozen) = self.range_up
            && let Some(scroll) = self.range_down
        {
            if scroll.1 <= frozen {
                self.range_down = None;
            } else if scroll.0 <= frozen {
                self.range_down = Some((frozen + 1, scroll.1));
            }
        }

        let (mut size_total, mut size, mut size_scrolling, properties_left, properties_right) =
            build_columns(columns, &self.range_left, &self.range_right, horizontal); //&self.page);
        let (size_total_y, size_y, size_scrolling_y, properties_up, properties_down) =
            build_rows(rows, &self.range_up, &self.range_down, vertical); //&self.page);
        size_total.pixels.height = size_total_y.pixels.height;
        size_total.rows = size_total_y.rows;
        size.pixels.height = size_y.pixels.height;
        size.rows = size_y.rows;
        size_scrolling.pixels.height = size_scrolling_y.pixels.height;
        size_scrolling.rows = size_scrolling_y.rows;

        // Determine the indices into `Vec<Element<>>`.
        let mut cells_up_left = 1; // Always has the corner space.
        let mut cells_up_right = 0;
        let mut cells_down_left = 0;
        let mut cells_down_right = 0;
        let mut index_up_right = 0;
        let mut index_down_left = 0;
        let mut index_down_right = 0;
        if self.range_left.is_some() {
            cells_up_left += properties_left.len();
        }
        if self.range_up.is_some() {
            cells_up_left += properties_up.len();
            if self.range_left.is_some() {
                cells_up_left += properties_left.len() * properties_up.len();
            }
        }
        if self.range_right.is_some() {
            index_up_right = cells_up_left;
            cells_up_right = properties_right.len();
            if self.range_up.is_some() {
                cells_up_right += properties_right.len() * properties_up.len();
            }
        };
        if self.range_down.is_some() {
            index_down_left = index_up_right + cells_up_right;
            cells_down_left = properties_down.len();
            if self.range_left.is_some() {
                cells_down_left += properties_down.len() * properties_left.len();
            }
            if self.range_right.is_some() {
                index_down_right = index_down_left + cells_down_left;
                cells_down_right = properties_down.len() * properties_right.len();
            }
        };
        let cells_total = index_down_right + cells_down_right;

        // Ensure scrolled offset is within bounds.
        if let Some(offset) = self.scrolled_offset.as_mut() {
            if !properties_right.is_empty() {
                offset.x = offset.x.clamp(0.0, size_scrolling.pixels.width);
            }
            if !properties_down.is_empty() {
                offset.y = offset.y.clamp(0.0, size_scrolling.pixels.height);
            }
        }

        ContentLayout {
            // Regions
            range_left: self.range_left,
            range_up: self.range_up,
            range_right: self.range_right,
            range_down: self.range_down,
            cells_up_left,
            cells_up_right,
            cells_down_left,
            cells_down_right,
            cells_total,
            index_up_right,
            index_down_left,
            index_down_right,

            // Layout
            properties_left,
            properties_up,
            properties_right,
            properties_down,
            size_total,
            size,
            size_scrolling,
            scrolled_offset: self.scrolled_offset,
        }
    }
}

//
// ----- ContentBuilder
//

pub struct ContentBuilder<'a, Message, Theme, Renderer>
where
    Renderer: renderer::Renderer,
    Theme: style::Catalog,
{
    // Content
    corner: Option<Element<'a, Message, Theme, Renderer>>,
    left_columns: Vec<Element<'a, Message, Theme, Renderer>>,
    up_rows: Vec<Element<'a, Message, Theme, Renderer>>,
    up_left_data: Vec<Element<'a, Message, Theme, Renderer>>,
    up_left_active: Vec<bool>,
    right_columns: Vec<Element<'a, Message, Theme, Renderer>>,
    up_right_data: Vec<Element<'a, Message, Theme, Renderer>>,
    up_right_active: Vec<bool>,
    down_rows: Vec<Element<'a, Message, Theme, Renderer>>,
    down_left_data: Vec<Element<'a, Message, Theme, Renderer>>,
    down_left_active: Vec<bool>,
    down_right_data: Vec<Element<'a, Message, Theme, Renderer>>,
    down_right_active: Vec<bool>,

    // Layout
    layout: &'a ContentLayout,
}

impl<'a, Message: 'a, Theme, Renderer> ContentBuilder<'a, Message, Theme, Renderer>
where
    Renderer: renderer::Renderer,
    Theme: style::Catalog,
{
    /// Creates a new builder struct for [`Content`] with the supplied reference
    /// to the [`ContentLayout`] struct stored in the application's state.
    pub fn new(layout: &'a ContentLayout) -> Self {
        Self {
            // Content
            corner: None,
            left_columns: Vec::new(),
            up_rows: Vec::new(),
            up_left_data: Vec::new(),
            up_left_active: Vec::new(),
            right_columns: Vec::new(),
            up_right_data: Vec::new(),
            up_right_active: Vec::new(),
            down_rows: Vec::new(),
            down_left_data: Vec::new(),
            down_left_active: Vec::new(),
            down_right_data: Vec::new(),
            down_right_active: Vec::new(),

            // Layout
            layout,
        }
    }

    //
    // ----- Content
    //

    /// Set the corner element.
    pub fn set_corner(&mut self, element: Element<'a, Message, Theme, Renderer>) {
        self.corner = Some(element);
    }

    /// Push a left column element.
    pub fn push_left_column(&mut self, element: Element<'a, Message, Theme, Renderer>) {
        self.left_columns.push(element);
    }

    /// Push an up row element.
    pub fn push_up_row(&mut self, element: Element<'a, Message, Theme, Renderer>) {
        self.up_rows.push(element);
    }

    /// Push an up left region data element, and whether it is active.
    pub fn push_up_left_data(
        &mut self,
        element: Element<'a, Message, Theme, Renderer>,
        active: bool,
    ) {
        self.up_left_data.push(element);
        self.up_left_active.push(active);
    }

    /// Push a right column element.
    pub fn push_right_column(&mut self, element: Element<'a, Message, Theme, Renderer>) {
        self.right_columns.push(element);
    }

    /// Push an up right region data element, and whether it is active.
    pub fn push_up_right_data(
        &mut self,
        element: Element<'a, Message, Theme, Renderer>,
        active: bool,
    ) {
        self.up_right_data.push(element);
        self.up_right_active.push(active);
    }

    /// Push a down row element.
    pub fn push_down_row(&mut self, element: Element<'a, Message, Theme, Renderer>) {
        self.down_rows.push(element);
    }

    /// Push a down left region data element, and whether it is active.
    pub fn push_down_left_data(
        &mut self,
        element: Element<'a, Message, Theme, Renderer>,
        active: bool,
    ) {
        self.down_left_data.push(element);
        self.down_left_active.push(active);
    }

    /// Push a down right region data element, and whether it is active.
    pub fn push_down_right_data(
        &mut self,
        element: Element<'a, Message, Theme, Renderer>,
        active: bool,
    ) {
        self.down_right_data.push(element);
        self.down_right_active.push(active);
    }

    //
    // ----- Private
    //

    fn build(mut self) -> Content<'a, Message, Theme, Renderer> {
        let mut active_up_left = Vec::with_capacity(self.layout.cells_up_left);
        let mut active_up_right = Vec::with_capacity(self.layout.cells_up_right);
        let mut active_down_left = Vec::with_capacity(self.layout.cells_down_left);
        let mut active_down_right = Vec::with_capacity(self.layout.cells_down_right);
        let mut elements = Vec::with_capacity(self.layout.cells_total);

        // Construct up left region.
        elements.push(if let Some(corner) = self.corner {
            corner
        } else {
            Space::new().into()
        });
        active_up_left.push(true);
        if !self.layout.properties_left.is_empty() {
            self.left_columns
                .resize_with(self.layout.properties_left.len(), || Space::new().into());
            elements.append(&mut self.left_columns);
            let mut active = Vec::with_capacity(self.layout.properties_left.len());
            active.resize_with(self.layout.properties_left.len(), || true);
            active_up_left.append(&mut active);
        }
        if !self.layout.properties_up.is_empty() {
            self.up_rows
                .resize_with(self.layout.properties_up.len(), || Space::new().into());
            elements.append(&mut self.up_rows);
            let mut active = Vec::with_capacity(self.layout.properties_up.len());
            active.resize_with(self.layout.properties_up.len(), || true);
            active_up_left.append(&mut active);
            if !self.layout.properties_left.is_empty() {
                let count = self.layout.properties_left.len() * self.layout.properties_up.len();
                self.up_left_data.resize_with(count, || Space::new().into());
                elements.append(&mut self.up_left_data);
                self.up_left_active.resize_with(count, || false);
                active_up_left.append(&mut self.up_left_active);
            }
        }

        // Construct up right region.
        if !self.layout.properties_right.is_empty() {
            self.right_columns
                .resize_with(self.layout.properties_right.len(), || Space::new().into());
            elements.append(&mut self.right_columns);
            let mut active = Vec::with_capacity(self.layout.properties_right.len());
            active.resize_with(self.layout.properties_right.len(), || true);
            active_up_right.append(&mut active);
            if !self.layout.properties_up.is_empty() {
                let count = self.layout.properties_right.len() * self.layout.properties_up.len();
                self.up_right_data
                    .resize_with(count, || Space::new().into());
                elements.append(&mut self.up_right_data);
                self.up_right_active.resize_with(count, || false);
                active_up_right.append(&mut self.up_right_active);
            }
        }

        // Construct down left region and down right region.
        if !self.layout.properties_down.is_empty() {
            self.down_rows
                .resize_with(self.layout.properties_down.len(), || Space::new().into());
            elements.append(&mut self.down_rows);
            let mut active = Vec::with_capacity(self.layout.properties_down.len());
            active.resize_with(self.layout.properties_down.len(), || true);
            active_down_left.append(&mut active);
            if !self.layout.properties_left.is_empty() {
                let count = self.layout.properties_left.len() * self.layout.properties_down.len();
                self.down_left_data
                    .resize_with(count, || Space::new().into());
                elements.append(&mut self.down_left_data);
                self.down_left_active.resize_with(count, || false);
                active_down_left.append(&mut self.down_left_active);
            }
            if !self.layout.properties_right.is_empty() {
                let count = self.layout.properties_right.len() * self.layout.properties_down.len();
                self.down_right_data
                    .resize_with(count, || Space::new().into());
                elements.append(&mut self.down_right_data);
                self.down_right_active.resize_with(count, || false);
                active_down_right.append(&mut self.down_right_active);
            }
        }

        Content {
            // Content
            elements,
            active_up_left,
            active_up_right,
            active_down_left,
            active_down_right,

            // Layout
            layout: self.layout,
        }
    }
}

impl<'a, Message, Theme, Renderer> From<ContentBuilder<'a, Message, Theme, Renderer>>
    for Content<'a, Message, Theme, Renderer>
where
    Message: Clone + 'a,
    Renderer: renderer::Renderer + 'a,
    Theme: style::Catalog + 'a,
{
    fn from(
        content: ContentBuilder<'a, Message, Theme, Renderer>,
    ) -> Content<'a, Message, Theme, Renderer> {
        content.build()
    }
}