Skip to content

ToggleSwitchWidget

A horizontal toggle switch that displays multiple options side-by-side and allows selection using arrow keys or mouse clicks.

ToggleSwitch is a focusable widget that presents a set of options in a compact horizontal layout. Users can navigate between options using left/right arrow keys or click directly on an option. It's commonly used for binary choices (On/Off) or selecting from a small set of mutually exclusive options (Low/Medium/High).

Basic Usage

Create a toggle switch using the fluent API by passing an array of options. Use OnSelectionChanged to respond when the user changes the selection:

csharp

State Management

ToggleSwitch selection state is managed internally by the node and preserved across re-renders. Use the OnSelectionChanged event to synchronize with your own application state when needed.

Multiple Options

ToggleSwitch isn't limited to binary choices—you can provide as many options as needed. Use the selectedIndex parameter to set the initial selection:

csharp

The widget automatically wraps around when navigating with arrow keys:

  • Pressing Right on the last option moves to the first option
  • Pressing Left on the first option moves to the last option

Selection Changed Event

Use OnSelectionChanged to respond when the user changes the selection:

csharp

The ToggleSelectionChangedEventArgs provides:

  • SelectedIndex - The index of the newly selected option (0-based)
  • SelectedOption - The text of the newly selected option
  • Widget - The source ToggleSwitchWidget
  • Node - The underlying ToggleSwitchNode
  • Context - Access to the application context

Keyboard and Mouse Navigation

ToggleSwitch supports multiple input methods:

InputAction
Left ArrowSelect previous option (wraps to last)
Right ArrowSelect next option (wraps to first)
Mouse ClickSelect the clicked option

Mouse Support

In terminals that support mouse input, users can click directly on any option to select it. The widget calculates which option was clicked based on the X position within the rendered text.

Focus Behavior

ToggleSwitch visually indicates its focus state with different bracket colors:

StateAppearance
UnfocusedSubtle gray brackets < Option >
FocusedBright brackets with highlighted selection
csharp

When focused:

  • The selected option has a bright background (white by default)
  • The brackets change to a brighter color
  • Arrow key navigation is active

When unfocused:

  • The selected option still shows a subtle background (gray by default)
  • The brackets are dimmed
  • The selection is visible but less prominent

Focus Navigation

  • Tab - Move focus to the next focusable widget
  • Shift+Tab - Move focus to the previous focusable widget

Theming

Customize ToggleSwitch appearance using theme elements:

csharp
var theme = Hex1bTheme.Create()
    .Set(ToggleSwitchTheme.FocusedSelectedForegroundColor, Hex1bColor.Black)
    .Set(ToggleSwitchTheme.FocusedSelectedBackgroundColor, Hex1bColor.Cyan)
    .Set(ToggleSwitchTheme.UnfocusedSelectedForegroundColor, Hex1bColor.White)
    .Set(ToggleSwitchTheme.UnfocusedSelectedBackgroundColor, Hex1bColor.DarkGray)
    .Set(ToggleSwitchTheme.LeftBracket, "[ ")
    .Set(ToggleSwitchTheme.RightBracket, " ]");

await using var terminal = Hex1bTerminal.CreateBuilder()
    .WithHex1bApp((app, options) =>
    {
        options.Theme = theme;
        return ctx => /* ... */;
    })
    .Build();

await terminal.RunAsync();

Available Theme Elements

ElementTypeDefaultDescription
FocusedSelectedForegroundColorHex1bColorBlackText color of selected option when focused
FocusedSelectedBackgroundColorHex1bColorWhiteBackground of selected option when focused
UnfocusedSelectedForegroundColorHex1bColorBlackText color of selected option when unfocused
UnfocusedSelectedBackgroundColorHex1bColorGrayBackground of selected option when unfocused
UnselectedForegroundColorHex1bColorDefaultText color of unselected options
UnselectedBackgroundColorHex1bColorDefaultBackground of unselected options
FocusedBracketForegroundColorHex1bColorWhiteBracket color when focused
FocusedBracketBackgroundColorHex1bColorDefaultBracket background when focused
UnfocusedBracketForegroundColorHex1bColorGrayBracket color when unfocused
UnfocusedBracketBackgroundColorHex1bColorDefaultBracket background when unfocused
LeftBracketstring"< "Left bracket decoration
RightBracketstring" >"Right bracket decoration
Separatorstring" | "Separator between options

Common Use Cases

Binary Toggles

Perfect for on/off, yes/no, enabled/disabled choices:

csharp

Mode Selection

Choose between a small set of modes:

csharp

Settings Panels

Multiple toggles in a form layout:

csharp

Released under the MIT License.