From 1996d501c346d4795182c75fd6c9249073315cc1 Mon Sep 17 00:00:00 2001 From: Zack Noyes Date: Mon, 6 May 2024 17:31:16 +1000 Subject: [PATCH 1/2] add logic and tests for an alternatives block mode --- logic.typ | 27 +++++++++++++++++++++++---- tests/alternatives.typ | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/logic.typ b/logic.typ index 64e6c74..ef053a6 100644 --- a/logic.typ +++ b/logic.typ @@ -127,7 +127,11 @@ } } -#let alternatives-match(subslides-contents, position: bottom + left) = { +#let alternatives-match( + subslides-contents, + position: bottom + left, + block_mode: false +) = context { let subslides-contents = if type(subslides-contents) == "dictionary" { subslides-contents.pairs() } else { @@ -136,8 +140,22 @@ let subslides = subslides-contents.map(it => it.first()) let contents = subslides-contents.map(it => it.last()) - style(styles => { - let sizes = contents.map(c => measure(c, styles)) + + if block_mode { + layout(size => { + // Determine how much height each contents will take when given full width + let sizes = contents.map(c => measure(block(width: size.width, c))) + let max-height = calc.max(..sizes.map(sz => sz.height)) + for (subslides, content) in subslides-contents { + only(subslides, block( + width: size.width, + height: max-height, + align(position, content) + )) + } + }) + } else { + let sizes = contents.map(c => measure(c)) let max-width = calc.max(..sizes.map(sz => sz.width)) let max-height = calc.max(..sizes.map(sz => sz.height)) for (subslides, content) in subslides-contents { @@ -147,7 +165,8 @@ align(position, content) )) } - }) + } + } #let alternatives( diff --git a/tests/alternatives.typ b/tests/alternatives.typ index 39febea..8be48a0 100644 --- a/tests/alternatives.typ +++ b/tests/alternatives.typ @@ -39,3 +39,23 @@ #alternatives-fn(count: 5, subslide => numbering("(i)", subslide)) ] + +#polylux-slide[ + == Test that block mode works + + #alternatives(block_mode: true)[ + This is inline content + ][ + #rect(width: 100%, height: 60pt, fill: red)[ + This rectangle should fill the page and have a red background + ] + ] + + #alternatives(position: horizon + center, block_mode: true)[ + #rect(width: 100%, height: 60pt, fill: green)[ + This rectangle should fill the page and have a green background and + be `horizon + center` centered. + ] + ] + +] \ No newline at end of file From 7c8778609cf90f3b6fa3b968ba5919378b7e7134 Mon Sep 17 00:00:00 2001 From: Zack Noyes Date: Mon, 6 May 2024 17:55:00 +1000 Subject: [PATCH 2/2] added section to book --- book/src/dynamic/alternatives.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/book/src/dynamic/alternatives.md b/book/src/dynamic/alternatives.md index e0a00f6..352b625 100644 --- a/book/src/dynamic/alternatives.md +++ b/book/src/dynamic/alternatives.md @@ -66,6 +66,16 @@ All functions described on this page have such a `position` argument. Similar to `#one-by-one`, `#alternatives` also has an optional `start` argument that works just the same. +### Block mode + +By default, `#alternatives` lays out elements as if they are inline (it wraps +them in a `#box`). Sometimes, this will not be desirable, such as when laying +out images using `#alternatives`. + +If the `block_mode` argument is set to `true`, then `#alternatives` will lay +out elements as block level elements, taking up the full available width and +adjusting to use the height of the largest element. + ## `#alternatives-match` `#alternatives` has a couple of "cousins" that might be more convenient in some situations.