A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Accessibility: Cabinet focus moves to the candidates (M2) (Refs #2340) (#2434)
Beri38db11cd0a01
diff --git a/changelog.d/2340-cabinet-focus.md b/changelog.d/2340-cabinet-focus.mdnew file mode 100644index 000000000..8fcf98631--- /dev/null+++ b/changelog.d/2340-cabinet-focus.md@@ -0,0 +1,4 @@+### Changed+- Cabinet, keyboard (Refs #2340, accessibility audit M2): pressing Enter on a seat now moves the focus to the first+ candidate who can be appointed, so you no longer Tab through up to 50 names to reach one. Escape in the candidate+ list puts the focus back on that seat and leaves the Cabinet open. A mouse click on a seat still moves no focus.diff --git a/unity/Assets/Scripts/UI/CabinetPresenter.cs b/unity/Assets/Scripts/UI/CabinetPresenter.csindex 18b70e4b6..fbad1a853 100644--- a/unity/Assets/Scripts/UI/CabinetPresenter.cs+++ b/unity/Assets/Scripts/UI/CabinetPresenter.cs@@ -41,6 +41,10 @@ namespace Statecraft.Game.UIreadonly Dictionary<string, (Button appoint, Label reason)> rows = new Dictionary<string, (Button, Label)>();string? pickFor; string chipsFor = "";bool dirty;+ /// <summary>The seat button last pressed (#2340 M2): Escape from the candidate list gives it the focus back.</summary>+ string? pressedSeat;+ readonly PressSource presses;+ int escapedAt = -1;public CabinetPresenter(VisualElement root, SimBridge bridge, UiText text, PortraitCatalog? portraits){@@ -52,7 +56,8 @@ namespace Statecraft.Game.UIpmOffices = screen.Q<VisualElement>("cabinet-pm-offices"); chips = screen.Q<VisualElement>("cabinet-parties");hemicycle = screen.Q<Hemicycle>("cabinet-hemicycle");hemicycle.Mirrored = text["ui.lang"] == UiLanguage.Hebrew; // the coalition from the right in a right-to-left UI- onPm = () => Select(PrimeMinister);+ presses = new PressSource(screen);+ onPm = () => Press(PrimeMinister, pmCard.name);pmCard.clicked += onPm;onPmGeometry = _ => FramePm();screen.Q<Label>("cabinet-house-unit").text = text["cabinet.members_unit"];@@ -69,6 +74,7 @@ namespace Statecraft.Game.UI{bridge.Changed -= OnChanged;pmCard.clicked -= onPm;+ presses.Dispose();pmPortrait?.UnregisterCallback(onPmGeometry); pmPortrait = null;foreach (var e in new[] { side, pmOffices, status }) e.UnregisterCallback(onSideGeometry);if (screen.userData is System.Action) screen.userData = null;@@ -87,6 +93,36 @@ namespace Statecraft.Game.UIpublic void Select(string? office) { Selected = office; Render(); }+ /// <summary>A seat's button (#2340 M2): selects its office, and from the keyboard hands the focus to the first candidate+ /// that can be appointed. The list comes before the cards in the Tab order, so that candidate was up to 60 Tabs+ /// away. A pointer press moves no focus (PointerBlur's rule). None can be appointed: the focus stays on the seat.</summary>+ void Press(string office, string seat)+ {+ pressedSeat = seat;+ Select(office);+ if (!presses.ByKeyboard) return;+ var first = candidates.Query<Button>().Where(b => b.enabledInHierarchy).First();+ if (first == null) return;+ first.Focus();+ // The list was hidden until this press, so it may not take the focus before its styles resolve.+ first.schedule.Execute(() => { if (first.panel != null && first.focusController?.focusedElement is VisualElement v && v.name == seat) first.Focus(); });+ }++ /// <summary>Escape with the focus in the candidate list (#2340 M2): gives the focus back to the seat that listed them,+ /// and the Cabinet stays open. True when it did, and also in the frame ScreenNav's Cancel already did it, so+ /// GameRoot's poll does not close the screen in the same press (AdvisorPresenter.Escape's pattern).</summary>+ public bool Escape()+ {+ if (escapedAt == Time.frameCount) return true;+ var focused = screen.focusController?.focusedElement as VisualElement;+ if (focused == null || !candidates.Contains(focused)) return false;+ var seat = (pressedSeat == null ? null : screen.Q<Button>(pressedSeat)) ?? screen.Q<Button>("seat-" + Selected);+ if (seat == null) return false;+ escapedAt = Time.frameCount;+ FocusReturn.To(seat);+ return true;+ }+void OnChanged() { if (ScreenNav.IsOpen(screen)) Render(); else dirty = true; }string Describe(Person p)@@ -154,7 +190,7 @@ namespace Statecraft.Game.UIforeach (var seat in pmHeld){var office = seat.Office.Id;- var b = new Button(() => Select(office)) { name = "seat-" + office, text = text.Format("cabinet.select_office", ContentNames.Of(text, seat.Office, pm, s.Date)) };+ var b = new Button(() => Press(office, "seat-" + office)) { name = "seat-" + office, text = text.Format("cabinet.select_office", ContentNames.Of(text, seat.Office, pm, s.Date)) };b.AddToClassList("pm-office");pmOffices.Add(b);}@@ -172,7 +208,7 @@ namespace Statecraft.Game.UIforeach (var (key, seat, _) in grid){var office = seat.Office.Id;- var card = new Button(() => Select(office)) { name = "seat-" + key };+ var card = new Button(() => Press(office, "seat-" + key)) { name = "seat-" + key };card.AddToClassList("seat");PersonCard.Build(card, "seat-" + key, text);cards[key] = card;diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex a7d1b4def..e40767afe 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -87,6 +87,7 @@ namespace Statecraft.Game.UI/// <summary>Set by SwitchLanguage for its Begin (#2054): whether the start screen it rebuilds offered Continue.</summary>bool keptResumed = true;AdvisorPresenter? advisor;+ CabinetPresenter? cabinet;EncyclopediaPresenter? encyclopedia;/// <summary>Tests supply an in-memory approval copy for Hebrew layout; the shipped review fields are unchanged.</summary>internal AdvisorCatalog? AdvisorCatalogForTests { get; set; }@@ -381,7 +382,7 @@ namespace Statecraft.Game.UI// "Report a bug or idea" (#1169), from Settings and the start screen: the version line names the content GameBoot logged.feedback = Own(new FeedbackPresenter(root, text, ReportLinks.Version(Application.version, inputs.Content.Hash)));var feedbackPanel = feedback;- Own(new CabinetPresenter(root, bridge, text, portraits));+ cabinet = Own(new CabinetPresenter(root, bridge, text, portraits));// After End Turn, what changed in the top bar and the sim's causes (#1037); under an outcome clip, which plays first.report = Own(new TurnReportPresenter(root, bridge, text, nav!));// "Share" (#1170), from Settings and the turn report: the frame is captured at its end, before the panel shows.@@ -389,7 +390,7 @@ namespace Statecraft.Game.UI// Star, Report and Share as round icons at the map screen's bottom left (#1236): the same actions as Settings' buttons.Own(new CommunityIconsPresenter(root, text, feedbackPanel, sharePanel));// Both panels in one hook: the same Escape never closes the screen under the panel too.- nav!.TakesEscape = () => feedbackPanel.Escape() || sharePanel.Escape() || advisor?.CloseOverlay() == true || advisor?.OverlayClosedThisFrame == true || advisor?.Escape() == true || this.info?.Escape() == true; // the field: the panel is made below+ nav!.TakesEscape = () => feedbackPanel.Escape() || sharePanel.Escape() || advisor?.CloseOverlay() == true || advisor?.OverlayClosedThisFrame == true || advisor?.Escape() == true || cabinet?.Escape() == true || this.info?.Escape() == true; // the field: the panel is made belowvar names = map.SettlementsFor(inputs.Map.SettlementsYaml).ToDictionary(s => s.Id, s => s.Name); // the map's parse (issue #321)// The capture dialog (GDD Phase 6 PR 10, #802): in the decision layer, after the card, which goes first; the// places' Hebrew names from the same parse.@@ -952,7 +953,7 @@ namespace Statecraft.Game.UIfor (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose();owned.Clear();if (hoveredCity != null) { hoveredCity = null; HandCursor(false); } // the next Begin's pills start unlit- companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;+ companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; cabinet = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;// A ?profile run cut short (a stopped coroutine never reaches its restore line) must not leave the terrain at the// profile's pixel error (Task 24 review M1). Nothing else changes it, so restoring is always right.if (map != null && map.Terrain != null) map.Terrain.heightmapPixelError = MapBuilder.TerrainPixelError;@@ -1152,7 +1153,8 @@ namespace Statecraft.Game.UIif (!done) done = end?.Close() == true || end?.ClosedThisFrame == true;if (!done) done = report?.Close() == true || report?.ClosedThisFrame == true;done |= start?.Escape() == true;- done |= nav?.Close() == true;+ // Escape in the Cabinet's candidate list goes back to the seat, not out of the screen (#2340 M2).+ done |= cabinet?.Escape() == true || nav?.Close() == true;// A screen/card that consumed this Cancel must not also dismiss the map selection in this frame.if (!done && nav?.ClosedThisFrame != true && document.rootVisualElement.focusController?.focusedElement == null&& capture?.Escape() != true && card?.Escape() != true && card?.CollapsedThisFrame != true)diff --git a/unity/Assets/Tests/EditMode/KeyboardTurnTests.cs b/unity/Assets/Tests/EditMode/KeyboardTurnTests.csindex 11c813b5b..791b030c7 100644--- a/unity/Assets/Tests/EditMode/KeyboardTurnTests.cs+++ b/unity/Assets/Tests/EditMode/KeyboardTurnTests.cs@@ -223,8 +223,17 @@ public class KeyboardTurnTestsyield return Enter();Assert.AreEqual("cabinet", ScreenOpen(), "Enter opens the Cabinet");yield return TabTo(v => v.name.StartsWith("seat-", StringComparison.Ordinal) && v.name != "seat-prime_minister", "a minister's seat");+ var seat = FocusedName;yield return Enter();- yield return TabTo(v => ui.Q("cabinet-candidates").Contains(v), "a candidate");+ // #2340 M2: Enter on a seat hands the focus to its first candidate that can be appointed (the list comes before+ // the cards in the Tab order, so it used to be up to 60 Tabs away), and Escape from the list goes back to the seat.+ var first = ui.Q("cabinet-candidates").Query<Button>().ToList().First(b => b.enabledInHierarchy);+ Assert.AreEqual(first.name, FocusedName, $"Enter on {seat} focuses its first candidate that can be appointed, with no Tab");+ yield return Escape();+ Assert.AreEqual("cabinet", ScreenOpen(), "Escape in the candidate list does not close the Cabinet");+ Assert.AreEqual(seat, FocusedName, "Escape in the candidate list gives the focus back to the seat");+ yield return Enter();+ Assert.AreEqual(first.name, FocusedName, "Enter on the seat again focuses the same candidate");yield return Enter();Assert.IsTrue(Bridge.Queued.OfType<Appoint>().Any(), "the appointment is ordered");// The lists' own scroll bars used to take Tab after their last row (K1). They are out of the order now@@ -298,6 +307,55 @@ public class KeyboardTurnTestsAssert.AreNotEqual("city-train-halutz", FocusedName, "and leaves the rebuilt button unlit");}+ /// <summary>#2340 M2, review of #2434 item 1: Enter on a seat nobody can be appointed to (the Prime Minister's: the player+ /// is the PM) lists every person, each refused, and the focus stays on the seat instead of going nowhere.</summary>+ [UnityTest]+ public IEnumerator ASeatWithNoCandidateToAppointKeepsTheFocus()+ {+ yield return Boot();+ GameSceneTests.HideTheOpeningCard(ui);+ var navButton = ui.Q("nav-cabinet");+ for (int i = 0; i < 10 && Focused != navButton; i++) { yield return null; navButton.Focus(); }+ yield return Enter();+ Assert.AreEqual("cabinet", ScreenOpen(), "precondition: Enter opens the Cabinet");+ yield return TabTo(v => v.name == "seat-prime_minister", "the Prime Minister's seat", 120);+ yield return Enter();+ var listed = ui.Q("cabinet-candidates").Query<Button>().ToList();+ Assert.IsNotEmpty(listed, "precondition: the Prime Minister's office lists every person");+ Assert.IsFalse(listed.Any(b => b.enabledInHierarchy), "precondition: nobody can be appointed to it");+ Assert.AreEqual("seat-prime_minister", FocusedName, "the focus stays on the seat");+ }++ /// <summary>#2340 M2, review of #2434 item 2: one Escape reaches the Cabinet twice in one frame, as ScreenNav's Cancel+ /// (TakesEscape) and as GameRoot's Escape poll (`cabinet.Escape() || nav.Close()`, asked here as Update asks it: an+ /// edit-mode test has no keyboard device). The first gives the focus back to the seat; the second, with the focus no+ /// longer in the list, must not close the screen.</summary>+ [UnityTest]+ public IEnumerator OneEscapeFromTheCandidatesIsHandledOnceWhenBothPathsFire()+ {+ yield return Boot();+ GameSceneTests.HideTheOpeningCard(ui);+ var navButton = ui.Q("nav-cabinet");+ for (int i = 0; i < 10 && Focused != navButton; i++) { yield return null; navButton.Focus(); }+ yield return Enter();+ Assert.AreEqual("cabinet", ScreenOpen(), "precondition: Enter opens the Cabinet");+ yield return TabTo(v => v.name.StartsWith("seat-", StringComparison.Ordinal) && v.name != "seat-prime_minister", "a minister's seat", 120);+ var seat = FocusedName;+ yield return Enter();+ Assert.IsTrue(ui.Q("cabinet-candidates").Contains(At), "precondition: Enter on the seat focuses a candidate");+ Func<string, object?> field = name => typeof(GameRoot).GetField(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.GetValue(root);+ var cabinet = (CabinetPresenter)field("cabinet")!; var nav = (ScreenNav)field("nav")!;+ var frame = Time.frameCount;+ var at = At;+ using (var e = NavigationCancelEvent.GetPooled()) { e.target = at; at.SendEvent(e); }+ Assert.AreEqual(seat, FocusedName, "Cancel gives the focus back to the seat");+ var used = cabinet.Escape() || nav.Close();+ Assert.AreEqual(frame, Time.frameCount, "both in the same frame");+ Assert.IsTrue(used, "the poll's Escape is used");+ Assert.AreEqual("cabinet", ScreenOpen(), "and the Cabinet stays open");+ Assert.AreEqual(seat, FocusedName, "with the focus on the seat");+ }+string? ScreenOpen() => ScreenNav.Screens.FirstOrDefault(s => ui.Q("nav-" + s).ClassListContains(ScreenNav.OpenClass));/// <summary>Moves the camera's look-at point onto Tel Aviv's hex, on the ground, as a pan would (MapFocusTests).</summary>