guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
guillaume::components::Switch Class Reference

Configuration of a switch: its variant, its thumb icon, its label and the identifiers of its thumb, icon and label children. More...

#include <headers/guillaume/components/switch.hpp>

Inheritance diagram for guillaume::components::Switch:
Collaboration diagram for guillaume::components::Switch:

Public Types

enum class  Variant { Enabled , Disabled , WithIcon }
 Visual variant. More...
using ValueChangeHandler = std::function<void(bool)>

Public Member Functions

 Switch (void)=default
 Create the configuration of an enabled switch with no icon and no label.
 ~Switch (void)=default
 Default destructor.
Variant getVariant (void) const noexcept
 Get the visual variant.
Switch & setVariant (Variant variant)
 Choose the visual variant.
const std::string & getIconGlyph (void) const noexcept
 Get the name of the glyph drawn on the thumb.
Switch & setIconGlyph (const std::string &glyph)
 Set the name of the glyph drawn on the thumb.
const std::string & getLabel (void) const noexcept
 Get the label text.
Switch & setLabel (const std::string &label)
 Set the label text.
ecs::Entity::Identifier getThumb (void) const noexcept
 Get the child entity drawn as the thumb.
Switch & setThumb (ecs::Entity::Identifier identifier)
 Record which child entity is the thumb.
ecs::Entity::Identifier getIcon (void) const noexcept
 Get the child entity that draws the thumb icon.
Switch & setIcon (ecs::Entity::Identifier identifier)
 Record which child entity draws the thumb icon.
ecs::Entity::Identifier getLabelEntity (void) const noexcept
 Get the child entity that draws the label.
Switch & setLabelEntity (ecs::Entity::Identifier identifier)
 Record which child entity draws the label.
const ValueChangeHandler & getOnValueChange (void) const noexcept
 Get the callback run when the user toggles the switch.
Switch & setOnValueChange (ValueChangeHandler handler)
 Set the callback run when the user toggles the switch.
Public Member Functions inherited from guillaume::ecs::Component
bool hasChanged (void) const
 Tell whether a value of the component changed since the flags were last reset (the end of the previous frame).
void setHasChanged (bool hasChanged)
 Raise or lower the changed flag.
 Component (void)=default
 Create a component that is not marked as changed.
virtual ~Component (void)=default
 Virtual destructor, so components can be destroyed through the base class.

Static Public Attributes

static constexpr float ThumbSize = 24.0f
 Diameter of the thumb, in dp.
static constexpr float ThumbInset = 4.0f
 Inset of the thumb from the track ends, in dp.
static constexpr float LabelGap = 16.0f
 Gap between the track and the label, in dp.

Detailed Description

Configuration of a switch: its variant, its thumb icon, its label and the identifiers of its thumb, icon and label children.

Whether the switch is on is not stored here but in the components::Value of the same entity: 1 is on, 0 is off.

Role in the frame:

To switch from code call systems::Switch::setChecked(); a user press goes through systems::Switch::toggle(), which also runs the value-change callback.

See also
systems::Switch, components::Value

Definition at line 55 of file switch.hpp.

Member Typedef Documentation

◆ ValueChangeHandler

using guillaume::components::Switch::ValueChangeHandler = std::function<void(bool)>

Called with the new value (true when on) when the user toggles the switch.

Definition at line 69 of file switch.hpp.

Member Enumeration Documentation

◆ Variant

Visual variant.

Enabled is the regular look. Disabled greys the switch and makes it ignore presses. WithIcon is the variant of a switch whose thumb carries an icon; it is styled like Enabled.

Definition at line 65 of file switch.hpp.

Member Function Documentation

◆ getIcon()

ecs::Entity::Identifier guillaume::components::Switch::getIcon ( void ) const
noexcept

Get the child entity that draws the thumb icon.

Returns
Its identifier, or ecs::Entity::InvalidIdentifier when the thumb has no icon.

Definition at line 85 of file switch.cpp.

◆ getIconGlyph()

const std::string & guillaume::components::Switch::getIconGlyph ( void ) const
noexcept

Get the name of the glyph drawn on the thumb.

Returns
The glyph name, empty when the thumb has no icon.

Definition at line 43 of file switch.cpp.

◆ getLabel()

const std::string & guillaume::components::Switch::getLabel ( void ) const
noexcept

Get the label text.

Returns
The text, empty when the switch has no label.

Definition at line 57 of file switch.cpp.

◆ getLabelEntity()

ecs::Entity::Identifier guillaume::components::Switch::getLabelEntity ( void ) const
noexcept

Get the child entity that draws the label.

Returns
Its identifier, or ecs::Entity::InvalidIdentifier when the switch has no label child.

Definition at line 99 of file switch.cpp.

◆ getOnValueChange()

const Switch::ValueChangeHandler & guillaume::components::Switch::getOnValueChange ( void ) const
noexcept

Get the callback run when the user toggles the switch.

Returns
The callback, empty when none is set.

Definition at line 114 of file switch.cpp.

◆ getThumb()

ecs::Entity::Identifier guillaume::components::Switch::getThumb ( void ) const
noexcept

Get the child entity drawn as the thumb.

Returns
Its identifier, or ecs::Entity::InvalidIdentifier when the switch has no thumb child.

Definition at line 71 of file switch.cpp.

◆ getVariant()

Switch::Variant guillaume::components::Switch::getVariant ( void ) const
noexcept

Get the visual variant.

Returns
The variant (Enabled by default).

Definition at line 29 of file switch.cpp.

◆ setIcon()

Switch & guillaume::components::Switch::setIcon ( ecs::Entity::Identifier identifier)

Record which child entity draws the thumb icon.

Marks the component changed when the identifier differs. The system keeps the icon centered on the thumb.

Parameters
identifierThe icon child.
Returns
This component, for chaining.

Definition at line 90 of file switch.cpp.

Here is the call graph for this function:

◆ setIconGlyph()

Switch & guillaume::components::Switch::setIconGlyph ( const std::string & glyph)

Set the name of the glyph drawn on the thumb.

Marks the component changed when the name differs. The widget class reads it when it creates the icon child; an empty name creates none.

Parameters
glyphThe glyph name in the icon font.
Returns
This component, for chaining.

Definition at line 48 of file switch.cpp.

Here is the call graph for this function:

◆ setLabel()

Switch & guillaume::components::Switch::setLabel ( const std::string & label)

Set the label text.

Marks the component changed when the text differs. The system copies it to the switch's Accessibility label; the text drawn on screen is the one of the label child.

Parameters
labelThe label text.
Returns
This component, for chaining.

Definition at line 62 of file switch.cpp.

Here is the call graph for this function:

◆ setLabelEntity()

Switch & guillaume::components::Switch::setLabelEntity ( ecs::Entity::Identifier identifier)

Record which child entity draws the label.

Marks the component changed when the identifier differs.

Parameters
identifierThe label child.
Returns
This component, for chaining.

Definition at line 104 of file switch.cpp.

Here is the call graph for this function:

◆ setOnValueChange()

Switch & guillaume::components::Switch::setOnValueChange ( ValueChangeHandler handler)

Set the callback run when the user toggles the switch.

It is not run when the value is set from code. Does not mark the component changed.

Parameters
handlerThe callback; it receives true when the switch was just turned on.
Returns
This component, for chaining.

Definition at line 119 of file switch.cpp.

Here is the call graph for this function:

◆ setThumb()

Switch & guillaume::components::Switch::setThumb ( ecs::Entity::Identifier identifier)

Record which child entity is the thumb.

Marks the component changed when the identifier differs.

Parameters
identifierThe thumb child.
Returns
This component, for chaining.

Definition at line 76 of file switch.cpp.

Here is the call graph for this function:

◆ setVariant()

Switch & guillaume::components::Switch::setVariant ( Variant variant)

Choose the visual variant.

Marks the component changed when the variant differs. The system enables or disables the entity's interaction to match.

Parameters
variantEnabled, Disabled or WithIcon.
Returns
This component, for chaining.

Definition at line 34 of file switch.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ LabelGap

float guillaume::components::Switch::LabelGap = 16.0f
staticconstexpr

Gap between the track and the label, in dp.

Definition at line 78 of file switch.hpp.

◆ ThumbInset

float guillaume::components::Switch::ThumbInset = 4.0f
staticconstexpr

Inset of the thumb from the track ends, in dp.

Definition at line 75 of file switch.hpp.

◆ ThumbSize

float guillaume::components::Switch::ThumbSize = 24.0f
staticconstexpr

Diameter of the thumb, in dp.

Definition at line 72 of file switch.hpp.


The documentation for this class was generated from the following files: