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) diff --git a/UnitTests/View/MouseTests.cs b/UnitTests/View/MouseTests.cs new file mode 100644 index 000000000..103c19727 --- /dev/null +++ b/UnitTests/View/MouseTests.cs @@ -0,0 +1,37 @@ +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() { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }); + Assert.True (superView.HasFocus); + Assert.Equal (expectedHasFocus, testView.HasFocus); + } + + // TODO: Add more tests that ensure the above test works with positive adornments +} diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index 4b098d1ba..94d9f6068 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 (focusedView, 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 () {