/
Constraint.elm
211 lines (166 loc) · 6.54 KB
/
Constraint.elm
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
module Constraint exposing (Model, focusOn, init, positions, unfocus, updateAttachments)
import Dict exposing (Dict)
type Model
= Model
{ -- comment ID to comment height
heights : Dict Int Float
-- comment ID to ideal position
, attachments : Dict Int Float
-- comment ID to actual position
, positions : Dict Int Float
-- margin, in pixels, to leave around comments
, margin : Float
-- is a comment selected? Which one?
, focus : Maybe Int
}
init :
{ heights : Dict Int Float
, attachments : Dict Int Float
, margin : Float
}
-> Model
init { heights, attachments, margin } =
Model
{ heights = heights
, attachments = attachments
, positions = Dict.empty
, margin = margin
, focus = Nothing
}
|> solve
solve : Model -> Model
solve ((Model { focus }) as model) =
if focus == Nothing then
solveWithoutFocus model
else
solveWithFocus model
solveWithoutFocus : Model -> Model
solveWithoutFocus (Model guts) =
Model
{ guts
| positions =
guts.attachments
|> Dict.toList
|> List.sortBy Tuple.second
|> List.foldl
(\( id, idealPosition ) ( finalPositions, progressLine ) ->
let
height =
Dict.get id guts.heights
|> Maybe.withDefault 0
in
if idealPosition >= progressLine then
( Dict.insert id idealPosition finalPositions
, idealPosition + height + guts.margin
)
else
( Dict.insert id progressLine finalPositions
, progressLine + height + guts.margin
)
)
( Dict.empty, 0 )
|> Tuple.first
}
solveWithFocus : Model -> Model
solveWithFocus (Model guts) =
case guts.focus of
Just id ->
let
(Model newGuts) =
solveWithoutFocus (Model guts)
( goUp, goDown ) =
newGuts.attachments
|> Dict.toList
|> List.sortBy Tuple.second
-- == 0 is a trick to get the compiler to generate
-- more efficient code. Will not always be needed!
|> splitAtReversing (\( curId, _ ) -> curId - id == 0)
( downwardPositions, _ ) =
List.foldl
(\( curId, idealPosition ) ( finalPositions, progressLine ) ->
let
height =
Dict.get curId newGuts.heights |> Maybe.withDefault 0
in
if idealPosition >= progressLine then
( Dict.insert curId idealPosition finalPositions
, idealPosition + height + newGuts.margin
)
else
( Dict.insert curId progressLine finalPositions
, progressLine + height + newGuts.margin
)
)
( newGuts.positions, 0 )
goDown
( downwardAndUpwardPositions, _ ) =
List.foldl
(\( curId, idealPosition ) ( finalPositions, progressLine ) ->
let
height =
Dict.get curId newGuts.heights |> Maybe.withDefault 0
currentPosition =
Dict.get curId newGuts.positions |> Maybe.withDefault idealPosition
in
if currentPosition + height + newGuts.margin <= progressLine then
( finalPositions
, currentPosition
)
else
let
finalPosition =
currentPosition - (currentPosition + height + newGuts.margin - progressLine)
in
( Dict.insert curId finalPosition finalPositions
, finalPosition
)
)
( downwardPositions
, case goDown of
( _, start ) :: _ ->
start
_ ->
-- infinity
1 / 0
)
goUp
in
Model { guts | positions = downwardAndUpwardPositions }
Nothing ->
solveWithFocus (Model guts)
updateAttachments : Dict Int Float -> Model -> Model
updateAttachments attachments (Model guts) =
if attachments == guts.attachments then
Model guts
else
Model { guts | attachments = attachments } |> solve
focusOn : Int -> Model -> Model
focusOn id (Model guts) =
Model { guts | focus = Just id } |> solve
unfocus : Model -> Model
unfocus (Model guts) =
Model { guts | focus = Nothing } |> solve
positions : Model -> Dict Int Float
positions (Model guts) =
guts.positions
-- utility
{-| Split a list at the first item that passes the test.
In the return value, the left list will be reversed and the right will be
forward. The matching element (if any) will be the first item of the right
list.
-}
splitAtReversing : (a -> Bool) -> List a -> ( List a, List a )
splitAtReversing test list =
splitAtReversingHelp test list []
{-| internal function so Elm can do TCO here
-}
splitAtReversingHelp : (a -> Bool) -> List a -> List a -> ( List a, List a )
splitAtReversingHelp test list acc =
case list of
a :: rest ->
if test a then
( acc, list )
else
splitAtReversingHelp test rest (a :: acc)
[] ->
( acc, list )