diff --git a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs index 43206c8..ca483cd 100644 --- a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs +++ b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs @@ -189,7 +189,7 @@ internal sealed class ChatFace : IImGuiPanel // The pinned tabs, then search fills the rest (at least 120 px: pins that do not fit go into the overflow button). float sx = start.X + _view.LastTabButtonWidth + 4; - sx = DrawPinnedButtons(sx, start.Y, h, buttonsX - 4 - sx - 120); + sx = DrawPinnedButtons(sx, start.Y, h, buttonsX - 4 - sx - 120 - 2); float sw = buttonsX - 4 - sx; if (sw > 40) _view.DrawSearch(new Vector2(sx, start.Y), sw); return start.Y + h + 4; @@ -205,7 +205,6 @@ internal sealed class ChatFace : IImGuiPanel private float DrawPinnedButtons(float x, float y, float h, float avail) { _pinOverflow = Array.Empty(); - if (avail <= 0) return x; string[] pins = ChatPins.Visible(ChatModel.PinnedTabs, ChatModel.AllTabs()); if (pins.Length == 0) return x; @@ -224,14 +223,9 @@ internal sealed class ChatFace : IImGuiPanel widths[i] = ImGuiNET.ImGui.CalcTextSize(pins[i]).X + 12 + (badges[i] != null ? ChatView.BadgeWidth(badges[i]!) + 3 : 0); total += widths[i] + gap; } - int fit = pins.Length; - if (total > avail) - { - float room = avail - overflowW - gap, used = 0; - fit = 0; - while (fit < pins.Length && used + widths[fit] + gap <= room) used += widths[fit++] + gap; - if (room < 0) fit = 0; - } + // Not all fit: the overflow button is always drawn, and its width comes out of the room + // before search's 120 px. Only when even that leaves search under its floor does search go. + (int fit, bool overflow) = ChatPins.Layout(widths, avail, overflowW, gap); float start = x; for (int i = 0; i < fit; i++) @@ -252,7 +246,7 @@ internal sealed class ChatFace : IImGuiPanel ImGuiNET.ImGui.PopID(); x += widths[i] + gap; } - if (fit < pins.Length) + if (overflow) { _pinOverflow = pins.Skip(fit).ToArray(); if (IconButton("##chat_pinmore", PhosphorIcons.CaretRight, new Vector2(x, y), new Vector2(overflowW, h), Text, BtnFill, font: UiFont.Ui11)) @@ -282,7 +276,7 @@ internal sealed class ChatFace : IImGuiPanel } if (ImGuiNET.ImGui.BeginPopup("##chat_pinmenu")) { - if (ImGuiNET.ImGui.Selectable("Unpin")) ChatModel.TogglePin(_pinMenuTab); + if (ImGuiNET.ImGui.Selectable("Unpin")) ChatModel.Unpin(_pinMenuTab); ImGuiNET.ImGui.EndPopup(); } } @@ -348,23 +342,28 @@ internal sealed class ChatFace : IImGuiPanel string? badge = !active && unread.Count > 0 ? (unread.Count > 99 ? "99+" : unread.Count.ToString()) : null; ImGuiNET.ImGui.PushID(tab); var p = new Vector2(start.X, y); - if (Button("##row", "", p, new Vector2(w, rowH), Text, active ? TabActive : BtnFill)) - ChatModel.SelectTab(tab); - if (ImGuiNET.ImGui.IsItemClicked(ImGuiMouseButton.Right)) _view.OpenTabMenu(tab); - if (unread.Mention && !active) ImGuiNET.ImGui.SetItemTooltip("Someone mentioned you here"); - else ImGuiNET.ImGui.SetItemTooltip(ChatModel.IsBaseTab(tab) - ? "Right-click: open in a new window" - : "Right-click to rename, delete or open this tab in a new window"); - // The pin, at the row's right edge, submitted after the row so it takes the click. + // The pin, at the row's right edge, submitted after the row so it takes the click + // (the row allows the overlap, as NavFace and MetaFace.List do). const float pinW = 18; - bool pinned = pinnedNow.Contains(tab, StringComparer.OrdinalIgnoreCase); var pinPos = new Vector2(p.X + w - pinW - 1, y + 1); var pinSize = new Vector2(pinW, rowH - 2); + bool overPin = ImGuiNET.ImGui.IsMouseHoveringRect(pinPos, pinPos + pinSize); // the row's own menu and tooltip stay off the pin + ImGuiNET.ImGui.SetNextItemAllowOverlap(); + if (Button("##row", "", p, new Vector2(w, rowH), Text, active ? TabActive : BtnFill)) + ChatModel.SelectTab(tab); + if (!overPin && ImGuiNET.ImGui.IsItemClicked(ImGuiMouseButton.Right)) _view.OpenTabMenu(tab); + if (!overPin) // the pin has its own tooltip + if (unread.Mention && !active) ImGuiNET.ImGui.SetItemTooltip("Someone mentioned you here"); + else ImGuiNET.ImGui.SetItemTooltip(ChatModel.IsBaseTab(tab) + ? "Right-click: open in a new window" + : "Right-click to rename, delete or open this tab in a new window"); + bool pinned = pinnedNow.Contains(tab, StringComparer.OrdinalIgnoreCase); ImGuiNET.ImGui.SetCursorScreenPos(pinPos); if (ImGuiNET.ImGui.InvisibleButton("##pin", pinSize)) ChatModel.TogglePin(tab); bool pinHot = ImGuiNET.ImGui.IsItemHovered(); ImGuiNET.ImGui.SetItemTooltip(pinned ? "Unpin from the top bar" : "Pin to the top bar"); - if (pinned) dl.AddRectFilled(pinPos, pinPos + pinSize, TabActive, 3); + // A lighter fill than the row's own, or the chip vanishes on the current row. + if (pinned) dl.AddRectFilled(pinPos, pinPos + pinSize, active ? Lighten(TabActive) : TabActive, 3); PhosphorIcons.DrawCentered(dl, ImGuiFonts.Get(UiFont.Ui11), PhosphorIcons.PushPin, pinPos, pinSize, pinned || pinHot ? Text : Mute); float badgeW = badge != null ? ChatView.BadgeWidth(badge) : 0; float ty = y + (rowH - ImGuiNET.ImGui.GetFontSize()) * 0.5f; diff --git a/src/RynthCore.Engine/UI/Data/ChatData.cs b/src/RynthCore.Engine/UI/Data/ChatData.cs index 5d7644c..6899ca2 100644 --- a/src/RynthCore.Engine/UI/Data/ChatData.cs +++ b/src/RynthCore.Engine/UI/Data/ChatData.cs @@ -502,6 +502,14 @@ internal static class ChatModel SaveSettings(); } + /// Unpins (one way: a stale menu cannot pin it again). Saved. Any thread. + public static void Unpin(string tab) + { + lock (EditLock) + Volatile.Write(ref _pinnedTabs, ChatPins.Remove(_pinnedTabs, tab)); + SaveSettings(); + } + public static ChatFilterRule[] Filters => Volatile.Read(ref _filters); /// Bumped on every rule / tab / mention-setting change. The pump re-routes when it moves. diff --git a/src/RynthCore.Engine/UI/Data/ChatPins.cs b/src/RynthCore.Engine/UI/Data/ChatPins.cs index 76854cc..a81b9cd 100644 --- a/src/RynthCore.Engine/UI/Data/ChatPins.cs +++ b/src/RynthCore.Engine/UI/Data/ChatPins.cs @@ -28,6 +28,14 @@ internal static class ChatPins return pins.Append(tab).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) + { + if (string.IsNullOrWhiteSpace(tab)) return pins.ToArray(); + tab = tab.Trim(); + return pins.Any(p => Same(p, tab)) ? pins.ToArray() : 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) { @@ -43,6 +51,25 @@ internal static class ChatPins /// Drops the pin of a deleted tab. internal static string[] Remove(string[] pins, string tab) => pins.Where(p => !Same(p, tab)).ToArray(); + /// + /// How many of the pin buttons (button widths in , each followed by + /// ) fit in , and whether an overflow button is + /// needed. When not all fit the overflow button (width + gap) is + /// paid for first, so the buttons that fit are a prefix of the pins. Nothing fits in a + /// negative room: Fit 0 with the overflow button still wanted. + /// + internal static (int Fit, bool Overflow) Layout(float[] widths, float avail, float overflowW, float gap) + { + if (widths.Length == 0) return (0, false); + float total = 0; + foreach (float wd in widths) total += wd + gap; + if (total <= avail) return (widths.Length, false); + float room = avail - overflowW - gap, used = 0; + int fit = 0; + while (fit < widths.Length && used + widths[fit] + gap <= room) used += widths[fit++] + gap; + return (fit, true); + } + /// The pins that have a tab now, in pin order, spelled as the tab is. internal static string[] Visible(string[] pins, string[] allTabs) { diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index 7764e41..a96fdf9 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -1639,5 +1639,29 @@ internal static class Program 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"); - } + + Case("Add_IsIdempotent"); + string[] added = ChatPins.Add(new[] { "Chat" }, "Network"); + Check(added.SequenceEqual(new[] { "Chat", "Network" }), "appended"); + Check(ChatPins.Add(added, "network").SequenceEqual(added), "a pinned name (any case) adds nothing"); + 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("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"); + + Case("Layout_AllFit"); + Check(ChatPins.Layout(new[] { 50f, 60f }, 114, 22, 2) == (2, false), "exactly full (50+2+60+2)"); + Check(ChatPins.Layout(new[] { 50f, 60f }, 300, 22, 2) == (2, false), "room to spare"); + + Case("Layout_SomeFit"); + // 50+2 fits in 114-22-2=90, then 52+60+2=114 > 90: one pin, the rest in overflow. + Check(ChatPins.Layout(new[] { 50f, 60f }, 113, 22, 2) == (1, true), "one fits, overflow button"); + Check(ChatPins.Layout(new[] { 50f, 60f, 10f }, 113, 22, 2) == (1, true), "a later small pin does not jump the queue"); + + Case("Layout_NoneFit"); + Check(ChatPins.Layout(new[] { 50f, 60f }, 60, 22, 2) == (0, true), "room for the overflow button only"); + Check(ChatPins.Layout(new[] { 50f }, 10, 22, 2) == (0, true), "less than the overflow button: still wanted"); + Check(ChatPins.Layout(new[] { 50f }, -30, 22, 2) == (0, true), "negative room: still wanted"); } }