LayoutSolver

io.worxbend.tui.core.LayoutSolver
object LayoutSolver

Turns a list of Constraints into one integer size per segment.

This is the "how many cells does each segment get" half of Layout; where those cells sit on the axis (spacing, Flex offsets, clamping to the rectangle) is Layout's own job. The split is what lets either half be read without skimming the other.

The solver is a pure function of (constraints, available) and holds no state, so it is safe to call from any thread.

The algorithm is three steps:

  1. every constraint states its fixed demand, and those are handed out first;
  2. whatever is left over is shared among the constraints that grow (Fill by weight, Min and Max with weight one), honoring Max caps and re-distributing what a cap refuses;
  3. if nothing grew and the constraints between them claim the whole axis proportionally, the cells lost to integer division are handed back so the segments fill the container exactly.

Two constraints are resolved before those steps run. Auto becomes the Length the caller's measure function answers for that segment, or a Fill(1) when it answers None (or when the caller had nothing to measure with). Bounded is solved as its base and then checked: a segment that came out below its floor or above its ceiling is frozen at that bound as a fixed demand and the whole axis is solved again for the others, until no bound is violated. Each round freezes at least one segment, so there are at most as many rounds as bounded constraints.

Applications reach this through Layout.split and rarely call solve directly; it is public because how a toolkit decides sizes is the part users most need to be able to read, and a private object documents it to nobody.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def solve(constraints: Seq[Constraint], available: Int): IndexedSeq[Int]

The solved size of each constraint, in the order given, for an axis of available cells (spacing already deducted). Sizes may exceed available when the fixed demands do; Layout clamps at the far edge.

The solved size of each constraint, in the order given, for an axis of available cells (spacing already deducted). Sizes may exceed available when the fixed demands do; Layout clamps at the far edge.

With no measure function an Auto constraint is a Fill(1); see the three-argument overload.

Attributes

def solve(constraints: Seq[Constraint], available: Int, measure: Int => Option[Int]): IndexedSeq[Int]

solve with the content's own sizes: measure(i) is the number of cells segment i needs along the axis, or None when it cannot say. Only Auto constraints (and the Auto base of a Bounded) read it; a measured size is floored at zero and claimed as a Length, and None is claimed as a Fill(1) — never as zero.

solve with the content's own sizes: measure(i) is the number of cells segment i needs along the axis, or None when it cannot say. Only Auto constraints (and the Auto base of a Bounded) read it; a measured size is floored at zero and claimed as a Length, and None is claimed as a Fill(1) — never as zero.

Attributes