From 348c0dc199519c3a419d18a49dcf864b987f3b29 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 10 Mar 2024 07:09:56 -0800 Subject: [PATCH 1/4] View.OnMouseClick now Can/Setfocus. Label now uses Command.HotKey on MouseClick --- Terminal.Gui/View/ViewMouse.cs | 5 +++++ Terminal.Gui/Views/Label.cs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/View/ViewMouse.cs b/Terminal.Gui/View/ViewMouse.cs index 2ab4d54ba..3e13f2647 100644 --- a/Terminal.Gui/View/ViewMouse.cs +++ b/Terminal.Gui/View/ViewMouse.cs @@ -146,6 +146,11 @@ public partial class View return true; } + if (!HasFocus && CanFocus) + { + SetFocus (); + } + return args.Handled; } } diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index e4e6b5ac9..7b7b331c1 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -30,7 +30,7 @@ public class Label : View private void Label_MouseClick (object sender, MouseEventEventArgs e) { - e.Handled = InvokeCommand (Command.Accept) == true; + e.Handled = InvokeCommand (Command.HotKey) == true; } private void Label_TitleChanged (object sender, StateEventArgs e) From dabb8df0ac28478899d565f4d4ece298caec8993 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 10 Mar 2024 07:16:41 -0800 Subject: [PATCH 2/4] Adds Label mouse click test --- UnitTests/Views/LabelTests.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index 4b098d1ba..1ebcd4121 100644 --- a/UnitTests/Views/LabelTests.cs +++ b/UnitTests/Views/LabelTests.cs @@ -51,6 +51,27 @@ public class LabelTests Assert.True (nextSubview.HasFocus); } + + [Fact] + public void MouseClick_SetsFocus_OnNextSubview () + { + var superView = new View () { CanFocus = true, Height = 1, Width = 15}; + var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 }; + var label = new Label () { X = 2, Title = "_x" }; + var nextSubview = new View () { CanFocus = true, X = 4, Width = 4, Height = 1 }; + superView.Add (label, nextSubview); + superView.BeginInit (); + superView.EndInit (); + + Assert.False (focusedView.HasFocus); + Assert.False (label.HasFocus); + Assert.False (nextSubview.HasFocus); + + label.OnMouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }); + Assert.False (label.HasFocus); + Assert.True (nextSubview.HasFocus); + } + [Fact] public void HotKey_Command_Does_Not_Accept () { From 7d6d5280f187e7829bb52ed805850cc9f0272303 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 10 Mar 2024 07:30:42 -0800 Subject: [PATCH 3/4] Adds CanFocus/SetFocus test --- UnitTests/View/MouseTests.cs | 36 +++++++++++++++++++++++++++++++++++ UnitTests/Views/LabelTests.cs | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 UnitTests/View/MouseTests.cs diff --git a/UnitTests/View/MouseTests.cs b/UnitTests/View/MouseTests.cs new file mode 100644 index 000000000..f5850c1d3 --- /dev/null +++ b/UnitTests/View/MouseTests.cs @@ -0,0 +1,36 @@ +using Xunit.Abstractions; + +namespace Terminal.Gui.ViewTests; + +public class MouseTests (ITestOutputHelper output) +{ + + [Theory] + [InlineData(false, false, false)] + [InlineData (true, false, true)] + [InlineData (true, true, true)] + public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool expectedHasFocus) + { + var superView = new View () { CanFocus = true, Height = 1, Width = 15 }; + var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 }; + var testView = new View () { CanFocus = canFocus, X = 4, Width = 4, Height = 1 }; + superView.Add (focusedView, testView); + superView.BeginInit (); + superView.EndInit (); + + focusedView.SetFocus(); + + Assert.True (superView.HasFocus); + Assert.True (focusedView.HasFocus); + Assert.False (testView.HasFocus); + + if (setFocus) + { + testView.SetFocus (); + } + + testView.OnMouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }); + Assert.True (superView.HasFocus); + Assert.Equal(expectedHasFocus, testView.HasFocus); + } +} diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index 1ebcd4121..94d9f6068 100644 --- a/UnitTests/Views/LabelTests.cs +++ b/UnitTests/Views/LabelTests.cs @@ -59,7 +59,7 @@ public class LabelTests var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 }; var label = new Label () { X = 2, Title = "_x" }; var nextSubview = new View () { CanFocus = true, X = 4, Width = 4, Height = 1 }; - superView.Add (label, nextSubview); + superView.Add (focusedView, label, nextSubview); superView.BeginInit (); superView.EndInit (); From 0649cd2149d35129cb11d4f500abf1477ec437c9 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 10 Mar 2024 07:32:42 -0800 Subject: [PATCH 4/4] Adds CanFocus/SetFocus test --- UnitTests/View/MouseTests.cs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/UnitTests/View/MouseTests.cs b/UnitTests/View/MouseTests.cs index f5850c1d3..103c19727 100644 --- a/UnitTests/View/MouseTests.cs +++ b/UnitTests/View/MouseTests.cs @@ -4,21 +4,20 @@ namespace Terminal.Gui.ViewTests; public class MouseTests (ITestOutputHelper output) { - [Theory] - [InlineData(false, false, false)] + [InlineData (false, false, false)] [InlineData (true, false, true)] [InlineData (true, true, true)] public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool expectedHasFocus) { - var superView = new View () { CanFocus = true, Height = 1, Width = 15 }; - var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 }; - var testView = new View () { CanFocus = canFocus, X = 4, Width = 4, Height = 1 }; + var superView = new View { CanFocus = true, Height = 1, Width = 15 }; + var focusedView = new View { CanFocus = true, Width = 1, Height = 1 }; + var testView = new View { CanFocus = canFocus, X = 4, Width = 4, Height = 1 }; superView.Add (focusedView, testView); superView.BeginInit (); - superView.EndInit (); - - focusedView.SetFocus(); + superView.EndInit (); + + focusedView.SetFocus (); Assert.True (superView.HasFocus); Assert.True (focusedView.HasFocus); @@ -29,8 +28,10 @@ public class MouseTests (ITestOutputHelper output) testView.SetFocus (); } - testView.OnMouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }); + testView.OnMouseEvent (new() { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }); Assert.True (superView.HasFocus); - Assert.Equal(expectedHasFocus, testView.HasFocus); + Assert.Equal (expectedHasFocus, testView.HasFocus); } + + // TODO: Add more tests that ensure the above test works with positive adornments }