Revamp UI a whole lot

This commit is contained in:
2026-11-18 19:42:44 +01:00
parent 260abc2c98
commit fdde7f9d80
57 changed files with 2667 additions and 935 deletions
+9 -104
View File
@@ -18,11 +18,14 @@ namespace KernelPanic.UI
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 =>
@@ -35,7 +38,7 @@ namespace KernelPanic.UI
scroll.contentContainer.Contains(control))
scroll.ScrollTo(control);
});
AttachHints(root);
SuppressAutomaticTooltips(root);
}
private static void AttachPreferences(VisualElement root)
@@ -106,110 +109,12 @@ namespace KernelPanic.UI
}, 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)
// 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)
{
var hint = new ScrollView(ScrollViewMode.Vertical) { focusable = false };
hint.AddToClassList("runtime-hint");
hint.style.display = DisplayStyle.None;
hint.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
hint.verticalScrollerVisibility = ScrollerVisibility.Auto;
hint.contentContainer.focusable = false;
hint.contentViewport.focusable = false;
foreach (var control in new VisualElement[] { hint.verticalScroller, hint.verticalScroller.slider,
hint.verticalScroller.lowButton, hint.verticalScroller.highButton })
control.focusable = false;
var body = new Label { pickingMode = PickingMode.Ignore, enableRichText = false, name = "RuntimeHintText" };
body.style.whiteSpace = WhiteSpace.Normal;
body.style.flexShrink = 0;
hint.Add(body);
VisualElement owner = null;
IVisualElementScheduledItem pending = null;
IVisualElementScheduledItem pendingHide = null;
IVisualElementScheduledItem refresh = null;
bool InsideHint(VisualElement element) => element == hint || (element != null && hint.Contains(element));
void Hide()
{
pending?.Pause();
pendingHide?.Pause();
refresh?.Pause();
owner = null;
hint.style.display = DisplayStyle.None;
}
void RefreshVisible()
{
if (owner?.panel == null || owner.resolvedStyle.display == DisplayStyle.None || string.IsNullOrWhiteSpace(owner.tooltip))
{ Hide(); return; }
body.text = owner.tooltip;
float width = Mathf.Max(1, Mathf.Min(380, root.layout.width - 24));
float heightLimit = Mathf.Max(1, root.layout.height - 24);
// Reserve the vertical scrollbar in the measurement so long descriptions wrap
// inside the viewport instead of growing the popup beyond the window edge.
float measured = body.MeasureTextSize(body.text, Mathf.Max(1, width - 44),
VisualElement.MeasureMode.Exactly, 0, VisualElement.MeasureMode.Undefined).y;
float height = Mathf.Min(heightLimit, Mathf.Max(44, measured + 24));
hint.style.width = width;
hint.style.maxWidth = width;
hint.style.height = height;
hint.style.maxHeight = heightLimit;
Vector2 point = root.WorldToLocal(owner.worldBound.min);
hint.style.left = Mathf.Clamp(point.x, 12, Mathf.Max(12, root.layout.width - width - 12));
hint.style.top = Mathf.Clamp(point.y - height - 8, 12, Mathf.Max(12, root.layout.height - height - 12));
}
void Inspect(VisualElement element)
{
pendingHide?.Pause();
if (InsideHint(element)) return;
while (element != null && element != root && string.IsNullOrWhiteSpace(element.tooltip)) element = element.parent;
if (element == null || element == root)
{
// Allow the pointer to cross the small gap from the inspected item to its popup.
pendingHide = root.schedule.Execute(Hide).StartingIn(180);
return;
}
if (owner == element) return;
Hide();
owner = element;
pending = root.schedule.Execute(() =>
{
if (owner?.panel == null || owner.resolvedStyle.display == DisplayStyle.None) { Hide(); return; }
if (hint.parent == null) root.Add(hint);
hint.BringToFront();
hint.style.display = DisplayStyle.Flex;
hint.scrollOffset = Vector2.zero;
RefreshVisible();
refresh ??= root.schedule.Execute(RefreshVisible).Every(100);
refresh.Resume();
}).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>(evt => { if (!InsideHint(evt.target as VisualElement)) Hide(); }, TrickleDown.TrickleDown);
root.RegisterCallback<WheelEvent>(evt => { if (!InsideHint(evt.target as VisualElement)) Hide(); }, TrickleDown.TrickleDown);
root.RegisterCallback<KeyDownEvent>(evt =>
{
var focused = root.focusController?.focusedElement as VisualElement;
if (owner != null && hint.style.display.value != DisplayStyle.None &&
(focused == owner || (focused != null && owner.Contains(focused))) &&
(evt.keyCode == KeyCode.PageDown || evt.keyCode == KeyCode.PageUp))
{
float direction = evt.keyCode == KeyCode.PageDown ? 1 : -1;
hint.scrollOffset = new Vector2(0, Mathf.Clamp(hint.scrollOffset.y + direction * hint.contentViewport.layout.height * 0.8f,
0, hint.verticalScroller.highValue));
evt.StopImmediatePropagation();
return;
}
Hide();
}, TrickleDown.TrickleDown);
root.RegisterCallback<DetachFromPanelEvent>(_ => Hide());
root.RegisterCallback<TooltipEvent>(evt => evt.StopImmediatePropagation(), TrickleDown.TrickleDown);
}
public static void SetDistro(VisualElement root, DistroDefinition distro)