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);