Skip to content

Environment ​

The environment holds values a component can request without its parent passing them down as props.

Requesting values ​

A parameter added to render receives a value of its type from the nearest enclosing Provider, or from app data.

cpp
View render(ColorScheme scheme, const Theme& theme, Assets& assets) const {
    const auto logo = assets.load<Pixmap>("assets/logo.png");
    ...
}

If nothing above provides the type, Barton passes a default-constructed value. A few values are always available:

TypeWhat it is
ColorSchemeLight or dark, from the system unless overridden
const Theme&Colors the built-in components style themselves with
Assets&Loads images and fonts in the background. See Assets

Providing values ​

Wrapping views in a Provider makes a value available to everything beneath it. Any copyable type works.

cpp
struct Tint {
    Color panel = Color::from_rgb_u8(52, 58, 72);
};

struct Panel {
    View render(const Tint& tint) const {
        return Container { background_color = tint.panel };
    }
};

Row {
    Panel {},
    Provider { Tint { .panel = Color::RED }, Panel {} },
}

The first panel gets the default Tint. The second gets red. The same works for ColorScheme, which is how a window forces dark mode.

App data ​

Data shared by every window is registered once on the App and read by type. See Windows & menus.

Barton is in early development.