From f8d55c7c2a3a0563f7551513bac276ffded3dbe2 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sat, 7 Nov 2026 20:16:09 -0800 Subject: [PATCH] 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 --- src/RynthCore.Engine/ImGui/BarMenuHover.cs | 17 +++++++++++++- src/RynthCore.Engine/ImGui/ImGuiBar.cs | 15 +++++++----- tools/SectionsTests/Program.cs | 27 +++++++++++++++++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/RynthCore.Engine/ImGui/BarMenuHover.cs b/src/RynthCore.Engine/ImGui/BarMenuHover.cs index d7371b4..3dc6d87 100644 --- a/src/RynthCore.Engine/ImGui/BarMenuHover.cs +++ b/src/RynthCore.Engine/ImGui/BarMenuHover.cs @@ -17,11 +17,14 @@ namespace RynthCore.Engine.ImGuiBackend; internal sealed class BarMenuHover { public const double OpenDelay = 0.15, CloseDelay = 0.30; + /// Frames further apart than this (or a clock that runs backwards) reset the state. + 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; /// The bar button id whose dropdown is open, else null. public string? OpenFor { get; private set; } @@ -32,6 +35,18 @@ internal sealed class BarMenuHover /// 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; } -} \ No newline at end of file +} diff --git a/src/RynthCore.Engine/ImGui/ImGuiBar.cs b/src/RynthCore.Engine/ImGui/ImGuiBar.cs index 1b26705..ed75aee 100644 --- a/src/RynthCore.Engine/ImGui/ImGuiBar.cs +++ b/src/RynthCore.Engine/ImGui/ImGuiBar.cs @@ -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); + } + + /// The bar's letter fallback: the first letter or digit of , upper-cased, else "?". + 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); diff --git a/tools/SectionsTests/Program.cs b/tools/SectionsTests/Program.cs index b5a6711..52568cb 100644 --- a/tools/SectionsTests/Program.cs +++ b/tools/SectionsTests/Program.cs @@ -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()