From 8fd8876818773e44d6e4c7e47471c67a2390547f Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 22 Aug 2024 15:31:33 +0100 Subject: [PATCH 1/2] Fixes #3682. CanFocus true for Label works only for keyboard and not mouse. --- Terminal.Gui/Views/Label.cs | 5 ++++ UnitTests/Views/LabelTests.cs | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index 55f174c344..8763e6d974 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -28,6 +28,11 @@ public Label () private void Label_MouseClick (object sender, MouseEventEventArgs e) { + if (CanFocus) + { + return; + } + e.Handled = InvokeCommand (Command.HotKey) == true; } diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index 1c6372b81e..f339be41c9 100644 --- a/UnitTests/Views/LabelTests.cs +++ b/UnitTests/Views/LabelTests.cs @@ -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); + } } From a385ae144c197fc57b81bfd77b4a4511de8923bb Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 22 Aug 2024 15:40:13 +0100 Subject: [PATCH 2/2] Simplifying code. --- Terminal.Gui/Views/Label.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index 8763e6d974..344120ebaf 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -28,12 +28,10 @@ public Label () private void Label_MouseClick (object sender, MouseEventEventArgs e) { - if (CanFocus) + if (!CanFocus) { - return; + e.Handled = InvokeCommand (Command.HotKey) == true; } - - e.Handled = InvokeCommand (Command.HotKey) == true; } private void Label_TitleChanged (object sender, EventArgs e)