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.

a11y: the focus rule looks past the element's own display (Refs #41) (#1507)

EllaSharakanski0e2c35de0c5a
diff --git a/changelog.d/a11y-focus-hidden-parent-41.md b/changelog.d/a11y-focus-hidden-parent-41.md
new file mode 100644
index 000000000..6a919b0ec
--- /dev/null
+++ b/changelog.d/a11y-focus-hidden-parent-41.md
@@ -0,0 +1,6 @@
+### Fixed
+
+- Keyboard: the focus is no longer dropped when the only candidates for it sit inside a hidden parent. A control
+ inside a closed screen keeps its own `display: flex`, so the rule that decides where the focus may go accepted it,
+ the focus then failed to land, and nothing had it. The rule now looks at every ancestor up to the panel, so such a
+ control is skipped and a usable fallback takes the focus instead.
diff --git a/unity/Assets/Scripts/UI/FocusReturn.cs b/unity/Assets/Scripts/UI/FocusReturn.cs
index 3ea8965fd..dfc380ae2 100644
--- a/unity/Assets/Scripts/UI/FocusReturn.cs
+++ b/unity/Assets/Scripts/UI/FocusReturn.cs
@@ -20,9 +20,21 @@ namespace Statecraft.Game.UI
/// <summary>Whether an element can actually take the keyboard focus now: a control that is disabled or not drawn
/// cannot, and asking it to leaves the focus nowhere. Public so the one rule has one home: ScreenNav asks it
- /// which control to hand an opened screen (#41).</summary>
- public static bool CanTakeFocus(VisualElement? e) =>
- e?.panel != null && e.focusable && e.enabledInHierarchy && e.resolvedStyle.display != DisplayStyle.None;
+ /// which control to hand an opened screen (#41).
+ ///
+ /// "Not drawn" has to mean every ancestor up to the panel, not the element alone (#41, found reviewing #1489).
+ /// `display: none` on a parent takes its children out of the layout, but each child keeps its own
+ /// `resolvedStyle.display`, so a button inside a closed screen still reads `Flex` and used to pass this test.
+ /// `Focus()` then did nothing and the focus was left nowhere, which is the one thing this predicate exists to
+ /// rule out. A closed screen is the plain case: `screen-budget` is `none` while its `screen-close-budget` is
+ /// `Flex` and enabled.</summary>
+ public static bool CanTakeFocus(VisualElement? e)
+ {
+ if (e?.panel == null || !e.focusable || !e.enabledInHierarchy) return false;
+ for (var v = e; v != null; v = v.parent)
+ if (v.resolvedStyle.display == DisplayStyle.None) return false;
+ return true;
+ }
/// <summary>Focuses <paramref name="target"/>; failing that the first control that can take the focus in each of
/// <paramref name="scopes"/> in turn; and last of all <paramref name="last"/> (accessibility audit, #41, the
diff --git a/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs b/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs
new file mode 100644
index 000000000..bbd195b00
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs
@@ -0,0 +1,123 @@
+#nullable enable
+using System;
+using System.Collections;
+using System.IO;
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.Map;
+using Statecraft.Game.UI;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using UnityEngine.TestTools;
+using UnityEngine.UIElements;
+using Object = UnityEngine.Object;
+
+/// <summary>#41, found while reviewing #1489. `FocusReturn.CanTakeFocus` asked only whether the element itself is
+/// drawn (`resolvedStyle.display != None`), not whether an ancestor hides it. An element inside a hidden parent keeps
+/// its own `display: Flex`, so it passed the test, and then `Focus()` did not take and the focus was left nowhere,
+/// which is the very thing the predicate exists to prevent.
+///
+/// A closed screen is exactly that shape: `screen-budget` is `display: none` while its `screen-close-budget` button
+/// is `Flex` and enabled. Measured on main before the fix:
+///
+/// screen-close-budget: ownDisplay=Flex enabledInHierarchy=True CanTakeFocus=True
+/// after Focus() the focus is on &lt;null&gt; (took=False)
+///
+/// It does not bite in the shipped paths today, because every caller passes scopes inside an open panel, but a
+/// caller that hands `ToOrWithin` a hidden subtree gets a silent focus drop instead of the fallback.</summary>
+public class FocusHiddenParentTests
+{
+ string saveDir = "";
+ RenderTexture? rt; PanelSettings? ps;
+ VisualElement ui = null!;
+ GameRoot root = null!;
+
+ [SetUp] public void TempSaves() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-hidden-" + Guid.NewGuid().ToString("N"));
+
+ [TearDown]
+ public void DiscardScene()
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ if (ps != null) Object.DestroyImmediate(ps);
+ if (rt != null) { rt.Release(); Object.DestroyImmediate(rt); }
+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);
+ }
+
+ Focusable? Focused => ui.panel?.focusController?.focusedElement;
+ string FocusedName => (Focused as VisualElement)?.name is string n && n.Length > 0 ? n : Focused == null ? "none" : Focused.GetType().Name;
+ IEnumerator Frames(int n) { for (int i = 0; i < n; i++) yield return null; }
+
+ IEnumerator Boot()
+ {
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ var doc = Object.FindFirstObjectByType<UIDocument>();
+ rt = new RenderTexture(1280, 720, 0);
+ ps = Object.Instantiate(doc.panelSettings);
+ ps.targetTexture = rt;
+ doc.panelSettings = ps;
+ Object.FindFirstObjectByType<MapScene>().Build(MapFocusTests.Inputs.Map);
+ root = Object.FindFirstObjectByType<GameRoot>();
+ root.Store = new SaveStore(saveDir);
+ root.Begin(MapFocusTests.Inputs, 0);
+ ui = doc.rootVisualElement;
+ for (int i = 0; i < 30 && !(ui.Q("top-bar").worldBound.height > 0); i++) yield return null;
+ yield return Frames(5);
+ GameSceneTests.HideTheOpeningCard(ui);
+ yield return Frames(3);
+ }
+
+ [UnityTest]
+ public IEnumerator AScopeInsideAHiddenParentIsSkippedForTheLastStop()
+ {
+ yield return Boot();
+
+ var hidden = ui.Q<VisualElement>("screen-budget");
+ Assert.IsNotNull(hidden, "precondition: the Budget screen is in the tree");
+ Assert.AreEqual(DisplayStyle.None, hidden.resolvedStyle.display, "precondition: it is closed, so display:none");
+ var inside = ui.Q<Button>("screen-close-budget");
+ Assert.IsNotNull(inside, "precondition: it has a button of its own");
+ Assert.AreEqual(DisplayStyle.Flex, inside.resolvedStyle.display,
+ "precondition: the button's own display is Flex; only its parent hides it");
+ Assert.IsTrue(inside.enabledInHierarchy, "precondition: and it is enabled");
+
+ var last = ui.Q<Button>("end-turn");
+ Assert.IsNotNull(last, "precondition: End Turn is the visible last stop");
+
+ // nothing focused to start, so a drop is unambiguous
+ (Focused as VisualElement)?.Blur();
+ yield return null;
+
+ FocusReturn.ToOrWithin(null, last, hidden);
+ yield return Frames(3);
+
+ Assert.IsNotNull(Focused,
+ $"the focus is not dropped when the only candidates are inside a hidden parent; it is on {FocusedName}");
+ Assert.AreEqual("end-turn", FocusedName,
+ "the hidden screen's own button is skipped and the last stop takes the focus");
+ }
+
+ /// <summary>The ordinary case still works, and this one has to prove it rather than pass on the fallback: a scope
+ /// that is drawn gives up its own first control. The owner caught the first version asserting only that something
+ /// had the focus, which `end-turn` would have satisfied even if the minimap gave up nothing.</summary>
+ [UnityTest]
+ public IEnumerator AVisibleScopeStillGivesUpItsFirstControl()
+ {
+ yield return Boot();
+
+ var minimap = ui.Q<VisualElement>("minimap");
+ Assert.IsNotNull(minimap, "precondition: the minimap is in the tree");
+ Assert.AreNotEqual(DisplayStyle.None, minimap.resolvedStyle.display, "precondition: and it is drawn");
+ var first = ui.Q<Button>("minimap-overview");
+ Assert.IsNotNull(first, "precondition: minimap-overview is its first button");
+
+ (Focused as VisualElement)?.Blur();
+ yield return null;
+
+ FocusReturn.ToOrWithin(null, ui.Q<Button>("end-turn"), minimap);
+ yield return Frames(3);
+
+ Assert.IsTrue(minimap.Contains(Focused as VisualElement),
+ $"the focus lands inside the drawn scope, not on the last stop; it is on {FocusedName}");
+ Assert.AreEqual("minimap-overview", FocusedName, "and on its first button");
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs.meta b/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs.meta
new file mode 100644
index 000000000..d86703e2b
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/FocusHiddenParentTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: b81c8341d7c7a412ebd44fdff7c856eb
\ No newline at end of file