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

State of a segmented button: a row of joined toggle buttons of which one (or several) can be selected. More...

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

Inheritance diagram for guillaume::components::SegmentedButton:
Collaboration diagram for guillaume::components::SegmentedButton:

Classes

struct  Segment
 What one segment shows and whether it can be pressed. More...

Public Types

enum class  SelectionMode { Single , Multi }
 How many segments can be selected at once. More...
using ChangeHandler = std::function<void(std::size_t)>
 Called with the index of the segment the user just pressed.

Public Member Functions

 SegmentedButton (void)=default
 Create the state of a single-selection segmented button with no segment.
 ~SegmentedButton (void)=default
 Default destructor.
const std::vector< Segment > & getSegments (void) const noexcept
 Get the options of every segment.
SegmentedButton & setSegments (const std::vector< Segment > &segments)
 Replace the options of every segment.
SegmentedButton & setSegmentDisabled (std::size_t index, bool disabled)
 Record whether one segment is disabled.
SelectionMode getSelectionMode (void) const noexcept
 Get the selection mode.
SegmentedButton & setSelectionMode (SelectionMode mode)
 Choose how many segments can be selected at once.
const std::vector< ecs::Entity::Identifier > & getButtons (void) const noexcept
 Get the toggle button child of each segment.
SegmentedButton & setButtons (const std::vector< ecs::Entity::Identifier > &buttons)
 Record the toggle button child of each segment.
const ChangeHandler & getOnChange (void) const noexcept
 Get the callback run when the user presses a segment.
SegmentedButton & setOnChange (ChangeHandler handler)
 Set the callback run when the user presses a segment.
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 MinSegmentWidth = 76.0f
 Minimum width of a segment, in dp.

Detailed Description

State of a segmented button: a row of joined toggle buttons of which one (or several) can be selected.

It holds the options of each segment, the selection mode, the press callback and the identifier of the toggle button child of each segment. The selection itself is not stored here: a segment is selected when its button child is (components::Button::isSelected).

Role in the frame:

  • entities::SegmentedButton writes the configuration, creates one button child per segment and records their identifiers here.
  • systems::SegmentedButton (Layout phase) reads it: it gives a selected segment a check glyph, makes all segments as wide as the widest, places them in a row and rounds only the outer corners of the row.

To select a segment from code, call systems::SegmentedButton::select() or deselect(); to read the selection, isSelected() or selectedIndices().

See also
systems::SegmentedButton, components::Button, components::CommonButton

Definition at line 60 of file segmented_button.hpp.

Member Typedef Documentation

◆ ChangeHandler

using guillaume::components::SegmentedButton::ChangeHandler = std::function<void(std::size_t)>

Called with the index of the segment the user just pressed.

Definition at line 94 of file segmented_button.hpp.

Member Enumeration Documentation

◆ SelectionMode

How many segments can be selected at once.

Single: selecting a segment deselects the others, and pressing the selected segment keeps it selected. Multi: each segment toggles on its own.

Definition at line 70 of file segmented_button.hpp.

Member Function Documentation

◆ getButtons()

const std::vector< ecs::Entity::Identifier > & guillaume::components::SegmentedButton::getButtons ( void ) const
noexcept

Get the toggle button child of each segment.

Returns
Their identifiers, in segment order; entry i is the button of getSegments()[i].

Definition at line 71 of file segmented_button.cpp.

Here is the caller graph for this function:

◆ getOnChange()

const SegmentedButton::ChangeHandler & guillaume::components::SegmentedButton::getOnChange ( void ) const
noexcept

Get the callback run when the user presses a segment.

Returns
The callback, empty when none is set.

Definition at line 87 of file segmented_button.cpp.

◆ getSegments()

const std::vector< SegmentedButton::Segment > & guillaume::components::SegmentedButton::getSegments ( void ) const
noexcept

Get the options of every segment.

Returns
The options, in the order the segments are laid out.

Definition at line 30 of file segmented_button.cpp.

◆ getSelectionMode()

SegmentedButton::SelectionMode guillaume::components::SegmentedButton::getSelectionMode ( void ) const
noexcept

Get the selection mode.

Returns
Single (the default) or Multi.

Definition at line 56 of file segmented_button.cpp.

◆ setButtons()

SegmentedButton & guillaume::components::SegmentedButton::setButtons ( const std::vector< ecs::Entity::Identifier > & buttons)

Record the toggle button child of each segment.

Marks the component changed when the list differs.

Parameters
buttonsTheir identifiers, in segment order.
Returns
This component, for chaining.

Definition at line 76 of file segmented_button.cpp.

Here is the call graph for this function:

◆ setOnChange()

SegmentedButton & guillaume::components::SegmentedButton::setOnChange ( ChangeHandler handler)

Set the callback run when the user presses a segment.

It runs after the selection was updated, and not for a selection made from code. Does not mark the component changed.

Parameters
handlerThe callback; it receives the index of the pressed segment.
Returns
This component, for chaining.

Definition at line 92 of file segmented_button.cpp.

Here is the call graph for this function:

◆ setSegmentDisabled()

SegmentedButton & guillaume::components::SegmentedButton::setSegmentDisabled ( std::size_t index,
bool disabled )

Record whether one segment is disabled.

It only updates the stored option, and marks the component changed when it differs. Use systems::SegmentedButton::setSegmentDisabled() to also disable and restyle the segment's button.

Parameters
indexThe segment index (ignored when out of range).
disabledTrue when the segment cannot be pressed.
Returns
This component, for chaining.

Definition at line 45 of file segmented_button.cpp.

Here is the call graph for this function:

◆ setSegments()

SegmentedButton & guillaume::components::SegmentedButton::setSegments ( const std::vector< Segment > & segments)

Replace the options of every segment.

Marks the component changed when the list differs. The widget class creates one button child per entry.

Parameters
segmentsThe new options, in layout order.
Returns
This component, for chaining.

Definition at line 36 of file segmented_button.cpp.

Here is the call graph for this function:

◆ setSelectionMode()

SegmentedButton & guillaume::components::SegmentedButton::setSelectionMode ( SelectionMode mode)

Choose how many segments can be selected at once.

Marks the component changed when the mode differs. It does not touch the current selection.

Parameters
modeSingle or Multi.
Returns
This component, for chaining.

Definition at line 61 of file segmented_button.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ MinSegmentWidth

float guillaume::components::SegmentedButton::MinSegmentWidth = 76.0f
staticconstexpr

Minimum width of a segment, in dp.

Definition at line 97 of file segmented_button.hpp.


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