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.

Advisors: closing the tour gives the focus back to what opened it (Refs #3028) (#3066)

Eyal Golancbcb09a0df1a
diff --git a/changelog.d/3028-advisor-focus.md b/changelog.d/3028-advisor-focus.md
new file mode 100644
index 000000000..2df825f57
--- /dev/null
+++ b/changelog.d/3028-advisor-focus.md
@@ -0,0 +1,3 @@
+- Closing the advisors' opening tour gives the keyboard focus back to the control that opened it, such as the
+ Encyclopedia's button, instead of always to End turn (Refs #3028). End turn still takes it when that control cannot
+ (it is disabled or gone), and when a message you reached with Tab closes.
diff --git a/unity/Assets/Scripts/UI/AdvisorPresenter.cs b/unity/Assets/Scripts/UI/AdvisorPresenter.cs
index 011303b0e..703cb365b 100644
--- a/unity/Assets/Scripts/UI/AdvisorPresenter.cs
+++ b/unity/Assets/Scripts/UI/AdvisorPresenter.cs
@@ -56,6 +56,8 @@ namespace Statecraft.Game.UI
readonly EventCallback<GeometryChangedEvent> onMediaGeometry;
string watchScreens = "";
VisualElement? mediaOpener;
+ /// <summary>The control that had the focus when the player opened the tour: closing it gives the focus back there (#3028).</summary>
+ VisualElement? chainOpener;
bool watchStoppedOnPress;
int overlayClosedAt = -1;
internal bool OverlayClosedThisFrame => overlayClosedAt == Time.frameCount;
@@ -225,7 +227,7 @@ namespace Statecraft.Game.UI
if (session.Chain.Step >= 0 && ReferenceEquals(openingGame, bridge.Session)) return;
session.Queue.Reset(); session.Fired.Clear(); session.FiredTurn = Turn;
Shown = null; covered = null;
- session.Chain.Step = -1; session.Chain.PutBack = null; adopted = null;
+ session.Chain.Step = -1; session.Chain.PutBack = null; adopted = null; chainOpener = null;
ClearTarget();
Notice(AdvisorSignals.NewGame);
if (session.AutoShow && session.Settings.HasFile && !session.Settings.OpeningSeen
@@ -369,7 +371,9 @@ namespace Statecraft.Game.UI
follow.Pause();
layer.UnregisterCallback(onLaidOut);
var focused = layer.focusController?.focusedElement as VisualElement;
- if (focused != null && layer.Contains(focused)) { focused.Blur(); FocusReturn.To(hudRoot.Q<Button>("end-turn")); }
+ // Back to the tour's opener; End turn when there is none or it cannot take the focus now, and for a queue row the
+ // player reached by Tab (#3028).
+ if (focused != null && layer.Contains(focused)) { focused.Blur(); FocusReturn.ToOrWithin(chainOpener, hudRoot.Q<Button>("end-turn")); }
if (!ScreenNav.Moves()) { hudRoot.RemoveFromClassList(OpenClass); return; }
popup.AddToClassList(ScreenNav.ClosingClass);
fading?.Pause();
@@ -505,6 +509,11 @@ namespace Statecraft.Game.UI
session.Chain.Step = first;
if (hooks.Card?.Shown != null && !hooks.Card.Collapsed) hooks.Card.Collapse(takeFocus: false);
ShowStep(first);
+ // A focus already in the popup keeps the first opener. A focus in a screen, as on the Encyclopedia's button, goes to
+ // that screen's rail button, where CloseScreens (ScreenNav.Close) sends it: from inside that press the focus
+ // controller still names the button in the closed screen, as UI Toolkit applies the change after the press.
+ if (hudRoot.focusController?.focusedElement is VisualElement had && !layer.Contains(had))
+ chainOpener = ScreenNav.ButtonOfScreenAround(hudRoot, had) ?? had;
FocusReturn.To(gotIt); // the player opens it, so Enter goes on (spec 16)
}
@@ -526,6 +535,7 @@ namespace Statecraft.Game.UI
Shown = null; covered = null;
ClearTarget();
Hide();
+ chainOpener = null;
if (restoreCard && hooks.Card?.Shown != null)
{
if (session.Chain.CardWasOpen && hooks.Card.Collapsed) hooks.Card.Reopen();
diff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.cs
index 473ca44ce..c99e8bcbe 100644
--- a/unity/Assets/Scripts/UI/ScreenNav.cs
+++ b/unity/Assets/Scripts/UI/ScreenNav.cs
@@ -313,6 +313,19 @@ namespace Statecraft.Game.UI
return true;
}
+ /// <summary>The top-bar button of the screen that holds <paramref name="e"/>, or null when it is in no screen: where
+ /// <see cref="Close"/> hands a focus that was on the screen's contents. A presenter that closes the screens from a
+ /// press inside one (the Encyclopedia's "Show the opening advice again") asks this rather than the focus controller:
+ /// a focus change made while UI Toolkit is still dispatching that press is applied only after it, so focusedElement
+ /// still names the control in the closed screen (#3028).</summary>
+ public static Button? ButtonOfScreenAround(VisualElement root, VisualElement? e)
+ {
+ for (var v = e; v != null; v = v.parent)
+ foreach (var s in Screens)
+ if (v.name == "screen-" + s) return root.Q<Button>("nav-" + s);
+ return null;
+ }
+
/// <summary>The open screen's name, or null.</summary>
public string? Current { get; private set; }
/// <summary>Raised with <see cref="Current"/> each time a screen opens or closes (TurnReportPresenter: a report that
diff --git a/unity/Assets/Tests/EditMode/AdvisorOpeningTests.cs b/unity/Assets/Tests/EditMode/AdvisorOpeningTests.cs
index 6edf0a68c..a8bad5ffa 100644
--- a/unity/Assets/Tests/EditMode/AdvisorOpeningTests.cs
+++ b/unity/Assets/Tests/EditMode/AdvisorOpeningTests.cs
@@ -440,6 +440,67 @@ public class AdvisorOpeningTests
Assert.AreEqual("Show the opening advice again", rig.Ui.Q<Button>("encyclopedia-opening").text);
}
+ /// <summary>#3028 (spec 16: the popup "gives it back with FocusReturn"): closing the tour gives the focus back to the
+ /// control that opened it. From the Encyclopedia that is its rail button: the screen's own button is in a screen that
+ /// closed, and ScreenNav.Close has already handed the focus to the rail button before the tour opens.</summary>
+ [TestCase(false)] [TestCase(true)]
+ public void ClosingTheTourOpenedFromTheEncyclopediaGivesTheFocusBackToItsButton(bool hebrew)
+ {
+ using var rig = new Rig(hebrew ? He : En, Store(), approved: true);
+ rig.Card.Collapse(takeFocus: false); // a new game opens on a card: out of the way
+ Focusable? Focused() => rig.Ui.Root.focusController.focusedElement;
+ var rail = rig.Ui.Q<Button>("nav-encyclopedia");
+ rail.Focus(); UiTestKit.Press(rail); // Tab to the Encyclopedia and Enter
+ rig.Ui.Q<Button>("encyclopedia-opening").Focus(); rig.Ui.Press("encyclopedia-opening");
+ Assert.IsTrue(rig.Advisor.InChain, "precondition: the tour is open");
+ Assert.IsNull(rig.Nav.Current, "precondition: the Encyclopedia closed");
+ Assert.AreSame(rig.Ui.Q<Button>("advisor-got-it"), Focused(), "precondition: Next has the focus");
+ Assert.IsTrue(rig.Advisor.Escape());
+ Assert.IsFalse(rig.Advisor.InChain);
+ Assert.AreSame(rail, Focused(), "back to the Encyclopedia's button, not End turn");
+ }
+
+ /// <summary>#3028: a tour opened with the focus on any control gives it back there when it closes, by Escape or by the
+ /// X alike, and each tour remembers its own opener.</summary>
+ [TestCase(false)] [TestCase(true)]
+ public void ClosingATourOpenedByHandGivesTheFocusBackToTheOpener(bool hebrew)
+ {
+ using var rig = new Rig(hebrew ? He : En, Store(), approved: true);
+ rig.Card.Collapse(takeFocus: false);
+ Focusable? Focused() => rig.Ui.Root.focusController.focusedElement;
+ var next = rig.Ui.Q<Button>("advisor-got-it");
+ var cabinet = rig.Ui.Q<Button>("nav-cabinet"); var budget = rig.Ui.Q<Button>("nav-budget");
+ cabinet.Focus(); rig.Advisor.OpenChain();
+ Assert.IsTrue(rig.Advisor.InChain, "precondition: the tour is open");
+ Assert.AreSame(next, Focused(), "precondition: Next has the focus");
+ Assert.IsTrue(rig.Advisor.Escape());
+ Assert.AreSame(cabinet, Focused(), "Escape: back to the opener, not End turn");
+ budget.Focus(); rig.Advisor.OpenChain();
+ Assert.AreSame(next, Focused(), "precondition: Next has the focus again");
+ rig.Ui.Press("advisor-close"); // Press moves no focus: it is still on Next
+ Assert.IsFalse(rig.Advisor.InChain);
+ Assert.AreSame(budget, Focused(), "the X: back to this tour's opener");
+ }
+
+ /// <summary>#3028 guard: an opener that cannot take the focus when the tour closes (disabled, or gone from the panel)
+ /// leaves it on End turn, never nowhere.</summary>
+ [TestCase(false, false)] [TestCase(false, true)] [TestCase(true, false)] [TestCase(true, true)]
+ public void AnOpenerThatCannotTakeTheFocusFallsBackToEndTurn(bool hebrew, bool detached)
+ {
+ using var rig = new Rig(hebrew ? He : En, Store(), approved: true);
+ rig.Card.Collapse(takeFocus: false);
+ Focusable? Focused() => rig.Ui.Root.focusController.focusedElement;
+ var opener = rig.Ui.Q<Button>("nav-cabinet");
+ if (detached) { opener = new Button { name = "test-opener" }; rig.Ui.Root.Q("root").Add(opener); }
+ opener.Focus();
+ Assert.AreSame(opener, Focused(), "precondition: the opener has the focus");
+ rig.Advisor.OpenChain();
+ Assert.AreSame(rig.Ui.Q<Button>("advisor-got-it"), Focused(), "precondition: Next has the focus");
+ if (detached) opener.RemoveFromHierarchy(); else opener.SetEnabled(false);
+ Assert.IsTrue(rig.Advisor.Escape());
+ Assert.AreSame(rig.Ui.Q<Button>("end-turn"), Focused(), detached ? "the opener left the panel" : "the opener is disabled");
+ }
+
[Test]
public void WithNoCardTheEventStepIsLeftOut()
{
diff --git a/unity/Assets/Tests/EditMode/AdvisorPopupTests.cs b/unity/Assets/Tests/EditMode/AdvisorPopupTests.cs
index bcbc2135e..b242af589 100644
--- a/unity/Assets/Tests/EditMode/AdvisorPopupTests.cs
+++ b/unity/Assets/Tests/EditMode/AdvisorPopupTests.cs
@@ -237,6 +237,26 @@ public class AdvisorPopupTests
Assert.IsTrue(rig.Session.Settings.Seen("t.teach"), "Escape is the X");
}
+ /// <summary>#3028: a queue row takes no focus, so the player reaches it only by Tab. When it closes with the focus inside,
+ /// the focus goes to End turn, not to the control the player was on before (which only the opening tour gives back).</summary>
+ [TestCase(false)] [TestCase(true)]
+ public void AQueueRowReachedByTabGivesTheFocusToEndTurnWhenItCloses(bool hebrew)
+ {
+ using var rig = !hebrew ? new Rig() : new Rig(catalog: Fixture.Replace("level: new,", "level: new, he_review: approved,"),
+ text: UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.StringsHebrew).text +
+ "\nadvisor.t.teach.text: \"Teach.\"\nadvisor.t.warn.text: \"Warn.\"\nadvisor.t.crit.text: \"Critical.\"\n", "strings.he.yaml"));
+ Focusable? Focused() => rig.Ui.Root.focusController.focusedElement;
+ var cabinet = rig.Ui.Q<Button>("nav-cabinet");
+ cabinet.Focus();
+ rig.Advisor.NewGame();
+ Assert.IsTrue(rig.Advisor.IsOpen, "precondition: the row shows");
+ Assert.AreSame(cabinet, Focused(), "precondition: it took no focus");
+ rig.Ui.Q<Button>("advisor-got-it").Focus(); // as Tab would
+ Assert.IsTrue(rig.Advisor.Escape());
+ Assert.IsFalse(rig.Advisor.IsOpen);
+ Assert.AreSame(rig.Ui.Q<Button>("end-turn"), Focused());
+ }
+
/// <summary>Spec 16: one Escape with the focus in the popup closes the popup only, as in the report panel (#1169).
/// This unit fixture injects ScreenNav's TakesEscape ordering; GameRoot's boot hookup is covered by AdvisorGameTests.
/// The Escape poll asks the popup before it