Skip to content

Commit dd12df7

Browse files
Copilottig
andauthored
Fixes #4176. Removes View.MouseClick (#4450)
* Initial plan * Add comprehensive MouseClick removal analysis Co-authored-by: tig <[email protected]> * Fix code review feedback on analysis document Co-authored-by: tig <[email protected]> * Add actual line numbers to analysis document Co-authored-by: tig <[email protected]> * Final polish on analysis document - clarify TextView Co-authored-by: tig <[email protected]> * Phase 3: Port 6 simple Views from MouseClick to Selecting/MouseBindings Co-authored-by: tig <[email protected]> * Phase 2: Port 10 Scenarios from MouseClick to Selecting/remove Co-authored-by: tig <[email protected]> * Update analysis document - Phases 2 & 3 complete Co-authored-by: tig <[email protected]> * Fix failing tests - Button multi-button support and Label focus handling Co-authored-by: tig <[email protected]> * Phase 4: Port ScrollBar from OnMouseClick to OnSelecting; remove analysis doc Co-authored-by: tig <[email protected]> * Phase 5: Update/remove tests that explicitly test MouseClick API Co-authored-by: tig <[email protected]> * Code cleanup * Remove MouseClick event Consolidated mouse event handling by removing the `MouseClick` event and integrating its functionality into the `MouseEvent` event. Simplified `MouseEventArgs` documentation and added support for invoking commands bound to mouse events. Reorganized code by removing `Mouse Pressed Events` and `Mouse Click Events` regions, introducing a new `WhenGrabbed Handlers` region. Updated tests to replace `MouseClick` with `MouseEvent`, adjusted test logic, and improved variable naming for clarity. Removed redundant assertions and unused code related to `MouseClick`. Improved event propagation logic to ensure proper handling of unhandled events. Performed general code cleanup to enhance readability and maintainability. * Updated deep dives. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tig <[email protected]> Co-authored-by: Tig <[email protected]>
1 parent 0eafb59 commit dd12df7

File tree

31 files changed

+817
-676
lines changed

31 files changed

+817
-676
lines changed

Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class CharacterMap : Scenario
2525

2626
public override List<Key> GetDemoKeyStrokes ()
2727
{
28-
List<Key> keys = new ();
28+
List<Key> keys = [];
2929

3030
for (var i = 0; i < 200; i++)
3131
{
@@ -91,7 +91,7 @@ public override void Main ()
9191
};
9292
top.Add (jumpEdit);
9393

94-
_charMap.SelectedCodePointChanged += (sender, args) =>
94+
_charMap.SelectedCodePointChanged += (_, args) =>
9595
{
9696
if (Rune.IsValid (args.Value))
9797
{
@@ -134,27 +134,33 @@ public override void Main ()
134134
_categoryList.Table = CreateCategoryTable (0, isDescending);
135135

136136
// if user clicks the mouse in TableView
137-
_categoryList.MouseClick += (s, e) =>
138-
{
139-
_categoryList.ScreenToCell (e.Position, out int? clickedCol);
140-
141-
if (clickedCol != null && e.Flags.HasFlag (MouseFlags.Button1Clicked))
142-
{
143-
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
144-
string prevSelection = table.Data.ElementAt (_categoryList.SelectedRow).Category;
145-
isDescending = !isDescending;
146-
147-
_categoryList.Table = CreateCategoryTable (clickedCol.Value, isDescending);
148-
149-
table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
150-
151-
_categoryList.SelectedRow = table.Data
152-
.Select ((item, index) => new { item, index })
153-
.FirstOrDefault (x => x.item.Category == prevSelection)
154-
?.index
155-
?? -1;
156-
}
157-
};
137+
_categoryList.Selecting += (_, e) =>
138+
{
139+
// Only handle mouse clicks
140+
if (e.Context is not CommandContext<MouseBinding> { Binding.MouseEventArgs: { } mouseArgs })
141+
{
142+
return;
143+
}
144+
145+
_categoryList.ScreenToCell (mouseArgs.Position, out int? clickedCol);
146+
147+
if (clickedCol != null && mouseArgs.Flags.HasFlag (MouseFlags.Button1Clicked))
148+
{
149+
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
150+
string prevSelection = table.Data.ElementAt (_categoryList.SelectedRow).Category;
151+
isDescending = !isDescending;
152+
153+
_categoryList.Table = CreateCategoryTable (clickedCol.Value, isDescending);
154+
155+
table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
156+
157+
_categoryList.SelectedRow = table.Data
158+
.Select ((item, index) => new { item, index })
159+
.FirstOrDefault (x => x.item.Category == prevSelection)
160+
?.index
161+
?? -1;
162+
}
163+
};
158164

159165
int longestName = UnicodeRange.Ranges.Max (r => r.Category.GetColumns ());
160166

@@ -167,7 +173,7 @@ public override void Main ()
167173

168174
_categoryList.Width = _categoryList.Style.ColumnStyles.Sum (c => c.Value.MinWidth) + 4;
169175

170-
_categoryList.SelectedCellChanged += (s, args) =>
176+
_categoryList.SelectedCellChanged += (_, args) =>
171177
{
172178
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
173179
_charMap.StartCodePoint = table.Data.ToArray () [args.NewRow].Start;
@@ -219,7 +225,7 @@ void JumpEditOnAccept (object? sender, CommandEventArgs e)
219225

220226
_errorLabel.Visible = true;
221227

222-
uint result = 0;
228+
uint result;
223229

224230
if (jumpEdit.Text.Length == 1)
225231
{
@@ -283,7 +289,7 @@ void JumpEditOnAccept (object? sender, CommandEventArgs e)
283289
?? -1;
284290
_categoryList.EnsureSelectedCellIsVisible ();
285291

286-
// Ensure the typed glyph is selected
292+
// Ensure the typed glyph is selected
287293
_charMap.SelectedCodePoint = (int)result;
288294
_charMap.SetFocus ();
289295

Examples/UICatalog/Scenarios/ContextMenus.cs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override void Main ()
3535
Application.Run (_appWindow);
3636
_appWindow.Dispose ();
3737
_appWindow.KeyDown -= OnAppWindowOnKeyDown;
38-
_appWindow.MouseClick -= OnAppWindowOnMouseClick;
38+
_appWindow.Selecting -= OnAppWindowOnSelecting;
3939
_winContextMenu?.Dispose ();
4040

4141
// Shutdown - Calling Application.Shutdown is required.
@@ -81,23 +81,26 @@ void AppWindowOnInitialized (object? sender, EventArgs e)
8181
_appWindow.Add (_tfBottomRight);
8282

8383
_appWindow.KeyDown += OnAppWindowOnKeyDown;
84-
_appWindow.MouseClick += OnAppWindowOnMouseClick;
84+
_appWindow.Selecting += OnAppWindowOnSelecting;
8585

8686
CultureInfo originalCulture = Thread.CurrentThread.CurrentUICulture;
87-
_appWindow.IsRunningChanged += (s, e) => {
87+
_appWindow.IsRunningChanged += (_, e) => {
8888
if (!e.Value)
8989
{
9090
Thread.CurrentThread.CurrentUICulture = originalCulture;
9191
} };
9292
}
9393

94-
void OnAppWindowOnMouseClick (object? s, MouseEventArgs e)
94+
void OnAppWindowOnSelecting (object? s, CommandEventArgs e)
9595
{
96-
if (e.Flags == MouseFlags.Button3Clicked)
96+
if (e.Context is CommandContext<MouseBinding> { Binding.MouseEventArgs: { } mouseArgs })
9797
{
98-
// ReSharper disable once AccessToDisposedClosure
99-
_winContextMenu?.MakeVisible (e.ScreenPosition);
100-
e.Handled = true;
98+
if (mouseArgs.Flags == MouseFlags.Button3Clicked)
99+
{
100+
// ReSharper disable once AccessToDisposedClosure
101+
_winContextMenu?.MakeVisible (mouseArgs.ScreenPosition);
102+
e.Handled = true;
103+
}
101104
}
102105
}
103106

@@ -139,7 +142,7 @@ private void CreateWinContextMenu (IApplication? app)
139142
Title = "M_ore options",
140143
SubMenu = new (
141144
[
142-
new MenuItem
145+
new ()
143146
{
144147
Title = "_Setup...",
145148
HelpText = "Perform setup",
@@ -153,7 +156,7 @@ private void CreateWinContextMenu (IApplication? app)
153156
),
154157
Key = Key.T.WithCtrl
155158
},
156-
new MenuItem
159+
new ()
157160
{
158161
Title = "_Maintenance...",
159162
HelpText = "Maintenance mode",
@@ -194,7 +197,7 @@ private Menu GetSupportedCultureMenu ()
194197

195198
if (index == -1)
196199
{
197-
// Create English because GetSupportedCutures doesn't include it
200+
// Create English because GetSupportedCultures doesn't include it
198201
culture.Id = "_English";
199202
culture.Title = "_English";
200203
culture.HelpText = "en-US";
@@ -240,38 +243,31 @@ void CreateAction (List<MenuItem> cultures, MenuItem culture)
240243

241244
public override List<Key> GetDemoKeyStrokes ()
242245
{
243-
List<Key> keys = new ();
244-
245-
keys.Add (Key.F10.WithShift);
246-
keys.Add (Key.Esc);
247-
248-
keys.Add (Key.Space.WithCtrl);
249-
keys.Add (Key.CursorDown);
250-
keys.Add (Key.Enter);
251-
252-
keys.Add (Key.F10.WithShift);
253-
keys.Add (Key.Esc);
254-
255-
keys.Add (Key.Tab);
256-
257-
keys.Add (Key.Space.WithCtrl);
258-
keys.Add (Key.CursorDown);
259-
keys.Add (Key.CursorDown);
260-
keys.Add (Key.Enter);
261-
262-
keys.Add (Key.F10.WithShift);
263-
keys.Add (Key.Esc);
264-
265-
keys.Add (Key.Tab);
266-
267-
keys.Add (Key.Space.WithCtrl);
268-
keys.Add (Key.CursorDown);
269-
keys.Add (Key.CursorDown);
270-
keys.Add (Key.CursorDown);
271-
keys.Add (Key.Enter);
272-
273-
keys.Add (Key.F10.WithShift);
274-
keys.Add (Key.Esc);
246+
List<Key> keys =
247+
[
248+
Key.F10.WithShift,
249+
Key.Esc,
250+
Key.Space.WithCtrl,
251+
Key.CursorDown,
252+
Key.Enter,
253+
Key.F10.WithShift,
254+
Key.Esc,
255+
Key.Tab,
256+
Key.Space.WithCtrl,
257+
Key.CursorDown,
258+
Key.CursorDown,
259+
Key.Enter,
260+
Key.F10.WithShift,
261+
Key.Esc,
262+
Key.Tab,
263+
Key.Space.WithCtrl,
264+
Key.CursorDown,
265+
Key.CursorDown,
266+
Key.CursorDown,
267+
Key.Enter,
268+
Key.F10.WithShift,
269+
Key.Esc
270+
];
275271

276272
return keys;
277273
}

Examples/UICatalog/Scenarios/EditorsAndHelpers/EventLog.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,16 @@ public View? ViewToLog
7373

7474
if (_viewToLog is { })
7575
{
76-
_viewToLog.Initialized += (s, args) =>
76+
_viewToLog.Initialized += (s, _) =>
7777
{
7878
var sender = s as View;
7979
Log ($"Initialized: {GetIdentifyingString (sender)}");
8080
};
8181

82-
_viewToLog.MouseClick += (s, args) => { Log ($"MouseClick: {args}"); };
83-
_viewToLog.MouseWheel += (s, args) => { Log ($"MouseWheel: {args}"); };
84-
_viewToLog.HandlingHotKey += (s, args) => { Log ($"HandlingHotKey: {args.Context}"); };
85-
_viewToLog.Selecting += (s, args) => { Log ($"Selecting: {args.Context}"); };
86-
_viewToLog.Accepting += (s, args) => { Log ($"Accepting: {args.Context}"); };
82+
_viewToLog.MouseWheel += (_, args) => { Log ($"MouseWheel: {args}"); };
83+
_viewToLog.HandlingHotKey += (_, args) => { Log ($"HandlingHotKey: {args.Context}"); };
84+
_viewToLog.Selecting += (_, args) => { Log ($"Selecting: {args.Context}"); };
85+
_viewToLog.Accepting += (_, args) => { Log ($"Accepting: {args.Context}"); };
8786
}
8887
}
8988
}

Examples/UICatalog/Scenarios/ListColumns.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ public class ListColumns : Scenario
1818
private TableView? _listColView;
1919
private CheckBox? _alternatingColorsCheckBox;
2020
private CheckBox? _alwaysUseNormalColorForVerticalCellLinesCheckBox;
21-
private CheckBox? _bottomlineCheckBox;
21+
private CheckBox? _bottomLineCheckBox;
2222
private CheckBox? _cellLinesCheckBox;
2323
private CheckBox? _cursorCheckBox;
2424
private CheckBox? _expandLastColumnCheckBox;
2525
private CheckBox? _orientVerticalCheckBox;
2626
private CheckBox? _scrollParallelCheckBox;
2727
private CheckBox? _smoothScrollingCheckBox;
28-
private CheckBox? _toplineCheckBox;
28+
private CheckBox? _topLineCheckBox;
2929

3030
/// <summary>
31-
/// Builds a simple list in which values are the index. This helps testing that scrolling etc is working
31+
/// Builds a simple list in which values are the index. This helps test that scrolling etc. is working
3232
/// correctly and not skipping out values when paging
3333
/// </summary>
3434
/// <param name="items"></param>
@@ -112,25 +112,22 @@ public override void Main ()
112112
Normal = new (Color.White, Color.BrightBlue)
113113
};
114114

115-
// if user clicks the mouse in TableView
116-
_listColView.MouseClick += (s, e) => { _listColView.ScreenToCell (e.Position, out int? clickedCol); };
117-
118115
_listColView.KeyBindings.ReplaceCommands (Key.Space, Command.Accept);
119116

120117
// Setup menu checkboxes
121-
_toplineCheckBox = new ()
118+
_topLineCheckBox = new ()
122119
{
123120
Title = "_TopLine",
124121
CheckedState = _listColView.Style.ShowHorizontalHeaderOverline ? CheckState.Checked : CheckState.UnChecked
125122
};
126-
_toplineCheckBox.CheckedStateChanged += (s, e) => ToggleTopline ();
123+
_topLineCheckBox.CheckedStateChanged += (s, e) => ToggleTopline ();
127124

128-
_bottomlineCheckBox = new ()
125+
_bottomLineCheckBox = new ()
129126
{
130127
Title = "_BottomLine",
131128
CheckedState = _listColView.Style.ShowHorizontalBottomline ? CheckState.Checked : CheckState.UnChecked
132129
};
133-
_bottomlineCheckBox.CheckedStateChanged += (s, e) => ToggleBottomline ();
130+
_bottomLineCheckBox.CheckedStateChanged += (s, e) => ToggleBottomline ();
134131

135132
_cellLinesCheckBox = new ()
136133
{
@@ -221,11 +218,11 @@ public override void Main ()
221218
[
222219
new MenuItem
223220
{
224-
CommandView = _toplineCheckBox
221+
CommandView = _topLineCheckBox
225222
},
226223
new MenuItem
227224
{
228-
CommandView = _bottomlineCheckBox
225+
CommandView = _bottomLineCheckBox
229226
},
230227
new MenuItem
231228
{
@@ -422,12 +419,12 @@ private void ToggleAlwaysUseNormalColorForVerticalCellLines ()
422419

423420
private void ToggleBottomline ()
424421
{
425-
if (_listColView is null || _bottomlineCheckBox is null)
422+
if (_listColView is null || _bottomLineCheckBox is null)
426423
{
427424
return;
428425
}
429426

430-
_listColView.Style.ShowHorizontalBottomline = _bottomlineCheckBox.CheckedState == CheckState.Checked;
427+
_listColView.Style.ShowHorizontalBottomline = _bottomLineCheckBox.CheckedState == CheckState.Checked;
431428
_listColView.Update ();
432429
}
433430

@@ -490,12 +487,12 @@ private void ToggleSmoothScrolling ()
490487

491488
private void ToggleTopline ()
492489
{
493-
if (_listColView is null || _toplineCheckBox is null)
490+
if (_listColView is null || _topLineCheckBox is null)
494491
{
495492
return;
496493
}
497494

498-
_listColView.Style.ShowHorizontalHeaderOverline = _toplineCheckBox.CheckedState == CheckState.Checked;
495+
_listColView.Style.ShowHorizontalHeaderOverline = _topLineCheckBox.CheckedState == CheckState.Checked;
499496
_listColView.Update ();
500497
}
501498

0 commit comments

Comments
 (0)