3 releases (breaking)

0.3.0 Jan 26, 2022
0.2.0 Sep 18, 2021
0.1.0 Jul 17, 2021

#706 in Algorithms

Download history 82/week @ 2022-11-27 211/week @ 2022-12-04 177/week @ 2022-12-11 133/week @ 2022-12-18 111/week @ 2022-12-25 81/week @ 2023-01-01 86/week @ 2023-01-08 92/week @ 2023-01-15 76/week @ 2023-01-22 165/week @ 2023-01-29 87/week @ 2023-02-05 83/week @ 2023-02-12 105/week @ 2023-02-19 64/week @ 2023-02-26 61/week @ 2023-03-05 26/week @ 2023-03-12

278 downloads per month
Used in 12 crates (4 directly)

MIT license

1.5K SLoC


Morphorm is a crate for determining the position and size of UI elements which are organised in a visual tree. The algorithm is written from scratch but is based on the subform layout system.

How to use

To try and keep things as generic as possible Morphorm does not provide any containers for representing the layout properties or visual tree. Instead, three traits must be implemented by the users' containers in order to utilise the layout algorithm:

  • Node represents a UI element which can be sized and positioned. The node itself could contain the desired layout properties, or the properties can be provided by an external source (such as an ECS component store), which is provided by the Data associated type.
  • Hierarchy represents the visual tree of nodes. Morphorm requires three iterators to be provided: an upward iterator which iterates the tree from bottom to top in depth first order; a downward iterator which iterates the tree from the root in depth first order; and a child iterator which iterates through the children of a specified node.
  • Cache represents a store for the output of the layout computation as well as intermediate values used. The store is indexed by a reference to the node type and so the computed results cannot be stored within the node itself (due to the borrow checker).

Once the appropriate traits have been implmented, layout can be performed on the whole tree, e.g.

layout(&mut world.cache, &world.tree, &world.store);

In this example the cache, tree, and a store for the node properties are kept in an ECS world struct and a node is simply an entity id.

See examples for details.

Layout system description


An overview of stacks can be found here: overview (stacks)