Skip to content

PixelUI.Toggle ​

Extends: PixelUI.Widget

A toggle switch widget with on/off states and customizable appearance. Features a sliding thumb animation and optional labels.

Properties ​

NameTypeDescription
valuebooleanCurrent toggle state (true = on, false = off)
labelOnstringLabel text when toggle is on
labelOffstringLabel text when toggle is off
trackColorOnPixelUI.ColorTrack color when on
trackColorOffPixelUI.ColorTrack color when off
trackColorDisabledPixelUI.ColorTrack color when disabled
thumbColorPixelUI.ColorColor of the sliding thumb
knobColorDisabledPixelUI.ColorThumb color when disabled
onLabelColorPixelUI.Color?Text color for "on" label
offLabelColorPixelUI.Color?Text color for "off" label
focusBgPixelUI.Color?Background color when focused
focusFgPixelUI.Color?Foreground color when focused
focusOutlinePixelUI.Color?Outline color when focused
showLabelbooleanWhether to show the label text
disabledbooleanWhether the toggle is disabled
knobMarginintegerHorizontal inner margin for the knob travel
knobWidthinteger?Optional fixed knob width
transitionDurationnumberSeconds for knob transition animation
transitionEasingfun(t:number):numberEasing function for knob transition
private_thumbProgress
private_animationHandle
onChangefun(self:PixelUI.Toggle,

Methods ​

new ​

lua
new()

_cancelAnimation ​

lua
_cancelAnimation()

_setThumbProgress ​

lua
_setThumbProgress()

_animateThumb ​

lua
_animateThumb()

_emitChange ​

lua
_emitChange()

setOnChange ​

lua
setOnChange()

setValue ​

lua
setValue()

isOn ​

lua
isOn()

toggle ​

lua
toggle()

setLabels ​

lua
setLabels()

setShowLabel ​

lua
setShowLabel()

setDisabled ​

lua
setDisabled()

isDisabled ​

lua
isDisabled()

setColors ​

lua
setColors()

setTransition ​

lua
setTransition()

setKnobStyle ​

lua
setKnobStyle()

draw ​

lua
draw()

handleEvent ​

lua
handleEvent()

Examples ​

Basic
lua
local pixelui = require("pixelui")
local app = pixelui.app()

-- Simple toggle switch
local toggle = app:toggle({
    x = 2, y = 2,
    width = 8, height = 1,
    value = false,
    onChange = function(self, value)
        -- value is true or false
    end
})
app.root:addChild(toggle)

app:run()
Advanced
lua
local pixelui = require("pixelui")
local app = pixelui.app()

-- Customized toggle with labels and colors
local statusLabel = app:label({
    x = 2, y = 5,
    text = "Status: OFF",
    fg = colors.white
})

local toggle = app:toggle({
    x = 2, y = 2,
    width = 10, height = 1,
    value = false,
    labelOn = "ON",
    labelOff = "OFF",
    showLabel = true,
    trackColorOn = colors.green,
    trackColorOff = colors.red,
    thumbColor = colors.white,
    transitionDuration = 0.2,
    transitionEasing = pixelui.easings.easeOutQuad,
    onChange = function(self, value)
        statusLabel:setText("Status: " .. (value and "ON" or "OFF"))
    end
})

app.root:addChild(toggle)
app.root:addChild(statusLabel)

app:run()

Released under the MIT License.