/
Constraint.elm
223 lines (178 loc) · 7.37 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
212
213
214
215
216
217
218
219
220
221
222
223
module Constraint exposing (Model, focusOn, init, positions, unfocus, updateIdealPositions)
import Dict exposing (Dict)
{-| Lay out comments according to these constraints:
1. if there is a focused comment, it must be at it's ideal position
2. comments may not overlap
3. comments should be as close as possible to their ideal position (but the
first comment in an otherwise-overlapping sequence should be at it's ideal position)
Notably missing here: the ability to add new comments. That's just not something
that we need to solve for yet. Once we do, it should not be too hard to add
(recalculate the `comments` ordering with respect to the new height and ideal
position data, and re-solve.)
-}
type Model
= Model
{ -- comment ID to dimensions. This needs to be sorted by idealPosition
-- since this algorithm will be walking the data that way several
-- times.
comments : List ( Int, { height : Float, idealPosition : 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
, idealPositions : Dict Int Float
, margin : Float
}
-> Model
init { heights, idealPositions, margin } =
Model
{ comments =
-- take the intersection of the heights and ideal positions. Gotta have both for a comment to do this algorithm!
Dict.merge
(\_ _ result -> result)
(\key height idealPosition -> Dict.insert key { height = height, idealPosition = idealPosition })
(\_ _ result -> result)
heights
idealPositions
Dict.empty
|> Dict.toList
|> List.sortBy (\( _, { idealPosition } ) -> idealPosition)
, 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.comments
|> List.foldl
(\( id, { idealPosition, height } ) ( finalPositions, progressLine ) ->
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 ) =
-- == 0 is a trick to get the compiler to generate
-- more efficient code. Will not always be needed!
splitAtReversing (\( curId, _ ) -> curId - id == 0) newGuts.comments
( downwardPositions, _ ) =
List.foldl
(\( curId, { height, idealPosition } ) ( finalPositions, progressLine ) ->
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, { height, idealPosition } ) ( finalPositions, progressLine ) ->
let
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
( _, { idealPosition } ) :: _ ->
idealPosition
_ ->
-- infinity
1 / 0
)
goUp
in
Model { guts | positions = downwardAndUpwardPositions }
Nothing ->
solveWithFocus (Model guts)
updateIdealPositions : Dict Int Float -> Model -> Model
updateIdealPositions attachments (Model guts) =
Model
{ guts
| comments =
guts.comments
|> List.filterMap
(\( id, metrics ) ->
Maybe.map
(\newIdealPosition -> ( id, { metrics | idealPosition = newIdealPosition } ))
(Dict.get id attachments)
)
|> List.sortBy (\( _, { idealPosition } ) -> idealPosition)
}
|> 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 )