Skip to content

Layout: LayoutItem and LayoutDirection, laid out by reading order - #209

Closed
fleon wants to merge 2 commits into
mainfrom
layout-engine
Closed

fleon wants to merge 2 commits into
mainfrom
layout-engine

Conversation

@fleon

@fleon fleon commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

A small rect-based layout engine in PlatformSprite, with reading direction built in from the start, so screens stop being position arithmetic and mirror for right-to-left languages (Arabic, Hebrew, Persian, Urdu).

What it adds

  • LayoutItem: rows and columns with gaps, grids, overlays and insets, laid out in the rects SceneLayout gives.
    • Items state their own size (design tokens, or a label's measured text). Nodes are never measured.
    • place(in:) sets each leaf's resting position once, on load and when the layout changes, never per frame. It leaves running actions alone.
    • Leaves: node(_:size:anchor:), label(_:height:), space, and leaf(_:place:) for anything else.
    • Modifiers: aligned, inset, sized and direction. .fill alignment takes the whole rect, for a panel's face behind its content.
    • Nothing flexes or wraps yet, by design.
  • Reading order: rows run from the leading edge, grids fill from it, and leading/trailing alignment and insets mirror in a right-to-left language. direction(_:) keeps what a subtree that shows a physical direction (a board, a track) holds as drawn. The subtree itself still stands where its parent's direction puts it, as UIKit's semanticContentAttribute affects only a view's subviews.
  • LayoutDirection:
    • current follows the game's language. Typography.follow sets it, so games that already call it get it with no change.
    • forced lets a game mirror the interface from a debug launch argument.
    • side(_:) and x(_:of:) turn an edge into a side or an x, labelAlignment(_:) aligns a label, and mirrorScale flips directional art such as a back chevron.
    • LocalizationLanguage.layoutDirection gives a language's direction from its code.
  • SceneLayout: cornerButtonCenter, edgeButtonX and edgeX also take a HorizontalEdge, so a back button stands in the leading top corner.
  • Docs: an AGENTS.md rule (lay screens out with LayoutItem, name places leading/trailing) and a README toolkit entry.

Before / after

BeforeAfter
back.position = layout.cornerButtonCenter(
    side: -1, end: 1, size: buttonSize)
settings.position = layout.cornerButtonCenter(
    side: 1, end: 1, size: buttonSize)
back.position = layout.cornerButtonCenter(
    .leading, end: 1, size: buttonSize)
settings.position = layout.cornerButtonCenter(
    .trailing, end: 1, size: buttonSize)
let top = layout.contentTop - titleGap
title.position = pt(layout.contentRect.midX, top)
let gridWidth = CGFloat(columns) * tile
    + CGFloat(columns - 1) * gap
let left = layout.contentRect.midX - gridWidth / 2
for (i, tile) in tiles.enumerated() {
    let col = CGFloat(i % columns)
    let row = CGFloat(i / columns)
    tile.position = pt(
        left + col * (tileSize + gap) + tileSize / 2,
        top - titleHeight - listGap
            - row * (tileSize + gap) - tileSize / 2)
}
LayoutItem.column([
    .label(title, height: titleHeight),
    .grid(tiles.map { .node($0, size: sz(tileSize)) },
          columns: columns, gap: sz(gap)),
], gap: listGap)
.inset(.init(top: titleGap))
.aligned(.center, .top)
.place(in: layout.contentRect)
icon.position = pt(face.minX + artInset, 0)
text.horizontalAlignmentMode = .left
text.position = pt(icon.position.x
    + artWidth / 2 + artGap, 0)
LayoutItem.row([
    .node(icon, size: sz(artWidth)),
    .label(text, height: lineHeight),
], gap: artGap)
.inset(.init(leading: artInset))
.aligned(.leading)
.place(in: face)
text.horizontalAlignmentMode =
    LayoutDirection.current.labelAlignment(.leading)

Verification

LayoutItemTests and LayoutDirectionTests cover:

  • rows, columns, grids, overlays, insets and sized, each in both directions;
  • a right-to-left grid exactly mirroring the left-to-right one;
  • a fixed-direction subtree keeping its order inside a mirrored row, while standing by its parent's direction (leading in a column, aligned in an overlay);
  • node anchors;
  • language codes;
  • the edge-based SceneLayout helpers against the side-based ones.

Checks:

  • swift test
  • swift test --package-path Platform
  • Scripts/check-android.sh arm64-v8a
  • Platform/'s tests on Mac Catalyst, and builds for iOS and tvOS

No game moves onto it in this PR, so nothing on screen changes. The first adopters follow as game PRs that move their submodule.

…g order, and LayoutDirection

A small rect-based layout over SceneLayout's rects. Items state their
size; placing sets resting positions once. Leading and trailing mirror
in a right-to-left language; direction(_:) keeps a physical subtree as
drawn. LayoutDirection.current follows Typography.follow's language,
with forced for a debug override. SceneLayout's corner and edge helpers
take a HorizontalEdge.
@fleon
fleon marked this pull request as ready for review November 23, 2026 15:56

private func place(in rect: CGRect, direction inherited: LayoutDirection, hDefault: HAlign, vDefault: VAlign) {
let direction = fixedDirection ?? inherited
let frame = frame(in: rect, direction: direction, h: hAlign ?? hDefault, v: vAlign ?? vDefault)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

direction(_:) changes where the item stands in its parent's rect, not just how its contents are laid out: this frame is worked out with fixedDirection in place of the inherited direction (and frame(in:) at line 218 does the same). So .leading/.trailing on a fixed-direction item, or the .leading it gets from a column, picks the physical side. Example in Arabic: .column([title, track.direction(.leftToRight)], align: .leading) puts the title on the right but the track on the left, and board.direction(.leftToRight).aligned(.leading, .top) in an overlay goes to the top left. UIKit's semanticContentAttribute only affects the view's subviews; the view's own place follows its superview. The AGENTS.md rule ("keeps its own with direction(_:)") reads the same way. Work out this frame with inherited, and apply fixedDirection only to the children, i.e. to the direction passed down and used for rightToLeft/insets.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest push. The frame is now worked out with the inherited direction (in place and in frame(in:)), and fixedDirection applies only to the contents: the direction passed down, rows, grids and insets. A new test, aFixedDirectionItemStandsByItsParents, covers both examples: in right-to-left, a left-to-right track in a leading column stands on the right with its contents in left-to-right order, and a fixed-direction board aligned leading in an overlay goes to the top right. AGENTS.md and the README now say the same.

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Claude review (house rules)
Reviewed 7 files (AGENTS.md, README.md, LayoutDirection.swift, LayoutItem.swift, SceneLayout.swift, TextHelpers.swift, LayoutItemTests.swift) against AGENTS.md; this repository has no CONTRIBUTING.md.
Left 1 inline comment.

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Claude review (house rules): correction to the comment above. This repository does have a CONTRIBUTING.md. I've now read it, and it doesn't change the review: still 1 inline comment.

@fleon
fleon marked this pull request as draft November 23, 2026 16:29
@fleon
fleon marked this pull request as ready for review November 23, 2026 16:30
Comment on lines +196 to +203
item.size = CGSize(width: width ?? size.width, height: height ?? size.height)
return item
}

/// What this item holds laid out in `direction` whatever the language:
/// for what shows a physical direction (a board, a track). Where the
/// item itself stands in its parent still follows the parent's.
public func direction(_ direction: LayoutDirection) -> LayoutItem {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The anchor is read from label.horizontalAlignmentMode once, when the item is built. Anything that changes the mode later leaves the label positioned for the old mode. The third row of the PR's own Before / after table does exactly that: it sets text.horizontalAlignmentMode = LayoutDirection.current.labelAlignment(.leading) after .label(text, …) and .place(in:). The label is built .center, so it gets anchor 0.5 and its position lands on the middle of its slot. Switching to .left then starts the text there, half its width toward the trailing side of where the row put it. In right-to-left, .right ends the text at that middle point instead. Reading the mode inside the place closure would help when it's set between building and placing. The documented pattern also needs to set the alignment before building the item.

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Claude review (house rules)
Reviewed 7 files, after reading AGENTS.md and CONTRIBUTING.md (if present). Left 1 inline comment.

@fleon fleon closed this Nov 23, 2026
@fleon
fleon deleted the layout-engine branch November 23, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant