package toffee
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
CSS layout engine for OCaml (Flexbox, Grid, Block)
Install
dune-project
Dependency
Authors
Maintainers
Sources
mosaic-0.1.0.tbz
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/src/compute_grid/placement.ml.html
Source file placement.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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460(* Implements placing items in the grid and resolving the implicit grid. https://www.w3.org/TR/css-grid-1/#placement *) open Geometry open Style open Tree (* Helper module for grid placement operations *) module OriginZeroPlacement = struct let is_definite = function | Grid.Origin_zero_placement.Line _ -> true | _ -> false end (* Helper module for Line<OriginZeroPlacement> operations *) module PlacementLine = struct let is_definite line = OriginZeroPlacement.is_definite line.Line.start || OriginZeroPlacement.is_definite line.Line.end_ let indefinite_span line = match (line.Line.start, line.Line.end_) with | Grid.Origin_zero_placement.Line _, Grid.Origin_zero_placement.Auto -> 1 | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Line _ -> 1 | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Auto -> 1 | Grid.Origin_zero_placement.Line _, Grid.Origin_zero_placement.Span span -> span | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Line _ -> span | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Auto -> span | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Span span -> span | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Span _ -> span | Grid.Origin_zero_placement.Line _, Grid.Origin_zero_placement.Line _ -> failwith "indefinite_span should only be called on indefinite grid tracks" let resolve_definite_grid_lines line = match (line.Line.start, line.Line.end_) with | ( Grid.Origin_zero_placement.Line start, Grid.Origin_zero_placement.Line end_ ) when start = end_ -> Line.{ start; end_ = start + 1 } | ( Grid.Origin_zero_placement.Line start, Grid.Origin_zero_placement.Line end_ ) -> let start_line = min start end_ in let end_line = max start end_ in Line.{ start = start_line; end_ = end_line } | ( Grid.Origin_zero_placement.Line start, Grid.Origin_zero_placement.Span span ) -> Line.{ start; end_ = start + span } | Grid.Origin_zero_placement.Line start, Grid.Origin_zero_placement.Auto -> Line.{ start; end_ = start + 1 } | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Line end_ -> Line.{ start = end_ - span; end_ } | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Line end_ -> Line.{ start = end_ - 1; end_ } | _ -> failwith "resolve_definite_grid_lines called on indefinite placement" let resolve_indefinite_grid_tracks line position = match (line.Line.start, line.Line.end_) with | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Auto -> Line.{ start = position; end_ = position + 1 } | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Auto -> Line.{ start = position; end_ = position + span } | Grid.Origin_zero_placement.Auto, Grid.Origin_zero_placement.Span span -> Line.{ start = position; end_ = position + span } | Grid.Origin_zero_placement.Span span, Grid.Origin_zero_placement.Span _ -> Line.{ start = position; end_ = position + span } | _ -> failwith "resolve_indefinite_grid_tracks should only be called on indefinite \ grid tracks" end (* Module for InBothAbsAxis<Line<OriginZeroPlacement>> operations *) module PlacementInBothAxes = struct let get_absolute placement axis = match axis with | Absolute_axis.Horizontal -> placement.In_both_abs_axis.horizontal | Absolute_axis.Vertical -> placement.In_both_abs_axis.vertical let get placement axis = get_absolute placement (Abstract_axis.to_absolute_naive axis) end (* 8.5. Grid Item Placement Algorithm Place a single definitely placed item into the grid *) let place_definite_grid_item placement primary_axis = (* Resolve spans to tracks *) let primary_span = PlacementInBothAxes.get placement primary_axis |> PlacementLine.resolve_definite_grid_lines in let secondary_span = PlacementInBothAxes.get placement (Abstract_axis.other primary_axis) |> PlacementLine.resolve_definite_grid_lines in (primary_span, secondary_span) (* 8.5. Grid Item Placement Algorithm Step 2. Place remaining children with definite secondary axis positions *) let place_definite_secondary_axis_item cell_occupancy_matrix placement auto_flow = let primary_axis = Grid.Auto_flow.primary_axis auto_flow in let secondary_axis = Abstract_axis.other primary_axis in let secondary_axis_placement = PlacementInBothAxes.get placement secondary_axis |> PlacementLine.resolve_definite_grid_lines in let primary_axis_grid_start_line = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) |> Grid_track_counts.implicit_start_line in let starting_position = if Grid.Auto_flow.is_dense auto_flow then primary_axis_grid_start_line else match Cell_occupancy.last_of_type cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) secondary_axis_placement.Line.start Cell_occupancy.AutoPlaced with | Some pos -> pos | None -> primary_axis_grid_start_line in let position = ref starting_position in let rec loop () = let primary_axis_placement = PlacementInBothAxes.get placement primary_axis |> fun line -> PlacementLine.resolve_indefinite_grid_tracks line !position in let does_fit = Cell_occupancy.line_area_is_unoccupied cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) primary_axis_placement secondary_axis_placement in if does_fit then (primary_axis_placement, secondary_axis_placement) else ( incr position; loop ()) in loop () (* 8.5. Grid Item Placement Algorithm Step 4. Position the remaining grid items *) let place_indefinitely_positioned_item cell_occupancy_matrix placement auto_flow grid_position = let primary_axis = Grid.Auto_flow.primary_axis auto_flow in let secondary_axis = Abstract_axis.other primary_axis in let primary_placement_style = PlacementInBothAxes.get placement primary_axis in let secondary_placement_style = PlacementInBothAxes.get placement secondary_axis in let secondary_span = PlacementLine.indefinite_span secondary_placement_style in let has_definite_primary_axis_position = PlacementLine.is_definite primary_placement_style in let primary_axis_grid_start_line = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) |> Grid_track_counts.implicit_start_line in let primary_axis_grid_end_line = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) |> Grid_track_counts.implicit_end_line in let secondary_axis_grid_start_line = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive secondary_axis) |> Grid_track_counts.implicit_start_line in let line_area_is_occupied primary_span secondary_span = not (Cell_occupancy.line_area_is_unoccupied cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) primary_span secondary_span) in let primary_idx, secondary_idx = grid_position in let primary_idx = ref primary_idx in let secondary_idx = ref secondary_idx in if has_definite_primary_axis_position then ( let primary_span = PlacementLine.resolve_definite_grid_lines primary_placement_style in (* Compute secondary axis starting position for search *) secondary_idx := if Grid.Auto_flow.is_dense auto_flow then (* If auto-flow is dense then we always search from the first track *) secondary_axis_grid_start_line else if primary_span.Line.start < !primary_idx then !secondary_idx + 1 else !secondary_idx; (* Item has fixed primary axis position: so we simply increment the secondary axis position until we find a space that the item fits in *) let rec loop () = let secondary_span = Line.{ start = !secondary_idx; end_ = !secondary_idx + secondary_span } in (* If area is occupied, increment the index and try again *) if line_area_is_occupied primary_span secondary_span then ( incr secondary_idx; loop ()) else (* Once we find a free space, return that position *) (primary_span, secondary_span) in loop ()) else let primary_span = PlacementLine.indefinite_span primary_placement_style in (* Item does not have any fixed axis, so we search along the primary axis until we hit the end of the already existent tracks, and then we reset the primary axis back to zero and increment the secondary axis index. We continue in this vein until we find a space that the item fits in. *) let rec loop () = let primary_span = Line.{ start = !primary_idx; end_ = !primary_idx + primary_span } in let secondary_span = Line.{ start = !secondary_idx; end_ = !secondary_idx + secondary_span } in (* If the primary index is out of bounds, then increment the secondary index and reset the primary index back to the start of the grid *) let primary_out_of_bounds = primary_span.Line.end_ > primary_axis_grid_end_line in if primary_out_of_bounds then ( incr secondary_idx; primary_idx := primary_axis_grid_start_line; loop ()) else if line_area_is_occupied primary_span secondary_span then ( (* If area is occupied, increment the primary index and try again *) incr primary_idx; loop ()) else (* Once we find a free space that's in bounds, return that position *) (primary_span, secondary_span) in loop () (* Record the grid item in both CellOccupancyMatrix and the GridItems list once a definite placement has been determined *) let record_grid_placement cell_occupancy_matrix items node index style parent_align_items parent_justify_items primary_axis primary_span secondary_span placement_type = (* Mark area of grid as occupied *) Cell_occupancy.mark_area_as cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) primary_span secondary_span placement_type; (* Create grid item *) let col_span, row_span = match Abstract_axis.to_absolute_naive primary_axis with | Absolute_axis.Horizontal -> (primary_span, secondary_span) | Absolute_axis.Vertical -> (secondary_span, primary_span) in let item = Grid_item.new_with_placement_style_and_order ~node ~col_span ~row_span ~style ~parent_align_items ~parent_justify_items ~source_order:index in items := item :: !items (* 8.5. Grid Item Placement Algorithm Place items into the grid, generating new rows/column into the implicit grid as required [Specification](https://www.w3.org/TR/css-grid-2/#auto-placement-algo) *) let place_grid_items (type t) (module Tree : LAYOUT_PARTIAL_TREE with type t = t) ~cell_occupancy_matrix ~items ~tree ~parent_node ~grid_auto_flow ~align_items ~justify_items ~named_line_resolver = let primary_axis = Grid.Auto_flow.primary_axis grid_auto_flow in let secondary_axis = Abstract_axis.other primary_axis in let map_child_style_to_origin_zero_placement = let explicit_col_count = Cell_occupancy.track_counts cell_occupancy_matrix Absolute_axis.Horizontal |> Grid_track_counts.explicit in let explicit_row_count = Cell_occupancy.track_counts cell_occupancy_matrix Absolute_axis.Vertical |> Grid_track_counts.explicit in fun (index, node, style) -> let horizontal_placement = (* Get grid_column from style and resolve named lines *) let grid_placement = Style.grid_column style in let resolved_placement = Named.resolve_column_names named_line_resolver grid_placement in Line.map (fun placement -> Grid.Placement.into_origin_zero_placement placement explicit_col_count) resolved_placement in let vertical_placement = (* Get grid_row from style and resolve named lines *) let grid_placement = Style.grid_row style in let resolved_placement = Named.resolve_row_names named_line_resolver grid_placement in Line.map (fun placement -> Grid.Placement.into_origin_zero_placement placement explicit_row_count) resolved_placement in let origin_zero_placement = In_both_abs_axis. { horizontal = horizontal_placement; vertical = vertical_placement } in (index, node, origin_zero_placement, style) in (* Helper to iterate children, filtering out display:none and position:absolute items *) let children_array = let count = Tree.child_count tree parent_node in let acc = ref [] in for index = count - 1 downto 0 do let child_id = Tree.get_child_id tree parent_node index in let child_style = Tree.get_core_container_style tree child_id in if Style.box_generation_mode child_style <> Box_generation_mode.None && Style.position child_style <> Position.Absolute then acc := (index, child_id, child_style) :: !acc done; Array.of_list !acc in (* 1. Place children with definite positions *) Array.iter (fun (index, child_node, child_style) -> let _, _, child_placement, style = map_child_style_to_origin_zero_placement (index, child_node, child_style) in if PlacementLine.is_definite child_placement.In_both_abs_axis.horizontal && PlacementLine.is_definite child_placement.In_both_abs_axis.vertical then let primary_span, secondary_span = place_definite_grid_item child_placement primary_axis in record_grid_placement cell_occupancy_matrix items child_node index style align_items justify_items primary_axis primary_span secondary_span Cell_occupancy.DefinitelyPlaced) children_array; (* 2. Place remaining children with definite secondary axis positions *) Array.iter (fun (index, child_node, child_style) -> let _, _, child_placement, style = map_child_style_to_origin_zero_placement (index, child_node, child_style) in let secondary_definite = PlacementLine.is_definite (PlacementInBothAxes.get child_placement secondary_axis) in let primary_indefinite = not (PlacementLine.is_definite (PlacementInBothAxes.get child_placement primary_axis)) in if secondary_definite && primary_indefinite then let primary_span, secondary_span = place_definite_secondary_axis_item cell_occupancy_matrix child_placement grid_auto_flow in record_grid_placement cell_occupancy_matrix items child_node index style align_items justify_items primary_axis primary_span secondary_span Cell_occupancy.AutoPlaced) children_array; (* 3. Determine the number of columns in the implicit grid By the time we get to this point in the execution, this is actually already accounted for: 3.1 Start with the columns from the explicit grid => Handled by grid size estimate which is used to pre-size the GridOccupancyMatrix 3.2 Among all the items with a definite column position (explicitly positioned items, items positioned in the previous step, and items not yet positioned but with a definite column) add columns to the beginning and end of the implicit grid as necessary to accommodate those items. => Handled by expand_to_fit_range which expands the GridOccupancyMatrix as necessary -> Called by mark_area_as -> Called by record_grid_placement 3.3 If the largest column span among all the items without a definite column position is larger than the width of the implicit grid, add columns to the end of the implicit grid to accommodate that column span. => Handled by grid size estimate which is used to pre-size the GridOccupancyMatrix *) (* 4. Position the remaining grid items (which either have definite position only in the secondary axis or indefinite positions in both axis) *) let primary_neg_tracks = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive primary_axis) |> Grid_track_counts.negative_implicit in let secondary_neg_tracks = Cell_occupancy.track_counts cell_occupancy_matrix (Abstract_axis.to_absolute_naive secondary_axis) |> Grid_track_counts.negative_implicit in let grid_start_position = (-primary_neg_tracks, -secondary_neg_tracks) in let grid_position = ref grid_start_position in Array.iter (fun (index, child_node, child_style) -> let _, _, child_placement, style = map_child_style_to_origin_zero_placement (index, child_node, child_style) in if not (PlacementLine.is_definite (PlacementInBothAxes.get child_placement secondary_axis)) then ( (* Compute placement *) let primary_span, secondary_span = place_indefinitely_positioned_item cell_occupancy_matrix child_placement grid_auto_flow !grid_position in (* Record item *) record_grid_placement cell_occupancy_matrix items child_node index style align_items justify_items primary_axis primary_span secondary_span Cell_occupancy.AutoPlaced; (* If using the "dense" placement algorithm then reset the grid position back to grid_start_position ready for the next item Otherwise set it to the position of the current item so that the next item it placed after it. *) grid_position := if Grid.Auto_flow.is_dense grid_auto_flow then grid_start_position else (primary_span.Line.end_, secondary_span.Line.start))) children_array; (* Reverse the items list to maintain original order (we accumulated in reverse for performance) *) items := List.rev !items
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>