cushy/widgets/
expand.rs

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
use figures::{IntoSigned, Size};

use crate::context::{AsEventContext, EventContext, LayoutContext};
use crate::widget::{MakeWidget, RootBehavior, WidgetRef, WrappedLayout, WrapperWidget};
use crate::widgets::Space;
use crate::ConstraintLimit;

/// A widget that expands its child widget to fill the parent.
///
/// Some parent widgets support weighting children when there is more than one
/// [`Expand`]ed widget.
#[derive(Debug, Clone)]
pub struct Expand {
    kind: ExpandKind,
    child: WidgetRef,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum ExpandKind {
    Weighted(u8),
    Horizontal,
    Vertical,
}

impl Default for Expand {
    fn default() -> Self {
        Self::empty()
    }
}

impl Expand {
    /// Returns a widget that expands `child` to fill the parent widget.
    #[must_use]
    pub fn new(child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            kind: ExpandKind::Weighted(1),
        }
    }

    /// Returns a widget that expands `child` to fill the parent widget horizontally.
    #[must_use]
    pub fn horizontal(child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            kind: ExpandKind::Horizontal,
        }
    }

    /// Returns a widget that expands `child` to fill the parent widget vertically.
    #[must_use]
    pub fn vertical(child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            kind: ExpandKind::Vertical,
        }
    }

    /// Returns a widget that expands to fill its parent, but has no contents.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            child: WidgetRef::new(Space::clear()),
            kind: ExpandKind::Weighted(1),
        }
    }

    /// Returns a widget that expands to fill its parent horizontally, but has no contents.
    #[must_use]
    pub fn empty_horizontally() -> Self {
        Self {
            child: WidgetRef::new(Space::clear()),
            kind: ExpandKind::Horizontal,
        }
    }

    /// Returns a widget that expands to fill its parent vertically, but has no contents.
    #[must_use]
    pub fn empty_vertically() -> Self {
        Self {
            child: WidgetRef::new(Space::clear()),
            kind: ExpandKind::Vertical,
        }
    }

    /// Returns a widget that expands `child` to fill the parent widget, using
    /// `weight` when competing with available space with other [`Expand`]s.
    ///
    /// Note: Not all container widgets support weighted expansion.
    #[must_use]
    pub fn weighted(weight: u8, child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            kind: ExpandKind::Weighted(weight),
        }
    }

    /// Returns a reference to the child widget.
    #[must_use]
    pub const fn child(&self) -> &WidgetRef {
        &self.child
    }

    #[must_use]
    pub(crate) fn weight(&self, vertical: bool) -> Option<u8> {
        match (self.kind, vertical) {
            (ExpandKind::Weighted(weight), _) => Some(weight),
            (ExpandKind::Horizontal, false) | (ExpandKind::Vertical, true) => Some(1),
            (ExpandKind::Horizontal | ExpandKind::Vertical, _) => None,
        }
    }

    pub(crate) const fn expand_kind(&self) -> ExpandKind {
        self.kind
    }
}

impl WrapperWidget for Expand {
    fn child_mut(&mut self) -> &mut WidgetRef {
        &mut self.child
    }

    fn root_behavior(&mut self, _context: &mut EventContext<'_>) -> Option<RootBehavior> {
        Some(RootBehavior::Expand)
    }

    fn align_child(&self) -> bool {
        true
    }

    fn layout_child(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> WrappedLayout {
        let available_space = match &self.kind {
            ExpandKind::Weighted(_) => available_space.map(|lim| ConstraintLimit::Fill(lim.max())),
            ExpandKind::Horizontal => Size::new(
                ConstraintLimit::Fill(available_space.width.max()),
                ConstraintLimit::SizeToFit(available_space.height.max()),
            ),
            ExpandKind::Vertical => Size::new(
                ConstraintLimit::SizeToFit(available_space.width.max()),
                ConstraintLimit::Fill(available_space.height.max()),
            ),
        };
        let child = self.child.mounted(&mut context.as_event_context());
        let size = context.for_other(&child).layout(available_space);

        let (width, height) = match &self.kind {
            ExpandKind::Weighted(_) => (
                available_space.width.fit_measured(size.width),
                available_space.height.fit_measured(size.height),
            ),
            ExpandKind::Horizontal => (
                available_space.width.fit_measured(size.width),
                size.height.min(available_space.height.max()),
            ),
            ExpandKind::Vertical => (
                size.width.min(available_space.width.max()),
                available_space.height.fit_measured(size.height),
            ),
        };

        Size::new(width, height).into_signed().into()
    }
}