Skip to main content

ui

Macro ui 

Source
macro_rules! ui {
    ($tree:expr, $($node:tt)*) => { ... };
    (%%, $($node:tt)*) => { ... };
    ($($random:tt)*, $($node:tt)*) => { ... };
    (@@_node $tree:expr;; $binding:ident = $ha:tt | $va:tt $node:expr) => { ... };
    (@@_node $tree:expr;; $binding:ident = $ha:tt | $va:tt $node:expr => [ $($child:tt)* ]) => { ... };
    (@@_node $tree:expr;; $ha:tt | $va:tt $node:expr) => { ... };
    (@@_node $tree:expr;; $ha:tt | $va:tt $node:expr => [ $($child:tt)* ]) => { ... };
    (@@_child $tree:expr;; $node:expr => [ ]) => { ... };
    (
        @@_child
        $tree:expr;;
        $node:expr
        => [
            $binding:ident =
            $ha:tt |
            $va:tt
            $child:expr
            $(=> [ $($grand:tt)* ])?
            $(, $($rest:tt)*)?
        ]
    ) => { ... };
    (
        @@_child
        $tree:expr;;
        $node:expr
        => [
            $ha:tt |
            $va:tt
            $child:expr
            $(=> [ $($grand:tt)* ])?
            $(, $($rest:tt)*)?
        ]
    ) => { ... };
    (@@_align $ha:tt | $va: tt $node:expr) => { ... };
    (@@_align <) => { ... };
    (@@_align -) => { ... };
    (@@_align >) => { ... };
    (@@_align +) => { ... };
    (@@_align $a:tt) => { ... };
    (@@_node $tree:expr;; $($tt:tt)*) => { ... };
    (@@_child $tree:expr;; $node:expr => [ $($tt:tt)* ]) => { ... };
    ($($tt:tt)*) => { ... };
}
Expand description

A macro for making the process of creating a UI subtree easier.

The macro takes a mutable reference to the tree, or %%, signifying to create one, and a base node, and returns the index of the base node in the tree. If a tree is created, the index is stored as the first element of a tuple and the tree is the second.

Each node is represented by two alignment symbols, separated by a |, a constructor for the node, and an optional list of children, contained in square brackets after =>.

The alignment characters are orderer horizontal then vertical, with the following allowed:

+ - Full

- - Center

< - Begin

> - End

Additionally, before a node’s alignment, you may write name = to assign the index of the node to a variable.

§Example usage

use layuit::padding::{Spacer, Minimum};
use layuit::stacks::HStack;
 
let minimum;
let (_, tree) = layuit::ui!(
    %%,
    +|+ HStack::new() => [
        -|< Spacer::sized((10.0, 10.0)),
        minimum = -|- Minimum::new().with_min((20.0, 20.0)) => [
            -|- Spacer::sized((10.0, 10.0))
        ],
        -|> Spacer::sized((10.0, 10.0))
    ]
);

// Resulting tree:
// HStack (Full, Full)
// ├─ Spacer (Center, Begin)
// ├─ Minimum (Center, Center) = minimum
// │  └─ Spacer (Center, Center)
// └─ Spacer (Center, End)