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
use std::fmt::Debug;

use figures::units::UPx;
use figures::{Fraction, IntoSigned, Point, Rect, ScreenScale, Size, Zero};

use crate::context::{AsEventContext, EventContext, LayoutContext};
use crate::styles::{Edges, FlexibleDimension};
use crate::value::{IntoValue, Value};
use crate::widget::{MakeWidget, RootBehavior, WidgetRef, WrappedLayout, WrapperWidget};
use crate::ConstraintLimit;

/// A widget aligns its contents to its container's boundaries.
#[derive(Debug)]
pub struct Align {
    child: WidgetRef,
    edges: Value<Edges<FlexibleDimension>>,
}

impl Align {
    /// Returns a new spacing widget containing `widget`, surrounding it with
    /// `margin`.
    pub fn new(margin: impl IntoValue<Edges<FlexibleDimension>>, widget: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(widget),
            edges: margin.into_value(),
        }
    }

    /// Returns a new spacing widget that centers `widget` vertically and
    /// horizontally.
    pub fn centered(widget: impl MakeWidget) -> Self {
        Self::new(FlexibleDimension::Auto, widget)
    }

    /// Sets the left edge of alignment to 0 and returns self.
    #[must_use]
    pub fn align_left(mut self) -> Self {
        self.edges
            .map_mut(|mut edges| edges.left = FlexibleDimension::ZERO);
        self
    }

    /// Sets the top edge of alignment to 0 and returns self.
    #[must_use]
    pub fn align_top(mut self) -> Self {
        self.edges
            .map_mut(|mut edges| edges.top = FlexibleDimension::ZERO);
        self
    }

    /// Sets the bottom edge of alignment to 0 and returns self.
    #[must_use]
    pub fn align_bottom(mut self) -> Self {
        self.edges
            .map_mut(|mut edges| edges.bottom = FlexibleDimension::ZERO);
        self
    }

    /// Sets the right edge of alignment to 0 and returns self.
    #[must_use]
    pub fn align_right(mut self) -> Self {
        self.edges
            .map_mut(|mut edges| edges.right = FlexibleDimension::ZERO);
        self
    }

    /// Sets the left and right edges of alignment to 0 and returns self.
    #[must_use]
    pub fn fit_horizontally(mut self) -> Self {
        self.edges.map_mut(|mut edges| {
            edges.left = FlexibleDimension::ZERO;
            edges.right = FlexibleDimension::ZERO;
        });
        self
    }

    /// Sets the top and bottom edges of alignment to 0 and returns self.
    #[must_use]
    pub fn fit_vertically(mut self) -> Self {
        self.edges.map_mut(|mut edges| {
            edges.top = FlexibleDimension::ZERO;
            edges.bottom = FlexibleDimension::ZERO;
        });
        self
    }

    fn measure(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Layout {
        let margin = self.edges.get();
        let vertical = FrameInfo::new(context.gfx.scale(), margin.top, margin.bottom);
        let horizontal = FrameInfo::new(context.gfx.scale(), margin.left, margin.right);

        let content_available = Size::new(
            horizontal.child_constraint(available_space.width),
            vertical.child_constraint(available_space.height),
        );

        let child = self.child.mounted(&mut context.as_event_context());
        let content_size = context.for_other(&child).layout(content_available);

        let (left, right, width) = horizontal.measure(available_space.width, content_size.width);
        let (top, bottom, height) = vertical.measure(available_space.height, content_size.height);

        Layout {
            margin: Edges {
                left,
                top,
                right,
                bottom,
            },
            content: Size::new(width, height),
        }
    }
}

struct FrameInfo {
    a: Option<UPx>,
    b: Option<UPx>,
}

impl FrameInfo {
    fn new(scale: Fraction, a: FlexibleDimension, b: FlexibleDimension) -> Self {
        let a = match a {
            FlexibleDimension::Auto => None,
            FlexibleDimension::Dimension(dimension) => Some(dimension.into_upx(scale)),
        };
        let b = match b {
            FlexibleDimension::Auto => None,
            FlexibleDimension::Dimension(dimension) => Some(dimension.into_upx(scale)),
        };
        Self { a, b }
    }

    fn child_constraint(&self, available: ConstraintLimit) -> ConstraintLimit {
        match (self.a, self.b) {
            (Some(a), Some(b)) => available - (a + b),
            // If we have at least one auto-measurement, force the constraint
            // into ClippedAfter mode to make the widget attempt to size the
            // content to fit.
            (Some(one), None) | (None, Some(one)) => {
                ConstraintLimit::SizeToFit(available.max() - one)
            }
            (None, None) => ConstraintLimit::SizeToFit(available.max()),
        }
    }

    fn measure(&self, available: ConstraintLimit, content: UPx) -> (UPx, UPx, UPx) {
        match available {
            ConstraintLimit::Fill(size) => {
                let size = size.max(content);
                let remaining = size.saturating_sub(content);
                let (a, b) = match (self.a, self.b) {
                    (Some(a), Some(b)) => (a, b),
                    (Some(a), None) => (a, remaining - a),
                    (None, Some(b)) => (remaining - b, b),
                    (None, None) => {
                        let a = remaining / 2;
                        let b = remaining - a;
                        (a, b)
                    }
                };

                (a, b, size - a - b)
            }
            ConstraintLimit::SizeToFit(_) => (
                self.a.unwrap_or_default(),
                self.b.unwrap_or_default(),
                content,
            ),
        }
    }
}

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

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

    fn layout_child(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> WrappedLayout {
        let layout = self.measure(available_space, context);

        WrappedLayout {
            child: Rect::new(
                Point::new(layout.margin.left, layout.margin.top).into_signed(),
                layout.content.into_signed(),
            ),
            size: layout.content + layout.margin.size(),
        }
    }
}

impl AsMut<WidgetRef> for Align {
    fn as_mut(&mut self) -> &mut WidgetRef {
        &mut self.child
    }
}

#[derive(Debug)]
struct Layout {
    margin: Edges<UPx>,
    content: Size<UPx>,
}