diff --git a/src/RynthCore.Engine/UI/Data/ChatPins.cs b/src/RynthCore.Engine/UI/Data/ChatPins.cs new file mode 100644 index 0000000..76854cc --- /dev/null +++ b/src/RynthCore.Engine/UI/Data/ChatPins.cs @@ -0,0 +1,57 @@ +// ============================================================================ +// RynthCore.Engine - UI/Data/ChatPins.cs +// Pinned chat tabs (2026-11-15): which tabs sit in the main chat window's top +// bar. Pure list bookkeeping (no ChatModel or ImGui) so tools/ChatFilterTests +// compiles it as is. Names compare OrdinalIgnoreCase, every function returns a +// new array, and the order of the array is the order of the buttons. +// +// A pin on a tab that is not there (a server tab while its server is not +// active) is kept but not drawn: Visible filters, nothing removes. +// ============================================================================ + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace RynthCore.Engine.UI.Data; + +internal static class ChatPins +{ + private static bool Same(string a, string b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase); + + /// Pins at the end, or unpins it when it is pinned. A blank name changes nothing. + internal static string[] Toggle(string[] pins, string tab) + { + if (string.IsNullOrWhiteSpace(tab)) return pins.ToArray(); + tab = tab.Trim(); + if (pins.Any(p => Same(p, tab))) return Remove(pins, tab); + return pins.Append(tab).ToArray(); + } + + /// The pin follows a renamed tab and keeps its place. If is already pinned, one pin remains. + internal static string[] Rename(string[] pins, string from, string to) + { + var result = new List(pins.Length); + foreach (string p in pins) + { + string name = Same(p, from) ? to : p; + if (!result.Any(r => Same(r, name))) result.Add(name); + } + return result.ToArray(); + } + + /// Drops the pin of a deleted tab. + internal static string[] Remove(string[] pins, string tab) => pins.Where(p => !Same(p, tab)).ToArray(); + + /// The pins that have a tab now, in pin order, spelled as the tab is. + internal static string[] Visible(string[] pins, string[] allTabs) + { + var result = new List(pins.Length); + foreach (string p in pins) + { + string? tab = allTabs.FirstOrDefault(t => Same(t, p)); + if (tab != null && !result.Any(r => Same(r, tab))) result.Add(tab); + } + return result.ToArray(); + } +} \ No newline at end of file diff --git a/tools/ChatFilterTests/ChatFilterTests.csproj b/tools/ChatFilterTests/ChatFilterTests.csproj index 9b050bb..353492c 100644 --- a/tools/ChatFilterTests/ChatFilterTests.csproj +++ b/tools/ChatFilterTests/ChatFilterTests.csproj @@ -39,6 +39,8 @@ + + diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index 043014f..7764e41 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -57,6 +57,7 @@ internal static class Program ServerTabSettingsRoundTrip(); ServerTabDiscovery(); ServerTabPlans(); + PinBookkeeping(); SharedFileReload(); LineColorTags(); LineColorKinds(); @@ -1603,4 +1604,40 @@ internal static class Program Check(ChatFilterPresets.Get("no.such") == null, "an unknown id is null"); Check(ChatFilterPresets.All.Length >= 10, "a useful set"); } + + // 2026-11-15: pinned tab bookkeeping (ChatPins), pure. + private static void PinBookkeeping() + { + Case("Toggle_AddsAtEndAndRemoves"); + string[] p = ChatPins.Toggle(Array.Empty(), "Chat"); + p = ChatPins.Toggle(p, "Network"); + Check(p.SequenceEqual(new[] { "Chat", "Network" }), "added in pin order"); + string[] q = ChatPins.Toggle(p, "chat"); + Check(q.SequenceEqual(new[] { "Network" }), "a differently cased name unpins"); + Check(p.SequenceEqual(new[] { "Chat", "Network" }), "the input array is not changed"); + Check(ChatPins.Toggle(q, " ").Length == 1, "a blank name pins nothing"); + + Case("Rename_KeepsPosition"); + string[] r = ChatPins.Rename(new[] { "Chat", "Network", "Trade" }, "network", "DN"); + Check(r.SequenceEqual(new[] { "Chat", "DN", "Trade" }), "renamed in place"); + Check(ChatPins.Rename(r, "Nope", "X").SequenceEqual(r), "an unpinned tab changes nothing"); + string[] merged = ChatPins.Rename(new[] { "A", "B" }, "A", "b"); + Check(merged.Length == 1 && merged[0] == "b", "a rename onto a pinned name leaves one pin"); + + Case("Remove_DropsOnlyThatPin"); + Check(ChatPins.Remove(new[] { "Chat", "Network" }, "NETWORK").SequenceEqual(new[] { "Chat" }), "removed"); + Check(ChatPins.Remove(new[] { "Chat" }, "Nope").SequenceEqual(new[] { "Chat" }), "absent = unchanged"); + + Case("Visible_SkipsHiddenTabsKeepsPin"); + string[] pins = { "Network", "Chat" }; + string[] vis = ChatPins.Visible(pins, new[] { "All", "Chat" }); + Check(vis.SequenceEqual(new[] { "Chat" }), "the hidden tab is not drawn"); + Check(pins.SequenceEqual(new[] { "Network", "Chat" }), "the pin is still held"); + Check(ChatPins.Visible(pins, new[] { "All", "Chat", "network" }).SequenceEqual(new[] { "network", "Chat" }), + "back when the server is back, in pin order, as the tab is spelled now"); + + Case("Visible_BaseTabsPinnable"); + Check(ChatPins.Visible(ChatPins.Toggle(Array.Empty(), "All"), new[] { "All", "Chat" }).SequenceEqual(new[] { "All" }), "All"); + Check(ChatPins.Visible(ChatPins.Toggle(Array.Empty(), "Chat"), new[] { "All", "Chat" }).SequenceEqual(new[] { "Chat" }), "Chat"); + } }