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
{-# LANGUAGE RecordWildCards #-}
module Graphics.Layout.Box where
data Border m n = Border {
top :: m, bottom :: m, left :: n, right :: n
}
mapX :: (n -> nn) -> Border m n -> Border m nn
mapY :: (m -> mm) -> Border m n -> Border mm n
mapX cb self = self { left = cb $ left self, right = cb $ right self }
mapY cb self = self { top = cb $ top self, bottom = cb $ bottom self }
data Size m n = Size {inline :: n, block :: m} deriving (Eq, Show)
data PaddedBox m n = PaddedBox {
min :: Size m n,
max :: Size m n,
size :: Size m n,
padding :: Border m n,
border :: Border m n,
margin :: Border m n
}
zeroBox :: PaddedBox Double Double
zeroBox = PaddedBox {
min = Size 0 0,
max = Size 0 0,
size = Size 0 0,
padding = Border 0 0 0 0,
border = Border 0 0 0 0,
margin = Border 0 0 0 0
}
lengthBox = PaddedBox {
min = Size Auto Auto,
max = Size Auto Auto,
size = Size Auto Auto,
padding = Border zero zero zero zero,
border = Border zero zero zero zero,
margin = Border zero zero zero zero
} where zero = Pixels 0
width PaddedBox {..} = left margin + left border + left padding +
inline size + right padding + right border + right margin
height PaddedBox {..} = top margin + top border + top padding +
block size + bottom padding + bottom border + bottom margin
minWidth PaddedBox {..} = left margin + left border + left padding +
inline min + right padding + right border + right margin
minHeight PaddedBox {..} = top margin + top border + top padding +
block min + bottom padding + bottom border + bottom margin
maxWidth PaddedBox {..} = left margin + left border + left padding +
inline max + right padding + right border + right margin
maxHeight PaddedBox {..} = top margin + top border + top padding +
block max + bottom padding + bottom border + bottom margin
data Length = Pixels Double | Percent Double | Auto | Preferred | Min deriving Eq
lowerLength :: Double -> Length -> Double
lowerLength _ (Pixels x) = x
lowerLength outerwidth (Percent x) = x * outerwidth
lowerLength _ _ = 0