Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block mode for #alternatives family #164

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions book/src/dynamic/alternatives.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 23 additions & 4 deletions logic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -147,7 +165,8 @@
align(position, content)
))
}
})
}

}

#let alternatives(
Expand Down
20 changes: 20 additions & 0 deletions tests/alternatives.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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.
]
]

]