zng

Module container

source
Expand description

Container widget.

Base widget for all widgets that are designed around a single child widget or a primary child widget surrounded by secondary widgets.

§Child Inserts

The example below demonstrates a container with a primary child that fills the available space not taken by the other children. The top child is also separated from the primary child by 5dip.

use zng::prelude::*;

Container! {
    child_top = {
        node: Text!("secondary (top)"),
        spacing: 5,
    };
    child = Text! { txt = "primary"; widget::background_color = colors::BLUE };
    child_bottom = {
        node: Text!("secondary (bottom)"),
        spacing: 0,
    };
}

Note that Window! inherits from Container! to the example above could become the skeleton of a classic app window:

Window! {
    child_top = tools(), 0;
    child = content();
    child_bottom = status(), 0;
}

Note that a similar layout could be achieved using widgets like Grid!, but the child insert properties are a convenient way to define this kind of widget, also a container widget without child inserts does not pay any extra cost, the insertion layout implementation if fully contained to the insert properties.

§Child Nodes

The child can by any UiNode type, not just widgets, you can use this to plug nodes directly on the UI.

use zng::{prelude::*, prelude_wgt::*};

Container! {
    widget::background_color = colors::BLACK;
    child_align = layout::Align::CENTER;
    child = {
        let size = Size::splat(40);
        let mut render_size = PxSize::zero();
        match_node_leaf(move |op| match op {
            UiNodeOp::Measure { desired_size, .. } => *desired_size = size.layout(),
            UiNodeOp::Layout { final_size, .. } => {
                render_size = Size::splat(40).layout();
                *final_size = render_size;
            },
            UiNodeOp::Render { frame } => frame.push_color(
                PxRect::from_size(render_size),
                FrameValue::Value(colors::GREEN.into())
            ),
            _ => {}
        })
    }
}

§Full API

See zng_wgt_container for the full widget API.

Structs§

Enums§

Functions§

  • P The widget’s child.
  • P Insert node below the widget’s child.
  • P Insert node to the right of the widget’s child in LTR contexts or to the right of the widget’s child in RTL contexts.
  • P Insert node in the placement relative to the widget’s child.
  • P Insert node to the left of the widget’s child.
  • P Insert node below the widget’s child, outside of the child layout.
  • P Insert node to the right of the widget’s child in LTR contexts or to the right of the widget’s child in RTL contexts, outside of the child layout.
  • P Insert node in the placement relative to the widget’s child, outside of the child layout.
  • P Insert node to the left of the widget’s child, outside of the child layout.
  • P Insert node to the right of the widget’s child, outside of the child layout.
  • P Insert node to the left of the widget’s child in LTR contexts or to the right in RTL contexts, outside of the child layout.
  • P Insert node above the widget’s child, outside of the child layout.
  • P Insert node over the widget’s child.
  • P Insert node to the right of the widget’s child.
  • P Insert node to the left of the widget’s child in LTR contexts or to the right in RTL contexts.
  • P Insert node above the widget’s child.
  • P Insert node under the widget’s child.