From ff7c9119d86ee987886b4418d01ae4bd18207944 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sun, 15 Nov 2026 08:44:58 -0800 Subject: [PATCH] RynthChat: pin channels to the top bar A push-pin button on each drawer row pins the tab to the main window's header: one button per pin after the channel button (unread badge, right-click to unpin), the ones that do not fit in an overflow button, search keeps 120 px. ChatModel.TogglePin saves; rename, delete and the server-tab seed now go through ChatPins. Extra windows and "Tabs across the top" draw no pins. Co-Authored-By: Claude Opus 5.5 --- src/RynthCore.Engine/ImGui/Panels/ChatFace.cs | 120 +++++++++++++++++- src/RynthCore.Engine/UI/Data/ChatData.cs | 10 +- .../UI/Data/ChatServerTabs.cs | 6 +- 3 files changed, 127 insertions(+), 9 deletions(-) diff --git a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs index f0529b5..43206c8 100644 --- a/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs +++ b/src/RynthCore.Engine/ImGui/Panels/ChatFace.cs @@ -29,6 +29,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Numerics; using ImGuiNET; using RynthCore.Engine.Compatibility; @@ -73,6 +74,11 @@ internal sealed class ChatFace : IImGuiPanel private float _inputScroll; private bool _openSettings; + // Pinned tabs in the header (2026-11-15): the ones that did not fit (the overflow button's list), + // and the pin whose right-click menu is open. + private string[] _pinOverflow = Array.Empty(); + private bool _openPinMore, _openPinMenu; + private string _pinMenuTab = ""; /// A line colour changed and isn't saved yet (saved once the picker is let go). private bool _coloursDirty; /// The Chat history slider's value while it's held (0: not held). @@ -181,12 +187,105 @@ internal sealed class ChatFace : IImGuiPanel } if (ImGuiNET.ImGui.IsItemClicked(ImGuiMouseButton.Right)) _view.OpenTabMenu(ChatModel.CurrentTab()); - // Search fills the rest. - float sx = start.X + _view.LastTabButtonWidth + 4, sw = buttonsX - 4 - sx; + // 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); + float sw = buttonsX - 4 - sx; if (sw > 40) _view.DrawSearch(new Vector2(sx, start.Y), sw); return start.Y + h + 4; } + /// + /// The pinned tabs as buttons after the channel button (drawer mode, main window only): the + /// tab's name, unread badge, highlighted when current; click selects, right-click offers + /// Unpin. Those that do not fit in go into one overflow button. + /// A pin on a hidden tab (its server is not active) is not drawn and not lost. + /// Returns the x after them (+4), or when nothing was drawn. + /// + 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; + + const float gap = 2, overflowW = 22; + var dl = ImGuiNET.ImGui.GetWindowDrawList(); + string current = ChatModel.CurrentTab(); + var unreadAll = _view.Unread; + var badges = new string?[pins.Length]; + var widths = new float[pins.Length]; + float total = 0; + for (int i = 0; i < pins.Length; i++) + { + unreadAll.TryGetValue(pins[i], out var unread); + bool active = string.Equals(pins[i], current, StringComparison.OrdinalIgnoreCase); + badges[i] = !active && unread.Count > 0 ? (unread.Count > 99 ? "99+" : unread.Count.ToString()) : null; + 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; + } + + float start = x; + for (int i = 0; i < fit; i++) + { + string tab = pins[i]; + bool active = string.Equals(tab, current, StringComparison.OrdinalIgnoreCase); + unreadAll.TryGetValue(tab, out var unread); + ImGuiNET.ImGui.PushID("pin_" + tab); + if (Button("##pin", "", new Vector2(x, y), new Vector2(widths[i], h), Text, active ? TabActive : BtnFill)) + ChatModel.SelectTab(tab); + if (ImGuiNET.ImGui.IsItemClicked(ImGuiMouseButton.Right)) { _pinMenuTab = tab; _openPinMenu = true; } + if (unread.Mention && !active) ImGuiNET.ImGui.SetItemTooltip("Someone mentioned you here"); + else ImGuiNET.ImGui.SetItemTooltip("Right-click to unpin"); + float textW = ImGuiNET.ImGui.CalcTextSize(tab).X; + dl.AddText(new Vector2(x + 6, y + (h - ImGuiNET.ImGui.GetFontSize()) * 0.5f), Text, tab); + if (badges[i] != null) + ChatView.DrawBadge(dl, new Vector2(x + 6 + textW + 3, y + 4), ChatView.BadgeWidth(badges[i]!), h - 8, badges[i]!, unread.Mention); + ImGuiNET.ImGui.PopID(); + x += widths[i] + gap; + } + if (fit < pins.Length) + { + _pinOverflow = pins.Skip(fit).ToArray(); + if (IconButton("##chat_pinmore", PhosphorIcons.CaretRight, new Vector2(x, y), new Vector2(overflowW, h), Text, BtnFill, font: UiFont.Ui11)) + _openPinMore = true; + ImGuiNET.ImGui.SetItemTooltip("More pinned tabs"); + x += overflowW + gap; + } + return x > start ? x + 2 : x; // the 4 px after the channel button, 2 here and 2 left from the last gap + } + + /// The overflow list and the Unpin menu of the pinned buttons (drawn in DrawPopups). + private void DrawPinPopups() + { + if (_openPinMore) { _openPinMore = false; ImGuiNET.ImGui.OpenPopup("##chat_pinmore"); } + if (_openPinMenu) { _openPinMenu = false; ImGuiNET.ImGui.OpenPopup("##chat_pinmenu"); } + if (ImGuiNET.ImGui.BeginPopup("##chat_pinmore")) + { + string current = ChatModel.CurrentTab(); + foreach (string tab in _pinOverflow) + { + _view.Unread.TryGetValue(tab, out var unread); + bool active = string.Equals(tab, current, StringComparison.OrdinalIgnoreCase); + string label = !active && unread.Count > 0 ? $"{tab} ({unread.Count})" : tab; + if (ImGuiNET.ImGui.Selectable(label + "##pinsel_" + tab, active)) ChatModel.SelectTab(tab); + } + ImGuiNET.ImGui.EndPopup(); + } + if (ImGuiNET.ImGui.BeginPopup("##chat_pinmenu")) + { + if (ImGuiNET.ImGui.Selectable("Unpin")) ChatModel.TogglePin(_pinMenuTab); + ImGuiNET.ImGui.EndPopup(); + } + } /// /// "Tabs across the top" (ChatModel.TabsAcross): every tab in a row that wraps left of /// the buttons, as before the drawer; right-click a tab to rename, delete or open it in a @@ -240,6 +339,7 @@ internal sealed class ChatFace : IImGuiPanel var dl = ImGuiNET.ImGui.GetWindowDrawList(); string current = ChatModel.CurrentTab(); var unreadAll = _view.Unread; + string[] pinnedNow = ChatModel.PinnedTabs; float y = start.Y; foreach (string tab in ChatModel.AllTabs()) { @@ -255,12 +355,23 @@ internal sealed class ChatFace : IImGuiPanel 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. + 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); + 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); + 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; - dl.PushClipRect(p, p + new Vector2(w - (badge != null ? badgeW + 8 : 0), rowH), true); + dl.PushClipRect(p, p + new Vector2(w - pinW - 2 - (badge != null ? badgeW + 4 : 0), rowH), true); dl.AddText(new Vector2(p.X + 6, ty), active ? Teal : Text, tab); dl.PopClipRect(); - if (badge != null) ChatView.DrawBadge(dl, new Vector2(p.X + w - badgeW - 4, y + 3), badgeW, rowH - 6, badge, unread.Mention); + if (badge != null) ChatView.DrawBadge(dl, new Vector2(p.X + w - pinW - badgeW - 4, y + 3), badgeW, rowH - 6, badge, unread.Mention); ImGuiNET.ImGui.PopID(); y += rowH + 2; } @@ -362,6 +473,7 @@ internal sealed class ChatFace : IImGuiPanel ChatView.PushPopupStyle(); DrawSettingsPopup(); _view.DrawPopups(); + DrawPinPopups(); ChatView.PopPopupStyle(); } diff --git a/src/RynthCore.Engine/UI/Data/ChatData.cs b/src/RynthCore.Engine/UI/Data/ChatData.cs index e773554..5d7644c 100644 --- a/src/RynthCore.Engine/UI/Data/ChatData.cs +++ b/src/RynthCore.Engine/UI/Data/ChatData.cs @@ -494,6 +494,14 @@ internal static class ChatModel /// The tabs pinned to the main window's top bar, in pin order. Any thread. public static string[] PinnedTabs => Volatile.Read(ref _pinnedTabs); + /// Pins to the top bar, or unpins it. Saved with the settings. Any thread. + public static void TogglePin(string tab) + { + lock (EditLock) + Volatile.Write(ref _pinnedTabs, ChatPins.Toggle(_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. @@ -925,7 +933,7 @@ internal static class ChatModel foreach (RynthChatFilterDto want in e.Inserts) list.Insert(Math.Min(at++, list.Count), FromDto(want)); if (e.CreateTab != null && !IsBaseTab(e.CreateTab)) Volatile.Write(ref _customTabs, _customTabs.Append(e.CreateTab).ToArray()); - if (e.Pin != null) Volatile.Write(ref _pinnedTabs, _pinnedTabs.Append(e.Pin).ToArray()); + if (e.Pin != null) Volatile.Write(ref _pinnedTabs, ChatPins.Add(_pinnedTabs, e.Pin)); if (e.Seed != null) Volatile.Write(ref _seededTabs, _seededTabs.Append(e.Seed).ToArray()); if (e.TabServer != null && !IsBaseTab(e.TabServer)) { diff --git a/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs index 817a2a1..cb32417 100644 --- a/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs +++ b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs @@ -325,9 +325,7 @@ internal static class ChatServerTabs { Dictionary map = NewMap(tabServers); if (map.Remove(from, out string? server)) map[to] = server; - string[] pinned = pins.Select(t => string.Equals(t, from, StringComparison.OrdinalIgnoreCase) ? to : t) - .Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); - return (map, pinned); + return (map, ChatPins.Rename(pins.ToArray(), from, to)); } /// A tab deleted: its server entry and its pin go (the seed mark stays, elsewhere). @@ -336,7 +334,7 @@ internal static class ChatServerTabs { Dictionary map = NewMap(tabServers); map.Remove(tab); - return (map, pins.Where(t => !string.Equals(t, tab, StringComparison.OrdinalIgnoreCase)).ToArray()); + return (map, ChatPins.Remove(pins.ToArray(), tab)); } // -- Export / Import -------------------------------------------------------------