Crate cascada

Crate cascada 

Source
Expand description

Cascada is a lightweight, high-performance layout engine for UI frameworks.

It is designed for developers building UI libraries, such as GUIs or TUI’s, who want a fast, predictable layout system without the complexity of implementing their own.

§Usage

The core of this library is the Layout trait, which is implemented for different use cases. There are currently four types of layout nodes:

Create a root layout node and pass it into the solve_layout function with the total available space.

use cascada::{HorizontalLayout, EmptyLayout, solve_layout, IntrinsicSize, Size, Layout};

// Add three equally sized nodes.
let child = EmptyLayout::new()
    .intrinsic_size(IntrinsicSize::fill());

let mut layout = HorizontalLayout::new()
    .intrinsic_size(IntrinsicSize::fill())
    .add_children([
        child.clone(),
        child.clone(),
        child,
    ]);

solve_layout(&mut layout,Size::unit(3000.0));

let size = &layout.children()[0].size();
assert_eq!(size.width,1000.0);

To get the size and position of the layout nodes you can iterate over the tree.

use cascada::{HorizontalLayout, EmptyLayout, solve_layout, IntrinsicSize, Size, Layout};
let child = EmptyLayout::new()
    .intrinsic_size(IntrinsicSize::fill());

let mut layout = HorizontalLayout::new()
    .intrinsic_size(IntrinsicSize::fill())
    .add_children([
        child.clone(),
        child.clone(),
        child,
    ]);

solve_layout(&mut layout,Size::unit(3000.0));

for node in layout.iter(){
    println!("Size: {:?}",node.size());
    println!("Position: {:?}",node.position());
}

Or you could use ids to get specific nodes from the tree.

use cascada::{HorizontalLayout, EmptyLayout, solve_layout, IntrinsicSize, Size, Layout, GlobalId};

let id = GlobalId::new();
let child = EmptyLayout::new()
    .set_id(id)
    .intrinsic_size(IntrinsicSize::fixed(20.0,50.0));

let mut layout = HorizontalLayout::new()
    .intrinsic_size(IntrinsicSize::fill())
    .add_children([
        child.clone(),
        child.clone(),
        child,
    ]);

solve_layout(&mut layout,Size::unit(3000.0));


let child = layout.get(id).unwrap();
assert_eq!(child.size().width,20.0);

See the layout module documentation for more details.

§Features flags

  • debug-tools: Enables the debug_tree method.

Re-exports§

pub use layout::*;

Modules§

debug
ffi
Experimental ffi.
layout
The layout engine uses IntrinsicSize and BoxConstraints to calculate the final size and position of the layout nodes. The core idea of the layout engine is that there are only three different sizes that a Layout node will want to be:

Structs§

Bounds
The bounds of any object that has a Size and Position.
BoxConstraints
Describes the maximum and minimum size of a layout node.
GlobalId
A global unique identifier.
IntrinsicSize
This is the preferred size of a [Layout] node.
Padding
The space between the edges of a Layout node and its content.
Position
The x and y position of a layout node.
Size
The width and height of a layout node.

Enums§

AxisAlignment
Describes how a Layout should align its children.
BoxSizing
Describes the size a [Layout] will try to be.
LayoutError