From ef61144592ba80661e6bbe54224979eb28d758e1 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sun, 15 Nov 2026 10:19:08 -0800 Subject: [PATCH] RynthChat: All is pinned by default pinnedTabs absent (never set, or a file from before pins) means All is pinned (ChatPins.FromSettings); saving always writes the array, so unpinning everything persists as [] and is not mistaken for "never set". Co-Authored-By: Claude Opus 5.5 --- src/RynthCore.Engine/UI/Data/ChatData.cs | 8 +++----- src/RynthCore.Engine/UI/Data/ChatPins.cs | 21 +++++++++++++++++++++ tools/ChatFilterTests/Program.cs | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/RynthCore.Engine/UI/Data/ChatData.cs b/src/RynthCore.Engine/UI/Data/ChatData.cs index 6899ca2..10247de 100644 --- a/src/RynthCore.Engine/UI/Data/ChatData.cs +++ b/src/RynthCore.Engine/UI/Data/ChatData.cs @@ -476,7 +476,7 @@ internal static class ChatModel private static string[] _seededTabs = Array.Empty(); // owner keys private static Dictionary _tabServers = NewMap(); // tab -> server id private static Dictionary _globalRooms = NewMap(); // server id -> room - private static string[] _pinnedTabs = Array.Empty(); // pin order + private static string[] _pinnedTabs = ChatPins.FromSettings(null); // pin order (All until a file says otherwise) // Published by ApplyServerTabs (render thread), read lock-free by the pump and the faces. private static volatile string[] _activeServers = Array.Empty(); private static volatile SectionChatTabDecl[] _tabDecls = Array.Empty(); @@ -1816,9 +1816,7 @@ internal static class ChatModel string[] seeded = (dto.SeededTabs ?? Array.Empty()) .Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()) .Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); - string[] pinned = (dto.PinnedTabs ?? Array.Empty()) - .Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()) - .Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); + string[] pinned = ChatPins.FromSettings(dto.PinnedTabs); // absent = All; [] stays empty // Under EditLock, as ApplyServerTabs (render thread) edits the same lists. lock (EditLock) { @@ -1893,7 +1891,7 @@ internal static class ChatModel SeededTabs = NullIfEmpty(Volatile.Read(ref _seededTabs)), TabServers = NullIfEmpty(Volatile.Read(ref _tabServers)), GlobalRooms = NullIfEmpty(Volatile.Read(ref _globalRooms)), - PinnedTabs = NullIfEmpty(Volatile.Read(ref _pinnedTabs)), + PinnedTabs = Volatile.Read(ref _pinnedTabs), // always written, empty included: [] = nothing pinned, absent = All Filters = Filters.Select(ToDto).ToArray(), // Only presets the user touched: new presets in later builds start off. Presets = RynthCore.Engine.UI.Panels.RynthChatPresets.All diff --git a/src/RynthCore.Engine/UI/Data/ChatPins.cs b/src/RynthCore.Engine/UI/Data/ChatPins.cs index a81b9cd..fafffd8 100644 --- a/src/RynthCore.Engine/UI/Data/ChatPins.cs +++ b/src/RynthCore.Engine/UI/Data/ChatPins.cs @@ -28,6 +28,27 @@ internal static class ChatPins return pins.Append(tab).ToArray(); } + /// The tab pinned when nothing was ever saved. + internal const string DefaultPin = "All"; + + /// + /// The pins a settings file means: absent (never set, or a file from before pins) = All + /// pinned; a saved array, empty included, is exactly what the player left. Blank names are + /// dropped and repeats (any case) kept once. + /// + internal static string[] FromSettings(string[]? saved) + { + if (saved == null) return new[] { DefaultPin }; + var result = new List(saved.Length); + foreach (string s in saved) + { + if (string.IsNullOrWhiteSpace(s)) continue; + string name = s.Trim(); + if (!result.Any(r => Same(r, name))) result.Add(name); + } + return result.ToArray(); + } + /// Pins at the end unless it is pinned already (idempotent, unlike Toggle). A blank name changes nothing. internal static string[] Add(string[] pins, string tab) { diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index a96fdf9..06a1b06 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -1647,6 +1647,23 @@ internal static class Program Check(ChatPins.Add(added, "").Length == 2 && ChatPins.Add(added, " ").Length == 2, "a blank name adds nothing"); Check(ChatPins.Add(Array.Empty(), " Net ").SequenceEqual(new[] { "Net" }), "trimmed"); + Case("PinsDefault_AbsentMeansAll"); + Check(ChatPins.FromSettings(null).SequenceEqual(new[] { "All" }), "never set = All pinned"); + RynthChatSettingsDto oldFile = JsonSerializer.Deserialize( + "{\"version\":1,\"customTabs\":[\"Mine\"]}", RynthChatJsonContext.Default.RynthChatSettingsDto)!; + Check(oldFile.PinnedTabs == null && ChatPins.FromSettings(oldFile.PinnedTabs).SequenceEqual(new[] { "All" }), + "a file from before pins = All pinned"); + + Case("PinsDefault_EmptyRoundTrips"); + var noPins = new RynthChatSettingsDto { PinnedTabs = ChatPins.Remove(ChatPins.FromSettings(null), "All") }; + string noPinsJson = JsonSerializer.Serialize(noPins, RynthChatJsonContext.Default.RynthChatSettingsDto); + Check(noPinsJson.Contains("\"pinnedTabs\":[]", StringComparison.Ordinal), "unpinning everything is written as []: " + noPinsJson); + RynthChatSettingsDto noPinsBack = JsonSerializer.Deserialize(noPinsJson, RynthChatJsonContext.Default.RynthChatSettingsDto)!; + Check(noPinsBack.PinnedTabs != null && ChatPins.FromSettings(noPinsBack.PinnedTabs).Length == 0, "and loads as nothing pinned, not All"); + string nullJson = JsonSerializer.Serialize(new RynthChatSettingsDto(), RynthChatJsonContext.Default.RynthChatSettingsDto); + Check(!nullJson.Contains("pinnedTabs", StringComparison.Ordinal), "WhenWritingNull still drops only null"); + string[] kept = ChatPins.FromSettings(new[] { "Chat", " ", "chat", "Network" }); + Check(kept.SequenceEqual(new[] { "Chat", "Network" }), "a saved list is cleaned, order kept"); Case("Layout_ZeroPins"); Check(ChatPins.Layout(Array.Empty(), 500, 22, 2) == (0, false), "nothing to draw, no overflow button"); Check(ChatPins.Layout(Array.Empty(), -50, 22, 2) == (0, false), "also in no room");