WIP: RynthChat: pin channels to the top bar #16

Draft
AwfulWaffle wants to merge 12 commits from AwfulWaffle/RynthCore:chat/pins into main
4 changed files with 79 additions and 21 deletions
Showing only changes of commit 6e271c0955 - Show all commits

RynthChat pins: review fixes

Drawer pin was unclickable: the row now allows overlap and the pin takes
the click (and the row menu/tooltip stay off the pin). Header layout is a
pure ChatPins.Layout: the overflow button is always drawn when pins do not
all fit and its width is reserved before search's 120 px. The pinned chip
uses a lighter fill on the current row. ChatPins.Add (idempotent) seeds the
server pin; ChatModel.Unpin (one way) backs the header Unpin menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Daniel Wallace 2026-11-15 08:49:20 -08:00

View file

@ -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<string>();
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;

View file

@ -502,6 +502,14 @@ internal static class ChatModel
SaveSettings();
}
/// <summary>Unpins <paramref name="tab"/> (one way: a stale menu cannot pin it again). Saved. Any thread.</summary>
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);
/// <summary>Bumped on every rule / tab / mention-setting change. The pump re-routes when it moves.</summary>

View file

@ -28,6 +28,14 @@ internal static class ChatPins
return pins.Append(tab).ToArray();
}
/// <summary>Pins <paramref name="tab"/> at the end unless it is pinned already (idempotent, unlike Toggle). A blank name changes nothing.</summary>
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();
}
/// <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)
{
@ -43,6 +51,25 @@ internal static class ChatPins
/// <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>
/// How many of the pin buttons (button widths in <paramref name="widths"/>, each followed by
/// <paramref name="gap"/>) fit in <paramref name="avail"/>, and whether an overflow button is
/// needed. When not all fit the overflow button (width <paramref name="overflowW"/> + 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.
/// </summary>
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);
}
/// <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)
{

View file

@ -1639,5 +1639,29 @@ internal static class Program
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");
}
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<string>(), " Net ").SequenceEqual(new[] { "Net" }), "trimmed");
Case("Layout_ZeroPins");
Check(ChatPins.Layout(Array.Empty<float>(), 500, 22, 2) == (0, false), "nothing to draw, no overflow button");
Check(ChatPins.Layout(Array.Empty<float>(), -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"); }
}