Files
linuxdistro-roguelite/Assets/Scripts/UI/TerminalTheme.cs
T

145 lines
7.5 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);
StyleSheet readability = Resources.Load<StyleSheet>("Readability");
if (readability != null) root.styleSheets.Add(readability);
StyleSheet program = Resources.Load<StyleSheet>("TerminalProgram");
if (program != null) root.styleSheets.Add(program);
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("very-short-screen", evt.newRect.height < 520);
});
AttachPreferences(root);
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);
});
SuppressAutomaticTooltips(root);
}
private static void AttachPreferences(VisualElement root)
{
bool listening = false;
void Apply()
{
root.EnableInClassList("reduced-motion", UIPreferences.ReducedMotion);
root.EnableInClassList("text-large", UIPreferences.TextSize == UITextSize.Large);
root.EnableInClassList("density-comfortable", UIPreferences.Density == UILayoutDensity.Comfortable);
}
void Subscribe()
{
if (!listening) UIPreferences.Changed += Apply;
listening = true;
Apply();
}
root.RegisterCallback<AttachToPanelEvent>(_ => Subscribe());
root.RegisterCallback<DetachFromPanelEvent>(_ =>
{
if (listening) UIPreferences.Changed -= Apply;
listening = false;
});
Apply();
if (root.panel != null) Subscribe();
}
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;
// A focus choice made since the rebuild takes precedence over restoration.
var current = region.focusController?.focusedElement as VisualElement;
if (current != null && current != focused) return;
bool CanRestoreTo(VisualElement element) => element != null && element.focusable && IsAvailable(element);
VisualElement replacement = string.IsNullOrEmpty(name) ? null : region.Q(name);
if (!CanRestoreTo(replacement)) replacement = null;
if (replacement == null && !string.IsNullOrEmpty(text))
replacement = region.Query<Button>().Where(button => button.text == text && CanRestoreTo(button)).First();
replacement ??= region.Query<Button>().Where(button => CanRestoreTo(button)).First();
if (replacement != null) replacement.Focus();
else if (IsAvailable(region)) { region.focusable = true; region.Focus(); }
});
static bool IsAvailable(VisualElement element)
{
if (element.panel == null || !element.enabledInHierarchy || !element.visible) return false;
for (var parent = element; parent != null; parent = parent.parent)
{
// Inline changes may precede the next style/layout update.
if (parent.style.display == DisplayStyle.None || parent.resolvedStyle.display == DisplayStyle.None ||
parent.style.visibility == Visibility.Hidden) return false;
}
return true;
}
}
public static void ConstrainModalFocus(VisualElement overlay)
{
// Tab produces a NavigationMoveEvent in both editor and runtime panels.
// Handling KeyDown as well would move focus twice for a single key.
overlay.RegisterCallback<NavigationMoveEvent>(evt =>
{
if (evt.direction != NavigationMoveEvent.Direction.Next && evt.direction != NavigationMoveEvent.Direction.Previous) 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 && element is not DropdownField) 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));
overlay.panel.focusController.IgnoreEvent(evt);
evt.StopImmediatePropagation();
controls[(index + (evt.direction == NavigationMoveEvent.Direction.Previous ? -1 : 1) + controls.Count) % controls.Count].Focus();
}, TrickleDown.TrickleDown);
}
// Dense terminal screens should never grow a new row merely because focus or the
// pointer moved. Keep tooltip text on controls for deliberate inspectors and tests,
// but suppress Unity's automatic floating tooltip window as well.
private static void SuppressAutomaticTooltips(VisualElement root)
{
root.RegisterCallback<TooltipEvent>(evt => evt.StopImmediatePropagation(), TrickleDown.TrickleDown);
}
public static void SetDistro(VisualElement root, DistroDefinition distro)
{
foreach (string id in new[] { "arch", "ubuntu", "fedora", "mint", "gentoo", "nixos" })
root.EnableInClassList("distro-" + id, distro != null && distro.Id == id);
}
}
}