diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-01-29 22:32:01 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-01-29 22:32:01 +0100 |
| commit | 44ebefbec23114d6ed7a96e7e3cb9078441aff31 (patch) | |
| tree | 104b51725e2741a7bea774128475c824cc2e60bf | |
| parent | 24d513d8917de0a8c23916837e5ea7fbb550db8b (diff) | |
Add `hide` class
| -rw-r--r-- | src/library/hidden.rs | 32 | ||||
| -rw-r--r-- | src/library/mod.rs | 3 | ||||
| -rw-r--r-- | tests/ref/layout/hide.png | bin | 0 -> 936 bytes | |||
| -rw-r--r-- | tests/typ/layout/hide.typ | 5 |
4 files changed, 40 insertions, 0 deletions
diff --git a/src/library/hidden.rs b/src/library/hidden.rs new file mode 100644 index 00000000..7287bf3d --- /dev/null +++ b/src/library/hidden.rs @@ -0,0 +1,32 @@ +//! Hiding of nodes without affecting layout. + +use super::prelude::*; + +/// A node that hides its child without affecting layout. +#[derive(Debug, Hash)] +pub struct HideNode(pub PackedNode); + +#[class] +impl HideNode { + fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Node> { + Ok(Node::inline(Self(args.expect("body")?))) + } +} + +impl Layout for HideNode { + fn layout( + &self, + ctx: &mut LayoutContext, + regions: &Regions, + styles: StyleChain, + ) -> Vec<Constrained<Rc<Frame>>> { + let mut frames = self.0.layout(ctx, regions, styles); + + // Clear the frames. + for Constrained { item: frame, .. } in &mut frames { + *frame = Rc::new(Frame { elements: vec![], ..**frame }); + } + + frames + } +} diff --git a/src/library/mod.rs b/src/library/mod.rs index 51d57c3e..81c8fcdc 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -10,6 +10,7 @@ pub mod deco; pub mod flow; pub mod grid; pub mod heading; +pub mod hidden; pub mod image; pub mod link; pub mod list; @@ -33,6 +34,7 @@ pub use deco::*; pub use flow::*; pub use grid::*; pub use heading::*; +pub use hidden::*; pub use link::*; pub use list::*; pub use pad::*; @@ -116,6 +118,7 @@ pub fn new() -> Scope { std.def_class::<TransformNode<Move>>("move"); std.def_class::<TransformNode<Scale>>("scale"); std.def_class::<TransformNode<Rotate>>("rotate"); + std.def_class::<HideNode>("hide"); std.def_class::<StackNode>("stack"); std.def_class::<GridNode>("grid"); std.def_class::<ColumnsNode>("columns"); diff --git a/tests/ref/layout/hide.png b/tests/ref/layout/hide.png Binary files differnew file mode 100644 index 00000000..8a60cf90 --- /dev/null +++ b/tests/ref/layout/hide.png diff --git a/tests/typ/layout/hide.typ b/tests/typ/layout/hide.typ new file mode 100644 index 00000000..a979b24f --- /dev/null +++ b/tests/typ/layout/hide.typ @@ -0,0 +1,5 @@ +// Test the `hide` function. + +--- +AB #h(1fr) CD \ +#hide[A]B #h(1fr) C#hide[D] |
