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
open! Oxbow_core
module Item = struct
type t =
{ consumes : bool
; width_fac : float
}
end
let split ~total ~count =
if count <= 0
then []
else (
let size = total / count in
let rem = total mod count in
List.init count (fun i -> if i < rem then size + 1 else size))
;;
let rec columns ~consumes = function
| [] -> []
| x :: xs ->
(match columns ~consumes xs, consumes x with
| col :: cols, true -> (x :: col) :: cols
| cols, _ -> [ x ] :: cols)
;;
let layout ~(usable : int Rect.canonical) ~offset (items : ('a * Item.t) list) =
let column_width (col : ('a * Item.t) list) =
let fac = (List.hd col |> snd).width_fac in
float_of_int usable.w *. fac |> int_of_float |> max 1
in
let place ~cursor col =
let width = column_width col in
let heights = split ~total:usable.h ~count:(List.length col) in
let _, placed =
List.combine col heights
|> List.fold_left_map
(fun y ((member, _), h) ->
y + h, (member, Rect.{ x = cursor; y; w = width; h }))
usable.y
in
placed
in
let _, per_column =
List.fold_left_map
(fun cursor col ->
let width = column_width col in
cursor + width, place ~cursor col)
(usable.x - offset)
(columns ~consumes:(fun (_, (it : Item.t)) -> it.consumes) items)
in
List.concat per_column
;;
let scroll ~(align : Align.t) ~viewport_w ~max_offset ~offset ~col:(x, w) =
let ideal =
match align with
| Left -> x
| Centered -> x - ((viewport_w - w) / 2)
| Visible ->
if w > viewport_w
then x
else if x < offset
then x
else if x + w > offset + viewport_w
then x + w - viewport_w
else offset
in
match align with
| Centered -> ideal
| Left | Visible -> min ideal max_offset |> max 0
;;