Bar: hover dropdown for a plugin window's barMenu

BarMenuHover (pure timer: 0.15 s open, 0.30 s close, switch at once, no reopen after a click until the icon is left), the dropdown drawn under the bar icon, and a barMenu-only plugin reload now reaches the bar (WindowPlan.MenuSignature, refreshed in PluginWindows.Sync without re-registering the panel).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 20:09:48 -08:00
commit 60a7bddf59
7 changed files with 266 additions and 9 deletions

View file

@ -0,0 +1,64 @@
// ============================================================================
// RynthCore.Engine - ImGui/BarMenuHover.cs
// The timing of the bar icon dropdown (a plugin window's "barMenu", plugin API
// v94): pure, so tools/SectionsTests compiles it as is. ImGuiBar feeds it once
// a frame.
//
// An icon must be hovered OpenDelay before its dropdown opens (a sweep of the
// mouse across the bar opens nothing). Once open, the dropdown stays while the
// mouse is over the icon or the dropdown, and for CloseDelay after it leaves
// both (the gap between them is crossed within that). Hovering another icon
// while one is open switches at once. After a click (Close) the same icon does
// not reopen until the mouse has left it and hovered it again.
// ============================================================================
namespace RynthCore.Engine.ImGuiBackend;
internal sealed class BarMenuHover
{
public const double OpenDelay = 0.15, CloseDelay = 0.30;
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
/// <summary>The bar button id whose dropdown is open, else null.</summary>
public string? OpenFor { get; private set; }
/// <summary>
/// Once per frame. <paramref name="hoveredButton"/>: id of the menu-bearing bar button under the mouse, else null.
/// <paramref name="overMenu"/>: the mouse is over the open dropdown. <paramref name="now"/>: seconds.
/// </summary>
public void Update(string? hoveredButton, bool overMenu, double now)
{
if (_closedByClick != null && hoveredButton != _closedByClick) _closedByClick = null;
string? hovered = hoveredButton != null && hoveredButton == _closedByClick ? null : hoveredButton;
if (hovered != _hovered) { _hovered = hovered; _hoverStart = now; }
if (OpenFor != null)
{
if (hovered != null) { OpenFor = hovered; _leftAt = null; }
else if (overMenu) _leftAt = null;
else
{
_leftAt ??= now;
if (now - _leftAt.Value >= CloseDelay) { OpenFor = null; _leftAt = null; }
}
}
else if (hovered != null && now - _hoverStart >= OpenDelay)
{
OpenFor = hovered;
_leftAt = null;
}
}
/// <summary>An item was clicked, or the bar went away.</summary>
public void Close()
{
OpenFor = null;
_leftAt = null;
_closedByClick = _hovered;
}
}

View file

@ -11,6 +11,9 @@
// draws that asked for one (its manifest's "windows" with "bar", UI/Sections/
// PluginWindows.cs; an engine panel such a window has taken over loses its own
// button), reload (RL) and pop out.
// A plugin window with a "barMenu" (plugin API v94) drops a column of icon buttons below its
// bar icon after the mouse rests on it (BarMenuHover: open and close delays); a click runs that
// item's chat command. The icon's own click still toggles the window.
// A button whose window is open, docked or popped out, draws highlighted in the
// theme's accent (RynthTheme.BarButtonOpen*); the ILT and Scripts buttons while
// any window in their menu is (UI/BarOpenState.cs; one IsOpen lookup a key).
@ -71,7 +74,18 @@ internal static class ImGuiBar
// the button toggles it through ScriptWindowRegistry, like the Scripts menu does.
// OpenKey: the panel whose open state lights the button (BarOpenState.KeyFor).
private sealed record ButtonInfo(string Title, string Id, string Icon, bool Letter, string Tooltip,
string OpenKey, ScriptWindow? Window = null);
string OpenKey, ScriptWindow? Window = null, UI.Sections.SectionBarMenuItem[]? Menu = null);
// 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 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
| ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoNav;
// The open check every highlight uses (one set lookup a key), as one delegate: no allocation a frame.
private static readonly Func<string, bool> IsOpenCheck = ImGuiPanelHost.IsOpen;
@ -468,7 +482,11 @@ internal static class ImGuiBar
SafeImGui.SetItemTooltip(collapsed ? "Show the bar" : "Fold the bar to this button");
PhosphorIcons.DrawCentered(dl, iconFont, collapsed ? PhosphorIcons.CaretRight : PhosphorIcons.CaretLeft, cPos, caretSize,
caretHot ? RynthTheme.RcLabel : 0xFF9A9A9Au);
if (collapsed) return;
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; }
// Rows (header): perRow icons a row, each row from iconsX (under the first icon);
// RL and pop out go at the end of the first row (NextSlot, or after the icons when
@ -487,6 +505,7 @@ internal static class ImGuiBar
ImFontPtr letterFont = ImGuiFonts.Get(UiFont.UiBold11);
// A button whose window is open (docked or popped out) draws in the theme's accent
// (BarOpenState); a menu button (ILT, Scripts) while any window it lists is open.
string? menuHovered = null;
foreach (ButtonInfo b in _buttons!)
{
NextSlot(ref slot, perRow, iconsX, s, iconFont, popped);
@ -496,7 +515,12 @@ internal static class ImGuiBar
if (b.Window != null) ScriptWindowRegistry.PlayerSetVisible(b.Window, !open);
else PanelRouter.Toggle(b.Title);
}
SafeImGui.SetItemTooltip(b.Tooltip);
if (b.Menu != null)
{
if (ImGuiNET.ImGui.IsItemHovered()) menuHovered = b.Id;
_menuRects.Add((b, ImGuiNET.ImGui.GetItemRectMin(), ImGuiNET.ImGui.GetItemRectMax()));
}
if (_menuHover.OpenFor != b.Id) SafeImGui.SetItemTooltip(b.Tooltip); // the dropdown sits where it would show
}
// The ILT section: one button, only on Infinite Leaftide with ILT panels registered
@ -533,6 +557,68 @@ internal static class ImGuiBar
// Drawn after the bar buttons' style is popped, so the menu has the normal look.
ScriptsMenu(scripts);
IltMenu();
_menuHover.Update(menuHovered, _overMenu, menuNow);
DrawBarMenu(s, square, iconFont, letterFont);
}
/// <summary>
/// The bar icon dropdown of the button <see cref="BarMenuHover.OpenFor"/> names: a column of square icon
/// buttons flush under the icon (flush above it when that would cross the bottom of the screen), clamped
/// to the screen sideways. Its own window, not a popup, so the bar keeps its hover. A click runs the
/// item's chat command as the player's. AC thread, inside the bar window.
/// </summary>
private static void DrawBarMenu(float s, Vector2 square, ImFontPtr iconFont, ImFontPtr letterFont)
{
_overMenu = false;
string? openFor = _menuHover.OpenFor;
if (openFor == null) return;
ButtonInfo? owner = null;
Vector2 min = default, max = default;
foreach (var r in _menuRects)
if (r.Button.Id == openFor) { owner = r.Button; min = r.Min; max = r.Max; break; }
if (owner?.Menu == null || owner.Menu.Length == 0) { _menuHover.Close(); return; }
UI.Sections.SectionBarMenuItem[] items = owner.Menu;
Vector2 display = ImGuiNET.ImGui.GetIO().DisplaySize;
float width = square.X + 8f * s; // WindowPadding (4, 3) each side
float height = items.Length * square.Y + (items.Length - 1) * ButtonGap * s + 6f * s;
float x = min.X, y = max.Y;
if (display.X > 1 && display.Y > 1)
{
if (y + height > display.Y && min.Y - height >= 0f) y = min.Y - height; // flush above instead
x = Math.Max(0f, Math.Min(x, display.X - width));
}
ImGuiNET.ImGui.SetNextWindowPos(new Vector2(x, y), ImGuiCond.Always);
PushBarWindowStyle(s);
PushBarButtonStyle(s);
bool begun = ImGuiNET.ImGui.Begin("##rc_bar_menu", MenuFlags);
bool clicked = false;
try
{
if (begun)
{
_overMenu = ImGuiNET.ImGui.IsWindowHovered(ImGuiHoveredFlags.AllowWhenBlockedByActiveItem);
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;
if (IconButton(owner.Id + "_m_" + item.Id, letter ? letterFont : iconFont, glyph, square, RynthTheme.BarText))
{
AcMainThreadQueue.EnqueueChatCommand(item.Command.Trim(), ChatCommandOrigin.Player);
clicked = true;
}
SafeImGui.SetItemTooltip(item.Tooltip);
}
}
}
finally
{
ImGuiNET.ImGui.End();
PopBarButtonStyle();
PopBarWindowStyle();
}
if (clicked) { _menuHover.Close(); _overMenu = false; }
}
/// <summary>
@ -836,7 +922,7 @@ internal static class ImGuiBar
bool letter = false;
string icon = w.Icon.Length > 0 ? w.Icon : IconFor(w.Title, out letter);
list.Add(new ButtonInfo(w.Title, "##bar_pw_" + w.Plugin + "_" + w.Id, icon, letter, w.Title,
BarOpenState.KeyFor(w.Title, null)));
BarOpenState.KeyFor(w.Title, null), Menu: w.Menu.Length > 0 ? w.Menu : null));
}
// Plugin script windows that asked for a button (RynthSort, Oracle, ...): after the
// engine's own. Only while the plugin is loaded and has submitted the window.

View file

@ -104,7 +104,17 @@ internal static class PluginWindows
foreach ((string title, SectionWindowDecl w) in want)
{
string sig = WindowPlan.Signature(w);
if (Live.TryGetValue(title, out Hosted? h) && h.Signature == sig) continue;
if (Live.TryGetValue(title, out Hosted? h) && h.Signature == sig)
{
// Same panel, but the bar's dropdown may differ (a plugin reload with a new "barMenu"):
// refresh the declaration without re-registering the panel, and let the bar rebuild.
if (WindowPlan.MenuSignature(h.Decl) != WindowPlan.MenuSignature(w))
{
Live[title] = h with { Decl = w };
changed = true;
}
continue;
}
SectionWindowDecl decl = w;
Func<IImGuiPanel> face = () => new PluginWindowFace(decl.Plugin, decl.Surface, decl.Title, decl.FitHeight, decl.MinWidth, decl.DrawScale);
if (takeOvers.Contains(title))

View file

@ -67,4 +67,11 @@ internal static class WindowPlan
public static string Signature(SectionWindowDecl w) =>
string.Join("|", w.Plugin, w.Surface, w.Icon, w.Width, w.Height, w.MinWidth, w.MinHeight, w.Bar, w.EdgeToEdge, w.TakeOver,
w.Frameless, w.FitHeight);
/// <summary>
/// What the bar's dropdown shows for a window (its "barMenu"): kept apart from <see cref="Signature"/> because
/// a menu change must reach the bar without re-registering the panel (that would reset the window's state).
/// </summary>
public static string MenuSignature(SectionWindowDecl w) =>
string.Join("\n", System.Linq.Enumerable.Select(w.Menu, i => string.Join("\t", i.Id, i.Tooltip, i.Icon, i.Command)));
}