Merge pull request #3311 from tig/v2_3310-OnMouseClick-CanSetFocus

This commit is contained in:
Tig
2024-03-10 10:52:54 -06:00
committed by GitHub
4 changed files with 64 additions and 1 deletions

View File

@@ -146,6 +146,11 @@ public partial class View
return true;
}
if (!HasFocus && CanFocus)
{
SetFocus ();
}
return args.Handled;
}
}

View File

@@ -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<string> e)

View File

@@ -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
}

View File

@@ -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 ()
{