Skip to content

Commit 7c00793

Browse files
committed
Fixed OptionSelector focus bug
1 parent ebcf66c commit 7c00793

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

Terminal.Gui/Views/Selectors/OptionSelector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ protected override bool OnHandlingHotKey (CommandEventArgs args)
5757
{
5858
return true;
5959
}
60+
SetFocus ();
6061
Value = Values? [0];
6162
return true;
6263
}

Tests/UnitTests/View/ViewCommandTests.cs

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Terminal.Gui.ViewTests;
55
public class ViewCommandTests
66
{
77
[Fact]
8-
public void Command_Accept_Handled_Stops_Propagation_To_IsDefault_Button_Peer ()
8+
public void Command_Accept_Handled_Stops_Propagation_To_IsDefaultAcceptView_Button_Peer ()
99
{
1010
var acceptOk = 0;
1111
var acceptCancel = 0;
@@ -28,7 +28,7 @@ public void Command_Accept_Handled_Stops_Propagation_To_IsDefault_Button_Peer ()
2828
}
2929

3030
[Fact]
31-
public void Command_Accept_Not_Handled_Propagates_To_IsDefault_Button_Peer ()
31+
public void Command_Accept_Not_Handled_Propagates_To_IsDefaultAcceptView_Button_Peer ()
3232
{
3333
var acceptOk = 0;
3434
var acceptCancel = 0;
@@ -48,9 +48,76 @@ public void Command_Accept_Not_Handled_Propagates_To_IsDefault_Button_Peer ()
4848
Assert.Equal (1, acceptCancel);
4949
}
5050

51+
[Fact]
52+
public void Command_Accept_Handled_Stops_Propagation_To_Not_IsDefaultAcceptView_Button_Peer ()
53+
{
54+
var acceptOk = 0;
55+
var acceptCancel = 0;
56+
Button btnOk = new () { Id = "btnOk", Text = "Ok", IsDefaultAcceptView = false };
57+
btnOk.Accepting += (s, e) => acceptOk++;
58+
Button btnCancel = new () { Id = "btnCancel", Text = "Cancel", IsDefaultAcceptView = false };
59+
60+
btnCancel.Accepting += (s, e) =>
61+
{
62+
acceptCancel++;
63+
e.Handled = true;
64+
};
65+
66+
View superView = new View () { Id = "superView" };
67+
superView.Add (btnOk, btnCancel);
68+
69+
btnCancel.InvokeCommand (Command.Accept);
70+
Assert.Equal (0, acceptOk);
71+
Assert.Equal (1, acceptCancel);
72+
}
73+
74+
[Fact]
75+
public void Command_Accept_Not_Handled_Propagates_To_Not_IsDefaultAcceptView_Button_Peer ()
76+
{
77+
var acceptOk = 0;
78+
var acceptCancel = 0;
79+
Button btnOk = new () { Id = "btnOk", Text = "Ok", IsDefaultAcceptView = false };
80+
btnOk.Accepting += (s, e) => acceptOk++;
81+
Button btnCancel = new () { Id = "btnCancel", Text = "Cancel", IsDefaultAcceptView = false };
82+
83+
btnCancel.Accepting += (s, e) =>
84+
{
85+
acceptCancel++;
86+
};
87+
View superView = new View () { Id = "superView" };
88+
superView.Add (btnOk, btnCancel);
89+
90+
btnCancel.InvokeCommand (Command.Accept);
91+
Assert.Equal (0, acceptOk);
92+
Assert.Equal (1, acceptCancel);
93+
}
94+
95+
[Fact]
96+
[AutoInitShutdown]
97+
public void HotKey_From_Non_IsDefaultAcceptView_Button_Raises_Accept_In_The_Default_Button ()
98+
{
99+
var acceptOk = 0;
100+
var acceptCancel = 0;
101+
Button btnOk = new () { Id = "Ok", Text = "_Ok", IsDefaultAcceptView = true };
102+
btnOk.Accepting += (s, e) => acceptOk++;
103+
Button btnCancel = new () { Id = "Cancel", Y = 1, Text = "_Cancel" };
104+
btnCancel.Accepting += (s, e) => acceptCancel++;
105+
Application.Top = new ();
106+
Application.Top.Add (btnOk, btnCancel);
107+
var rs = Application.Begin (Application.Top);
108+
109+
Application.RaiseKeyDownEvent(Key.C);
110+
Assert.Equal (1, acceptOk);
111+
Assert.Equal (1, acceptCancel);
112+
113+
Application.End (rs);
114+
Application.Top.Dispose ();
115+
Application.ResetState ();
116+
}
117+
51118
// See https://github.com/gui-cs/Terminal.Gui/issues/3913
52119
[Fact]
53-
public void Button_IsDefault_Raises_Accepted_Correctly ()
120+
public void Button_IsDefaultAcceptView_Raises_Accepted_Correctly ()
54121
{
55122
var aAcceptedCount = 0;
56123
var aCancelAccepting = false;

Tests/UnitTestsParallelizable/Views/OptionSelectorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void HotKey_No_SelectedItem_Selects_First ()
130130

131131
var selector = new OptionSelector
132132
{
133-
Title = "Radio_Group",
133+
HotKey = Key.G.WithAlt,
134134
RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"]
135135
};
136136
selector.SelectedItem = -1;
@@ -143,7 +143,7 @@ public void HotKey_No_SelectedItem_Selects_First ()
143143
selector.NewKeyDownEvent (Key.G.WithAlt);
144144

145145
Assert.Equal (0, selector.SelectedItem);
146-
Assert.True (selector.HasFocus);
146+
Assert.Equal (selector.SubViews.OfType<CheckBox> ().First (), superView.MostFocused);
147147
}
148148

149149
[Fact]

0 commit comments

Comments
 (0)