Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3682. CanFocus true for Label works only for keyboard and not mouse. #3683

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Terminal.Gui/Views/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public Label ()

private void Label_MouseClick (object sender, MouseEventEventArgs e)
{
e.Handled = InvokeCommand (Command.HotKey) == true;
if (!CanFocus)
{
e.Handled = InvokeCommand (Command.HotKey) == true;
}
}

private void Label_TitleChanged (object sender, EventArgs<string> e)
Expand Down
45 changes: 45 additions & 0 deletions UnitTests/Views/LabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,4 +1315,49 @@ public void Label_ResizeView_With_Dim_Absolute ()
Assert.Equal (expectedLabelBounds, label.Viewport);
super.Dispose ();
}

[Fact]
[AutoInitShutdown]
public void Label_CanFocus_True_Get_Focus_By_Keyboard_And_Mouse ()
{
Label label = new () { Text = "label" };
View view = new () { Y = 2, Width = 10, Height = 1, Text = "view", CanFocus = true };
Toplevel top = new ();
top.Add (label, view);
Application.Begin (top);

Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (0, 2, 10, 1), view.Frame);
Assert.Equal (view, top.MostFocused);
Assert.False (label.CanFocus);
Assert.False (label.HasFocus);
Assert.True (view.CanFocus);
Assert.True (view.HasFocus);

Assert.True (Application.OnKeyDown (Key.Tab));
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);

Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);

// Set label CanFocus to true
label.CanFocus = true;
Assert.True (Application.OnKeyDown (Key.Tab));
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);

Assert.True (Application.OnKeyDown (Key.Tab));
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);

Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);

Application.OnMouseEvent (new () { Position = new (0, 2), Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);
}
}
Loading