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
use std::{fmt, mem, ops};

use zng_var::{animation::Transitionable, impl_from_and_into_var};

use super::{impl_length_comp_conversions, Factor, Factor2d, FactorPercent, Layout1d, LayoutMask, Length, Px, PxVector};

/// Spacing in-between grid cells in [`Length`] units.
#[derive(Clone, Default, PartialEq, serde::Serialize, serde::Deserialize, Transitionable)]
pub struct GridSpacing {
    /// Spacing in-between columns, in length units.
    pub column: Length,
    /// Spacing in-between rows, in length units.
    pub row: Length,
}
impl GridSpacing {
    /// New column, row from any [`Length`] unit..
    pub fn new<C: Into<Length>, R: Into<Length>>(column: C, row: R) -> Self {
        GridSpacing {
            column: column.into(),
            row: row.into(),
        }
    }

    /// Same spacing for both columns and rows.
    pub fn new_all<S: Into<Length>>(same: S) -> Self {
        let same = same.into();
        GridSpacing {
            column: same.clone(),
            row: same,
        }
    }
}
impl super::Layout2d for GridSpacing {
    type Px = PxGridSpacing;

    fn layout_dft(&self, default: Self::Px) -> Self::Px {
        PxGridSpacing {
            column: self.column.layout_dft_x(default.column),
            row: self.row.layout_dft_y(default.row),
        }
    }

    fn affect_mask(&self) -> LayoutMask {
        self.column.affect_mask() | self.row.affect_mask()
    }
}
impl_length_comp_conversions! {
    fn from(column: C, row: R) -> GridSpacing {
        GridSpacing::new(column, row)
    }
}
impl_from_and_into_var! {
    /// Same spacing for both columns and rows.
    fn from(all: Length) -> GridSpacing {
        GridSpacing::new_all(all)
    }

    /// Column and row equal relative length.
    fn from(percent: FactorPercent) -> GridSpacing {
        GridSpacing::new_all(percent)
    }
    /// Column and row equal relative length.
    fn from(norm: Factor) -> GridSpacing {
        GridSpacing::new_all(norm)
    }

    /// Column and row equal exact length.
    fn from(f: f32) -> GridSpacing {
        GridSpacing::new_all(f)
    }
    /// Column and row equal exact length.
    fn from(i: i32) -> GridSpacing {
        GridSpacing::new_all(i)
    }

    /// Column and row in device pixel length.
    fn from(spacing: PxGridSpacing) -> GridSpacing {
        GridSpacing::new(spacing.column, spacing.row)
    }
}
impl fmt::Debug for GridSpacing {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_struct("GridSpacing")
                .field("column", &self.column)
                .field("row", &self.row)
                .finish()
        } else if self.column == self.row {
            write!(f, "{:?}", self.column)
        } else {
            write!(f, "({:?}, {:?})", self.column, self.row)
        }
    }
}
impl<S: Into<Factor2d>> ops::Mul<S> for GridSpacing {
    type Output = Self;

    fn mul(self, rhs: S) -> Self {
        let fct = rhs.into();

        GridSpacing {
            column: self.column * fct.x,
            row: self.row * fct.y,
        }
    }
}
impl<'a, S: Into<Factor2d>> ops::Mul<S> for &'a GridSpacing {
    type Output = GridSpacing;

    fn mul(self, rhs: S) -> Self::Output {
        self.clone() * rhs
    }
}
impl<S: Into<Factor2d>> ops::MulAssign<S> for GridSpacing {
    fn mul_assign(&mut self, rhs: S) {
        let column = mem::take(&mut self.column);
        let row = mem::take(&mut self.row);
        let fct = rhs.into();

        self.column = column * fct.x;
        self.row = row * fct.y;
    }
}
impl<S: Into<Factor2d>> ops::Div<S> for GridSpacing {
    type Output = Self;

    fn div(self, rhs: S) -> Self {
        let fct = rhs.into();

        GridSpacing {
            column: self.column / fct.x,
            row: self.row / fct.y,
        }
    }
}
impl<'a, S: Into<Factor2d>> ops::Div<S> for &'a GridSpacing {
    type Output = GridSpacing;

    fn div(self, rhs: S) -> Self::Output {
        self.clone() / rhs
    }
}
impl<S: Into<Factor2d>> ops::DivAssign<S> for GridSpacing {
    fn div_assign(&mut self, rhs: S) {
        let column = mem::take(&mut self.column);
        let row = mem::take(&mut self.row);
        let fct = rhs.into();

        self.column = column / fct.x;
        self.row = row / fct.y;
    }
}

/// Computed [`GridSpacing`].
#[derive(Clone, Default, Copy, Debug)]
pub struct PxGridSpacing {
    /// Spacing in-between columns, in layout pixels.
    pub column: Px,
    /// Spacing in-between rows, in layout pixels.
    pub row: Px,
}
impl PxGridSpacing {
    /// New grid spacing
    pub fn new(column: Px, row: Px) -> Self {
        Self { column, row }
    }
    /// Zero spacing.
    pub fn zero() -> Self {
        PxGridSpacing { column: Px(0), row: Px(0) }
    }

    /// Convert to vector.
    pub fn to_vector(self) -> PxVector {
        PxVector::new(self.column, self.row)
    }
}
impl ops::Add for GridSpacing {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self {
        self += rhs;
        self
    }
}
impl ops::AddAssign for GridSpacing {
    fn add_assign(&mut self, rhs: Self) {
        self.column += rhs.column;
        self.row += rhs.row;
    }
}
impl ops::Sub for GridSpacing {
    type Output = Self;

    fn sub(mut self, rhs: Self) -> Self {
        self -= rhs;
        self
    }
}
impl ops::SubAssign for GridSpacing {
    fn sub_assign(&mut self, rhs: Self) {
        self.column -= rhs.column;
        self.row -= rhs.row;
    }
}
impl From<PxGridSpacing> for PxVector {
    fn from(s: PxGridSpacing) -> Self {
        s.to_vector()
    }
}
impl From<PxVector> for PxGridSpacing {
    fn from(s: PxVector) -> Self {
        PxGridSpacing { column: s.x, row: s.y }
    }
}