From 041ebc4534088e5d550e9cc628a17c93ca087d2b Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 23 Jun 2024 13:30:56 +0200 Subject: layout: avoid shrink-to-fit padding/margin overflow --- src/layout/engine.nim | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/layout/engine.nim') diff --git a/src/layout/engine.nim b/src/layout/engine.nim index e8f8a02b..f98035c2 100644 --- a/src/layout/engine.nim +++ b/src/layout/engine.nim @@ -1119,17 +1119,22 @@ proc resolveFloatSizes(lctx: LayoutContext; space: AvailableSpace; minMaxSizes: lctx.resolveMinMaxSizes(space, inlinePadding, blockPadding, computed) ) - if not preserveHeight: # Note: preserveHeight is only true for flex. - sizes.space.h = maxContent() - if computed{"width"}.canpx(sizes.space.w): - let widthpx = computed{"width"}.spx(lctx, sizes.space.w, computed, - inlinePadding) + for dim in DimensionType: # prevent overflow + if sizes.space[dim].isDefinite() and space[dim].isDefinite(): + let overflow = sizes.space[dim].u + sizes.margin[dim].sum() + + sizes.padding[dim].sum() - space[dim].u + if overflow > 0: + sizes.space[dim].u = max(0, sizes.space[dim].u - overflow) + if computed{"width"}.canpx(space.w): + let widthpx = computed{"width"}.spx(lctx, space.w, computed, inlinePadding) sizes.space.w = stretch(clamp(widthpx, sizes.minWidth, sizes.maxWidth)) elif sizes.space.w.isDefinite(): sizes.space.w = fitContent(clamp(sizes.space.w.u, sizes.minWidth, sizes.maxWidth)) - if computed{"height"}.canpx(sizes.space.h): - let heightpx = computed{"height"}.spx(lctx, sizes.space.h, computed, + if not preserveHeight: # Note: preserveHeight is only true for flex. + sizes.space.h = maxContent() + if computed{"height"}.canpx(space.h): + let heightpx = computed{"height"}.spx(lctx, space.h, computed, blockPadding) sizes.space.h = stretch(clamp(heightpx, sizes.minHeight, sizes.maxHeight)) elif sizes.space.h.isDefinite(): -- cgit 1.4.1-2-gfad0