Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Tooltips: one coordinator per shared label decides which HoverTip shows it (Fixes #2583) (#2585)

Eyal Golanb95dd4b05852
diff --git a/changelog.d/2583-shared-tooltip.md b/changelog.d/2583-shared-tooltip.md
new file mode 100644
index 000000000..5480aea3c
--- /dev/null
+++ b/changelog.d/2583-shared-tooltip.md
@@ -0,0 +1,3 @@
+### Fixed
+
+- Icon-button tooltips now return to a still-focused button after the pointer leaves another HUD button group.
diff --git a/unity/Assets/Scripts/UI/HoverTip.cs b/unity/Assets/Scripts/UI/HoverTip.cs
index 1cac51098..c10df9eca 100644
--- a/unity/Assets/Scripts/UI/HoverTip.cs
+++ b/unity/Assets/Scripts/UI/HoverTip.cs
@@ -21,39 +21,47 @@ namespace Statecraft.Game.UI
public enum Side { Left, Right }
readonly VisualElement layer; readonly Label tip; readonly Func<Side> side;
+ readonly Coordinator coordinator;
readonly List<VisualElement> targets = new List<VisualElement>();
readonly EventCallback<PointerEnterEvent> onEnter; readonly EventCallback<PointerLeaveEvent> onLeave;
readonly EventCallback<FocusInEvent> onFocus; readonly EventCallback<FocusOutEvent> onBlur;
readonly EventCallback<ClickEvent> onClick; readonly EventCallback<NavigationSubmitEvent> onSubmit;
+ readonly EventCallback<DetachFromPanelEvent> onDetach;
VisualElement? hovered, focused, pressed, shownFor;
+ long hoveredAt, focusedAt;
+ bool disposed;
/// <param name="root">The UI root holding the <c>hud-tip</c> label.</param>
/// <param name="side">Where the tip goes, asked each time it shows (a mirrored layout may move the buttons).</param>
public HoverTip(VisualElement root, IEnumerable<VisualElement> buttons, Func<Side> side)
{
tip = root.Q<Label>(TipName); layer = tip.parent; this.side = side;
- onEnter = e => { hovered = (VisualElement)e.currentTarget; pressed = null; Refresh(); };
- onLeave = e => { if (hovered == e.currentTarget) hovered = null; Refresh(); };
- onFocus = e => { focused = (VisualElement)e.currentTarget; if (pressed == focused && hovered != focused) pressed = null; Refresh(); };
- onBlur = e => { if (focused == e.currentTarget) focused = null; Refresh(); };
- onClick = e => { pressed = (VisualElement)e.currentTarget; Refresh(); };
- onSubmit = e => { pressed = (VisualElement)e.currentTarget; Refresh(); };
+ // The coordinator lives on the label (userData), so it goes with the UI that holds it, even if an instance is never
+ // disposed (a test that drops its presenter): no static state outlives a panel (#2583).
+ if (!(tip.userData is Coordinator shared)) tip.userData = shared = new Coordinator(tip);
+ coordinator = shared;
+ onEnter = e => { hovered = (VisualElement)e.currentTarget; hoveredAt = coordinator.Next(); pressed = null; coordinator.Refresh(); };
+ onLeave = e => { if (hovered == e.currentTarget) { hovered = null; hoveredAt = 0; } coordinator.Refresh(); };
+ onFocus = e => { focused = (VisualElement)e.currentTarget; focusedAt = coordinator.Next(); if (pressed == focused && hovered != focused) pressed = null; coordinator.Refresh(); };
+ onBlur = e => { if (focused == e.currentTarget) { focused = null; focusedAt = 0; } coordinator.Refresh(); };
+ onClick = e => { pressed = (VisualElement)e.currentTarget; coordinator.Refresh(); };
+ onSubmit = e => { pressed = (VisualElement)e.currentTarget; coordinator.Refresh(); };
+ // A target taken out of the panel while hovered or focused can no longer hold the tip.
+ onDetach = e => { var x = (VisualElement)e.currentTarget; if (hovered == x) { hovered = null; hoveredAt = 0; } if (focused == x) { focused = null; focusedAt = 0; } if (pressed == x) pressed = null; coordinator.Refresh(); };
foreach (var b in buttons)
{
targets.Add(b);
b.RegisterCallback(onEnter); b.RegisterCallback(onLeave); b.RegisterCallback(onFocus); b.RegisterCallback(onBlur);
- b.RegisterCallback(onClick); b.RegisterCallback(onSubmit);
+ b.RegisterCallback(onClick); b.RegisterCallback(onSubmit); b.RegisterCallback(onDetach);
}
+ coordinator.Add(this);
}
/// <summary>The button the tip shows for now, or null.</summary>
public VisualElement? ShownFor => shownFor;
- void Refresh()
+ void Show(VisualElement target)
{
- var target = hovered ?? focused;
- if (target != null && target == pressed) target = null;
- if (target == null || string.IsNullOrEmpty(target.tooltip) || !(target.worldBound.width > 0)) { Hide(); return; }
var r = layer.WorldToLocal(target.worldBound);
tip.text = target.tooltip;
if (side() == Side.Left) { tip.style.left = StyleKeyword.Auto; tip.style.right = layer.layout.width - r.xMin + 8; }
@@ -63,23 +71,61 @@ namespace Statecraft.Game.UI
shownFor = target;
}
- void Hide()
+ sealed class Coordinator
{
- if (shownFor == null) return;
- tip.style.display = DisplayStyle.None;
- shownFor = null;
+ readonly Label tip;
+ readonly List<HoverTip> members = new List<HoverTip>();
+ long sequence;
+
+ public Coordinator(Label tip) => this.tip = tip;
+ public long Next() => ++sequence;
+ public void Add(HoverTip member) => members.Add(member);
+
+ public void Refresh()
+ {
+ HoverTip? owner = null;
+ long latest = 0;
+ foreach (var member in members)
+ {
+ member.shownFor = null;
+ if (member.hovered != null && member.hoveredAt > latest) { owner = member; latest = member.hoveredAt; }
+ }
+ if (owner == null)
+ {
+ foreach (var member in members)
+ if (member.focused != null && member.focusedAt > latest) { owner = member; latest = member.focusedAt; }
+ }
+ var target = owner?.hovered ?? owner?.focused;
+ if (owner == null || target == null || target == owner.pressed || string.IsNullOrEmpty(target.tooltip) || !(target.worldBound.width > 0))
+ {
+ tip.style.display = DisplayStyle.None;
+ return;
+ }
+ owner.Show(target);
+ }
+
+ public void Remove(HoverTip member)
+ {
+ members.Remove(member);
+ member.shownFor = null;
+ Refresh();
+ if (members.Count == 0 && tip.userData == (object)this) tip.userData = null;
+ }
}
public void Dispose()
{
+ if (disposed) return;
+ disposed = true;
foreach (var b in targets)
{
b.UnregisterCallback(onEnter); b.UnregisterCallback(onLeave); b.UnregisterCallback(onFocus); b.UnregisterCallback(onBlur);
- b.UnregisterCallback(onClick); b.UnregisterCallback(onSubmit);
+ b.UnregisterCallback(onClick); b.UnregisterCallback(onSubmit); b.UnregisterCallback(onDetach);
}
hovered = focused = pressed = null;
- Hide();
+ hoveredAt = focusedAt = 0;
targets.Clear();
+ coordinator.Remove(this);
}
}
}
diff --git a/unity/Assets/Tests/EditMode/MinimapReliefTests.cs b/unity/Assets/Tests/EditMode/MinimapReliefTests.cs
index cf6da7e1a..e9ef9c5ba 100644
--- a/unity/Assets/Tests/EditMode/MinimapReliefTests.cs
+++ b/unity/Assets/Tests/EditMode/MinimapReliefTests.cs
@@ -38,7 +38,8 @@ public class MinimapReliefTests
/// <summary>The real Game.uxml on the real PanelSettings at w x h, out of its loading state, in English or Hebrew, with the
/// top bar, the info panel on Lydda's hex (its longest region line), the border layer's caption and the minimap.</summary>
- static IEnumerator Rendered(int w, int h, bool hebrew, System.Action<VisualElement, MinimapPresenter> check, System.Action<VisualElement>? act = null)
+ static IEnumerator Rendered(int w, int h, bool hebrew, System.Action<VisualElement, MinimapPresenter> check,
+ System.Action<VisualElement>? act = null, bool checkDisposesMini = false, bool wireProductionTipGroups = false)
{
var text = hebrew ? He : En;
var rt = new RenderTexture(w, h, 0);
@@ -55,6 +56,15 @@ public class MinimapReliefTests
if (hebrew) root.Q("root").AddToClassList(UiLanguage.HebrewClass);
var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), text);
var hud = new HudPresenter(root, bridge, text);
+ FeedbackPresenter? feedback = null;
+ SharePresenter? share = null;
+ CommunityIconsPresenter? community = null;
+ if (wireProductionTipGroups)
+ {
+ feedback = new FeedbackPresenter(root, text, "v-test");
+ share = new SharePresenter(root, bridge, text, done => done(new byte[] { 1 }));
+ community = new CommunityIconsPresenter(root, text, feedback, share);
+ }
var info = new InfoPanelPresenter(root, bridge, text, Names(), regions: Regions());
var mini = new MinimapPresenter(root, Hexes, text);
var layer = root.Q<Label>("map-layer");
@@ -71,7 +81,8 @@ public class MinimapReliefTests
}
finally
{
- mini.Dispose(); info.Dispose(); hud.Dispose();
+ if (!checkDisposesMini) mini.Dispose();
+ info.Dispose(); community?.Dispose(); share?.Dispose(); feedback?.Dispose(); hud.Dispose();
Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);
}
}
@@ -257,6 +268,45 @@ public class MinimapReliefTests
});
}
+ /// <summary>#2583: the production HUD, community-icon and minimap HoverTip groups share the one hud-tip label.
+ /// Community and minimap hovers win over a focused rail button; leaving either or disposing the minimap restores it.</summary>
+ [UnityTest]
+ public IEnumerator ProductionPresentersShareTheTipAndRestoreTheFocusedRailButton()
+ {
+ yield return Rendered(1920, 1080, false, (root, mini) =>
+ {
+ var tip = root.Q<Label>(HoverTip.TipName);
+ var rail = root.Q<Button>("nav-cabinet");
+ var community = root.Q<Button>("hud-report");
+ var minimap = root.Q<Button>("minimap-overview");
+ void Enter(Button x) { using var e = PointerEnterEvent.GetPooled(new Event { type = EventType.MouseMove, mousePosition = x.worldBound.center }); e.target = x; x.SendEvent(e); }
+ void Leave(Button x) { using var e = PointerLeaveEvent.GetPooled(new Event { type = EventType.MouseMove, mousePosition = Vector2.zero }); e.target = x; x.SendEvent(e); }
+ void AssertShows(Button x, string because)
+ {
+ Assert.AreEqual(DisplayStyle.Flex, tip.style.display.value, because + ": the shared label is visible");
+ Assert.AreEqual(x.tooltip, tip.text, because + ": the shared label has the button's production tooltip");
+ }
+
+ try
+ {
+ rail.Focus();
+ AssertShows(rail, "focusing the real HUD rail button");
+ Enter(community);
+ AssertShows(community, "hovering the real community Report button");
+ Leave(community);
+ AssertShows(rail, "leaving the community icon restores the still-focused HUD rail button");
+ Enter(minimap);
+ AssertShows(minimap, "hovering the real minimap button");
+ Leave(minimap);
+ AssertShows(rail, "leaving the minimap restores the still-focused HUD rail button");
+ Enter(minimap);
+ AssertShows(minimap, "the minimap owns the shared tip before its presenter is disposed");
+ }
+ finally { mini.Dispose(); }
+ AssertShows(rail, "disposing the minimap presenter restores the still-focused HUD rail button");
+ }, checkDisposesMini: true, wireProductionTipGroups: true);
+ }
+
/// <summary>Review of #584: a switched-off layer's button dims, but its glyph keeps at least 3:1 against its disc
/// (WCAG 1.4.11): the disc's opacity blends the gold line toward the navy behind it.</summary>
[UnityTest]