Skip to main content

Crate egui_pop_fit

Crate egui_pop_fit 

Source
Expand description

§egui-pop-fit

A slot layout engine over plain boxes. It knows nothing of widgets, values or drawing: it packs ordered elements into a container in standard slot sizes, and says where each one goes.

It has no egui dependency: the egui-pop- prefix only marks it as part of the egui-pop workspace, whose facade re-exports it as egui_pop::fit. Results are plain rectangles, in twelfths of the unit and in points. It was published as egui_fit (0.1) before it joined the workspace; its API did not change.

  • Base unit. One row, scaled by density, given in points by the caller (Container::unit).
  • Allowed sizes. ¼, ⅓, ½, ⅔, ¾ of the unit and whole multiples (Size), counted in twelfths with integer arithmetic only.
  • Elements. Each declares forms (form 0 is the full form, the others smaller ones), each with a SizePolicy; its supported orientations, preferred first; an optional pinned orientation and pinned position.
  • Packing. A grid as wide as the box, in twelfths, with rows going down. Pinned elements are placed first, then the others in their order, each at the first free position in reading order: a later element may move up into an earlier gap (like CSS grid-auto-flow: dense).
  • Groups. layout_grouped takes Groups: runs of consecutive elements, like a fieldset’s controls, each with an optional header (its legend) on a band of its own. Packing stays dense within a group, but order is strict across groups: each starts on a new band below everything before it, and nothing later moves up into its gaps. Groups nest; pins count from the top of their band. The result gives each group’s band (GroupPlacement), for a host to frame it.
  • Growth. Growable elements then take the leftover room one slot step at a time, in rounds, up to their maximum.
  • Hysteresis. Pass the previous Layout to keep its column count, forms and orientations while the box stays within half a unit of a threshold.
  • Regions. layout_regions places cards in named Regions around and over a Surface (an image): corners, edges, the centre and an overlay, each with an Anchor, a gap and a priority. Each region packs its cards with the engine in the room the regions before it left, and folds its lowest-priority cards when room runs out.
use egui_pop_fit::{layout, Axis, Container, Element, Size, Size2, SizePolicy};

let half = Size2::new(Size::HALF, Size::ONE);
let row = Size2::new(Size::units(2), Size::ONE);
let elements = [
    Element::new(1, SizePolicy::fixed(half)),
    Element::new(2, SizePolicy::fixed(row)),
    Element::new(3, SizePolicy::growable(half, half, row, Axis::Width)),
];
// Two units wide (48 points, 24-point unit), growing down. `b` does not
// fit beside `a`, so `c` moves up into the gap, then grows in slot steps
// (½, ⅔, ¾, 1): the next step, 2, would not fit, so ½ unit stays empty.
let result = layout(&Container::new(48.0, 24.0), &elements, None);
assert_eq!(
    result.dump(),
    "grid 24x24 twelfths, one char = 6x12\n\
     acc.\n\
     bbbb\n\
     a key=1 form=0 horizontal at 0,0 size 6x12\n\
     b key=2 form=0 horizontal at 0,12 size 24x12\n\
     c key=3 form=0 horizontal at 6,0 size 12x12\n",
);
Experimental: may change in any release (docs/stability.md).

Structs§

AspectRatio
A width-to-height ratio. A ratio with a zero term is ignored.
Card
A card: one element of the slot engine, placed in a region.
CardPlacement
Where a card went.
Cell
A grid position, in twelfths of the unit from the container’s top left.
Container
The box to lay elements out in, in points.
Element
One element of a container, in its logical order.
GridRect
A rectangle on the grid, in twelfths of the unit.
Group
A run of consecutive elements packed apart from the rest.
GroupPlacement
The result for one group.
InvalidSize
The error of converting a number of twelfths that is not an allowed size.
Layout
A laid-out container.
Placement
The result for one element.
PointRect
A rectangle in points, relative to the container’s top left.
Region
A named place for cards on a surface.
RegionLayout
The cards laid out in their regions.
RegionPlacement
Where a region went.
Size
A standard slot size along one axis: ¼, ⅓, ½, ⅔, ¾ of the base unit, or a whole number of units.
Size2
A size on both axes.
SizePolicy
How an element is sized, written for its horizontal orientation (the vertical orientation swaps the axes).
Surface
The surface the regions sit on and around, in points.

Enums§

Anchor
Where a region sits on its surface. Start and end follow the direction: start is left in a left-to-right interface, right in a right-to-left one (Surface::right_to_left).
Axis
An axis of a size policy, in the policy’s own (horizontal) terms.
CardStatus
What became of a card.
Grow
Whether an element keeps its size or takes leftover room.
Orientation
An orientation an element can be laid out in.
Reason
Why an element is not placed, or not where it was pinned.
Side
Whether a region lies over the surface or around it.
Status
The outcome for one element.

Constants§

TWELFTHS_PER_UNIT
Twelfths in one base unit. 12 is the least common multiple of 2, 3 and 4, so every allowed size is a whole number of twelfths.

Functions§

layout
Lays elements out in container.
layout_grouped
Lays elements out in container, in groups.
layout_regions
Lays cards out in regions on and around surface.