Files
linuxdistro-roguelite/Assets/Scripts/UI/TerminalTheme.cs
T
2026-10-22 21:59:50 +02:00

136 lines
7.0 KiB
C#

using System.Collections.Generic;
using KernelPanic.Data;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// <summary>Shared presentation only: tokens, viewport classes, distro identity and motion.</summary>
internal static class TerminalTheme
{
public static void Attach(VisualElement root)
{
if (root.ClassListContains("kernel-ui")) return;
root.AddToClassList("kernel-ui");
var font = TerminalFontResolver.Resolve(null);
if (font != null) root.style.unityFontDefinition = new StyleFontDefinition(font);
StyleSheet sheet = Resources.Load<StyleSheet>("KernelTheme");
if (sheet != null) root.styleSheets.Add(sheet);
root.RegisterCallback<GeometryChangedEvent>(evt =>
{
root.EnableInClassList("compact", evt.newRect.width < 1100);
root.EnableInClassList("narrow", evt.newRect.width < 760);
root.EnableInClassList("short-screen", evt.newRect.height < 760);
});
root.EnableInClassList("reduced-motion", UIPreferences.ReducedMotion);
root.RegisterCallback<FocusInEvent>(evt =>
{
if (evt.target is not VisualElement control) return;
// Reveal keyboard focus in each enclosing scroll area, not just the innermost list.
for (var parent = control.parent; parent != null; parent = parent.parent)
// Scrollbars and the viewport belong to the scroll view, but not its scrollable content.
if (parent is ScrollView scroll && control != scroll.contentContainer &&
scroll.contentContainer.Contains(control))
scroll.ScrollTo(control);
});
AttachHints(root);
}
public static void PreserveFocus(VisualElement region)
{
var focused = region?.focusController?.focusedElement as VisualElement;
if (focused == null || !region.Contains(focused)) return;
string name = focused.name;
string text = (focused as Button)?.text;
region.schedule.Execute(() =>
{
if (focused.panel != null) return;
VisualElement replacement = string.IsNullOrEmpty(name) ? null : region.Q(name);
if (replacement == null && !string.IsNullOrEmpty(text))
replacement = region.Query<Button>().Where(button => button.text == text && button.enabledInHierarchy).First();
replacement ??= region.Query<Button>().Where(button => button.enabledInHierarchy).First();
if (replacement != null) replacement.Focus();
else { region.focusable = true; region.Focus(); }
});
}
public static void ConstrainModalFocus(VisualElement overlay)
{
overlay.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode != KeyCode.Tab) return;
var controls = new List<VisualElement>();
overlay.Query<VisualElement>().ForEach(element =>
{
if (!element.focusable || !element.enabledInHierarchy || !element.visible || element.tabIndex < 0) return;
if (element is not Button && element is not Toggle && element is not TextField) return;
for (var p = element; p != overlay; p = p.parent)
if (p == null || p.resolvedStyle.display == DisplayStyle.None) return;
controls.Add(element);
});
if (controls.Count == 0) return;
var focused = overlay.focusController.focusedElement as VisualElement;
int index = controls.FindIndex(control => control == focused || control.Contains(focused));
controls[(index + (evt.shiftKey ? -1 : 1) + controls.Count) % controls.Count].Focus();
overlay.panel.focusController.IgnoreEvent(evt);
evt.StopImmediatePropagation();
}, TrickleDown.TrickleDown);
}
// UI Toolkit's editor tooltip window is not available in standalone players.
// Use the same tooltip strings for pointer and keyboard inspection at runtime.
private static void AttachHints(VisualElement root)
{
var hint = new Label { pickingMode = PickingMode.Ignore, enableRichText = false };
hint.AddToClassList("runtime-hint");
hint.style.display = DisplayStyle.None;
VisualElement owner = null;
IVisualElementScheduledItem pending = null;
void Hide()
{
pending?.Pause();
owner = null;
hint.style.display = DisplayStyle.None;
}
void Inspect(VisualElement element)
{
while (element != null && element != root && string.IsNullOrWhiteSpace(element.tooltip)) element = element.parent;
if (element == null || element == root) { Hide(); return; }
if (owner == element) return;
Hide();
owner = element;
pending = root.schedule.Execute(() =>
{
if (owner?.panel == null || owner.resolvedStyle.display == DisplayStyle.None) { Hide(); return; }
hint.text = owner.tooltip;
if (hint.parent == null) root.Add(hint);
hint.BringToFront();
hint.style.display = DisplayStyle.Flex;
Vector2 point = root.WorldToLocal(owner.worldBound.min);
hint.style.maxWidth = Mathf.Min(380, root.layout.width - 24);
hint.style.left = Mathf.Clamp(point.x, 12, Mathf.Max(12, root.layout.width - 392));
hint.style.top = Mathf.Max(8, point.y - 8);
// Position above the inspected control once text has been measured.
hint.schedule.Execute(() => hint.style.top = Mathf.Clamp(point.y - hint.layout.height - 8, 8, Mathf.Max(8, root.layout.height - hint.layout.height - 8)));
}).StartingIn(450);
}
root.RegisterCallback<PointerOverEvent>(evt => Inspect(evt.target as VisualElement));
root.RegisterCallback<FocusInEvent>(evt => Inspect(evt.target as VisualElement));
root.RegisterCallback<PointerLeaveEvent>(_ => Hide());
root.RegisterCallback<FocusOutEvent>(_ => Hide());
root.RegisterCallback<PointerDownEvent>(_ => Hide(), TrickleDown.TrickleDown);
root.RegisterCallback<WheelEvent>(_ => Hide(), TrickleDown.TrickleDown);
root.RegisterCallback<KeyDownEvent>(_ => Hide(), TrickleDown.TrickleDown);
}
public static void SetDistro(VisualElement root, DistroDefinition distro)
{
foreach (string id in new[] { "arch", "ubuntu", "fedora", "mint" })
root.EnableInClassList("distro-" + id, distro != null && distro.Id == id);
}
}
}