Layers 2.0 is archived. The latest version is 3.0. Read the 3.0 docs

LAttribute Class

#include <Layers/lattribute.h>
Inherits: LObject

Public Functions

LAttribute(const LString & name)
LAttribute(const LString & name, double value)
LAttribute(const LString & name, const char * value)
LAttribute(const LString & name, const LVariant & value)
LAttribute(const LString & name, const json & value)
~LAttribute()
T as(const LStringList & state_combo = LStringList(), LStyle * context = nullptr)
const T * as_if(const LStringList & state_combo = LStringList(), LStyle * context = nullptr)
void break_link(bool update = true)
void clear_style_attribute()
void clone_to(LObject * parent)
void create_link(LAttribute * link_attr)
void create_link(LLink * link)
LAttributeList dependent_attributes(bool include_indirect_dependencies = false)
void disconnect_change(const LConnectionID & connection)
void disconnect_link_change(const LConnectionID & connection)
bool has_states()
LConnectionID on_change(std::function<void ()> callback)
LConnectionID on_link_change(std::function<void ()> callback)
LString path()
void resolve_links()
void set_style_attribute(LAttribute * style_attribute)
void set_value(const char * value)
void set_value(const LVariant & value)
LAttribute * state(const LStringList & state_combo)
LAttributeMap states(bool include_parent_states = true)
LLink * link()
LAttribute * style_attribute()
json to_json_object()
json to_json_value()
size_t type_index()
const LVariant & value()

Detailed Description

Represents a named value within the Layers theme framework.

Attributes are the building blocks of themes and styles. They store numbers, strings, booleans, or arrays through the LVariant type.

Most attributes are defined in JSON themes and styles rather than created programmatically. For JSON syntax and concepts, see the Layers README. This documentation covers the C++ interface for working with attributes in code.

Values

Create attributes with different value types:

LAttribute* fill =
    new LAttribute("Fill", "#ffffff");

LAttribute* thickness =
    new LAttribute("Thickness", 5.0);

Access values with as<T>():

LString f = fill->as<LString>();
// f = "#ffffff"

double t = thickness->as<double>();
// t = 5.0

For safe access, as_if<T>() returns nullptr if the type doesn't match:

if (const auto* f = fill->as_if<LString>())
    // fill must be an LString

Paths

Since attributes are owned, they exist within an object hierarchy.

Every attribute has a path identifying its location in the hierarchy.

Paths use / to separate between object names:

// Assume fill belongs to a button
"Button/Fill"

Linking

Linking lets one attribute reference another, further known as the link attribute. When linked, valuation returns the link attribute's value:

LAttribute* primary =
    new LAttribute("Primary", "#001122");

fill->create_link(primary);

f = fill->as<LString>();
// f = #001122 (value indirectly from primary)

An attribute that links to another is known as a dependent attribute.

You can acquire a list of dependent attributes:

LAttributeList deps = primary->dependent_attributes();
// deps contains the fill attribute

Remember to break links when they are no longer needed. When a link is broken, the attribute copies its value:

fill->break_link();

f = fill->as<LString>();
// f = #001122 (same value directly from fill now)

LAttributeList deps = primary->dependent_attributes();
// deps no longer contains the fill attribute

States

States let attributes have multiple values, accessed conditionally.

States are just sub-attributes in the object hierarchy:

lMake<LAttribute>(fill, "Selected", "#445566");
// fill is a parent receiving a "Selected" state

You can access a state value directly:

f = fill->state("Hovered")->as<LString>();
// f = #445566 (state value accessed directly)

However, prefer passing a state variable when accessing the parent's value:

// State variable, subject to change
LString state = "Selected";

f = fill->as<LString>(state);
// f = #445566 (state value accessed through fill)

You can check if an attribute has any states:

if (fill->has_states())
    // fill must have states

Change Notifications

Register callbacks to detect value changes:

LConnectionID conn = fill->on_change([]()
    { cout << "Fill changed!" << endl; });

// Remember to disconnect when it's no longer needed
fill->disconnect_change(conn);

Member Function Documentation

LAttribute(const LString & name)

Constructs an attribute with the given name.

LAttribute(const LString & name, double value)

Constructs an attribute with the given name and numeric value.

LAttribute(const LString & name, const char * value)

Constructs an attribute with the given name and string value.

LAttribute(const LString & name, const LVariant & value)

Constructs an attribute with the given name and variant value.

LAttribute(const LString & name, const json & value)

Constructs an attribute with the given name and JSON value.

~LAttribute()

No description available.

template<typename T>
T as(const LStringList & state_combo = LStringList(), LStyle * context = nullptr)

Returns the attribute's value as the specified type T.

If the attribute has states, state_combo is used to select the appropriate state. If the attribute is linked, the value is retrieved from the link attribute.

The context parameter is used for resolving relative links.

template<typename T>
const T * as_if(const LStringList & state_combo = LStringList(), LStyle * context = nullptr)

Returns a pointer to the attribute's value if it matches type T, otherwise returns nullptr.

Provides safe type checking before accessing attribute values.

void clear_style_attribute()

Clears the style attribute association.

void clone_to(LObject * parent)

No description available.

LAttributeList dependent_attributes(bool include_indirect_dependencies = false)

Returns a list of attributes that depend on this attribute.

If include_indirect_dependencies is true, includes transitive dependencies.

void disconnect_change(const LConnectionID & connection)

Disconnects the change callback associated with connection.

bool has_states()

Returns true if this attribute has state variants.

LConnectionID on_change(std::function<void ()> callback)

Registers a callback to be invoked when the attribute value changes.

Returns a connection ID that can be used to disconnect the callback.

LString path()

Returns the full path of this attribute.

void set_style_attribute(LAttribute * style_attribute)

Associates this attribute with a style attribute.

Style attributes provide default values that can be overridden.

void set_value(const char * value)

Sets the attribute value from a C-string.

void set_value(const LVariant & value)

Sets the attribute value from a variant.

LAttribute * state(const LStringList & state_combo)

Returns the state attribute matching the given state combination.

Returns nullptr if no matching state exists.

LAttributeMap states(bool include_parent_states = true)

Returns a map of all state attributes.

If include_parent_states is true, includes inherited states.

LAttribute * style_attribute()

Returns the style attribute associated with this attribute.

Returns nullptr if no style attribute is set.

json to_json_object()

Converts this attribute to a JSON object representation.

json to_json_value()

Converts this attribute's value to a JSON value.

size_t type_index()

Returns the type index of the current value in the variant.

const LVariant & value()

Returns a reference to the underlying variant value.

Appearance
Theme
—