Bar dropdown: clock guard in BarMenuHover, shared letter fallback

The hover timer resets on a backwards clock (pop-out's own ImGui context) or a frame gap over 0.5 s; the guard is pure and tested. The dropdown's letter fallback uses the same helper as IconFor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 20:16:09 -08:00
commit f8d55c7c2a
3 changed files with 51 additions and 8 deletions

View file

@ -17,11 +17,14 @@ namespace RynthCore.Engine.ImGuiBackend;
internal sealed class BarMenuHover
{
public const double OpenDelay = 0.15, CloseDelay = 0.30;
/// <summary>Frames further apart than this (or a clock that runs backwards) reset the state.</summary>
public const double ResetGap = 0.5;
private string? _hovered; // the icon being hovered, and since when (_hoverStart)
private double _hoverStart;
private double? _leftAt; // when the mouse left both the icon and the dropdown (open only)
private string? _closedByClick; // the icon hovered when Close() ran: ignored until it is left
private double _lastNow = double.NegativeInfinity;
/// <summary>The bar button id whose dropdown is open, else null.</summary>
public string? OpenFor { get; private set; }
@ -32,6 +35,18 @@ internal sealed class BarMenuHover
/// </summary>
public void Update(string? hoveredButton, bool overMenu, double now)
{
// The clock is one ImGui context's (the popped-out bar has its own, starting near 0), and a bar that
// wasn't drawn for a while sends no frames: either way, start from a closed dropdown.
if (now < _lastNow || now - _lastNow > ResetGap)
{
OpenFor = null;
_hovered = null;
_hoverStart = now;
_leftAt = null;
_closedByClick = null;
}
_lastNow = now;
if (_closedByClick != null && hoveredButton != _closedByClick) _closedByClick = null;
string? hovered = hoveredButton != null && hoveredButton == _closedByClick ? null : hoveredButton;
@ -61,4 +76,4 @@ internal sealed class BarMenuHover
_leftAt = null;
_closedByClick = _hovered;
}
}
}

View file

@ -79,9 +79,8 @@ internal static class ImGuiBar
// The bar icon dropdown (a plugin window's "barMenu"): the timer, last frame's "mouse is over the
// dropdown", the menu-bearing buttons' rects this frame, and when the timer was last fed (a gap
// means the bar wasn't drawn: start from a closed dropdown).
private static BarMenuHover _menuHover = new();
private static readonly BarMenuHover _menuHover = new();
private static bool _overMenu;
private static double _menuFedAt = double.NegativeInfinity;
private static readonly List<(ButtonInfo Button, Vector2 Min, Vector2 Max)> _menuRects = new();
private const ImGuiWindowFlags MenuFlags = ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.AlwaysAutoResize
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoDocking
@ -147,7 +146,13 @@ internal static class ImGuiBar
{
letter = !PanelIcons.TryGetValue(title, out string? icon);
if (!letter) return icon!;
foreach (char c in title)
return FirstLetter(title);
}
/// <summary>The bar's letter fallback: the first letter or digit of <paramref name="text"/>, upper-cased, else "?".</summary>
private static string FirstLetter(string text)
{
foreach (char c in text)
if (char.IsLetterOrDigit(c)) return char.ToUpperInvariant(c).ToString();
return "?";
}
@ -483,8 +488,6 @@ internal static class ImGuiBar
PhosphorIcons.DrawCentered(dl, iconFont, collapsed ? PhosphorIcons.CaretRight : PhosphorIcons.CaretLeft, cPos, caretSize,
caretHot ? RynthTheme.RcLabel : 0xFF9A9A9Au);
double menuNow = ImGuiNET.ImGui.GetTime();
if (menuNow - _menuFedAt > 0.5) _menuHover = new BarMenuHover(); // the bar wasn't drawn for a while
_menuFedAt = menuNow;
_menuRects.Clear();
if (collapsed) { _menuHover.Update(null, false, menuNow); _overMenu = false; return; }
@ -602,7 +605,7 @@ internal static class ImGuiBar
foreach (UI.Sections.SectionBarMenuItem item in items)
{
bool letter = item.Icon.Length == 0;
string glyph = letter ? (item.Tooltip.Length > 0 ? item.Tooltip.Substring(0, 1) : "?") : item.Icon;
string glyph = letter ? FirstLetter(item.Tooltip) : item.Icon;
if (IconButton(owner.Id + "_m_" + item.Id, letter ? letterFont : iconFont, glyph, square, RynthTheme.BarText))
{
AcMainThreadQueue.EnqueueChatCommand(item.Command.Trim(), ChatCommandOrigin.Player);

View file

@ -673,6 +673,7 @@ internal static class Program
h.Update("a", false, 0.0);
h.Update("a", false, 0.20);
Check(h.OpenFor == "a", "(setup) open on a");
Feed(h, "a", 0.30, 0.90);
h.Update(null, false, 1.0);
h.Update(null, false, 1.10);
h.Update(null, true, 1.20);
@ -682,6 +683,7 @@ internal static class Program
h = new BarMenuHover();
h.Update("a", false, 0.0);
h.Update("a", false, 0.20);
Feed(h, "a", 0.30, 0.90);
h.Update(null, false, 1.0);
h.Update(null, false, 1.29);
Check(h.OpenFor == "a", "gone for 0.29 s: still open");
@ -691,6 +693,7 @@ internal static class Program
h = new BarMenuHover();
h.Update("a", false, 0.0);
h.Update("a", false, 0.20);
Feed(h, "a", 0.30, 1.90);
h.Update("b", false, 2.0);
Check(h.OpenFor == "b", "an open dropdown switches to another icon at once");
@ -699,15 +702,37 @@ internal static class Program
h.Update("a", false, 0.20);
h.Close();
Check(h.OpenFor == null, "Close() closes");
h.Update("a", false, 0.30);
Feed(h, "a", 0.30, 4.90);
h.Update("a", false, 5.0);
Check(h.OpenFor == null, "still hovering the same icon: not reopened");
h.Update(null, false, 5.10);
Feed(h, null, 5.20, 5.90);
h.Update("a", false, 6.0);
Check(h.OpenFor == null, "hovered again but not for OpenDelay yet");
h.Update("a", false, 6.16);
Check(h.OpenFor == "a", "left and hovered again for OpenDelay: reopens");
Check(BarMenuHover.OpenDelay == 0.15 && BarMenuHover.CloseDelay == 0.30, "delays are 0.15 s and 0.30 s");
// The popped-out bar draws in another ImGui context whose clock starts near 0.
h = new BarMenuHover();
h.Update("a", false, 0.0);
h.Update("a", false, 0.20);
h.Update(null, false, 1000.0);
h.Update(null, false, 0.05);
Check(h.OpenFor == null, "the clock jumping back (a pop-out's own context): reset, closed");
// A bar that wasn't drawn for a while sends no frames.
h = new BarMenuHover();
h.Update("a", false, 0.0);
h.Update("a", false, 0.20);
h.Update("a", false, 0.20 + BarMenuHover.ResetGap + 0.01);
Check(h.OpenFor == null, "a forward gap of more than 0.5 s: reset, closed");
}
// Frames from..to every 0.1 s (a real run draws every frame; the timer resets on a gap over 0.5 s).
private static void Feed(BarMenuHover h, string? hovered, double from, double to)
{
for (double t = from; t <= to + 1e-9; t += 0.1) h.Update(hovered, false, t);
}
private static void BarMenuReload()