package toffee

Build not yet checked
  1. Overview
  2. Docs
CSS layout engine for OCaml (Flexbox, Grid, Block)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mosaic-0.3.0.tbz
sha256=a92cbbcbe016fb19fb0a749222da0e459a9567aef3f4c7340d9bd6b71c53ed6f
sha512=6a7ab054605538bb31a2b73a1a6d093981a2704c9f0af5852221c05735290d74aca7c7282298c4d0f91e86a800e2ccd94b4918af883cd963449c758eb6b272b3

doc/src/toffee.tree/toffee_tree.ml.html

Source file toffee_tree.ml

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
(** Core Types *)

module Geometry = Toffee_geometry
module Node_id = Node_id
module Run_mode = Run_mode
module Sizing_mode = Sizing_mode
module Collapsible_margin_set = Collapsible_margin_set
module Requested_axis = Requested_axis
module Available_space = Available_space
module Layout_input = Layout_input
module Layout_output = Layout_output
module Scrollable_overflow = Scrollable_overflow
module Layout = Layout
module Cache = Cache

(** Tree Types *)

module type TRAVERSE_PARTIAL_TREE = sig
  type t

  val child_ids : t -> Node_id.t -> Node_id.t Seq.t
  val child_count : t -> Node_id.t -> int
  val get_child_id : t -> Node_id.t -> int -> Node_id.t
end

module type TRAVERSE_TREE = sig
  include TRAVERSE_PARTIAL_TREE
end

module type LAYOUT_PARTIAL_TREE = sig
  include TRAVERSE_PARTIAL_TREE

  val get_core_container_style : t -> Node_id.t -> Toffee_style.t
  val set_unrounded_layout : t -> Node_id.t -> Layout.t -> unit
  val compute_child_layout : t -> Node_id.t -> Layout_input.t -> Layout_output.t
  val resolve_calc_value : t -> int -> float -> float
end

module type CACHE_TREE = sig
  type t

  val cache_get : t -> Node_id.t -> Layout_input.t -> Layout_output.t option
  val cache_store : t -> Node_id.t -> Layout_input.t -> Layout_output.t -> unit
  val cache_clear : t -> Node_id.t -> unit
end

module type ROUND_TREE = sig
  include TRAVERSE_TREE

  val get_unrounded_layout : t -> Node_id.t -> Layout.t
  val is_rounded : t -> Node_id.t -> x:float -> y:float -> bool

  val set_final_layout :
    t -> Node_id.t -> x:float -> y:float -> Layout.t -> unit
end

module type PRINT_TREE = sig
  include TRAVERSE_TREE

  val get_debug_label : t -> Node_id.t -> string
  val get_final_layout : t -> Node_id.t -> Layout.t
end

(** Print a debug representation of the computed layout for a tree of nodes *)
let print_tree (type a) (module Tree : PRINT_TREE with type t = a) tree root =
  Printf.printf "TREE\n";
  let rec print_node node_id has_sibling lines_string =
    let layout = Tree.get_final_layout tree node_id in
    let display = Tree.get_debug_label tree node_id in
    let num_children = Tree.child_count tree node_id in

    let fork_string = if has_sibling then "├── " else "└── " in

    (* Print the node info *)
    Printf.printf
      "%s%s %s [x: %-4g y: %-4g w: %-4g h: %-4g overflow: l:%g r:%g t:%g b:%g, \
       border: l:%g r:%g t:%g b:%g, padding: l:%g r:%g t:%g b:%g] (Node %d)\n"
      lines_string fork_string display layout.location.x layout.location.y
      layout.size.width layout.size.height
      (Scrollable_overflow.left layout.scrollable_overflow)
      (Scrollable_overflow.right layout.scrollable_overflow)
      (Scrollable_overflow.top layout.scrollable_overflow)
      (Scrollable_overflow.bottom layout.scrollable_overflow)
      layout.border.left layout.border.right layout.border.top
      layout.border.bottom layout.padding.left layout.padding.right
      layout.padding.top layout.padding.bottom (Node_id.to_int node_id);

    let bar = if has_sibling then "│   " else "    " in
    let new_string = lines_string ^ bar in

    (* Recurse into children *)
    for index = 0 to num_children - 1 do
      let child = Tree.get_child_id tree node_id index in
      let has_sibling = index < num_children - 1 in
      print_node child has_sibling new_string
    done
  in
  print_node root false ""