Overhaul UI and overall QoL

This commit is contained in:
2026-10-22 21:59:50 +02:00
parent 2ba43fb2d3
commit ac48c24a03
31 changed files with 3181 additions and 1641 deletions
+108
View File
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// <summary>A small command palette; input never reaches an OS shell.</summary>
internal sealed class TerminalCommandLine
{
internal static readonly string[] Commands =
{
"./start_run", "ls ~/collection", "curl gacha.sh | sh", "dpkg-reconfigure", "shutdown -h now"
};
private readonly List<string> _history = new();
private readonly TextField _input;
private readonly Label _output;
private readonly Action<int> _activate;
private readonly Func<bool> _available;
private int _historyIndex;
private string _draft = "";
public TerminalCommandLine(VisualElement root, Action<int> activate, Func<bool> available)
{
_input = root.Q<TextField>("CommandInput");
_output = root.Q<Label>("CommandOutput");
_activate = activate;
_available = available;
_input.RegisterCallback<KeyDownEvent>(OnKeyDown, TrickleDown.TrickleDown);
_input.RegisterValueChangedCallback(evt =>
{
if (!string.IsNullOrWhiteSpace(evt.newValue))
Show(string.Join(" ", Matches(evt.newValue)), false);
});
}
public bool Contains(IEventHandler target) => target is VisualElement element && (_input == element || _input.Contains(element));
public void Focus() => _input.Focus();
public void Echo(int index) => Show("$ " + Commands[index], false);
private static List<string> Matches(string prefix)
{
var matches = new List<string>();
foreach (string command in Commands)
if (command.StartsWith(prefix.TrimStart(), StringComparison.OrdinalIgnoreCase)) matches.Add(command);
return matches;
}
private void OnKeyDown(KeyDownEvent evt)
{
if (!_available()) return;
bool handled = true;
switch (evt.keyCode)
{
case KeyCode.Return:
case KeyCode.KeypadEnter:
Submit();
break;
case KeyCode.UpArrow:
if (_historyIndex == _history.Count) _draft = _input.value;
if (_historyIndex > 0) _input.SetValueWithoutNotify(_history[--_historyIndex]);
break;
case KeyCode.DownArrow:
if (_historyIndex < _history.Count) _historyIndex++;
_input.SetValueWithoutNotify(_historyIndex == _history.Count ? _draft : _history[_historyIndex]);
break;
case KeyCode.Tab:
var matches = Matches(_input.value);
if (matches.Count == 1) _input.value = matches[0];
else Show(string.Join(" ", matches), false);
break;
case KeyCode.Escape:
_input.value = "";
_input.Blur();
break;
default:
handled = false;
break;
}
// Text editing remains native; navigation must not also trigger a menu action.
if (handled) { _input.panel?.focusController.IgnoreEvent(evt); evt.StopImmediatePropagation(); }
}
private void Submit()
{
string command = _input.value.Trim();
if (command.Length == 0) return;
if (_history.Count == 0 || _history[_history.Count - 1] != command) _history.Add(command);
if (_history.Count > 40) _history.RemoveAt(0);
_historyIndex = _history.Count;
_draft = "";
_input.SetValueWithoutNotify("");
if (command == "clear") { Show("", false); return; }
if (command == "help") { Show("Tab complete / Up,Down history / Esc clear. Click a command or type it below.", false); return; }
int index = Array.FindIndex(Commands, item => string.Equals(item, command, StringComparison.OrdinalIgnoreCase));
if (index < 0) { Show($"command not found: {command} (help lists controls)", true); return; }
_input.Blur();
_activate(index);
}
private void Show(string text, bool error)
{
_output.text = text;
_output.EnableInClassList("command-error", error);
}
}
}