~alcinnz/CatTrap

ref: eae2c2003d8f7d331e6e97364ed25ede50be6d81 CatTrap/Graphics/Layout/Box.hs -rw-r--r-- 2.0 KiB
eae2c200 — Adrian Cochrane Unittest basic grid sizing. 1 year, 8 months ago
                                                                                
09970dfc Adrian Cochrane
272852e8 Adrian Cochrane
09970dfc Adrian Cochrane
4c05e6f6 Adrian Cochrane
eae2c200 Adrian Cochrane
09970dfc Adrian Cochrane
272852e8 Adrian Cochrane
09970dfc Adrian Cochrane
c06fd552 Adrian Cochrane
272852e8 Adrian Cochrane
09970dfc Adrian Cochrane
272852e8 Adrian Cochrane
09970dfc Adrian Cochrane
272852e8 Adrian Cochrane
c06fd552 Adrian Cochrane
09970dfc Adrian Cochrane
c06fd552 Adrian Cochrane
6d1a719d Adrian Cochrane
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