Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions .llm/skills/terminal-theming/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ metadata:

## Asset model

| Asset | Class | Role |
| ----------- | --------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| Theme pack | `TerminalThemePack` (`Runtime/CommandTerminal/Themes/`) | Named set of UI Toolkit style values (colors, sizes) applied to `TerminalUI` |
| Font pack | `TerminalFontPack` (`Runtime/CommandTerminal/Themes/`) | Bundle of `FontAsset`s selectable per terminal |
| Theme asset | `TerminalThemeAsset` (`Runtime/CommandTerminal/Themes/`) | Authoring-only SO whose 19 color fields auto-generate a sibling `.uss` via `TerminalThemeAssetPostProcessor` (Editor) |
| Stylesheets | `Styles/*.uss`, `Styles/*.tss` | Base USS (`BaseStyles.uss`), theme settings TSS chain |
| Persistence | `TerminalThemeConfiguration(s)`, `TerminalThemePersister` | Saved/serialized theme selections and per-theme overrides |
| Asset | Class | Role |
| ----------- | --------------------------------------------------------- | ---------------------------------------------------------------------------- |
| Theme pack | `TerminalThemePack` (`Runtime/CommandTerminal/Themes/`) | Named set of UI Toolkit style values (colors, sizes) applied to `TerminalUI` |
| Font pack | `TerminalFontPack` (`Runtime/CommandTerminal/Themes/`) | Bundle of `FontAsset`s selectable per terminal |
| Theme asset | `TerminalThemeAsset` (`Runtime/CommandTerminal/Themes/`) | Authoring asset that generates a sibling `.uss` on import |
| Stylesheets | `Styles/*.uss`, `Styles/*.tss` | Base USS (`BaseStyles.uss`), theme settings TSS chain |
| Persistence | `TerminalThemeConfiguration(s)`, `TerminalThemePersister` | Saved/serialized theme selections and per-theme overrides |

Built-in packs ship as ScriptableObject assets under `Packs/Themes/` and `Packs/Fonts/`.

## Theme assets (generated sheets)

`TerminalThemeAsset` colors -> sibling `<AssetName>.uss` written by `TerminalThemeAssetPostProcessor` on every import. Contracts enforced there:
New theme assets start with the 20 Dx Dark roles. The Inspector checks the serialized colors against the same contrast pairs as the shipped-theme gate. Generated USS includes
widget aliases for theme pack discovery. Old assets still generate their original widget colors.

`TerminalThemeAssetPostProcessor` writes `<AssetName>.uss` on import:

- Generated sheets carry the `TerminalThemeAsset.GeneratedMarkerPrefix` first-line stamp; only marker-carrying files are overwritten or deleted. A hand-written sheet sharing the
asset's name is preserved with a warning.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lines. Details live in the docs and the linked issue.

### Added

- Add semantic color roles and contrast checks to new theme assets ([#238](https://github.com/Ambiguous-Interactive/DxTerminal/issues/238)).
- Add a selection outline to Dx theme palette results and completion hints ([#238](https://github.com/Ambiguous-Interactive/DxTerminal/issues/238)).
- Add the Dx theme pack with Dx Dark, Dx Paper, and Phosphor ([#238](https://github.com/Ambiguous-Interactive/DxTerminal/issues/238)).
- Add serialized PlayerInput action names and custom-map routing through C# events ([#245](https://github.com/Ambiguous-Interactive/DxTerminal/issues/245)).
Expand Down
84 changes: 84 additions & 0 deletions Editor/CustomEditors/TerminalThemeAssetEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
namespace WallstopStudios.DxCommandTerminal.Editor.CustomEditors
{
using System;
using System.Collections.Generic;
using Themes;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(TerminalThemeAsset))]
[CanEditMultipleObjects]
internal sealed class TerminalThemeAssetEditor : UnityEditor.Editor
{
private static readonly GUIContent RolesLabel = new("Color roles");
private static readonly string[] LegacyExclusions = { TerminalThemeAsset.SemanticFlagPropertyName, TerminalThemeAsset.RolesPropertyName };

private List<string> _contrastWarnings;

[MenuItem("Assets/Create/Wallstop Studios/DxTerminal/Theme Asset", false, 1_111_124)]
private static void CreateAsset()
{
ProjectWindowUtil.CreateAsset(TerminalThemeAsset.CreateSemantic(), "NewTerminalTheme.asset");
}

public override void OnInspectorGUI()
{
TerminalThemeAsset asset = (TerminalThemeAsset)target;
bool valuesUpdated = serializedObject.UpdateIfRequiredOrScript();
SerializedProperty mode = serializedObject.FindProperty(TerminalThemeAsset.SemanticFlagPropertyName);
if (mode.hasMultipleDifferentValues)
{
EditorGUILayout.HelpBox("Select themes that use the same color fields.", MessageType.Info);
return;
}

if (!asset.UsesSemanticRoles)
{
_contrastWarnings = null;
DrawPropertiesExcluding(serializedObject, LegacyExclusions);
serializedObject.ApplyModifiedProperties();
return;
}

SerializedProperty roles = serializedObject.FindProperty(TerminalThemeAsset.RolesPropertyName);
EditorGUILayout.PropertyField(roles, RolesLabel, true);
bool valuesEdited = serializedObject.ApplyModifiedProperties();
if (serializedObject.isEditingMultipleObjects)
{
EditorGUILayout.HelpBox("Select one theme to check its contrast.", MessageType.Info);
return;
}

if (_contrastWarnings == null || valuesUpdated || valuesEdited)
{
UpdateContrastWarnings(asset.ReadRoleColors());
}

foreach (string warning in _contrastWarnings)
{
EditorGUILayout.HelpBox(warning, MessageType.Warning);
}

if (_contrastWarnings.Count == 0)
{
EditorGUILayout.HelpBox("Contrast passes on black, mid gray, and white backdrops.", MessageType.Info);
}
}

private void UpdateContrastWarnings(IReadOnlyDictionary<string, Color> colors)
{
_contrastWarnings ??= new List<string>();
_contrastWarnings.Clear();
Color surface = colors["surface"];
foreach ((string foreground, string background, double minimum) in ThemeContrast.Pairs)
{
Color? layer = string.Equals(background, "surface", StringComparison.Ordinal) ? null : colors[background];
(double ratio, string backdrop) = ThemeContrast.Measure(colors[foreground], surface, layer);
if (ratio < minimum)
{
_contrastWarnings.Add($"{foreground} on {background} over {backdrop} gives {ratio:F2}:1. Minimum is {minimum:F1}:1.");
}
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/CustomEditors/TerminalThemeAssetEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,11 @@ require the package's `BaseStyles.uss` on the panel settings; without it the bar

## Themes and Settings Assets

Create a theme without hand-writing USS: `Assets > Create > Wallstop Studios > DxTerminal > Theme Asset` makes a `TerminalThemeAsset` with color pickers for the 19 custom
properties a terminal theme sheet needs. The Editor automatically writes a sibling `.uss` named after the asset and keeps it in sync when you recolor or rename it (a rename
generates a fresh sheet file — Unity references assets by path-derived identity, so like renaming any stylesheet, packs using the old file need the new sheet re-assigned); drag
that sheet into a `TerminalThemePack`'s Themes list and it behaves exactly like the shipped sheets — `set-theme <name>` (or the palette) picks it up. Hand-written `.uss` themes
remain fully supported. Prefer asset names without "theme" in the middle (`DarkTheme`, `Neon Nights`); names like `Cool Theme 2` still work but their `set-theme` shorthand becomes
ambiguous — the full class name always resolves.
Choose `Assets > Create > Wallstop Studios > DxTerminal > Theme Asset` to start with Dx Dark colors. Edit the semantic roles in the Inspector. The contrast check reports any text
below 4.5:1 or outline below 3:1 across black, mid gray, and white game backdrops. Existing theme assets still use their widget color fields.

The Editor writes a sibling `.uss` and keeps it in sync. Add the sheet to a `TerminalThemePack` to make it available to `set-theme`. If you rename the asset, reassign the new sheet
in the pack. Hand-written `.uss` themes also work. Use the full class name if the `set-theme` shorthand is ambiguous, such as for `Cool Theme 2`.

To share one configuration across terminals, `Assets > Create > Wallstop Studios > DxTerminal > Terminal Settings` makes a `TerminalSettings` asset. Assign it to a `TerminalUI` or
`CommandPaletteUI` and its values (buffer sizes, input caret, caret blink rate, button text, hint mode, history dedup, ignored log types, disabled commands, Unity log forwarding,
Expand Down
91 changes: 80 additions & 11 deletions Runtime/CommandTerminal/Themes/TerminalThemeAsset.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
namespace WallstopStudios.DxCommandTerminal.Themes
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Helper;
using UnityEngine;

/*
Authoring-only theme asset (issue #72, option A): one asset per theme
with color fields for the 19 custom properties every terminal theme
sheet must define. An Editor postprocessor auto-writes the sibling
.uss file on every asset change; the generated sheet drops into a
TerminalThemePack like any hand-written one and is never read back by
this asset, so runtime behavior and shipped packs are unchanged.
*/
[CreateAssetMenu(menuName = "Wallstop Studios/DxTerminal/Theme Asset", fileName = "NewTerminalTheme", order = 1_111_124)]
public sealed class TerminalThemeAsset : ScriptableObject
{
/*
Expand All @@ -24,6 +16,31 @@ public sealed class TerminalThemeAsset : ScriptableObject
hand-written sheets are never clobbered.
*/
internal const string GeneratedMarkerPrefix = "/* Generated by TerminalThemeAsset";
internal const string SemanticFlagPropertyName = nameof(_useSemanticRoles);
internal const string RolesPropertyName = nameof(_roles);

private static readonly (string Widget, string Role)[] WidgetColors =
{
("terminal-bg", "surface"),
("button-bg", "surface-raised"),
("input-field-bg", "input"),
("button-selected-bg", "selection"),
("button-hover-bg", "hover"),
("scroll-bg", "surface"),
("scroll-color", "ink-muted"),
("scroll-inverse-bg", "ink-muted"),
("scroll-active-bg", "ink-body"),
("button-text", "ink-body"),
("button-selected-text", "ink"),
("button-hover-text", "ink"),
("input-text-color", "ink"),
("text-message", "ink-body"),
("text-warning", "warning"),
("text-input-echo", "accent-soft"),
("text-shell", "ink-muted"),
("text-error", "error-text"),
("caret-color", "accent"),
};

/*
The CSS class the generated sheet targets, derived from the asset
Expand All @@ -44,6 +61,18 @@ public string CssClassName
}
}

internal bool UsesSemanticRoles
{
get { return _useSemanticRoles; }
}

// Missing in legacy assets, which must keep generating their original colors.
[SerializeField]
private bool _useSemanticRoles;

[SerializeField]
private TerminalThemeRoles _roles = new();

[Header("Backgrounds")]
[SerializeField]
[Tooltip("Terminal window background")]
Expand Down Expand Up @@ -123,6 +152,13 @@ public string CssClassName
[Tooltip("Input caret color")]
private Color _caretColor = new(0.949f, 0.949f, 0.9686f, 1f);

internal static TerminalThemeAsset CreateSemantic()
{
TerminalThemeAsset asset = CreateInstance<TerminalThemeAsset>();
asset._useSemanticRoles = true;
return asset;
}

/*
Reads without an exists-check: the file can vanish between a
check and the read, so the read is the check. Expected failures
Expand Down Expand Up @@ -260,6 +296,41 @@ public string BuildUss()
*/
builder.Append(GeneratedMarkerPrefix).Append(" from '").Append(SafeDisplayName()).Append(".asset' - hand edits are overwritten on the next change. */\n");
builder.Append('.').Append(cssClass).Append(" {\n");
if (_useSemanticRoles)
{
AppendSemanticColors(builder);
}
else
{
AppendLegacyColors(builder);
}

builder.Append("}\n");
return builder.ToString();
}

internal IReadOnlyDictionary<string, Color> ReadRoleColors()
{
return _roles.ReadColors();
}

private void AppendSemanticColors(StringBuilder builder)
{
IReadOnlyDictionary<string, Color> roles = ReadRoleColors();
foreach (KeyValuePair<string, Color> role in roles)
{
AppendToken(builder, "--dxt-" + role.Key, role.Value);
}

builder.Append('\n');
foreach ((string widget, string role) in WidgetColors)
{
AppendToken(builder, "--" + widget, roles[role]);
}
}

private void AppendLegacyColors(StringBuilder builder)
{
builder.Append(" /* Backgrounds */\n");
AppendToken(builder, "--terminal-bg", _terminalBg);
AppendToken(builder, "--button-bg", _buttonBg);
Expand All @@ -284,8 +355,6 @@ public string BuildUss()
builder.Append(" /* Other UI Elements */\n");
AppendToken(builder, "--scroll-color", _scrollColor);
AppendToken(builder, "--caret-color", _caretColor);
builder.Append("}\n");
return builder.ToString();
}

private string SafeDisplayName()
Expand Down
Loading
Loading