diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-10-28 19:21:29 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-10-28 19:21:29 +0100 |
| commit | 78658133f2baf8e4af241592fa7da21948cd62b1 (patch) | |
| tree | cbe53ea8647c119a116ac66140fb2317f7654b21 /src | |
| parent | 64504d633d55074f902eda041b7c720d305d5338 (diff) | |
| download | chawan-78658133f2baf8e4af241592fa7da21948cd62b1.tar.gz | |
layout: implement position: fixed
just a special case of position: absolute
Diffstat (limited to 'src')
| -rw-r--r-- | src/layout/engine.nim | 32 | ||||
| -rw-r--r-- | src/layout/renderdocument.nim | 2 |
2 files changed, 18 insertions, 16 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim index 3a03c082..6ced24bb 100644 --- a/src/layout/engine.nim +++ b/src/layout/engine.nim @@ -179,9 +179,9 @@ func outerSize(box: BlockBox; dim: DimensionType): LayoutUnit = func minClamp(x: LayoutUnit; span: Span): LayoutUnit = return max(min(x, span.send), span.start) -#TODO these are not really static-like, just unimplemented +#TODO implement sticky const PositionStaticLike = { - PositionStatic, PositionFixed, PositionSticky + PositionStatic, PositionSticky } type @@ -1229,7 +1229,7 @@ const DisplayBlockLike = {DisplayBlock, DisplayListItem, DisplayInlineBlock} # Return true if no more margin collapsing can occur for the current strut. func canFlushMargins(box: BlockBox; sizes: ResolvedSizes): bool = - if box.computed{"position"} == PositionAbsolute: + if box.computed{"position"} in {PositionAbsolute, PositionFixed}: return false return sizes.padding.top != 0 or sizes.padding.bottom != 0 or box.inline != nil or box.computed{"display"} notin DisplayBlockLike or @@ -1318,6 +1318,14 @@ proc popPositioned(lctx: LayoutContext; overflow: var Overflow; #TODO this overflow looks wrong too lctx.popPositioned(overflow, space) +proc queueAbsolute(lctx: LayoutContext; box: BlockBox; offset: Offset) = + case box.computed{"position"} + of PositionAbsolute: + lctx.positioned[^1].queue.add(QueuedAbsolute(child: box, offset: offset)) + of PositionFixed: + lctx.positioned[0].queue.add(QueuedAbsolute(child: box, offset: offset)) + else: assert false + type BlockState = object offset: Offset @@ -1491,7 +1499,7 @@ proc addInlineAbsolute(ictx: var InlineContext; state: var InlineState; # flush if there is already something on the line *and* our outer # display is block. offset.y += ictx.cellHeight - lctx.positioned[^1].queue.add(QueuedAbsolute(child: box, offset: offset)) + lctx.queueAbsolute(box, offset) proc addInlineBlock(ictx: var InlineContext; state: var InlineState; box: BlockBox) = @@ -1520,7 +1528,7 @@ proc addInlineBlock(ictx: var InlineContext; state: var InlineState; proc addBox(ictx: var InlineContext; state: var InlineState; box: BlockBox) = - if box.computed{"position"} == PositionAbsolute: + if box.computed{"position"} in {PositionAbsolute, PositionFixed}: # This doesn't really have to be an inline block. I just want to # handle its positioning here. ictx.addInlineAbsolute(state, box) @@ -2390,12 +2398,9 @@ proc layoutFlex(bctx: var BlockContext; box: BlockBox; sizes: ResolvedSizes) = # basis was e.g. 0. Try to resize it to something more usable. childSizes.space[dim] = stretch(minu) lctx.layoutFlexChild(child, childSizes) - if child.computed{"position"} == PositionAbsolute: + if child.computed{"position"} in {PositionAbsolute, PositionFixed}: # Absolutely positioned flex children do not participate in flex layout. - lctx.positioned[^1].queue.add(QueuedAbsolute( - child: child, - offset: offset(x = 0, y = 0) - )) + lctx.queueAbsolute(child, offset(x = 0, y = 0)) continue if canWrap and (sizes.space[dim].t == scMinContent or sizes.space[dim].isDefinite and @@ -2560,7 +2565,7 @@ proc layoutBlockChildren(state: var BlockState; bctx: var BlockContext; parent: BlockBox) = for child in parent.nested: var dy: LayoutUnit = 0 # delta - if child.computed{"position"} == PositionAbsolute: + if child.computed{"position"} in {PositionAbsolute, PositionFixed}: # Delay this block's layout until its parent's dimensions are # actually known. var offset = state.offset @@ -2570,10 +2575,7 @@ proc layoutBlockChildren(state: var BlockState; bctx: var BlockContext; # position. #TODO but I'm not sure if this is really what the standard says... offset.y += bctx.marginTodo.sum() - bctx.lctx.positioned[^1].queue.add(QueuedAbsolute( - child: child, - offset: offset - )) + bctx.lctx.queueAbsolute(child, offset) continue if child.computed.establishesBFC(): dy = state.layoutBlockChildBFC(bctx, child) diff --git a/src/layout/renderdocument.nim b/src/layout/renderdocument.nim index 0dafc25c..dcd1d114 100644 --- a/src/layout/renderdocument.nim +++ b/src/layout/renderdocument.nim @@ -390,7 +390,7 @@ proc renderBlockBox(grid: var FlexibleGrid; state: var RenderState; if box == nil: # positioned marker discard state.absolutePos.pop() continue - if box.computed{"position"} == PositionAbsolute: + if box.computed{"position"} in {PositionAbsolute, PositionFixed}: if not box.computed{"left"}.auto: offset.x = state.absolutePos[^1].offset.x elif not box.computed{"right"}.auto: |