2026-03-08 20:22:53 +08:00
|
|
|
// The CMLeonOS Project (https://github.com/Leonmmcoset/CMLeonOS)
|
|
|
|
|
// Copyright (C) 2025-present LeonOS 2 Developer Team
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2026-03-01 17:03:49 +08:00
|
|
|
using Cosmos.System;
|
|
|
|
|
using System;
|
2026-03-23 21:34:21 +08:00
|
|
|
using System.Drawing;
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
|
|
|
namespace CMLeonOS.Gui.UILib
|
|
|
|
|
{
|
|
|
|
|
internal class Switch : CheckBox
|
|
|
|
|
{
|
|
|
|
|
public Switch(Window parent, int x, int y, int width, int height) : base(parent, x, y, width, height)
|
|
|
|
|
{
|
|
|
|
|
OnDown = SwitchDown;
|
|
|
|
|
OnClick = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const int maximumToggleDrag = 4;
|
2026-03-23 21:34:21 +08:00
|
|
|
private const int switchWidth = 34;
|
|
|
|
|
private const int switchHeight = 18;
|
|
|
|
|
private const int knobSize = 14;
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
|
|
|
private int lastMouseX = 0;
|
|
|
|
|
private int totalDragged = 0;
|
|
|
|
|
private bool held = false;
|
|
|
|
|
|
|
|
|
|
private void SwitchDown(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
lastMouseX = (int)MouseManager.X;
|
|
|
|
|
totalDragged = 0;
|
|
|
|
|
held = true;
|
|
|
|
|
Render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Release()
|
|
|
|
|
{
|
|
|
|
|
held = false;
|
|
|
|
|
if (totalDragged <= maximumToggleDrag)
|
|
|
|
|
{
|
|
|
|
|
// Interpret as a toggle.
|
|
|
|
|
Checked = !Checked;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Interpret as a drag rather than a toggle,
|
|
|
|
|
// setting the Checked state based on where
|
|
|
|
|
// the switch knob is.
|
2026-03-23 21:34:21 +08:00
|
|
|
Checked = knobX >= (switchWidth - knobSize) / 2d;
|
2026-03-01 17:03:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double knobX = -1;
|
|
|
|
|
private double knobGoal = 0;
|
|
|
|
|
|
|
|
|
|
internal override void Render()
|
|
|
|
|
{
|
2026-03-23 21:34:21 +08:00
|
|
|
knobGoal = Checked ? switchWidth - knobSize : 0;
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
|
|
|
if (held && MouseManager.MouseState != MouseState.Left)
|
|
|
|
|
{
|
|
|
|
|
Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (held)
|
|
|
|
|
{
|
|
|
|
|
int diff = (int)(MouseManager.X - lastMouseX);
|
|
|
|
|
lastMouseX = (int)MouseManager.X;
|
|
|
|
|
totalDragged += Math.Abs(diff);
|
2026-03-23 21:34:21 +08:00
|
|
|
knobX = Math.Clamp(knobX + diff, 0, switchWidth - knobSize);
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
|
|
|
WM.UpdateQueue.Enqueue(this);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
double oldKnobX = knobX;
|
|
|
|
|
if (knobX == -1)
|
|
|
|
|
{
|
|
|
|
|
knobX = knobGoal;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
double diff = knobGoal - knobX;
|
|
|
|
|
double move = diff / 8d;
|
|
|
|
|
knobX += move;
|
|
|
|
|
}
|
|
|
|
|
if (Math.Abs(knobX - oldKnobX) < 0.25)
|
|
|
|
|
{
|
|
|
|
|
knobX = knobGoal;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WM.UpdateQueue.Enqueue(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Clear(Background);
|
|
|
|
|
|
|
|
|
|
int switchX = 0;
|
2026-03-23 21:34:21 +08:00
|
|
|
int switchY = (Height / 2) - (switchHeight / 2);
|
2026-03-01 17:03:49 +08:00
|
|
|
|
2026-03-23 21:34:21 +08:00
|
|
|
int textX = switchWidth + 8;
|
2026-03-01 17:03:49 +08:00
|
|
|
int textY = (Height / 2) - (16 / 2);
|
|
|
|
|
|
2026-03-23 21:34:21 +08:00
|
|
|
DrawFilledRectangle(switchX, switchY, switchWidth, switchHeight, Checked ? UITheme.Accent : UITheme.SurfaceMuted);
|
|
|
|
|
DrawRectangle(switchX, switchY, switchWidth, switchHeight, Checked ? UITheme.AccentDark : UITheme.SurfaceBorder);
|
|
|
|
|
|
|
|
|
|
int knobY = switchY + ((switchHeight - knobSize) / 2);
|
|
|
|
|
DrawFilledRectangle((int)knobX + 1, knobY, knobSize, knobSize, Color.White);
|
|
|
|
|
DrawRectangle((int)knobX + 1, knobY, knobSize, knobSize, UITheme.SurfaceBorder);
|
2026-03-01 17:03:49 +08:00
|
|
|
|
|
|
|
|
DrawString(Text, Foreground, textX, textY);
|
|
|
|
|
|
|
|
|
|
WM.Update(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|