RynthChat: pinned tab bookkeeping
ChatPins: pure Toggle/Rename/Remove/Visible over the pinned-tab list (OrdinalIgnoreCase, order = button order). A pin on a hidden tab is kept but not returned by Visible. Tests in tools/ChatFilterTests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0eec23f6fd
commit
6f26d3f80b
3 changed files with 96 additions and 0 deletions
57
src/RynthCore.Engine/UI/Data/ChatPins.cs
Normal file
57
src/RynthCore.Engine/UI/Data/ChatPins.cs
Normal file
|
|
@ -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);
|
||||
|
||||
/// <summary>Pins <paramref name="tab"/> at the end, or unpins it when it is pinned. A blank name changes nothing.</summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>The pin follows a renamed tab and keeps its place. If <paramref name="to"/> is already pinned, one pin remains.</summary>
|
||||
internal static string[] Rename(string[] pins, string from, string to)
|
||||
{
|
||||
var result = new List<string>(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();
|
||||
}
|
||||
|
||||
/// <summary>Drops the pin of a deleted tab.</summary>
|
||||
internal static string[] Remove(string[] pins, string tab) => pins.Where(p => !Same(p, tab)).ToArray();
|
||||
|
||||
/// <summary>The pins that have a tab now, in pin order, spelled as the tab is.</summary>
|
||||
internal static string[] Visible(string[] pins, string[] allTabs)
|
||||
{
|
||||
var result = new List<string>(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();
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,8 @@
|
|||
<!-- 2026-11-15: server default tabs (a plugin manifest chatTabs) and Global room discovery. -->
|
||||
<Compile Include="..\..\src\RynthCore.Engine\UI\Sections\SectionManifest.cs" Link="Engine\SectionManifest.cs" />
|
||||
<Compile Include="..\..\src\RynthCore.Engine\UI\Data\ChatServerTabs.cs" Link="Engine\ChatServerTabs.cs" />
|
||||
<!-- 2026-11-15: pinned tab bookkeeping. -->
|
||||
<Compile Include="..\..\src\RynthCore.Engine\UI\Data\ChatPins.cs" Link="Engine\ChatPins.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -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<string>(), "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<string>(), "All"), new[] { "All", "Chat" }).SequenceEqual(new[] { "All" }), "All");
|
||||
Check(ChatPins.Visible(ChatPins.Toggle(Array.Empty<string>(), "Chat"), new[] { "All", "Chat" }).SequenceEqual(new[] { "Chat" }), "Chat");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue