A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
a11y: opening a screen puts the keyboard focus in it (Refs #41) (#1489)
EllaSharakanskic4e45d0136eb
diff --git a/changelog.d/a11y-screen-entry-41.md b/changelog.d/a11y-screen-entry-41.mdnew file mode 100644index 000000000..5f7f61251--- /dev/null+++ b/changelog.d/a11y-screen-entry-41.md@@ -0,0 +1,7 @@+### Fixed++- Keyboard: opening a screen from the side rail now puts the focus in the screen instead of leaving it on the rail+ button. Reaching the screen's first control took 16 Tab presses for the Cabinet and the Budget and 14 for Defence,+ because the focus stayed outside and Tab had to walk the rest of the HUD first. The focus goes to the screen's first+ control, leaving its close X one Shift+Tab back; a screen with nothing else that can take the focus takes its X. A+ screen opened with the mouse still moves no focus, and Escape and the X still hand the focus back to the rail button.diff --git a/docs/accessibility-audit-2026-10-14.md b/docs/accessibility-audit-2026-10-14.mdindex 2c7509ac9..071fa48ea 100644--- a/docs/accessibility-audit-2026-10-14.md+++ b/docs/accessibility-audit-2026-10-14.md@@ -372,9 +372,8 @@ What failed, and what changed:| K1 | Tab after the Cabinet's last card (and after the last candidate) lands on the list's own scroll bar. `FocusScroll` then called `ScrollView.ScrollTo` on it, which throws for anything outside the list's content: an error on every such Tab. | `FocusScroll` leaves an element outside the view's content alone (the scroll bar is always in sight). || K2 | Enter on a city panel button (Train, Assign, Build) rebuilt the panel and took the focus with it; the next Tab started again at Cabinet, as H2 did on the Budget. | `InfoPanelPresenter` gives the focus to the rebuilt button of the same name after a keyboard press; a mouse press still leaves none (PointerBlur's rule). |-Still open, not blocking the turn:--- From a screen's button it takes about 16 Tabs to reach the screen's first control.+Still open, not blocking the turn: nothing. The last entry, the long Tab run from a screen's button to its+first control, is closed below (23 October 2026, K5).Pinned in EditMode: the card's order (the card, each option, Hide, the side menu, New Game, End Turn, Star, Report,Share, the card again; Shift+Tab the reverse), End Turn's (the map stop, the minimap's three buttons, Star, Report,@@ -401,6 +400,24 @@ seat open), `CityPanelFocusTests` (the Assign minus at 0 keeps the focus inside`SitePanelFocusTests` (a Build order, which disables every Build in its part, leaves the focus on something thatcan hold it). No UI string was added, so nothing goes to #564.+## The screen-entry fix, 23 October 2026++The last of the three items the 23 October list left open (K5). Found on main `c389b492` before the change, with an+EditMode test that fails without it.++| | Finding | Fix |+|---|---|---|+| K5 | Opening a screen from the side rail leaves the focus on the rail button. A screen's content sits after the whole HUD in the Tab order, so reaching it means walking the rail's eight buttons, New Game, End Turn, the minimap's three buttons and star, report and share first. Walked in EditMode: 16 Tabs for the Cabinet and the Budget, 14 for Defence, the count being only where the player's button sits in the rail. `ScreenNav.Close` already hands the focus back to the rail button, so open was the asymmetric half. | `ScreenNav.Open` hands the focus to the screen it opened, after the screen's `OnOpened` action has built its rows. The screen's own X is first in its order, since the header is its first child, so the focus goes to the first control of the screen's content and the X stays one Shift+Tab back; a screen with no control of its own that can take the focus (Defence at the opening turn, whose ladder and call-up buttons are `display:none`) takes its X. Only a keyboard press hands the focus on, as `Close` already respects, so a mouse press still moves no focus. |++A scroll bar is never the landing control: `defence-body`'s scroller otherwise comes before any control of Defence's+own. The bars of the screens other than the Cabinet take no Tab stop today, because a scroll view hides its scroller+while the content fits, but their sliders stay focusable, so a bar becomes a stop once its list overflows. That is+K3's scope and is not changed here.++Pinned in EditMode: `ScreenEntryTests` (the Cabinet and the Budget land on their first control; Defence, which has+none usable yet, lands on its X; a pointer press hands no focus; Escape still returns it to the rail button). No UI+string was added, so nothing goes to #564.+## ScreenshotsIn `.superpowers/a11y-2026-10-14/` (1568x752 unless noted), with the driver and the contrast, colour-vision anddiff --git a/unity/Assets/Scripts/UI/FocusReturn.cs b/unity/Assets/Scripts/UI/FocusReturn.csindex 7a931bf25..3ea8965fd 100644--- a/unity/Assets/Scripts/UI/FocusReturn.cs+++ b/unity/Assets/Scripts/UI/FocusReturn.cs@@ -19,8 +19,9 @@ 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.</summary>- static bool CanTakeFocus(VisualElement? e) =>+ /// 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;/// <summary>Focuses <paramref name="target"/>; failing that the first control that can take the focus in each ofdiff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.csindex e8888a5c7..95eb28fb0 100644--- a/unity/Assets/Scripts/UI/ScreenNav.cs+++ b/unity/Assets/Scripts/UI/ScreenNav.cs@@ -1,5 +1,6 @@#nullable enableusing System.Collections.Generic;+using System.Linq;using UnityEngine.UIElements;namespace Statecraft.Game.UI@@ -304,6 +305,38 @@ namespace Statecraft.Game.UI(root.Q("root") ?? root).EnableInClassList(ScreenOpenClass, screen != null);DisableCovered(root);if (screen != null) (root.Q<VisualElement>("screen-" + screen).userData as System.Action)?.Invoke();+ // After the OnOpened action, which is what builds a screen's rows (CabinetPresenter, DefencePresenter):+ // before it there is nothing in the screen to hand the focus to.+ if (screen != null && presses.ByKeyboard) HandFocusTo(root.Q<VisualElement>("screen-" + screen));+ }++ /// <summary>Gives the keyboard focus to the screen just opened (accessibility audit, #41, the keyboard-only+ /// turn's last "Still open" item). Without it the focus stayed on the rail button that opened the screen, and+ /// since a screen's content sits after the whole HUD in the Tab order, reaching it meant walking the rail, New+ /// Game, End Turn, the minimap's three buttons and star, report and share first: 16 stops for the Cabinet and+ /// the Budget, 14 for Defence. Close is already the other half of this, handing the focus back to the rail+ /// button, so open was the asymmetric one.+ ///+ /// The screen's own X is first in its order, because the header is its first child, so the focus goes to the+ /// first control of the screen's content instead and leaves the X one Shift+Tab back; Escape and the X itself+ /// are unchanged. A screen with no other control that can take the focus takes its X, which is better than+ /// leaving the focus outside the screen the player just opened.</summary>+ void HandFocusTo(VisualElement screen)+ {+ var x = closes.FirstOrDefault(b => screen.Contains(b));+ VisualElement? first = null;+ foreach (var e in screen.Query<VisualElement>().Build())+ {+ if (e == screen || e == x || !FocusReturn.CanTakeFocus(e)) continue;+ // Not a scroll bar: Defence's defence-body puts its scroller before any control of its own, and a bar is+ // no use from the keyboard (#41 K3, which took the Cabinet's bars out of the Tab order). The other+ // screens' bars take no stop today, since a view hides its scroller while the content fits, but their+ // sliders stay focusable, so one becomes a stop once its list overflows; that is K3's scope, not this one's.+ if (e is Scroller || e.GetFirstAncestorOfType<Scroller>() != null) continue;+ first = e;+ break;+ }+ FocusReturn.To(first ?? x);}}}diff --git a/unity/Assets/Tests/EditMode/ScreenEntryShots.cs b/unity/Assets/Tests/EditMode/ScreenEntryShots.csnew file mode 100644index 000000000..736943162--- /dev/null+++ b/unity/Assets/Tests/EditMode/ScreenEntryShots.cs@@ -0,0 +1,110 @@+#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>Writes the before and after stills for #41's screen-entry fix (K5), in English and Hebrew, to+/// `.superpowers/a11y-screen-entry-41/` as the 14 October audit does (local, not committed). The "before" state is the+/// old path called directly: the screen is opened through `ScreenNav` and the focus is put back on the rail button,+/// which is where it stayed before `HandFocusTo`. Explicit, so it does not run with the checks.</summary>+[Explicit("writes stills for a pull request, not a check")]+public class ScreenEntryShots+{+ string saveDir = "";+ string shotDir = "";+ RenderTexture? rt; PanelSettings? ps;+ VisualElement ui = null!;+ GameRoot root = null!;++ [SetUp]+ public void TempSaves()+ {+ saveDir = Path.Combine(Path.GetTempPath(), "statecraft-entry-" + Guid.NewGuid().ToString("N"));+ shotDir = Path.Combine(Directory.GetCurrentDirectory(), ".superpowers", "a11y-screen-entry-41");+ Directory.CreateDirectory(shotDir);+ }++ [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;+ VisualElement At => Focused as VisualElement ?? throw new AssertionException("nothing has the focus");+ 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 Enter()+ {+ var at = At;+ using (var k = KeyDownEvent.GetPooled('\n', KeyCode.Return, EventModifiers.None)) { k.target = at; at.SendEvent(k); }+ using (var s = NavigationSubmitEvent.GetPooled()) { s.target = at; at.SendEvent(s); }+ yield return null;+ }++ IEnumerator Boot(bool hebrew)+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var doc = Object.FindFirstObjectByType<UIDocument>();+ rt = new RenderTexture(1280, 720, 24);+ 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);+ if (hebrew) root.Language = UiLanguage.Hebrew;+ 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);+ }++ void Shot(string name)+ {+ var prev = RenderTexture.active;+ RenderTexture.active = rt;+ var tex = new Texture2D(rt!.width, rt.height, TextureFormat.RGB24, false);+ tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);+ tex.Apply();+ RenderTexture.active = prev;+ File.WriteAllBytes(Path.Combine(shotDir, name + ".png"), tex.EncodeToPNG());+ Object.DestroyImmediate(tex);+ Debug.Log($"SHOT {name}: focus is on {FocusedName}");+ }++ IEnumerator Shots(bool hebrew)+ {+ var lang = hebrew ? "he" : "en";+ yield return Boot(hebrew);++ var nav = ui.Q<Button>("nav-cabinet");+ nav.Focus(); yield return null;+ yield return Enter(); // the fix hands the focus into the screen+ yield return Frames(5);+ Shot($"cabinet-after-{lang}");++ // before: what Open did until now, leave the focus on the rail button that opened the screen+ FocusReturn.To(ui.Q<Button>("nav-cabinet"));+ yield return Frames(3);+ Shot($"cabinet-before-{lang}");+ }++ [UnityTest] public IEnumerator English() { yield return Shots(false); }+ [UnityTest] public IEnumerator Hebrew() { yield return Shots(true); }+}diff --git a/unity/Assets/Tests/EditMode/ScreenEntryShots.cs.meta b/unity/Assets/Tests/EditMode/ScreenEntryShots.cs.metanew file mode 100644index 000000000..294717290--- /dev/null+++ b/unity/Assets/Tests/EditMode/ScreenEntryShots.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 1c4c9d24ff01f4e2390be8b456af65df\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/ScreenEntryTests.cs b/unity/Assets/Tests/EditMode/ScreenEntryTests.csnew file mode 100644index 000000000..075b19a08--- /dev/null+++ b/unity/Assets/Tests/EditMode/ScreenEntryTests.cs@@ -0,0 +1,164 @@+#nullable enable+using System;+using System.Collections;+using System.IO;+using System.Linq;+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, the keyboard-only turn's last "Still open" item: from a screen's rail button it took about 16 Tabs to+/// reach the screen's first control. `ScreenNav.Open` showed the screen but never handed the focus to it, so the focus+/// stayed on the rail button and Tab walked the rest of the HUD, the rail, New Game, End Turn, the minimap's three+/// buttons and star/report/share, before wrapping into the screen. `Close` already hands the focus back to the rail+/// button (`ScreenNav`, `FocusReturn.To(button)`), so open was the asymmetric half. Same harness as+/// `KeyboardTurnTests`.</summary>+public class ScreenEntryTests+{+ string saveDir = "";+ RenderTexture? rt; PanelSettings? ps;+ VisualElement ui = null!;+ GameRoot root = null!;++ [SetUp] public void TempSaves() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-screenentry-" + 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;+ VisualElement At => Focused as VisualElement ?? throw new AssertionException("nothing has the focus");+ 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 Enter()+ {+ var at = At;+ using (var k = KeyDownEvent.GetPooled('\n', KeyCode.Return, EventModifiers.None)) { k.target = at; at.SendEvent(k); }+ using (var s = NavigationSubmitEvent.GetPooled()) { s.target = at; at.SendEvent(s); }+ yield return null;+ }++ IEnumerator Escape()+ {+ var at = At;+ using (var e = NavigationCancelEvent.GetPooled()) { e.target = at; at.SendEvent(e); }+ 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);+ }++ /// <summary>Opens a screen the way a keyboard player does: focus its rail button, press Enter.</summary>+ IEnumerator OpenFromTheRail(string screen)+ {+ var nav = ui.Q<Button>("nav-" + screen);+ Assert.IsNotNull(nav, $"precondition: the rail has a button for {screen}");+ nav.Focus();+ yield return null;+ Assert.AreEqual("nav-" + screen, FocusedName, "precondition: the rail button has the focus before Enter");+ yield return Enter();+ yield return Frames(5);+ }++ [UnityTest]+ [TestCase("cabinet", "seat-prime_minister", ExpectedResult = null)]+ [TestCase("budget", "budget-defense_minister-down", ExpectedResult = null)]+ public IEnumerator OpeningAScreenFromTheKeyboardPutsTheFocusOnItsFirstControl(string screen, string expected)+ {+ yield return Boot();+ yield return OpenFromTheRail(screen);++ var panel = ui.Q<VisualElement>("screen-" + screen);+ Assert.IsNotNull(panel, $"precondition: screen-{screen} is in the tree");+ Assert.IsTrue(panel.Contains(At),+ $"the focus should be inside the screen the moment it opens, but it is on {FocusedName}");+ // and on the content, not the X: the header's close button is first in the screen's own order, so without+ // skipping it the first thing a keyboard player could do with a screen they just opened is close it.+ Assert.AreEqual(expected, FocusedName, "and on the screen's first content control");+ Assert.AreNotEqual("screen-close-" + screen, FocusedName, "not the close X");+ }++ /// <summary>Defence is the other case: at the opening turn it has no control of its own that can take the focus+ /// (its ladder and call-up buttons are in the tree but not usable yet, and its scroll bar is no use from the+ /// keyboard, #41 K3), so the screen's own X takes it. That is still better than leaving the focus on the rail+ /// outside the screen the player just opened. The precondition is checked rather than assumed, so if Defence later+ /// gains a usable control (#755 adds the call-up) this test says so instead of quietly passing.</summary>+ [UnityTest]+ public IEnumerator AScreenWithNoUsableControlOfItsOwnTakesItsCloseButton()+ {+ yield return Boot();+ yield return OpenFromTheRail("defence");++ var panel = ui.Q<VisualElement>("screen-defence");+ var x = ui.Q<Button>("screen-close-defence");+ // Measured at the opening turn: de-escalate, escalate, mob-down and mob-up are all display:none, and the+ // defence-body scroll bar is no use from the keyboard, so nothing in the screen but the X can hold the focus.+ var usable = panel.Query<VisualElement>().Build()+ .Where(e => e != panel && e != x && FocusReturn.CanTakeFocus(e)+ && !(e is Scroller) && e.GetFirstAncestorOfType<Scroller>() == null)+ .ToList();+ Assert.IsEmpty(usable, $"precondition: Defence has no control of its own that can take the focus at the opening turn");+ Assert.IsTrue(panel.Contains(At), $"the focus should be inside the screen, but it is on {FocusedName}");+ Assert.AreEqual("screen-close-defence", FocusedName, "and on its X, the only thing in it that can hold the focus");+ }++ /// <summary>The pointer must not move the focus (PointerBlur's rule), as Close already respects.</summary>+ [UnityTest]+ public IEnumerator APointerPressDoesNotHandTheFocusToTheScreen()+ {+ yield return Boot();+ var endTurn = ui.Q<Button>("end-turn");+ endTurn.Focus();+ yield return null;++ DecisionCardTests.PressWithPointer(ui.Q<Button>("nav-cabinet"));+ yield return Frames(5);++ Assert.AreEqual("cabinet", ScreenOpen(), "precondition: the press opened the Cabinet");+ var panel = ui.Q<VisualElement>("screen-cabinet");+ Assert.IsFalse(panel.Contains(At), $"a pointer press moves no focus, but it is on {FocusedName}");+ }++ /// <summary>Escape still hands the focus back to the rail button it came from.</summary>+ [UnityTest]+ public IEnumerator EscapeStillReturnsTheFocusToTheRailButton()+ {+ yield return Boot();+ yield return OpenFromTheRail("cabinet");+ yield return Escape();+ yield return Frames(3);+ Assert.IsNull(ScreenOpen(), "Escape closes the screen");+ Assert.AreEqual("nav-cabinet", FocusedName, "and the focus is back on the rail button");+ }++ string? ScreenOpen() => ScreenNav.Screens.FirstOrDefault(s => ui.Q("nav-" + s).ClassListContains(ScreenNav.OpenClass));+}diff --git a/unity/Assets/Tests/EditMode/ScreenEntryTests.cs.meta b/unity/Assets/Tests/EditMode/ScreenEntryTests.cs.metanew file mode 100644index 000000000..29f2e95f2--- /dev/null+++ b/unity/Assets/Tests/EditMode/ScreenEntryTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 0cb080c78e16f48719d7b06c2e1fc203\ No newline at end of file