Toggleο
Overviewο
The Toggle widget is a custom container component for TouchGFX designed to provide an interactive toggle switch interface. It visually represents a boolean state with a sliding handle that moves between on and off positions, accompanied by appropriate background rail images.
Purposeο
The primary purpose of the Toggle widget is to offer an intuitive, touch-interactive way to control binary settings in embedded GUI applications. It provides clear visual feedback for on/off states and supports a disabled appearance variant, making it suitable for settings screens, configuration panels, and control interfaces.
Componentsο
The widget consists of the following visual components:
Handle: A movable element (
Toggle_Handle.png) that slides horizontally to indicate the current stateRail Images:
railOn: Background rail for on state (Toggle_On.png)railOff: Background rail for off state (Toggle_Off.png)railOffGray: Alternative gray background rail for disabled appearance (Toggle_Def.png)
The handle moves between x=0 (off position) and x=30 (on position), providing smooth visual transition feedback.
Functionalityο
The widgetβs core functionality revolves around state management:
State Control: Boolean state (true = on, false = off) controlled via
setState(bool)and retrieved viagetState()Visual Updates: When state changes, the appropriate rail images are shown/hidden and the handle position is updated
Gray Background Mode: Optional disabled appearance via
setBackgroundGray(bool), which uses the gray rail instead of the standard off railTouch Interaction: Inherits click handling from the base class for user interaction
The widget automatically invalidates and redraws when state changes, ensuring immediate visual feedback.
Usage in TouchGFX Designerο
Import the Custom Container Package:
Open TouchGFX Designer
Go to
Custom ContainersβImportSelect the Toggle package file (manifest.xml)
Add to Screen:
Drag the Toggle custom container from the Custom Containers panel onto your screen
Position and resize as needed (recommended size accommodates the handle movement range)
Configure Properties:
The widget has no configurable properties in Designer beyond standard container properties
Initial state and gray background are controlled programmatically
Usage in C++ Codeο
Include the Header:
#include <gui/containers/Toggle.hpp>
Declare in View: In your View class (e.g.,
MainView.hpp):Toggle toggleWidget;
Initialize in Constructor/Setup:
add(toggleWidget); toggleWidget.setPosition(x, y, width, height);
Control State:
// Set toggle to on toggleWidget.setState(true); // Check current state bool currentState = toggleWidget.getState(); // Enable gray background (disabled appearance) toggleWidget.setBackgroundGray(true);
Example Integration:
void MainView::toggleSetting() { bool newState = !toggleWidget.getState(); toggleWidget.setState(newState); // Apply setting based on newState applySetting(newState); } void MainView::setDisabledMode(bool disabled) { toggleWidget.setBackgroundGray(disabled); toggleWidget.setState(false); // Reset to off when disabled }
Files Includedο
manifest.xml: Package manifestcontent/CustomContainerExport.touchgfx: Designer export definitioncontent/CustomContainerManifest.xml: Container manifestcontent/texts.xml: Text database (empty for this widget)files/gui/include/gui/containers/Toggle.hpp: Header filefiles/gui/src/containers/Toggle.cpp: Implementation filefiles/assets/images/: 4 PNG image assets (Toggle_Def.png, Toggle_Handle.png, Toggle_Off.png, Toggle_On.png)
Dependenciesο
TouchGFX Framework (minimum version 4.26.0)
Standard TouchGFX container and image components