// ============================================================================ // RynthCore.Engine - ImGui/ImGuiTextFocus.cs // Which RynthCore ImGui text box holds the keyboard, per ImGui context (the // main frame and each pop-out), seen from the box itself: FaceKit.TextBox and // the script-window replay call NoteActive while their InputText is the // active item. That is the same frame the click lands, one frame ahead of // io.WantTextInput (which ImGui publishes from the previous frame's widgets). // // Used two ways by Win32Backend's WndProc: // * keyboard capture: a box active in the last frame keeps keys from AC // even before io.WantTextInput catches up (the click-then-type frame); // * the leak diagnostic: a key-down or char that is about to reach AC while // a box was active logs one "[Input] key reached AC ..." line per burst. // // Threads: AC's main thread only (ImGui frames and the game WndProc both run // there). No locks. // ============================================================================ using System; using System.Collections.Generic; namespace RynthCore.Engine.ImGuiBackend; internal static class ImGuiTextFocus { private static string? _frameOwner; // the frame being built private static string? _mainOwner; // the last main frame's private static readonly Dictionary PopOwners = new(StringComparer.OrdinalIgnoreCase); /// A text box is the active item in the frame being built. AC thread, in the draw. public static void NoteActive(string id) { if (_frameOwner != null) return; string? panel = ImGuiPanelHost.DrawingTitle; _frameOwner = string.IsNullOrEmpty(panel) ? id : panel + " " + id; if (ImGuiPopOuts.InPopOutFrame) _frameOwner += " (popped out)"; } /// A frame (main or pop-out) is about to be built. public static void BeginFrame() => _frameOwner = null; /// The main frame was built. is its io.WantTextInput. public static void EndMainFrame(bool wantTextInput) { _mainOwner = _frameOwner ?? (wantTextInput ? "a text field in the client" : null); _frameOwner = null; } /// No main frame this tick (nothing ImGui open, or the frame failed): no box. public static void ClearMain() { _mainOwner = null; _frameOwner = null; } /// A box was active in the last main frame (FaceKit / script windows only). public static bool MainBoxActive => _mainOwner != null; /// A pop-out's frame was built. Returns true when one of its boxes was active. public static bool EndPopOutFrame(string title, bool wantTextInput) { string? owner = _frameOwner ?? (wantTextInput ? title + " (popped out)" : null); _frameOwner = null; if (owner != null) PopOwners[title] = owner; else PopOwners.Remove(title); return owner != null; } public static void ForgetPopOut(string title) => PopOwners.Remove(title); /// The box holding the keyboard (main frame first), or null. public static string? ActiveOwner { get { if (_mainOwner != null) return _mainOwner; foreach (string owner in PopOwners.Values) return owner; return null; } } }