@@ -10,7 +10,7 @@ public void Accept_Command_Raises_NoFocus ()
10
10
var view = new ViewEventTester ( ) ;
11
11
Assert . False ( view . HasFocus ) ;
12
12
13
- Assert . False ( view . InvokeCommand ( Command . Accept ) ) ; // false means it was not handled
13
+ Assert . False ( view . InvokeCommand ( Command . Accept ) ) ; // there's no superview, so it should return true?
14
14
15
15
Assert . Equal ( 1 , view . OnAcceptedCount ) ;
16
16
@@ -124,6 +124,148 @@ public void MouseClick_Does_Not_Invoke_Accept_Command ()
124
124
Assert . Equal ( 0 , view . OnAcceptedCount ) ;
125
125
}
126
126
127
+ // See https://github.com/gui-cs/Terminal.Gui/issues/3913
128
+ [ Fact ]
129
+ public void Button_IsDefault_Raises_Accepted_Correctly ( )
130
+ {
131
+ int A_AcceptedCount = 0 ;
132
+ bool A_CancelAccepting = false ;
133
+
134
+ int B_AcceptedCount = 0 ;
135
+ bool B_CancelAccepting = false ;
136
+
137
+ var w = new Window ( )
138
+ {
139
+ BorderStyle = LineStyle . None ,
140
+ Width = 10 ,
141
+ Height = 10
142
+ } ;
143
+
144
+ var btnA = new Button ( )
145
+ {
146
+ Width = 3 ,
147
+ IsDefault = true
148
+ } ;
149
+ btnA . Accepting += ( s , e ) =>
150
+ {
151
+ A_AcceptedCount ++ ;
152
+ e . Cancel = A_CancelAccepting ;
153
+ } ;
154
+
155
+ var btnB = new Button ( )
156
+ {
157
+ Width = 3 ,
158
+ X = Pos . Right ( btnA )
159
+ } ;
160
+
161
+ btnB . Accepting += ( s , e ) =>
162
+ {
163
+ B_AcceptedCount ++ ;
164
+ e . Cancel = B_CancelAccepting ;
165
+ } ;
166
+ w . Add ( btnA , btnB ) ;
167
+
168
+ w . LayoutSubviews ( ) ;
169
+
170
+ Application . Begin ( w ) ;
171
+ Assert . Same ( Application . Top , w ) ;
172
+
173
+ // Click button 2
174
+ var btn2Frame = btnB . FrameToScreen ( ) ;
175
+
176
+ Application . RaiseMouseEvent (
177
+ new MouseEventArgs ( )
178
+ {
179
+ ScreenPosition = btn2Frame . Location ,
180
+ Flags = MouseFlags . Button1Clicked
181
+ } ) ;
182
+
183
+ // Button A should have been accepted because B didn't cancel and A IsDefault
184
+ Assert . Equal ( 1 , A_AcceptedCount ) ;
185
+ Assert . Equal ( 1 , B_AcceptedCount ) ;
186
+
187
+ B_CancelAccepting = true ;
188
+ Application . RaiseMouseEvent (
189
+ new MouseEventArgs ( )
190
+ {
191
+ ScreenPosition = btn2Frame . Location ,
192
+ Flags = MouseFlags . Button1Clicked
193
+ } ) ;
194
+
195
+ // Button A (IsDefault) should NOT have been accepted because B canceled
196
+ Assert . Equal ( 1 , A_AcceptedCount ) ;
197
+ Assert . Equal ( 2 , B_AcceptedCount ) ;
198
+ }
199
+
200
+ // See: https://github.com/gui-cs/Terminal.Gui/issues/3905
201
+ [ Fact ]
202
+ public void Button_CanFocus_False_Raises_Accepted_Correctly ( )
203
+ {
204
+ int wAcceptedCount = 0 ;
205
+ bool wCancelAccepting = false ;
206
+ var w = new Window ( )
207
+ {
208
+ Title = "Window" ,
209
+ BorderStyle = LineStyle . None ,
210
+ Width = 10 ,
211
+ Height = 10
212
+ } ;
213
+
214
+ w . Accepting += ( s , e ) =>
215
+ {
216
+ wAcceptedCount ++ ;
217
+ e . Cancel = wCancelAccepting ;
218
+ } ;
219
+
220
+ int btnAcceptedCount = 0 ;
221
+ bool btnCancelAccepting = false ;
222
+ var btn = new Button ( )
223
+ {
224
+ Title = "Button" ,
225
+ Width = 3 ,
226
+ IsDefault = true ,
227
+ } ;
228
+ btn . CanFocus = true ;
229
+
230
+ btn . Accepting += ( s , e ) =>
231
+ {
232
+ btnAcceptedCount ++ ;
233
+ e . Cancel = btnCancelAccepting ;
234
+ } ;
235
+
236
+ w . Add ( btn ) ;
237
+
238
+ w . LayoutSubviews ( ) ;
239
+
240
+ Application . Begin ( w ) ;
241
+
242
+ // Click button just like a driver would
243
+ var btnFrame = btn . FrameToScreen ( ) ;
244
+ Application . RaiseMouseEvent (
245
+ new MouseEventArgs ( )
246
+ {
247
+ ScreenPosition = btnFrame . Location ,
248
+ Flags = MouseFlags . Button1Pressed
249
+ } ) ;
250
+
251
+ Application . RaiseMouseEvent (
252
+ new MouseEventArgs ( )
253
+ {
254
+ ScreenPosition = btnFrame . Location ,
255
+ Flags = MouseFlags . Button1Released
256
+ } ) ;
257
+
258
+ Application . RaiseMouseEvent (
259
+ new MouseEventArgs ( )
260
+ {
261
+ ScreenPosition = btnFrame . Location ,
262
+ Flags = MouseFlags . Button1Clicked
263
+ } ) ;
264
+
265
+ Assert . Equal ( 1 , btnAcceptedCount ) ;
266
+ Assert . Equal ( 2 , wAcceptedCount ) ;
267
+ }
268
+
127
269
#endregion OnAccept/Accept tests
128
270
129
271
#region OnSelect/Select tests
@@ -140,7 +282,7 @@ public void Select_Command_Raises_SetsFocus (bool canFocus)
140
282
Assert . Equal ( canFocus , view . CanFocus ) ;
141
283
Assert . False ( view . HasFocus ) ;
142
284
143
- Assert . Equal ( canFocus , view . InvokeCommand ( Command . Select ) ) ;
285
+ view . InvokeCommand ( Command . Select ) ;
144
286
145
287
Assert . Equal ( 1 , view . OnSelectingCount ) ;
146
288
0 commit comments