Files
sbox-public/game/addons/base/code/UI/Controls/SwitchControl.razor
2025-12-19 16:37:55 +00:00

69 lines
1.2 KiB
Plaintext

@using System
@namespace Sandbox.UI
@inherits BaseControl
@attribute [CustomEditor( typeof(bool) )]
<root class="switchcontrol @StateClass">
<div class="switch-frame">
<div class="switch-inner">
</div>
</div>
@if ( Label != null )
{
<div class="switch-label">
@Label
</div>
}
</root>
@code
{
public override bool SupportsMultiEdit => true;
public RenderFragment Label { get; set; }
public Action<bool> OnValueChanged { get; set; }
string StateClass => Value ? "active" : "inactive";
bool _value;
public bool Value
{
get => Property?.As.Bool ?? _value;
set
{
if ( Property is not null )
{
Property.As.Bool = value;
StateHasChanged();
return;
}
if (_value == value)
return;
_value = value;
StateHasChanged();
}
}
public SwitchControl()
{
}
protected override void OnMouseDown( MousePanelEvent e )
{
base.OnMouseDown( e );
Value = !Value;
OnValueChanged?.Invoke( Value );
e.StopPropagation();
}
}