Add hide class

This commit is contained in:
Laurenz 2022-01-29 22:32:01 +01:00
parent 24d513d891
commit 44ebefbec2
4 changed files with 40 additions and 0 deletions

32
src/library/hidden.rs Normal file
View File

@ -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
}
}

View File

@ -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");

BIN
tests/ref/layout/hide.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

View File

@ -0,0 +1,5 @@
// Test the `hide` function.
---
AB #h(1fr) CD \
#hide[A]B #h(1fr) C#hide[D]