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

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

/// A widget that resizes its contained widget to an explicit size.
#[derive(Debug)]
pub struct Resize {
    /// The range of allowed width for the child widget.
    pub width: DimensionRange,
    /// The range of allowed height for the child widget.
    pub height: DimensionRange,
    child: WidgetRef,
}

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

    /// Resizes `child` to `size`.
    #[must_use]
    pub fn to<T>(size: Size<T>, child: impl MakeWidget) -> Self
    where
        T: Into<DimensionRange>,
    {
        Self {
            child: WidgetRef::new(child),
            width: size.width.into(),
            height: size.height.into(),
        }
    }

    /// Resizes `child`'s width to `width`.
    #[must_use]
    pub fn from_width(width: impl Into<DimensionRange>, child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            width: width.into(),
            height: DimensionRange::from(..),
        }
    }

    /// Resizes `self` to `width`.
    ///
    /// `width` can be an any of:
    ///
    /// - [`Dimension`](crate::styles::Dimension)
    /// - [`Px`](crate::figures::units::Px)
    /// - [`Lp`](crate::figures::units::Lp)
    /// - A range of any fo the above.
    #[must_use]
    pub fn width(mut self, width: impl Into<DimensionRange>) -> Self {
        self.width = width.into();
        self
    }

    /// Resizes `self` to `height`.
    ///
    /// `height` can be an any of:
    ///
    /// - [`Dimension`](crate::styles::Dimension)
    /// - [`Px`](crate::figures::units::Px)
    /// - [`Lp`](crate::figures::units::Lp)
    /// - A range of any fo the above.
    #[must_use]
    pub fn height(mut self, height: impl Into<DimensionRange>) -> Self {
        self.height = height.into();
        self
    }

    /// Resizes `child`'s height to `height`.
    #[must_use]
    pub fn from_height(height: impl Into<DimensionRange>, child: impl MakeWidget) -> Self {
        Self {
            child: WidgetRef::new(child),
            width: DimensionRange::from(..),
            height: height.into(),
        }
    }
}

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

    fn root_behavior(&mut self, _context: &mut EventContext<'_>) -> Option<RootBehavior> {
        Some(RootBehavior::Resize(Size::new(self.width, self.height)))
    }

    fn layout_child(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> WrappedLayout {
        let child = self.child.mounted(&mut context.as_event_context());
        let (size, fill_layout) = if let (Some(width), Some(height)) =
            (self.width.exact_dimension(), self.height.exact_dimension())
        {
            (
                Size::new(width, height).map(|i| i.into_upx(context.gfx.scale())),
                true,
            )
        } else {
            let available_space = Size::new(
                override_constraint(available_space.width, self.width, context.gfx.scale()),
                override_constraint(available_space.height, self.height, context.gfx.scale()),
            );
            (
                context.for_other(&child).layout(available_space),
                matches!(available_space.width, ConstraintLimit::SizeToFit(_))
                    || matches!(available_space.height, ConstraintLimit::SizeToFit(_)),
            )
        };
        let size = Size::new(
            self.width.clamp(size.width, context.gfx.scale()),
            self.height.clamp(size.height, context.gfx.scale()),
        );
        if fill_layout {
            // Now that we have our known dimension, give the child an opportunity
            // to lay out with Fill semantics.
            context
                .for_other(&child)
                .layout(size.map(ConstraintLimit::Fill));
        }

        size.into_signed().into()
    }
}

fn override_constraint(
    constraint: ConstraintLimit,
    range: DimensionRange,
    scale: Fraction,
) -> ConstraintLimit {
    match constraint {
        ConstraintLimit::Fill(size) => ConstraintLimit::Fill(range.clamp(size, scale)),
        ConstraintLimit::SizeToFit(clipped_after) => match (range.minimum(), range.maximum()) {
            (Some(min), Some(max)) if min == max => ConstraintLimit::Fill(min.into_upx(scale)),
            _ => ConstraintLimit::SizeToFit(range.minimum().map_or_else(
                || range.clamp(clipped_after, scale),
                |min| min.into_upx(scale),
            )),
        },
    }
}