From f3148481c80076499032f42ddaca18952d05b28c Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 19 Sep 2024 14:09:26 -0600 Subject: [PATCH 01/75] Added Applicaton.Popover. Refactored FindDeepestView --- Terminal.Gui/Application/Application.Mouse.cs | 2 +- .../Application/Application.Popover.cs | 36 +++++ Terminal.Gui/Application/Application.Run.cs | 7 + Terminal.Gui/View/View.Layout.cs | 14 +- .../View/Adornment/AdornmentSubViewTests.cs | 21 +-- UnitTests/View/FindDeepestViewTests.cs | 134 ++++++++++------- UnitTests/View/Layout/Pos.CombineTests.cs | 15 +- UnitTests/View/Layout/ToScreenTests.cs | 141 +++++++++--------- 8 files changed, 228 insertions(+), 142 deletions(-) create mode 100644 Terminal.Gui/Application/Application.Popover.cs diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 9da49e6523..5285d69bec 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -139,7 +139,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } - var view = View.FindDeepestView (Top, mouseEvent.Position); + var view = View.FindDeepestView (mouseEvent.Position); if (view is { }) { diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs new file mode 100644 index 0000000000..8b5689e74f --- /dev/null +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -0,0 +1,36 @@ +#nullable enable +namespace Terminal.Gui; + +public static partial class Application // Popover handling +{ + /// The that is currently active as the sole-Application Popover. + /// The Popover. + public static View? Popover { get; internal set; } + public static bool ShowPopover (View popoverView) + { + if (Popover is { }) + { + Popover.Visible = false; + } + + if (!popoverView.IsInitialized) + { + popoverView.BeginInit (); + popoverView.EndInit (); + } + + Popover = popoverView; + Popover.Visible = true; + Popover.SetRelativeLayout (Screen.Size); + + return true; + } + + public static void HidePopover () + { + if (Popover is { }) + { + Popover.Visible = false; + } + } +} diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 4a1d89541f..85b43ca7b3 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -464,6 +464,13 @@ public static void Refresh () tl.Draw (); } + if (Popover is { LayoutNeeded: true }) + { + Popover.LayoutSubviews (); + } + + Popover?.Draw (); + Driver!.Refresh (); } diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 42fd1c31f3..b49ca949a9 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -12,22 +12,28 @@ public partial class View // Layout APIs /// if the specified SuperView-relative coordinates are within the View. public virtual bool Contains (in Point location) { return Frame.Contains (location); } - /// Finds the first Subview of that is visible at the provided location. + /// Finds the first Subview of or that is visible at the provided location. /// /// /// Used to determine what view the mouse is over. /// /// - /// The view to scope the search by. - /// .SuperView-relative coordinate. + /// Screen-relative coordinate. /// /// The view that was found at the coordinate. /// if no view was found. /// // CONCURRENCY: This method is not thread-safe. Undefined behavior and likely program crashes are exposed by unsynchronized access to InternalSubviews. - internal static View? FindDeepestView (View? start, in Point location) + internal static View? FindDeepestView (in Point location) { + View? start = Application.Top; + + if (Application.Popover?.Visible == true) + { + start = Application.Popover; + } + Point currentLocation = location; while (start is { Visible: true } && start.Contains (currentLocation)) diff --git a/UnitTests/View/Adornment/AdornmentSubViewTests.cs b/UnitTests/View/Adornment/AdornmentSubViewTests.cs index 188706a176..232bc0e713 100644 --- a/UnitTests/View/Adornment/AdornmentSubViewTests.cs +++ b/UnitTests/View/Adornment/AdornmentSubViewTests.cs @@ -14,12 +14,12 @@ public class AdornmentSubViewTests (ITestOutputHelper output) [InlineData (2, 1, true)] public void Adornment_WithSubView_FindDeepestView_Finds (int viewMargin, int subViewMargin, bool expectedFound) { - var view = new View () + Application.Top = new Toplevel() { Width = 10, Height = 10 }; - view.Margin.Thickness = new Thickness (viewMargin); + Application.Top.Margin.Thickness = new Thickness (viewMargin); var subView = new View () { @@ -29,24 +29,25 @@ public void Adornment_WithSubView_FindDeepestView_Finds (int viewMargin, int sub Height = 5 }; subView.Margin.Thickness = new Thickness (subViewMargin); - view.Margin.Add (subView); + Application.Top.Margin.Add (subView); - var foundView = View.FindDeepestView (view, new (0, 0)); + var foundView = View.FindDeepestView (new (0, 0)); bool found = foundView == subView || foundView == subView.Margin; Assert.Equal (expectedFound, found); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Fact] public void Adornment_WithNonVisibleSubView_FindDeepestView_Finds_Adornment () { - var view = new View () + Application.Top = new Toplevel () { Width = 10, Height = 10 - }; - view.Padding.Thickness = new Thickness (1); + Application.Top.Padding.Thickness = new Thickness (1); var subView = new View () { @@ -56,9 +57,11 @@ public void Adornment_WithNonVisibleSubView_FindDeepestView_Finds_Adornment () Height = 1, Visible = false }; - view.Padding.Add (subView); + Application.Top.Padding.Add (subView); - Assert.Equal (view.Padding, View.FindDeepestView (view, new (0, 0))); + Assert.Equal (Application.Top.Padding, View.FindDeepestView (new (0, 0))); + Application.Top?.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Fact] diff --git a/UnitTests/View/FindDeepestViewTests.cs b/UnitTests/View/FindDeepestViewTests.cs index c9f53d9a3a..605e26c1d0 100644 --- a/UnitTests/View/FindDeepestViewTests.cs +++ b/UnitTests/View/FindDeepestViewTests.cs @@ -111,12 +111,14 @@ public void Contains (int frameX, int frameY, int marginThickness, int borderThi [InlineData (2, 2)] public void Returns_Start_If_No_SubViews (int testX, int testY) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; - Assert.Same (start, View.FindDeepestView (start, new (testX, testY))); + Assert.Same (Application.Top, View.FindDeepestView (new (testX, testY))); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view @@ -126,13 +128,15 @@ public void Returns_Start_If_No_SubViews (int testX, int testY) [InlineData (20, 20)] public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY) { - var start = new View () + Application.Top = new () { X = 1, Y = 2, Width = 10, Height = 10, }; - Assert.Null (View.FindDeepestView (start, new (testX, testY))); + Assert.Null (View.FindDeepestView (new (testX, testY))); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Theory] @@ -141,14 +145,16 @@ public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY) [InlineData (20, 20)] public void Returns_Null_If_Start_Not_Visible (int testX, int testY) { - var start = new View () + Application.Top = new () { X = 1, Y = 2, Width = 10, Height = 10, Visible = false, }; - Assert.Null (View.FindDeepestView (start, new (testX, testY))); + Assert.Null (View.FindDeepestView (new (testX, testY))); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView returns the correct view if the start view has subviews @@ -163,7 +169,7 @@ public void Returns_Null_If_Start_Not_Visible (int testX, int testY) [InlineData (5, 6, true)] public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; @@ -173,11 +179,13 @@ public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubV X = 1, Y = 2, Width = 5, Height = 5, }; - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Theory] @@ -190,7 +198,7 @@ public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubV [InlineData (5, 6, false)] public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; @@ -201,11 +209,13 @@ public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expec Width = 5, Height = 5, Visible = false }; - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } @@ -219,7 +229,7 @@ public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expec [InlineData (5, 6, false)] public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, Visible = false @@ -230,13 +240,15 @@ public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int test X = 1, Y = 2, Width = 5, Height = 5, }; - start.Add (subview); + Application.Top.Add (subview); subview.Visible = true; Assert.True (subview.Visible); - Assert.False (start.Visible); - var found = View.FindDeepestView (start, new (testX, testY)); + Assert.False (Application.Top.Visible); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView works if the start view has positive Adornments @@ -253,22 +265,24 @@ public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int test [InlineData (6, 7, true)] public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; - start.Margin.Thickness = new Thickness (1); + Application.Top.Margin.Thickness = new Thickness (1); var subview = new View () { X = 1, Y = 2, Width = 5, Height = 5, }; - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView works if the start view has offset Viewport location @@ -283,23 +297,25 @@ public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool [InlineData (-1, 0, 0, false)] public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, ViewportSettings = ViewportSettings.AllowNegativeLocation }; - start.Viewport = new (offset, offset, 10, 10); + Application.Top.Viewport = new (offset, offset, 10, 10); var subview = new View () { X = 1, Y = 1, Width = 2, Height = 2, }; - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Theory] @@ -314,24 +330,26 @@ public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, [InlineData (6, 7, false)] public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; - start.Padding.Thickness = new Thickness (1); + Application.Top.Padding.Thickness = new Thickness (1); var subview = new View () { X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1), Width = 1, Height = 1, }; - start.Padding.Add (subview); - start.BeginInit(); - start.EndInit(); + Application.Top.Padding.Add (subview); + Application.Top.BeginInit(); + Application.Top.EndInit(); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } @@ -345,26 +363,28 @@ public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int t [InlineData (2, 2, typeof (Padding))] [InlineData (7, 7, typeof (Padding))] - [InlineData (5, 5, typeof (View))] + [InlineData (5, 5, typeof (Toplevel))] public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; - start.Margin.Thickness = new Thickness (1); - start.Border.Thickness = new Thickness (1); - start.Padding.Thickness = new Thickness (1); + Application.Top.Margin.Thickness = new Thickness (1); + Application.Top.Border.Thickness = new Thickness (1); + Application.Top.Padding.Thickness = new Thickness (1); var subview = new View () { X = 1, Y = 1, Width = 1, Height = 1, }; - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedAdornmentType, found!.GetType ()); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView works if the subview has positive Adornments @@ -381,7 +401,7 @@ public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Typ [InlineData (2, 3, true)] public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; @@ -392,11 +412,13 @@ public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, boo Width = 5, Height = 5, }; subview.Margin.Thickness = new Thickness (1); - start.Add (subview); + Application.Top.Add (subview); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == subview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Theory] @@ -412,7 +434,7 @@ public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, boo [InlineData (5, 5, true)] public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; @@ -435,13 +457,15 @@ public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int Height = 1, }; subview.Padding.Add (paddingSubview); - start.Add (subview); - start.BeginInit(); - start.EndInit(); + Application.Top.Add (subview); + Application.Top.BeginInit(); + Application.Top.EndInit(); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == paddingSubview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Theory] @@ -457,7 +481,7 @@ public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int [InlineData (5, 5, true)] public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10, }; @@ -484,13 +508,15 @@ public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview Height = 1, }; subview.Padding.Add (paddingSubview); - start.Add (subview); - start.BeginInit (); - start.EndInit (); + Application.Top.Add (subview); + Application.Top.BeginInit (); + Application.Top.EndInit (); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, found == paddingSubview); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } // Test that FindDeepestView works with nested subviews @@ -506,7 +532,7 @@ public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview [InlineData (5, 5, 2)] public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound) { - var start = new View () + Application.Top = new () { Width = 10, Height = 10 }; @@ -528,9 +554,11 @@ public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expec } } - start.Add (subviews [0]); + Application.Top.Add (subviews [0]); - var found = View.FindDeepestView (start, new (testX, testY)); + var found = View.FindDeepestView (new (testX, testY)); Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!)); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } } diff --git a/UnitTests/View/Layout/Pos.CombineTests.cs b/UnitTests/View/Layout/Pos.CombineTests.cs index 5f6ebc2909..b3dfdf1983 100644 --- a/UnitTests/View/Layout/Pos.CombineTests.cs +++ b/UnitTests/View/Layout/Pos.CombineTests.cs @@ -66,7 +66,7 @@ public void PosCombine_Will_Throws () [SetupFakeDriver] public void PosCombine_DimCombine_View_With_SubViews () { - Toplevel top = new Toplevel () { Width = 80, Height = 25 }; + Application.Top = new Toplevel () { Width = 80, Height = 25 }; var win1 = new Window { Id = "win1", Width = 20, Height = 10 }; var view1 = new View { @@ -85,18 +85,21 @@ public void PosCombine_DimCombine_View_With_SubViews () view2.Add (view3); win2.Add (view2); win1.Add (view1, win2); - top.Add (win1); - top.BeginInit (); - top.EndInit (); + Application.Top.Add (win1); + Application.Top.BeginInit (); + Application.Top.EndInit (); - Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame); + Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame); Assert.Equal (new Rectangle (0, 0, 5, 1), view1.Frame); Assert.Equal (new Rectangle (0, 0, 20, 10), win1.Frame); Assert.Equal (new Rectangle (0, 2, 10, 3), win2.Frame); Assert.Equal (new Rectangle (0, 0, 8, 1), view2.Frame); Assert.Equal (new Rectangle (0, 0, 7, 1), view3.Frame); - var foundView = View.FindDeepestView (top, new (9, 4)); + var foundView = View.FindDeepestView (new (9, 4)); Assert.Equal (foundView, view2); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); + } [Fact] diff --git a/UnitTests/View/Layout/ToScreenTests.cs b/UnitTests/View/Layout/ToScreenTests.cs index f78f0fe20d..bc20a6ee73 100644 --- a/UnitTests/View/Layout/ToScreenTests.cs +++ b/UnitTests/View/Layout/ToScreenTests.cs @@ -1,4 +1,5 @@ using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; namespace Terminal.Gui.LayoutTests; @@ -946,8 +947,8 @@ public void ViewportToScreen_Positive_NestedSuperView_WithAdornments (int frameX [AutoInitShutdown] public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top () { - Toplevel top = new (); - top.BorderStyle = LineStyle.Single; + Application.Top = new (); + Application.Top.BorderStyle = LineStyle.Single; var view = new View { @@ -957,17 +958,17 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top () Height = 1, Text = "0123456789" }; - top.Add (view); + Application.Top.Add (view); - Application.Begin (top); + Application.Begin (Application.Top); Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows)); - Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame); - Assert.Equal (new (0, 0, 80, 25), top.Frame); + Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame); + Assert.Equal (new (0, 0, 80, 25), Application.Top.Frame); ((FakeDriver)Application.Driver!).SetBufferSize (20, 10); - Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame); - Assert.Equal (new (0, 0, 20, 10), top.Frame); + Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame); + Assert.Equal (new (0, 0, 20, 10), Application.Top.Frame); _ = TestHelpers.AssertDriverContentsWithFrameAre ( @" @@ -986,64 +987,64 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top () ); // top - Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0))); - Point screen = top.Margin.ViewportToScreen (new Point (0, 0)); + Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (0, 0))); + Point screen = Application.Top.Margin.ViewportToScreen (new Point (0, 0)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - screen = top.Border.ViewportToScreen (new Point (0, 0)); + screen = Application.Top.Border.ViewportToScreen (new Point (0, 0)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - screen = top.Padding.ViewportToScreen (new Point (0, 0)); + screen = Application.Top.Padding.ViewportToScreen (new Point (0, 0)); Assert.Equal (1, screen.X); Assert.Equal (1, screen.Y); - screen = top.ViewportToScreen (new Point (0, 0)); + screen = Application.Top.ViewportToScreen (new Point (0, 0)); Assert.Equal (1, screen.X); Assert.Equal (1, screen.Y); - screen = top.ViewportToScreen (new Point (-1, -1)); + screen = Application.Top.ViewportToScreen (new Point (-1, -1)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - var found = View.FindDeepestView (top, new (0, 0)); - Assert.Equal (top.Border, found); + var found = View.FindDeepestView (new (0, 0)); + Assert.Equal (Application.Top.Border, found); Assert.Equal (0, found.Frame.X); Assert.Equal (0, found.Frame.Y); - Assert.Equal (new (3, 2), top.ScreenToFrame (new (3, 2))); - screen = top.ViewportToScreen (new Point (3, 2)); + Assert.Equal (new (3, 2), Application.Top.ScreenToFrame (new (3, 2))); + screen = Application.Top.ViewportToScreen (new Point (3, 2)); Assert.Equal (4, screen.X); Assert.Equal (3, screen.Y); - found = View.FindDeepestView (top, new (screen.X, screen.Y)); + found = View.FindDeepestView (new (screen.X, screen.Y)); Assert.Equal (view, found); //Assert.Equal (0, found.FrameToScreen ().X); //Assert.Equal (0, found.FrameToScreen ().Y); - found = View.FindDeepestView (top, new (3, 2)); - Assert.Equal (top, found); + found = View.FindDeepestView (new (3, 2)); + Assert.Equal (Application.Top, found); //Assert.Equal (3, found.FrameToScreen ().X); //Assert.Equal (2, found.FrameToScreen ().Y); - Assert.Equal (new (13, 2), top.ScreenToFrame (new (13, 2))); - screen = top.ViewportToScreen (new Point (12, 2)); + Assert.Equal (new (13, 2), Application.Top.ScreenToFrame (new (13, 2))); + screen = Application.Top.ViewportToScreen (new Point (12, 2)); Assert.Equal (13, screen.X); Assert.Equal (3, screen.Y); - found = View.FindDeepestView (top, new (screen.X, screen.Y)); + found = View.FindDeepestView (new (screen.X, screen.Y)); Assert.Equal (view, found); //Assert.Equal (9, found.FrameToScreen ().X); //Assert.Equal (0, found.FrameToScreen ().Y); - screen = top.ViewportToScreen (new Point (13, 2)); + screen = Application.Top.ViewportToScreen (new Point (13, 2)); Assert.Equal (14, screen.X); Assert.Equal (3, screen.Y); - found = View.FindDeepestView (top, new (13, 2)); - Assert.Equal (top, found); + found = View.FindDeepestView (new (13, 2)); + Assert.Equal (Application.Top, found); //Assert.Equal (13, found.FrameToScreen ().X); //Assert.Equal (2, found.FrameToScreen ().Y); - Assert.Equal (new (14, 3), top.ScreenToFrame (new (14, 3))); - screen = top.ViewportToScreen (new Point (14, 3)); + Assert.Equal (new (14, 3), Application.Top.ScreenToFrame (new (14, 3))); + screen = Application.Top.ViewportToScreen (new Point (14, 3)); Assert.Equal (15, screen.X); Assert.Equal (4, screen.Y); - found = View.FindDeepestView (top, new (14, 3)); - Assert.Equal (top, found); + found = View.FindDeepestView (new (14, 3)); + Assert.Equal (Application.Top, found); //Assert.Equal (14, found.FrameToScreen ().X); //Assert.Equal (3, found.FrameToScreen ().Y); @@ -1065,37 +1066,39 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top () screen = view.ViewportToScreen (new Point (-4, -3)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - found = View.FindDeepestView (top, new (0, 0)); - Assert.Equal (top.Border, found); + found = View.FindDeepestView (new (0, 0)); + Assert.Equal (Application.Top.Border, found); Assert.Equal (new (-1, -1), view.ScreenToFrame (new (3, 2))); screen = view.ViewportToScreen (new Point (0, 0)); Assert.Equal (4, screen.X); Assert.Equal (3, screen.Y); - found = View.FindDeepestView (top, new (4, 3)); + found = View.FindDeepestView (new (4, 3)); Assert.Equal (view, found); Assert.Equal (new (9, -1), view.ScreenToFrame (new (13, 2))); screen = view.ViewportToScreen (new Point (10, 0)); Assert.Equal (14, screen.X); Assert.Equal (3, screen.Y); - found = View.FindDeepestView (top, new (14, 3)); - Assert.Equal (top, found); + found = View.FindDeepestView (new (14, 3)); + Assert.Equal (Application.Top, found); Assert.Equal (new (10, 0), view.ScreenToFrame (new (14, 3))); screen = view.ViewportToScreen (new Point (11, 1)); Assert.Equal (15, screen.X); Assert.Equal (4, screen.Y); - found = View.FindDeepestView (top, new (15, 4)); - Assert.Equal (top, found); - top.Dispose (); + found = View.FindDeepestView (new (15, 4)); + Assert.Equal (Application.Top, found); + + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } [Fact] [AutoInitShutdown] public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top () { - var top = new Toplevel + Application.Top = new () { X = 3, Y = 2, @@ -1112,18 +1115,18 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top () Height = 1, Text = "0123456789" }; - top.Add (view); + Application.Top.Add (view); - Application.Begin (top); + Application.Begin (Application.Top); Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows)); - Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame); - Assert.Equal (new (3, 2, 20, 10), top.Frame); + Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame); + Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame); ((FakeDriver)Application.Driver!).SetBufferSize (30, 20); Assert.Equal (new (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows)); - Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame); - Assert.Equal (new (3, 2, 20, 10), top.Frame); + Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame); + Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame); Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre ( @" @@ -1146,45 +1149,45 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top () Assert.Equal (new (3, 2, 23, 10), frame); // top - Assert.Equal (new (-3, -2), top.ScreenToFrame (new (0, 0))); - Point screen = top.Margin.ViewportToScreen (new Point (-3, -2)); + Assert.Equal (new (-3, -2), Application.Top.ScreenToFrame (new (0, 0))); + Point screen = Application.Top.Margin.ViewportToScreen (new Point (-3, -2)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - screen = top.Border.ViewportToScreen (new Point (-3, -2)); + screen = Application.Top.Border.ViewportToScreen (new Point (-3, -2)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - screen = top.Padding.ViewportToScreen (new Point (-3, -2)); + screen = Application.Top.Padding.ViewportToScreen (new Point (-3, -2)); Assert.Equal (1, screen.X); Assert.Equal (1, screen.Y); - screen = top.ViewportToScreen (new Point (-3, -2)); + screen = Application.Top.ViewportToScreen (new Point (-3, -2)); Assert.Equal (1, screen.X); Assert.Equal (1, screen.Y); - screen = top.ViewportToScreen (new Point (-4, -3)); + screen = Application.Top.ViewportToScreen (new Point (-4, -3)); Assert.Equal (0, screen.X); Assert.Equal (0, screen.Y); - var found = View.FindDeepestView (top, new (-4, -3)); + var found = View.FindDeepestView (new (-4, -3)); Assert.Null (found); - Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2))); - screen = top.ViewportToScreen (new Point (0, 0)); + Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (3, 2))); + screen = Application.Top.ViewportToScreen (new Point (0, 0)); Assert.Equal (4, screen.X); Assert.Equal (3, screen.Y); - Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2))); + Assert.Equal (Application.Top.Border, View.FindDeepestView (new (3, 2))); //Assert.Equal (0, found.FrameToScreen ().X); //Assert.Equal (0, found.FrameToScreen ().Y); - Assert.Equal (new (10, 0), top.ScreenToFrame (new (13, 2))); - screen = top.ViewportToScreen (new Point (10, 0)); + Assert.Equal (new (10, 0), Application.Top.ScreenToFrame (new (13, 2))); + screen = Application.Top.ViewportToScreen (new Point (10, 0)); Assert.Equal (14, screen.X); Assert.Equal (3, screen.Y); - Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2))); + Assert.Equal (Application.Top.Border, View.FindDeepestView (new (13, 2))); //Assert.Equal (10, found.FrameToScreen ().X); //Assert.Equal (0, found.FrameToScreen ().Y); - Assert.Equal (new (11, 1), top.ScreenToFrame (new (14, 3))); - screen = top.ViewportToScreen (new Point (11, 1)); + Assert.Equal (new (11, 1), Application.Top.ScreenToFrame (new (14, 3))); + screen = Application.Top.ViewportToScreen (new Point (11, 1)); Assert.Equal (15, screen.X); Assert.Equal (4, screen.Y); - Assert.Equal (top, View.FindDeepestView (top, new (14, 3))); + Assert.Equal (Application.Top, View.FindDeepestView (new (14, 3))); // view Assert.Equal (new (-7, -5), view.ScreenToFrame (new (0, 0))); @@ -1200,32 +1203,32 @@ public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top () screen = view.ViewportToScreen (new Point (-6, -4)); Assert.Equal (1, screen.X); Assert.Equal (1, screen.Y); - Assert.Null (View.FindDeepestView (top, new (1, 1))); + Assert.Null (View.FindDeepestView (new (1, 1))); Assert.Equal (new (-4, -3), view.ScreenToFrame (new (3, 2))); screen = view.ViewportToScreen (new Point (-3, -2)); Assert.Equal (4, screen.X); Assert.Equal (3, screen.Y); - Assert.Equal (top, View.FindDeepestView (top, new (4, 3))); + Assert.Equal (Application.Top, View.FindDeepestView (new (4, 3))); Assert.Equal (new (-1, -1), view.ScreenToFrame (new (6, 4))); screen = view.ViewportToScreen (new Point (0, 0)); Assert.Equal (7, screen.X); Assert.Equal (5, screen.Y); - Assert.Equal (view, View.FindDeepestView (top, new (7, 5))); + Assert.Equal (view, View.FindDeepestView (new (7, 5))); Assert.Equal (new (6, -1), view.ScreenToFrame (new (13, 4))); screen = view.ViewportToScreen (new Point (7, 0)); Assert.Equal (14, screen.X); Assert.Equal (5, screen.Y); - Assert.Equal (view, View.FindDeepestView (top, new (14, 5))); + Assert.Equal (view, View.FindDeepestView (new (14, 5))); Assert.Equal (new (7, -2), view.ScreenToFrame (new (14, 3))); screen = view.ViewportToScreen (new Point (8, -1)); Assert.Equal (15, screen.X); Assert.Equal (4, screen.Y); - Assert.Equal (top, View.FindDeepestView (top, new (15, 4))); + Assert.Equal (Application.Top, View.FindDeepestView (new (15, 4))); Assert.Equal (new (16, -2), view.ScreenToFrame (new (23, 3))); screen = view.ViewportToScreen (new Point (17, -1)); Assert.Equal (24, screen.X); Assert.Equal (4, screen.Y); - Assert.Null (View.FindDeepestView (top, new (24, 4))); - top.Dispose (); + Assert.Null (View.FindDeepestView (new (24, 4))); + Application.Top.Dispose (); } } From 4073e233a4b2edd5cddaa73583a82a814df981bc Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 19 Sep 2024 16:04:39 -0600 Subject: [PATCH 02/75] Popover prototype --- .../Application/Application.Keyboard.cs | 6 ++ Terminal.Gui/Application/Application.Mouse.cs | 22 +++-- .../Application/Application.Popover.cs | 78 ++++++++++++---- Terminal.Gui/Application/Application.Run.cs | 5 ++ .../Application/ApplicationNavigation.cs | 33 ++++++- Terminal.Gui/Views/Menuv2.cs | 29 +++++- Terminal.Gui/Views/Shortcut.cs | 5 +- UICatalog/Scenarios/Bars.cs | 89 ++++++++++++------- UICatalog/Scenarios/ViewExperiments.cs | 24 +++++ UICatalog/UICatalog.cs | 1 + 10 files changed, 233 insertions(+), 59 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 0e92961a59..8bc38eec69 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -285,6 +285,12 @@ internal static void AddApplicationKeyBindings () Command.Quit, static () => { + if (Popover is {Visible: true}) + { + Popover.Visible = false; + + return true; + } RequestStop (); return true; } diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 5285d69bec..02f6edb4ca 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -1,4 +1,6 @@ #nullable enable +using Microsoft.CodeAnalysis.VisualBasic.Syntax; + namespace Terminal.Gui; public static partial class Application // Mouse handling @@ -139,7 +141,17 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } - var view = View.FindDeepestView (mouseEvent.Position); + View? view; + view = View.FindDeepestView (mouseEvent.Position); + + if ((mouseEvent.Flags == MouseFlags.Button1Pressed + || mouseEvent.Flags == MouseFlags.Button2Pressed + || mouseEvent.Flags == MouseFlags.Button3Pressed + || mouseEvent.Flags == MouseFlags.Button4Pressed) + && Popover is { Visible: true } && !ApplicationNavigation.IsInHierarchy (Popover, view, includeAdornments: true)) + { + Popover.Visible = false; + } if (view is { }) { @@ -203,10 +215,10 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) // We can combine this into the switch expression to reduce cognitive complexity even more and likely // avoid one or two of these checks in the process, as well. WantContinuousButtonPressedView = view switch - { - { WantContinuousButtonPressed: true } => view, - _ => null - }; + { + { WantContinuousButtonPressed: true } => view, + _ => null + }; // May be null before the prior condition or the condition may set it as null. // So, the checking must be outside the prior condition. diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index 8b5689e74f..894d30c783 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -3,34 +3,74 @@ namespace Terminal.Gui; public static partial class Application // Popover handling { - /// The that is currently active as the sole-Application Popover. - /// The Popover. - public static View? Popover { get; internal set; } - public static bool ShowPopover (View popoverView) + private static View? _popover; + + /// Gets or sets the Application Popover View. + /// + /// + /// To show or hide the Popover, set it's property. + /// + /// + public static View? Popover { - if (Popover is { }) + get => _popover; + set { - Popover.Visible = false; - } + if (_popover == value) + { + return; + } - if (!popoverView.IsInitialized) - { - popoverView.BeginInit (); - popoverView.EndInit (); - } + if (_popover is { }) + { + _popover.Visible = false; + _popover.VisibleChanged -= PopoverVisibleChanged; + } + + _popover = value; + + if (_popover is { }) + { + if (!_popover.IsInitialized) + { + _popover.BeginInit (); + _popover.EndInit (); + } + _popover.Arrangement |= ViewArrangement.Overlapped; + + if (_popover.ColorScheme is null) + { + _popover.ColorScheme = Top?.ColorScheme; + } - Popover = popoverView; - Popover.Visible = true; - Popover.SetRelativeLayout (Screen.Size); + _popover.SetRelativeLayout (Screen.Size); - return true; + _popover.VisibleChanged += PopoverVisibleChanged; + } + } } - public static void HidePopover () + private static void PopoverVisibleChanged (object? sender, EventArgs e) { - if (Popover is { }) + if (Popover is null) + { + return; + } + + if (Popover.Visible) + { + Popover.Arrangement |= ViewArrangement.Overlapped; + + if (Popover.ColorScheme is null) + { + Popover.ColorScheme = Top?.ColorScheme; + } + Popover.SetRelativeLayout (Screen.Size); + Popover.SetFocus (); + } + else { - Popover.Visible = false; + } } } diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 85b43ca7b3..c0ade86825 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -639,6 +639,11 @@ public static void End (RunState runState) _cachedRunStateToplevel = runState.Toplevel; + if (Popover is { }) + { + Popover = null; + } + runState.Toplevel = null; runState.Dispose (); diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index 2523709d07..79212e86c3 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -34,8 +34,9 @@ public ApplicationNavigation () /// /// /// + /// Will search the subview hierarchy of the adornments if true. /// - public static bool IsInHierarchy (View? start, View? view) + public static bool IsInHierarchy (View? start, View? view, bool includeAdornments = false) { if (view is null) { @@ -54,7 +55,31 @@ public static bool IsInHierarchy (View? start, View? view) return true; } - bool found = IsInHierarchy (subView, view); + bool found = IsInHierarchy (subView, view, includeAdornments); + + if (found) + { + return found; + } + } + + if (includeAdornments) + { + bool found = IsInHierarchy (start.Padding, view, includeAdornments); + + if (found) + { + return found; + } + + found = IsInHierarchy (start.Border, view, includeAdornments); + + if (found) + { + return found; + } + + found = IsInHierarchy (start.Margin, view, includeAdornments); if (found) { @@ -100,6 +125,10 @@ internal void SetFocused (View? value) /// public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { + if (Application.Popover is { Visible: true }) + { + Application.Popover.AdvanceFocus (direction, behavior); + } return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } } diff --git a/Terminal.Gui/Views/Menuv2.cs b/Terminal.Gui/Views/Menuv2.cs index 760ea271f7..a21d95a1bf 100644 --- a/Terminal.Gui/Views/Menuv2.cs +++ b/Terminal.Gui/Views/Menuv2.cs @@ -17,13 +17,40 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) Orientation = Orientation.Vertical; Width = Dim.Auto (); Height = Dim.Auto (DimAutoStyle.Content, 1); - ColorScheme = Colors.ColorSchemes ["Menu"]; Initialized += Menuv2_Initialized; + VisibleChanged += OnVisibleChanged; + } + + //private void OnMouseEvent (object sender, MouseEventEventArgs e) + //{ + // if (!e.MouseEvent.Flags.HasFlag(MouseFlags.ReportMousePosition)) + // { + // return; + // } + + + //} + + private void OnVisibleChanged (object sender, EventArgs e) + { + if (Visible) + { + //Application.GrabMouse(this); + } + else + { + if (Application.MouseGrabView == this) + { + //Application.UngrabMouse (); + } + } } private void Menuv2_Initialized (object sender, EventArgs e) { Border.Thickness = new Thickness (1, 1, 1, 1); + Border.LineStyle = LineStyle.Single; + ColorScheme = Colors.ColorSchemes ["Menu"]; } // Menuv2 arranges the items horizontally. diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index e7b9bc5dbc..5fc95ef1da 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -806,7 +806,10 @@ public override ColorScheme ColorScheme internal void SetColors () { // Border should match superview. - Border.ColorScheme = SuperView?.ColorScheme; + if (Border is { }) + { + Border.ColorScheme = SuperView?.ColorScheme; + } if (HasFocus) { diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 1bed7bc05c..64476fea8e 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; @@ -12,6 +11,8 @@ namespace UICatalog.Scenarios; [ScenarioCategory ("Controls")] public class Bars : Scenario { + private Menuv2 _popoverMenu; + public override void Main () { Application.Init (); @@ -19,7 +20,13 @@ public override void Main () app.Loaded += App_Loaded; + _popoverMenu = new Menuv2 + { + Id = "popoverMenu", + }; + Application.Run (app); + _popoverMenu.Dispose (); app.Dispose (); Application.Shutdown (); } @@ -117,7 +124,7 @@ private void App_Loaded (object sender, EventArgs e) //Width = Dim.Percent (40), Orientation = Orientation.Vertical, }; - ConfigureMenu (bar); + ConfigureMenu (bar); menuLikeExamples.Add (bar); @@ -141,23 +148,16 @@ private void App_Loaded (object sender, EventArgs e) label = new Label () { - Title = "PopOver Menu (Right click to show):", + Title = "Popover Menu (Right click to show):", X = Pos.Right (bar) + 1, Y = Pos.Top (label), }; menuLikeExamples.Add (label); - Menuv2 popOverMenu = new Menuv2 - { - Id = "popupMenu", - X = Pos.Left (label), - Y = Pos.Bottom (label), - }; - ConfigureMenu (popOverMenu); + ConfigureMenu (_popoverMenu); - popOverMenu.Arrangement = ViewArrangement.Overlapped; - popOverMenu.Visible = false; - //popOverMenu.Enabled = false; + _popoverMenu.ColorScheme = Colors.ColorSchemes ["Menu"]; + _popoverMenu.Visible = false; var toggleShortcut = new Shortcut { @@ -166,24 +166,31 @@ private void App_Loaded (object sender, EventArgs e) KeyBindingScope = KeyBindingScope.Application, Key = Key.F4.WithCtrl, }; - popOverMenu.Add (toggleShortcut); + _popoverMenu.Add (toggleShortcut); - popOverMenu.Accept += PopOverMenuOnAccept; + _popoverMenu.Accept += PopoverMenuOnAccept; - void PopOverMenuOnAccept (object o, HandledEventArgs handledEventArgs) + void PopoverMenuOnAccept (object o, HandledEventArgs handledEventArgs) { - if (popOverMenu.Visible) + if (_popoverMenu.Visible) { - popOverMenu.Visible = false; + _popoverMenu.Visible = false; } else { - popOverMenu.Visible = true; - popOverMenu.SetFocus (); + _popoverMenu.Visible = true; } } - menuLikeExamples.Add (popOverMenu); + foreach (Shortcut sh in _popoverMenu.Subviews.Where (s => s is Shortcut)!) + { + sh.Accept += (o, args) => + { + eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + //args.Handled = true; + }; + } menuLikeExamples.MouseClick += MenuLikeExamplesMouseClick; @@ -191,16 +198,11 @@ void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e) { if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)) { - popOverMenu.X = e.MouseEvent.Position.X; - popOverMenu.Y = e.MouseEvent.Position.Y; - popOverMenu.Visible = true; - //popOverMenu.Enabled = popOverMenu.Visible; - popOverMenu.SetFocus (); - } - else - { - popOverMenu.Visible = false; - //popOverMenu.Enabled = popOverMenu.Visible; + Application.Popover = _popoverMenu; + + _popoverMenu.X = e.MouseEvent.ScreenPosition.X; + _popoverMenu.Y = e.MouseEvent.ScreenPosition.Y; + _popoverMenu.Visible = true; } } @@ -417,6 +419,31 @@ private void ConfigMenuBar (Bar bar) Key = Key.D0.WithAlt, }; + fileMenuBarItem.Accept += (sender, args) => + { + var fileMenu = new Menuv2 + { + Id = "fileMenu", + }; + ConfigureMenu (fileMenu); + fileMenu.VisibleChanged += (sender, args) => + { + if (Application.Popover is { Visible: false }) + { + Application.Popover?.Dispose (); + Application.Popover = null; + } + }; + Application.Popover = fileMenu; + Rectangle screen = fileMenuBarItem.FrameToScreen (); + fileMenu.X = screen.X; + fileMenu.Y = screen.Y + screen.Height; + fileMenu.Visible = true; + + }; + + + var editMenuBarItem = new Shortcut { Title = "_Edit", diff --git a/UICatalog/Scenarios/ViewExperiments.cs b/UICatalog/Scenarios/ViewExperiments.cs index c9db4478d0..be4647dd83 100644 --- a/UICatalog/Scenarios/ViewExperiments.cs +++ b/UICatalog/Scenarios/ViewExperiments.cs @@ -54,6 +54,28 @@ public override void Main () Title = $"TopButton _{GetNextHotKey ()}", }; + var popoverView = new Button () + { + X = Pos.Center (), + Y = Pos.Center (), + Width = Dim.Percent (50), + Height = Dim.Percent (50), + Title = "Popover", + Text = "This is a popover", + Visible = false, + Arrangement = ViewArrangement.Resizable | ViewArrangement.Movable + }; + popoverView.BorderStyle = LineStyle.RoundedDotted; + popoverView.Accept += (sender, e) => Application.Popover!.Visible = false; + + button.Accept += ButtonAccept; + + void ButtonAccept (object sender, System.ComponentModel.HandledEventArgs e) + { + Application.Popover = popoverView; + Application.Popover!.Visible = true; + } + testFrame.Add (button); editor.AutoSelectViewToEdit = true; @@ -61,6 +83,7 @@ public override void Main () editor.AutoSelectAdornments = true; Application.Run (app); + popoverView.Dispose (); app.Dispose (); Application.Shutdown (); @@ -68,6 +91,7 @@ public override void Main () return; } + private int _hotkeyCount; private char GetNextHotKey () diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 8ddc93af0c..0f3e1e0aa7 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -389,6 +389,7 @@ private static void VerifyObjectsWereDisposed () // 'app' closed cleanly. foreach (Responder? inst in Responder.Instances) { + Debug.Assert (inst.WasDisposed); } From 9f84b3a6236d18b28ccd95d11042c1a4bf11d7bd Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 22 Sep 2024 08:02:13 -0600 Subject: [PATCH 03/75] Testing highlight --- Terminal.Gui/Views/Menuv2.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Terminal.Gui/Views/Menuv2.cs b/Terminal.Gui/Views/Menuv2.cs index a21d95a1bf..6289e38275 100644 --- a/Terminal.Gui/Views/Menuv2.cs +++ b/Terminal.Gui/Views/Menuv2.cs @@ -81,6 +81,7 @@ public override View Add (View view) shortcut.CanFocus = true; shortcut.KeyBindingScope = KeyBindingScope.Application; shortcut.Orientation = Orientation.Vertical; + shortcut.HighlightStyle |= HighlightStyle.Hover; // TODO: not happy about using AlignmentModes for this. Too implied. // TODO: instead, add a property (a style enum?) to Shortcut to control this From f589416428b3c6675ee3b297d3d73ab170df69fb Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 15:02:21 -0600 Subject: [PATCH 04/75] Fixed click outside issue --- Terminal.Gui/Application/Application.Mouse.cs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 26e2235d6b..601b8a8b85 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -152,6 +152,15 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) mouseEvent.View = deepestViewUnderMouse; } + if (Popover is { Visible: true } + && Popover != deepestViewUnderMouse + && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) + || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) + || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) + { + Popover.Visible = false; + } + MouseEvent?.Invoke (null, mouseEvent); if (mouseEvent.Handled) @@ -168,10 +177,10 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) // avoid one or two of these checks in the process, as well. WantContinuousButtonPressedView = deepestViewUnderMouse switch - { - { WantContinuousButtonPressed: true } => deepestViewUnderMouse, - _ => null - }; + { + { WantContinuousButtonPressed: true } => deepestViewUnderMouse, + _ => null + }; // May be null before the prior condition or the condition may set it as null. // So, the checking must be outside the prior condition. @@ -210,6 +219,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) else { Debug.Fail ("This should never happen"); + return; } @@ -219,9 +229,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}"); if (deepestViewUnderMouse.Id == "mouseDemo") - { - - } + { } while (deepestViewUnderMouse.NewMouseEvent (me) is not true && MouseGrabView is not { }) { @@ -255,13 +263,13 @@ internal static bool GrabMouse (View? deepestViewUnderMouse, MouseEvent mouseEve { if (MouseGrabView is { }) { - #if DEBUG_IDISPOSABLE if (MouseGrabView.WasDisposed) { throw new ObjectDisposedException (MouseGrabView.GetType ().FullName); } #endif + // If the mouse is grabbed, send the event to the view that grabbed it. // The coordinates are relative to the Bounds of the view that grabbed the mouse. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position); @@ -303,6 +311,7 @@ internal static void RaiseMouseEnterLeaveEvents (Point screenPosition, List viewsToLeave = _cachedViewsUnderMouse.Where (v => v is { } && !currentViewsUnderMouse.Contains (v)).ToList (); + foreach (View? view in viewsToLeave) { if (view is null) @@ -328,7 +337,8 @@ internal static void RaiseMouseEnterLeaveEvents (Point screenPosition, List Date: Mon, 23 Sep 2024 15:06:42 -0600 Subject: [PATCH 05/75] Fixed DialogTests --- UnitTests/Dialogs/DialogTests.cs | 56 ++++++++++++++++---------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/UnitTests/Dialogs/DialogTests.cs b/UnitTests/Dialogs/DialogTests.cs index 27feb5e302..5bb2ae4a8c 100644 --- a/UnitTests/Dialogs/DialogTests.cs +++ b/UnitTests/Dialogs/DialogTests.cs @@ -1279,39 +1279,39 @@ public void Location_When_Not_Application_Top_Not_Default () rs = Begin (d); // This is because of PostionTopLevels and EnsureVisibleBounds - Assert.Equal (new (3, 2), d.Frame.Location); + Assert.Equal (new (5, 5), d.Frame.Location); // #3127: Before - // Assert.Equal (new (17, 8), d.Frame.Size); - // TestHelpers.AssertDriverContentsWithFrameAre (@" - //╔══════════════════╗ - //║ ║ - //║ ┌───────────────┐ - //║ │ │ - //║ │ │ - //║ │ │ - //║ │ │ - //║ │ │ - //║ │ │ - //╚══└───────────────┘", _output); - - // #3127: After: Because Toplevel is now Width/Height = Dim.Filll - Assert.Equal (new (15, 6), d.Frame.Size); - - TestHelpers.AssertDriverContentsWithFrameAre ( - @" + Assert.Equal (new (17, 8), d.Frame.Size); + TestHelpers.AssertDriverContentsWithFrameAre (@" ╔══════════════════╗ ║ ║ -║ ┌─────────────┐ ║ -║ │ │ ║ -║ │ │ ║ -║ │ │ ║ -║ │ │ ║ -║ └─────────────┘ ║ ║ ║ -╚══════════════════╝", - _output - ); +║ ║ +║ ║ +║ ┌────────────── +║ │ +║ │ +║ │ +╚════│ ", _output); + +// // #3127: After: Because Toplevel is now Width/Height = Dim.Filll +// Assert.Equal (new (15, 6), d.Frame.Size); + +// TestHelpers.AssertDriverContentsWithFrameAre ( +// @" +//╔══════════════════╗ +//║ ║ +//║ ┌─────────────┐ ║ +//║ │ │ ║ +//║ │ │ ║ +//║ │ │ ║ +//║ │ │ ║ +//║ └─────────────┘ ║ +//║ ║ +//╚══════════════════╝", +// _output +// ); End (rs); d.Dispose (); } From c6232067d692460cb462722a174e4ac67af1cada Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 15:11:09 -0600 Subject: [PATCH 06/75] Fixed click outside issue (agbain) --- Terminal.Gui/Application/Application.Mouse.cs | 2 +- .../Application/ApplicationNavigation.cs | 61 --- Terminal.Gui/View/View.Hierarchy.cs | 61 +++ Terminal.Gui/View/View.Navigation.cs | 2 +- UICatalog/Scenarios/AdornmentsEditor.cs | 4 +- UICatalog/Scenarios/ArrangementEditor.cs | 4 +- UnitTests/Dialogs/DialogTests.cs | 473 +++++++++--------- 7 files changed, 304 insertions(+), 303 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 601b8a8b85..08154a6cff 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -153,7 +153,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) } if (Popover is { Visible: true } - && Popover != deepestViewUnderMouse + && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index 79212e86c3..c140dc0c4e 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -29,67 +29,6 @@ public ApplicationNavigation () /// public View? GetFocused () { return _focused; } - /// - /// Gets whether is in the Subview hierarchy of . - /// - /// - /// - /// Will search the subview hierarchy of the adornments if true. - /// - public static bool IsInHierarchy (View? start, View? view, bool includeAdornments = false) - { - if (view is null) - { - return false; - } - - if (view == start || start is null) - { - return true; - } - - foreach (View subView in start.Subviews) - { - if (view == subView) - { - return true; - } - - bool found = IsInHierarchy (subView, view, includeAdornments); - - if (found) - { - return found; - } - } - - if (includeAdornments) - { - bool found = IsInHierarchy (start.Padding, view, includeAdornments); - - if (found) - { - return found; - } - - found = IsInHierarchy (start.Border, view, includeAdornments); - - if (found) - { - return found; - } - - found = IsInHierarchy (start.Margin, view, includeAdornments); - - if (found) - { - return found; - } - } - - return false; - } - /// /// INTERNAL method to record the most focused in the application. /// diff --git a/Terminal.Gui/View/View.Hierarchy.cs b/Terminal.Gui/View/View.Hierarchy.cs index 02b737896b..2e02716281 100644 --- a/Terminal.Gui/View/View.Hierarchy.cs +++ b/Terminal.Gui/View/View.Hierarchy.cs @@ -243,6 +243,67 @@ public virtual void RemoveAll () return top; } + /// + /// Gets whether is in the Subview hierarchy of . + /// + /// + /// + /// Will search the subview hierarchy of the adornments if true. + /// + public static bool IsInHierarchy (View? start, View? view, bool includeAdornments = false) + { + if (view is null) + { + return false; + } + + if (view == start || start is null) + { + return true; + } + + foreach (View subView in start.Subviews) + { + if (view == subView) + { + return true; + } + + bool found = IsInHierarchy (subView, view, includeAdornments); + + if (found) + { + return found; + } + } + + if (includeAdornments) + { + bool found = IsInHierarchy (start.Padding, view, includeAdornments); + + if (found) + { + return found; + } + + found = IsInHierarchy (start.Border, view, includeAdornments); + + if (found) + { + return found; + } + + found = IsInHierarchy (start.Margin, view, includeAdornments); + + if (found) + { + return found; + } + } + + return false; + } + #region SubViewOrdering /// diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index 6774e959cd..56bc6d070e 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -420,7 +420,7 @@ public bool SetFocus () /// private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false) { - Debug.Assert (ApplicationNavigation.IsInHierarchy (SuperView, this)); + Debug.Assert (IsInHierarchy (SuperView, this)); // Pre-conditions if (_hasFocus) diff --git a/UICatalog/Scenarios/AdornmentsEditor.cs b/UICatalog/Scenarios/AdornmentsEditor.cs index 7bd6ec042c..4eb14dfe33 100644 --- a/UICatalog/Scenarios/AdornmentsEditor.cs +++ b/UICatalog/Scenarios/AdornmentsEditor.cs @@ -95,12 +95,12 @@ private void NavigationOnFocusedChanged (object? sender, EventArgs e) return; } - if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ())) + if (IsInHierarchy (this, Application.Navigation!.GetFocused ())) { return; } - if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ())) + if (!IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ())) { return; } diff --git a/UICatalog/Scenarios/ArrangementEditor.cs b/UICatalog/Scenarios/ArrangementEditor.cs index 13a48380d6..41ce93d245 100644 --- a/UICatalog/Scenarios/ArrangementEditor.cs +++ b/UICatalog/Scenarios/ArrangementEditor.cs @@ -131,12 +131,12 @@ private void NavigationOnFocusedChanged (object? sender, EventArgs e) View? view = Application.Navigation!.GetFocused (); - if (ApplicationNavigation.IsInHierarchy (this, view)) + if (IsInHierarchy (this, view)) { return; } - if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, view)) + if (!IsInHierarchy (AutoSelectSuperView, view)) { return; } diff --git a/UnitTests/Dialogs/DialogTests.cs b/UnitTests/Dialogs/DialogTests.cs index 5bb2ae4a8c..c4b36b7d32 100644 --- a/UnitTests/Dialogs/DialogTests.cs +++ b/UnitTests/Dialogs/DialogTests.cs @@ -39,7 +39,7 @@ public void Add_Button_Works () Width = width, Height = 1, ButtonAlignment = Alignment.Center, - Buttons = [new Button { Text = btn1Text }] + Buttons = [new() { Text = btn1Text }] }; // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..) @@ -64,7 +64,7 @@ public void Add_Button_Works () Width = width, Height = 1, ButtonAlignment = Alignment.Fill, - Buttons = [new Button { Text = btn1Text }] + Buttons = [new() { Text = btn1Text }] }; // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..) @@ -89,7 +89,7 @@ public void Add_Button_Works () Width = width, Height = 1, ButtonAlignment = Alignment.End, - Buttons = [new Button { Text = btn1Text }] + Buttons = [new() { Text = btn1Text }] }; // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..) @@ -114,7 +114,7 @@ public void Add_Button_Works () Width = width, Height = 1, ButtonAlignment = Alignment.Start, - Buttons = [new Button { Text = btn1Text }] + Buttons = [new() { Text = btn1Text }] }; // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..) @@ -159,17 +159,16 @@ public void ButtonAlignment_Four () int width = buttonRow.Length; d.SetBufferSize (buttonRow.Length, 3); - // Default - Center (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Center, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -179,14 +178,14 @@ public void ButtonAlignment_Four () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -196,14 +195,14 @@ public void ButtonAlignment_Four () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -213,14 +212,14 @@ public void ButtonAlignment_Four () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -275,14 +274,14 @@ public void ButtonAlignment_Four_On_Too_Small_Width () $"{CM.Glyphs.VLine}{CM.Glyphs.LeftBracket} yes {CM.Glyphs.LeftBracket} no {CM.Glyphs.LeftBracket} maybe {CM.Glyphs.LeftBracket} never {CM.Glyphs.RightBracket}{CM.Glyphs.VLine}"; (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -292,14 +291,14 @@ public void ButtonAlignment_Four_On_Too_Small_Width () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -308,14 +307,14 @@ public void ButtonAlignment_Four_On_Too_Small_Width () buttonRow = $"{CM.Glyphs.VLine}{btn1}{btn2}{btn3}{CM.Glyphs.LeftBracket} neve{CM.Glyphs.VLine}"; (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -351,14 +350,14 @@ public void ButtonAlignment_Four_WideOdd () // Default - Center (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Center, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -368,14 +367,14 @@ public void ButtonAlignment_Four_WideOdd () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -385,14 +384,14 @@ public void ButtonAlignment_Four_WideOdd () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -402,14 +401,14 @@ public void ButtonAlignment_Four_WideOdd () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -447,14 +446,14 @@ public void ButtonAlignment_Four_Wider () // Default - Center (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Center, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -464,14 +463,14 @@ public void ButtonAlignment_Four_Wider () Assert.Equal (width, buttonRow.GetColumns ()); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -481,14 +480,14 @@ public void ButtonAlignment_Four_Wider () Assert.Equal (width, buttonRow.GetColumns ()); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -498,14 +497,14 @@ public void ButtonAlignment_Four_Wider () Assert.Equal (width, buttonRow.GetColumns ()); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text }, - new Button { Text = btn4Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text }, + new Button { Text = btn4Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -532,11 +531,11 @@ public void ButtonAlignment_One () d.SetBufferSize (width, 1); (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btnText } - ); + title, + width, + Alignment.Center, + new Button { Text = btnText } + ); // Center TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); @@ -549,11 +548,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btnText } - ); + title, + width, + Alignment.Fill, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -564,11 +563,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btnText } - ); + title, + width, + Alignment.End, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -579,11 +578,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btnText } - ); + title, + width, + Alignment.Start, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -596,11 +595,11 @@ public void ButtonAlignment_One () d.SetBufferSize (width, 1); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btnText } - ); + title, + width, + Alignment.Center, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -611,11 +610,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btnText } - ); + title, + width, + Alignment.Fill, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -626,11 +625,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btnText } - ); + title, + width, + Alignment.End, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -641,11 +640,11 @@ public void ButtonAlignment_One () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btnText } - ); + title, + width, + Alignment.Start, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -677,13 +676,13 @@ public void ButtonAlignment_Three () d.SetBufferSize (buttonRow.Length, 3); (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text } - ); + title, + width, + Alignment.Center, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -693,13 +692,13 @@ public void ButtonAlignment_Three () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -709,13 +708,13 @@ public void ButtonAlignment_Three () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -725,13 +724,13 @@ public void ButtonAlignment_Three () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text }, - new Button { Text = btn3Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text }, + new Button { Text = btn3Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -761,12 +760,12 @@ public void ButtonAlignment_Two () d.SetBufferSize (buttonRow.Length, 3); (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btn1Text }, - new Button { Text = btn2Text } - ); + title, + width, + Alignment.Center, + new Button { Text = btn1Text }, + new Button { Text = btn2Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -776,12 +775,12 @@ public void ButtonAlignment_Two () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Fill, - new Button { Text = btn1Text }, - new Button { Text = btn2Text } - ); + title, + width, + Alignment.Fill, + new Button { Text = btn1Text }, + new Button { Text = btn2Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -791,12 +790,12 @@ public void ButtonAlignment_Two () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.End, - new Button { Text = btn1Text }, - new Button { Text = btn2Text } - ); + title, + width, + Alignment.End, + new Button { Text = btn1Text }, + new Button { Text = btn2Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -806,12 +805,12 @@ public void ButtonAlignment_Two () Assert.Equal (width, buttonRow.Length); (runstate, dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Start, - new Button { Text = btn1Text }, - new Button { Text = btn2Text } - ); + title, + width, + Alignment.Start, + new Button { Text = btn1Text }, + new Button { Text = btn2Text } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -845,8 +844,8 @@ public void ButtonAlignment_Two_Hidden () Button button1, button2; // Default (Center) - button1 = new Button { Text = btn1Text }; - button2 = new Button { Text = btn2Text }; + button1 = new() { Text = btn1Text }; + button2 = new() { Text = btn2Text }; (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Center, button1, button2); button1.Visible = false; RunIteration (ref runstate, ref firstIteration); @@ -857,8 +856,8 @@ public void ButtonAlignment_Two_Hidden () // Justify Assert.Equal (width, buttonRow.Length); - button1 = new Button { Text = btn1Text }; - button2 = new Button { Text = btn2Text }; + button1 = new() { Text = btn1Text }; + button2 = new() { Text = btn2Text }; (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Fill, button1, button2); button1.Visible = false; RunIteration (ref runstate, ref firstIteration); @@ -869,8 +868,8 @@ public void ButtonAlignment_Two_Hidden () // Right Assert.Equal (width, buttonRow.Length); - button1 = new Button { Text = btn1Text }; - button2 = new Button { Text = btn2Text }; + button1 = new() { Text = btn1Text }; + button2 = new() { Text = btn2Text }; (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.End, button1, button2); button1.Visible = false; RunIteration (ref runstate, ref firstIteration); @@ -880,8 +879,8 @@ public void ButtonAlignment_Two_Hidden () // Left Assert.Equal (width, buttonRow.Length); - button1 = new Button { Text = btn1Text }; - button2 = new Button { Text = btn2Text }; + button1 = new() { Text = btn1Text }; + button2 = new() { Text = btn2Text }; (runstate, dlg) = RunButtonTestDialog (title, width, Alignment.Start, button1, button2); button1.Visible = false; RunIteration (ref runstate, ref firstIteration); @@ -1010,7 +1009,7 @@ public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height, string Dialog.DefaultBorderStyle = LineStyle.Single; Dialog.DefaultShadow = ShadowStyle.None; Button.DefaultShadow = ShadowStyle.None; - + Iteration += (s, a) => { iterations++; @@ -1166,7 +1165,7 @@ public void FileDialog_FileSystemWatcher () [AutoInitShutdown] public void Location_Default () { - var d = new Dialog () + var d = new Dialog { Width = Dim.Percent (85), Height = Dim.Percent (85) @@ -1274,7 +1273,6 @@ public void Location_When_Not_Application_Top_Not_Default () X = 5, Y = 5, Width = Dim.Percent (85), Height = Dim.Percent (85) - }; rs = Begin (d); @@ -1283,7 +1281,9 @@ public void Location_When_Not_Application_Top_Not_Default () // #3127: Before Assert.Equal (new (17, 8), d.Frame.Size); - TestHelpers.AssertDriverContentsWithFrameAre (@" + + TestHelpers.AssertDriverContentsWithFrameAre ( + @" ╔══════════════════╗ ║ ║ ║ ║ @@ -1293,7 +1293,8 @@ public void Location_When_Not_Application_Top_Not_Default () ║ │ ║ │ ║ │ -╚════│ ", _output); +╚════│ ", + _output); // // #3127: After: Because Toplevel is now Width/Height = Dim.Filll // Assert.Equal (new (15, 6), d.Frame.Size); @@ -1346,11 +1347,11 @@ public void One_Button_Works () d.SetBufferSize (buttonRow.Length, 10); (runstate, Dialog dlg) = RunButtonTestDialog ( - title, - width, - Alignment.Center, - new Button { Text = btnText } - ); + title, + width, + Alignment.Center, + new Button { Text = btnText } + ); TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", _output); End (runstate); dlg.Dispose (); @@ -1360,7 +1361,7 @@ public void One_Button_Works () [AutoInitShutdown] public void Size_Default () { - var d = new Dialog () + var d = new Dialog { Width = Dim.Percent (85), Height = Dim.Percent (85) From e2a4620e327fde163b2ae0da558fa726953e596f Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 15:27:34 -0600 Subject: [PATCH 07/75] Enabled mouse wheel in Bar --- Terminal.Gui/Views/Bar.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index d30ced79e8..105359adcc 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -32,6 +32,7 @@ public Bar (IEnumerable shortcuts) _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e); Initialized += Bar_Initialized; + MouseEvent += OnMouseEvent; if (shortcuts is null) { @@ -44,6 +45,38 @@ public Bar (IEnumerable shortcuts) } } + private void OnMouseEvent (object? sender, MouseEventEventArgs e) + { + NavigationDirection direction = NavigationDirection.Forward; + + if (e.MouseEvent.Flags == MouseFlags.WheeledDown) + { + e.Handled = true; + } + + if (e.MouseEvent.Flags == MouseFlags.WheeledUp) + { + direction = NavigationDirection.Backward; + e.Handled = true; + } + + if (e.MouseEvent.Flags == MouseFlags.WheeledRight) + { + e.Handled = true; + } + + if (e.MouseEvent.Flags == MouseFlags.WheeledLeft) + { + direction = NavigationDirection.Backward; + e.Handled = true; + } + + if (e.Handled) + { + e.Handled = AdvanceFocus (direction, TabBehavior.TabStop); + } + } + private void Bar_Initialized (object? sender, EventArgs e) { ColorScheme = Colors.ColorSchemes ["Menu"]; } /// @@ -243,6 +276,7 @@ internal override void OnLayoutStarted (LayoutEventArgs args) } } + /// public bool EnableForDesign () { From 7b7649a3f1d8602831406f63147de1311c179453 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 15:28:04 -0600 Subject: [PATCH 08/75] Enabled mouse wheel in Bar --- Terminal.Gui/Application/Application.Popover.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index 894d30c783..0f9d64f5ec 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -36,6 +36,7 @@ public static View? Popover _popover.BeginInit (); _popover.EndInit (); } + _popover.Arrangement |= ViewArrangement.Overlapped; if (_popover.ColorScheme is null) @@ -65,12 +66,9 @@ private static void PopoverVisibleChanged (object? sender, EventArgs e) { Popover.ColorScheme = Top?.ColorScheme; } + Popover.SetRelativeLayout (Screen.Size); Popover.SetFocus (); } - else - { - - } } } From bfefabc70ae63017ea2127a90bfd75fb0c794ca2 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 23 Sep 2024 16:07:40 -0600 Subject: [PATCH 09/75] Progress. Broke arrangement --- .../Application/Application.Keyboard.cs | 11 +++++-- Terminal.Gui/Application/Application.Mouse.cs | 19 ++++++------ .../Application/Application.Popover.cs | 13 ++++++++ .../Application/ApplicationNavigation.cs | 9 +++++- Terminal.Gui/View/View.Navigation.cs | 2 +- UICatalog/Scenarios/ViewExperiments.cs | 30 ++++++++++++++++--- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 8bc38eec69..885f000b3a 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -95,7 +95,14 @@ public static bool OnKeyDown (Key keyEvent) return true; } - if (Top is null) + View? top = Top; + + if (Popover is { Visible: true }) + { + top = Popover; + } + + if (top is null) { foreach (Toplevel topLevel in TopLevels.ToList ()) { @@ -112,7 +119,7 @@ public static bool OnKeyDown (Key keyEvent) } else { - if (Top.NewKeyDownEvent (keyEvent)) + if (top.NewKeyDownEvent (keyEvent)) { return true; } diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 08154a6cff..5254bb535a 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -152,15 +152,6 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) mouseEvent.View = deepestViewUnderMouse; } - if (Popover is { Visible: true } - && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false - && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) - || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) - || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) - { - Popover.Visible = false; - } - MouseEvent?.Invoke (null, mouseEvent); if (mouseEvent.Handled) @@ -182,6 +173,16 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) _ => null }; + + if (Popover is { Visible: true } + && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false + && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) + || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) + || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) + { + Popover.Visible = false; + } + // May be null before the prior condition or the condition may set it as null. // So, the checking must be outside the prior condition. if (deepestViewUnderMouse is null) diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index 0f9d64f5ec..7ba036d66c 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -67,7 +67,20 @@ private static void PopoverVisibleChanged (object? sender, EventArgs e) Popover.ColorScheme = Top?.ColorScheme; } + View.GetLocationEnsuringFullVisibility ( + Popover, + Popover.Frame.X, + Popover.Frame.Y, + out int nx, + out int ny, + out StatusBar? sb + ); + + Popover.X = nx; + Popover.Y = ny; + Popover.SetRelativeLayout (Screen.Size); + Top.HasFocus = false; Popover.SetFocus (); } } diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index c140dc0c4e..677e066e43 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -27,7 +27,10 @@ public ApplicationNavigation () /// /// Gets the most focused in the application, if there is one. /// - public View? GetFocused () { return _focused; } + public View? GetFocused () + { + return _focused; + } /// /// INTERNAL method to record the most focused in the application. @@ -37,6 +40,10 @@ public ApplicationNavigation () /// internal void SetFocused (View? value) { + if (value is null) + { + + } if (_focused == value) { return; diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index 56bc6d070e..b448a1374f 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -697,7 +697,7 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false if (appFocused is { } || appFocused == this) { - Application.Navigation.SetFocused (newFocusedView ?? superViewOrParent); + Application.Navigation.SetFocused (newFocusedView ?? superViewOrParent ?? Application.Popover); } } diff --git a/UICatalog/Scenarios/ViewExperiments.cs b/UICatalog/Scenarios/ViewExperiments.cs index be4647dd83..d1990c7f59 100644 --- a/UICatalog/Scenarios/ViewExperiments.cs +++ b/UICatalog/Scenarios/ViewExperiments.cs @@ -54,19 +54,28 @@ public override void Main () Title = $"TopButton _{GetNextHotKey ()}", }; - var popoverView = new Button () + var popoverView = new View () { X = Pos.Center (), Y = Pos.Center (), - Width = Dim.Percent (50), - Height = Dim.Percent (50), + Width = 30, + Height = 10, Title = "Popover", Text = "This is a popover", Visible = false, + CanFocus = true, Arrangement = ViewArrangement.Resizable | ViewArrangement.Movable }; popoverView.BorderStyle = LineStyle.RoundedDotted; - popoverView.Accept += (sender, e) => Application.Popover!.Visible = false; + + Button popoverButton = new () + { + X = Pos.Center (), + Y = Pos.Center (), + Title = $"_Close", + }; + popoverButton.Accept += (sender, e) => Application.Popover!.Visible = false; + popoverView.Add (popoverButton); button.Accept += ButtonAccept; @@ -76,6 +85,19 @@ void ButtonAccept (object sender, System.ComponentModel.HandledEventArgs e) Application.Popover!.Visible = true; } + testFrame.MouseClick += TestFrameOnMouseClick; + + void TestFrameOnMouseClick (object sender, MouseEventEventArgs e) + { + if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) + { + popoverView.X = e.MouseEvent.ScreenPosition.X; + popoverView.Y = e.MouseEvent.ScreenPosition.Y; + Application.Popover = popoverView; + Application.Popover!.Visible = true; + } + } + testFrame.Add (button); editor.AutoSelectViewToEdit = true; From 3e8ed7abfee80964c8e4eaf9222184d0031ff898 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 10:50:29 -0600 Subject: [PATCH 10/75] Added popover tests. Fixed a bunch more CM issues related ot unreliable unit tests. Updated config.json to include Glyphs. --- .../Application/Application.Popover.cs | 12 +- Terminal.Gui/Drawing/Glyphs.cs | 5 + Terminal.Gui/Resources/config.json | 167 +++++++++++++++- Terminal.Gui/View/Adornment/Border.cs | 2 + Terminal.Gui/View/View.Hierarchy.cs | 8 +- Terminal.Gui/View/View.Layout.cs | 9 +- Terminal.Gui/View/View.Mouse.cs | 5 + Terminal.Gui/View/View.Navigation.cs | 2 +- .../Application/ApplicationPopoverTests.cs | 189 ++++++++++++++++++ .../Configuration/ConfigurationMangerTests.cs | 13 +- UnitTests/TestHelpers.cs | 72 +++++-- UnitTests/View/Layout/ToScreenTests.cs | 1 + UnitTests/View/SubviewTests.cs | 3 + UnitTests/Views/TableViewTests.cs | 1 + 14 files changed, 448 insertions(+), 41 deletions(-) create mode 100644 UnitTests/Application/ApplicationPopoverTests.cs diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index 7ba036d66c..b15710c555 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -62,10 +62,7 @@ private static void PopoverVisibleChanged (object? sender, EventArgs e) { Popover.Arrangement |= ViewArrangement.Overlapped; - if (Popover.ColorScheme is null) - { - Popover.ColorScheme = Top?.ColorScheme; - } + Popover.ColorScheme ??= Top?.ColorScheme; View.GetLocationEnsuringFullVisibility ( Popover, @@ -80,7 +77,12 @@ out StatusBar? sb Popover.Y = ny; Popover.SetRelativeLayout (Screen.Size); - Top.HasFocus = false; + + if (Top is { }) + { + Top.HasFocus = false; + } + Popover.SetFocus (); } } diff --git a/Terminal.Gui/Drawing/Glyphs.cs b/Terminal.Gui/Drawing/Glyphs.cs index 1268a576db..1d4bb0c86d 100644 --- a/Terminal.Gui/Drawing/Glyphs.cs +++ b/Terminal.Gui/Drawing/Glyphs.cs @@ -20,6 +20,11 @@ /// public class GlyphDefinitions { + // IMPORTANT: If you change these, make sure to update the ./Resources/config.json file as + // IMPORTANT: it is the source of truth for the default glyphs at runtime. + // IMPORTANT: Configuration Manager test SaveDefaults uses this class to generate the default config file + // IMPORTANT: in ./UnitTests/bin/Debug/netX.0/config.json + /// File icon. Defaults to ☰ (Trigram For Heaven) public Rune File { get; set; } = (Rune)'☰'; diff --git a/Terminal.Gui/Resources/config.json b/Terminal.Gui/Resources/config.json index 07ba336507..ec8cca508e 100644 --- a/Terminal.Gui/Resources/config.json +++ b/Terminal.Gui/Resources/config.json @@ -16,14 +16,173 @@ // to throw exceptions. "ConfigurationManager.ThrowOnJsonErrors": false, - "Application.NextTabKey": "Tab", - "Application.PrevTabKey": "Shift+Tab", + // --------------- Application Settings --------------- + "Key.Separator": "+", + + "Application.ArrangeKey": "Ctrl+F5", + "Application.Force16Colors": false, + "Application.ForceDriver": "", + "Application.IsMouseDisabled": false, "Application.NextTabGroupKey": "F6", + "Application.NextTabKey": "Tab", "Application.PrevTabGroupKey": "Shift+F6", + "Application.PrevTabKey": "Shift+Tab", "Application.QuitKey": "Esc", - "Application.ArrangeKey": "Ctrl+F5", - "Key.Separator": "+", + // --------------- Colors --------------- + + + // --------------- View Specific Settings --------------- + "ContextMenu.DefaultKey": "Shift+F10", + "FileDialog.MaxSearchResults": 10000, + "FileDialogStyle.DefaultUseColors": false, + "FileDialogStyle.DefaultUseUnicodeCharacters": false, + + // --------------- Glyphs --------------- + "Glyphs": { + "File": "☰", + "Folder": "꤉", + "HorizontalEllipsis": "…", + "VerticalFourDots": "⁞", + "CheckStateChecked": "☑", + "CheckStateUnChecked": "☐", + "CheckStateNone": "☒", + "Selected": "◉", + "UnSelected": "○", + "RightArrow": "►", + "LeftArrow": "◄", + "DownArrow": "▼", + "UpArrow": "▲", + "LeftDefaultIndicator": "►", + "RightDefaultIndicator": "◄", + "LeftBracket": "⟦", + "RightBracket": "⟧", + "BlocksMeterSegment": "▌", + "ContinuousMeterSegment": "█", + "Stipple": "░", + "Diamond": "◊", + "Close": "✘", + "Minimize": "❏", + "Maximize": "✽", + "Dot": "∙", + "BlackCircle": "●", + "Expand": "+", + "Collapse": "-", + "IdenticalTo": "≡", + "Move": "◊", + "SizeHorizontal": "↔", + "SizeVertical": "↕", + "SizeTopLeft": "↖", + "SizeTopRight": "↗", + "SizeBottomRight": "↘", + "SizeBottomLeft": "↙", + "Apple": "\uD83C\uDF4E", + "AppleBMP": "❦", + "HLine": "─", + "VLine": "│", + "HLineDbl": "═", + "VLineDbl": "║", + "HLineHvDa2": "╍", + "VLineHvDa3": "┇", + "HLineHvDa3": "┅", + "HLineHvDa4": "┉", + "VLineHvDa2": "╏", + "VLineHvDa4": "┋", + "HLineDa2": "╌", + "VLineDa3": "┆", + "HLineDa3": "┄", + "HLineDa4": "┈", + "VLineDa2": "╎", + "VLineDa4": "┊", + "HLineHv": "━", + "VLineHv": "┃", + "HalfLeftLine": "╴", + "HalfTopLine": "╵", + "HalfRightLine": "╶", + "HalfBottomLine": "╷", + "HalfLeftLineHv": "╸", + "HalfTopLineHv": "╹", + "HalfRightLineHv": "╺", + "HalfBottomLineLt": "╻", + "RightSideLineLtHv": "╼", + "BottomSideLineLtHv": "╽", + "LeftSideLineHvLt": "╾", + "TopSideLineHvLt": "╿", + "ULCorner": "┌", + "ULCornerDbl": "╔", + "ULCornerR": "╭", + "ULCornerHv": "┏", + "ULCornerHvLt": "┎", + "ULCornerLtHv": "┍", + "ULCornerDblSingle": "╓", + "ULCornerSingleDbl": "╒", + "LLCorner": "└", + "LLCornerHv": "┗", + "LLCornerHvLt": "┖", + "LLCornerLtHv": "┕", + "LLCornerDbl": "╚", + "LLCornerSingleDbl": "╘", + "LLCornerDblSingle": "╙", + "LLCornerR": "╰", + "URCorner": "┐", + "URCornerDbl": "╗", + "URCornerR": "╮", + "URCornerHv": "┓", + "URCornerHvLt": "┑", + "URCornerLtHv": "┒", + "URCornerDblSingle": "╖", + "URCornerSingleDbl": "╕", + "LRCorner": "┘", + "LRCornerDbl": "╝", + "LRCornerR": "╯", + "LRCornerHv": "┛", + "LRCornerDblSingle": "╜", + "LRCornerSingleDbl": "╛", + "LRCornerLtHv": "┙", + "LRCornerHvLt": "┚", + "LeftTee": "├", + "LeftTeeDblH": "╞", + "LeftTeeDblV": "╟", + "LeftTeeDbl": "╠", + "LeftTeeHvH": "┝", + "LeftTeeHvV": "┠", + "LeftTeeHvDblH": "┣", + "RightTee": "┤", + "RightTeeDblH": "╡", + "RightTeeDblV": "╢", + "RightTeeDbl": "╣", + "RightTeeHvH": "┥", + "RightTeeHvV": "┨", + "RightTeeHvDblH": "┫", + "TopTee": "┬", + "TopTeeDblH": "╤", + "TopTeeDblV": "╥", + "TopTeeDbl": "╦", + "TopTeeHvH": "┯", + "TopTeeHvV": "┰", + "TopTeeHvDblH": "┳", + "BottomTee": "┴", + "BottomTeeDblH": "╧", + "BottomTeeDblV": "╨", + "BottomTeeDbl": "╩", + "BottomTeeHvH": "┷", + "BottomTeeHvV": "┸", + "BottomTeeHvDblH": "┻", + "Cross": "┼", + "CrossDblH": "╪", + "CrossDblV": "╫", + "CrossDbl": "╬", + "CrossHvH": "┿", + "CrossHvV": "╂", + "CrossHv": "╋", + "ShadowVerticalStart": "▖", + "ShadowVertical": "▌", + "ShadowHorizontalStart": "▝", + "ShadowHorizontal": "▀", + "ShadowHorizontalEnd": "▘" + }, + + // --------------- Themes ----------------- "Theme": "Default", "Themes": [ { diff --git a/Terminal.Gui/View/Adornment/Border.cs b/Terminal.Gui/View/Adornment/Border.cs index 164a4249ea..6e584fb86e 100644 --- a/Terminal.Gui/View/Adornment/Border.cs +++ b/Terminal.Gui/View/Adornment/Border.cs @@ -580,6 +580,8 @@ out _ Application.UngrabMouse (); SetPressedHighlight (HighlightStyle.None); + EndArrangeMode (); + return true; } diff --git a/Terminal.Gui/View/View.Hierarchy.cs b/Terminal.Gui/View/View.Hierarchy.cs index 2e02716281..f89f4ec333 100644 --- a/Terminal.Gui/View/View.Hierarchy.cs +++ b/Terminal.Gui/View/View.Hierarchy.cs @@ -246,18 +246,18 @@ public virtual void RemoveAll () /// /// Gets whether is in the Subview hierarchy of . /// - /// - /// + /// The View at the start of the hierarchy. + /// The View to test. /// Will search the subview hierarchy of the adornments if true. /// public static bool IsInHierarchy (View? start, View? view, bool includeAdornments = false) { - if (view is null) + if (view is null || start is null) { return false; } - if (view == start || start is null) + if (view == start) { return true; } diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index f8af513a52..3c454d7016 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -46,9 +46,16 @@ out StatusBar? statusBar View? superView; statusBar = null!; + if (Application.Driver is null) + { + nx = targetX; + ny = targetY; + return null; + } + if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { - maxDimension = Driver.Cols; + maxDimension = Application.Driver.Cols; superView = Application.Top; } else diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 03c7028c00..2c555e4796 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -648,6 +648,11 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (Application.Popover?.Visible == true) { + if (Application.Top is { }) + { + viewsUnderMouse.Add (Application.Top); + } + start = Application.Popover; } diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index b448a1374f..10dacc2acd 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -420,7 +420,7 @@ public bool SetFocus () /// private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false) { - Debug.Assert (IsInHierarchy (SuperView, this)); + Debug.Assert (SuperView is null || IsInHierarchy (SuperView, this)); // Pre-conditions if (_hasFocus) diff --git a/UnitTests/Application/ApplicationPopoverTests.cs b/UnitTests/Application/ApplicationPopoverTests.cs new file mode 100644 index 0000000000..00c1c3fde2 --- /dev/null +++ b/UnitTests/Application/ApplicationPopoverTests.cs @@ -0,0 +1,189 @@ +using Xunit.Abstractions; + +namespace Terminal.Gui.ApplicationTests; + +using System; +using Terminal.Gui; +using Xunit; + +public class ApplicationPopoverTests +{ + [Fact] + public void Popover_SetAndGet () + { + // Arrange + var popover = new View (); + + // Act + Application.Popover = popover; + + // Assert + Assert.Equal (popover, Application.Popover); + } + + [Fact] + public void Popover_SetToNull () + { + // Arrange + var popover = new View (); + Application.Popover = popover; + + // Act + Application.Popover = null; + + // Assert + Assert.Null (Application.Popover); + } + + [Fact] + public void Popover_VisibleChangedEvent () + { + // Arrange + var popover = new View () + { + Visible = false + }; + Application.Popover = popover; + bool eventTriggered = false; + + popover.VisibleChanged += (sender, e) => eventTriggered = true; + + // Act + popover.Visible = true; + + // Assert + Assert.True (eventTriggered); + } + + [Fact] + public void Popover_InitializesCorrectly () + { + // Arrange + var popover = new View (); + + // Act + Application.Popover = popover; + + // Assert + Assert.True (popover.IsInitialized); + } + + [Fact] + public void Popover_SetsColorScheme () + { + // Arrange + var popover = new View (); + var topColorScheme = new ColorScheme (); + Application.Top = new Toplevel { ColorScheme = topColorScheme }; + + // Act + Application.Popover = popover; + + // Assert + Assert.Equal (topColorScheme, popover.ColorScheme); + } + + [Fact] + public void Popover_VisibleChangedToTrue_SetsFocus () + { + // Arrange + var popover = new View () + { + Visible = false, + CanFocus = true + }; + Application.Popover = popover; + + // Act + popover.Visible = true; + + // Assert + Assert.True (popover.Visible); + Assert.True (popover.HasFocus); + } + + [Fact] + public void Popover_VisibleChangedToFalse_Hides_And_Removes_Focus () + { + // Arrange + var popover = new View () + { + Visible = false, + CanFocus = true + }; + Application.Popover = popover; + popover.Visible = true; + + // Act + popover.Visible = false; + + // Assert + Assert.False (popover.Visible); + Assert.False (popover.HasFocus); + } + + [Fact] + public void Popover_Quit_Command_Hides () + { + // Arrange + var popover = new View () + { + Visible = false, + CanFocus = true + }; + Application.Popover = popover; + popover.Visible = true; + Assert.True (popover.Visible); + Assert.True (popover.HasFocus); + + // Act + Application.OnKeyDown (Application.QuitKey); + + // Assert + Assert.False (popover.Visible); + Assert.False (popover.HasFocus); + } + + //[Theory] + //[InlineData (0, 0, false)] + //[InlineData (5, 5, true)] + //[InlineData (10, 10, false)] + //[InlineData (5, 10, false)] + //[InlineData (9, 9, false)] + + + //public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) + //{ + // // Arrange + // Application.Top = new Toplevel () + // { + // Id = "top", + // Height = 10, + // Width = 10, + // }; + // var popover = new View () + // { + // Id = "popover", + // X = 5, + // Y = 5, + // Width = 1, + // Height = 1, + // Visible = false, + // CanFocus = true + // }; + + // Application.Popover = popover; + // popover.Visible = true; + // Assert.True (popover.Visible); + // Assert.True (popover.HasFocus); + + // // Act + // Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Clicked, ScreenPosition = new (mouseX, mouseY) }); + + // // Assert + // Assert.Equal (expectedVisible, popover.Visible); + + // Application.Top.Dispose (); + // Application.ResetState (ignoreDisposed: true); + //} +} diff --git a/UnitTests/Configuration/ConfigurationMangerTests.cs b/UnitTests/Configuration/ConfigurationMangerTests.cs index 5f8e767c01..87db75ba9c 100644 --- a/UnitTests/Configuration/ConfigurationMangerTests.cs +++ b/UnitTests/Configuration/ConfigurationMangerTests.cs @@ -184,8 +184,8 @@ void ConfigurationManager_Updated (object sender, ConfigurationManagerEventArgs Assert.True (fired); Updated -= ConfigurationManager_Updated; - Reset (); Locations = savedLocations; + Reset (); } [Fact] @@ -227,6 +227,7 @@ public void LoadConfigurationFromAllSources_ShouldLoadSettingsFromAllSources () [Fact] public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same () { + ConfigLocations savedLocations = Locations; Locations = ConfigLocations.DefaultOnly; // arrange @@ -269,16 +270,20 @@ public void Reset_and_ResetLoadWithLibraryResourcesOnly_are_same () Assert.Equal (KeyCode.Esc, Application.QuitKey.KeyCode); Assert.Equal (Key.F6, Application.NextTabGroupKey); Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey); + Locations = savedLocations; Reset (); } [Fact] public void Reset_Resets () { + ConfigLocations savedLocations = Locations; Locations = ConfigLocations.DefaultOnly; Reset (); Assert.NotEmpty (Themes!); Assert.Equal ("Default", Themes.Theme); + Locations = savedLocations; + Reset (); } //[Fact ()] @@ -426,10 +431,9 @@ public void TestConfigPropertyOmitClassName () Assert.True (scp!.Scope == typeof (ThemeScope)); Assert.True (scp.OmitClassName); + Locations = savedLocations; Reset (); Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo); - - Locations = savedLocations; } [Fact] @@ -826,9 +830,8 @@ public void TestConfigurationManagerUpdateFromJson () Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground); Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background); - Reset (); - Locations = savedLocations; + Reset (); } [Fact] diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index 265037cb53..62cad2635f 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -48,7 +48,7 @@ public AutoInitShutdownAttribute ( bool useFakeClipboard = true, bool fakeClipboardAlwaysThrowsNotSupportedException = false, bool fakeClipboardIsSupportedAlwaysTrue = false, - ConfigurationManager.ConfigLocations configLocation = ConfigurationManager.ConfigLocations.None + ConfigLocations configLocation = ConfigLocations.None ) { AutoInit = autoInit; @@ -59,7 +59,7 @@ public AutoInitShutdownAttribute ( FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException = fakeClipboardAlwaysThrowsNotSupportedException; FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue; - ConfigurationManager.Locations = configLocation; + Locations = configLocation; } private readonly Type _driverType; @@ -97,14 +97,17 @@ public override void After (MethodInfo methodUnderTest) { #if DEBUG_IDISPOSABLE Responder.Instances.Clear (); - Application.ResetState (ignoreDisposed: true); + Application.ResetState (true); #endif - ConfigurationManager.Reset (); - ConfigurationManager.Locations = CM.ConfigLocations.None; } + } + // Force the use of the default config file + Locations = ConfigLocations.DefaultOnly; + Reset (); - } + // Enable subsequent tests that call Init to get all config files (the default). + Locations = ConfigLocations.All; } public override void Before (MethodInfo methodUnderTest) @@ -113,7 +116,9 @@ public override void Before (MethodInfo methodUnderTest) if (AutoInit) { - ConfigurationManager.Reset (); + // Force the use of the default config file + Locations = ConfigLocations.DefaultOnly; + Reset (); #if DEBUG_IDISPOSABLE @@ -134,10 +139,15 @@ public override void Before (MethodInfo methodUnderTest) private bool AutoInit { get; } private List _savedValues; - - } +/// +/// Enables test functions annotated with the [TestRespondersDisposed] attribute to ensure all Views are disposed. +/// +/// +/// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly. +/// On After, sets Configuration.Locations to ConfigLocations.All. +/// [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)] public class TestRespondersDisposed : BeforeAfterTestAttribute { @@ -147,6 +157,10 @@ public override void After (MethodInfo methodUnderTest) { Debug.WriteLine ($"After: {methodUnderTest.Name}"); base.After (methodUnderTest); + + // Reset the to default All + Locations = ConfigLocations.All; + Reset (); #if DEBUG_IDISPOSABLE Assert.Empty (Responder.Instances); @@ -156,6 +170,11 @@ public override void After (MethodInfo methodUnderTest) public override void Before (MethodInfo methodUnderTest) { Debug.WriteLine ($"Before: {methodUnderTest.Name}"); + + // Force the use of the default config file + Locations = ConfigLocations.DefaultOnly; + Reset (); + base.Before (methodUnderTest); #if DEBUG_IDISPOSABLE @@ -167,15 +186,17 @@ public override void Before (MethodInfo methodUnderTest) } // TODO: Make this inherit from TestRespondersDisposed so that all tests that don't dispose Views correctly can be identified and fixed +/// +/// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new +/// FakeDriver(). The driver is set up with 25 rows and columns. +/// +/// +/// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly. +/// On After, sets Configuration.Locations to ConfigLocations.All. +/// [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)] public class SetupFakeDriverAttribute : BeforeAfterTestAttribute { - /// - /// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new - /// FakeDriver(). The driver is setup with 25 rows and columns. - /// - public SetupFakeDriverAttribute () { } - public override void After (MethodInfo methodUnderTest) { Debug.WriteLine ($"After: {methodUnderTest.Name}"); @@ -185,16 +206,25 @@ public override void After (MethodInfo methodUnderTest) Application.Driver = null; base.After (methodUnderTest); + + // Reset the to default All + Locations = ConfigLocations.All; + Reset (); } public override void Before (MethodInfo methodUnderTest) { Debug.WriteLine ($"Before: {methodUnderTest.Name}"); - Application.ResetState (ignoreDisposed: true); + // Force the use of the default config file + Locations = ConfigLocations.DefaultOnly; + Reset (); + + Application.ResetState (true); Assert.Null (Application.Driver); Application.Driver = new FakeDriver { Rows = 25, Cols = 25 }; base.Before (methodUnderTest); + } } @@ -701,11 +731,11 @@ private static string ReplaceNewLinesToPlatformSpecific (string toReplace) string replaced = toReplace; replaced = Environment.NewLine.Length switch - { - 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), - 1 => replaced.Replace ("\r\n", Environment.NewLine), - var _ => replaced - }; + { + 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), + 1 => replaced.Replace ("\r\n", Environment.NewLine), + var _ => replaced + }; return replaced; } diff --git a/UnitTests/View/Layout/ToScreenTests.cs b/UnitTests/View/Layout/ToScreenTests.cs index 17153badc0..8b0902102d 100644 --- a/UnitTests/View/Layout/ToScreenTests.cs +++ b/UnitTests/View/Layout/ToScreenTests.cs @@ -1,5 +1,6 @@ using Xunit.Abstractions; using static System.Net.Mime.MediaTypeNames; +using static Terminal.Gui.ConfigurationManager; namespace Terminal.Gui.LayoutTests; diff --git a/UnitTests/View/SubviewTests.cs b/UnitTests/View/SubviewTests.cs index e25d923a4b..7d6e779a57 100644 --- a/UnitTests/View/SubviewTests.cs +++ b/UnitTests/View/SubviewTests.cs @@ -499,4 +499,7 @@ public void MoveSubviewTowardsEnd () superView.MoveSubviewTowardsEnd (subview2); Assert.Equal (subview2, superView.Subviews [^1]); } + + //[Fact] + //public void IsInHierachy_ } diff --git a/UnitTests/Views/TableViewTests.cs b/UnitTests/Views/TableViewTests.cs index ce911c8fc0..350b937069 100644 --- a/UnitTests/Views/TableViewTests.cs +++ b/UnitTests/Views/TableViewTests.cs @@ -2568,6 +2568,7 @@ public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect () [SetupFakeDriver] public void TestTableViewCheckboxes_ByObject () { + Assert.Equal(ConfigurationManager.ConfigLocations.DefaultOnly, ConfigurationManager.Locations); TableView tv = GetPetTable (out EnumerableTableSource source); tv.LayoutSubviews (); IReadOnlyCollection pets = source.Data; From 1369bb25f0aef26cc534617aa254cea0ddfdbf58 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 11:05:15 -0600 Subject: [PATCH 11/75] Can't set ForceDriver to empty in Resources/config.json. --- Terminal.Gui/Application/Application.Driver.cs | 1 + Terminal.Gui/Resources/config.json | 2 +- Terminal.Gui/View/Layout/DimAuto.cs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Application/Application.Driver.cs b/Terminal.Gui/Application/Application.Driver.cs index 2abeb1337a..6509babfbf 100644 --- a/Terminal.Gui/Application/Application.Driver.cs +++ b/Terminal.Gui/Application/Application.Driver.cs @@ -16,6 +16,7 @@ public static partial class Application // Driver abstractions [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] public static bool Force16Colors { get; set; } + // BUGBUG: ForceDriver should be nullable. /// /// Forces the use of the specified driver (one of "fake", "ansi", "curses", "net", or "windows"). If not /// specified, the driver is selected based on the platform. diff --git a/Terminal.Gui/Resources/config.json b/Terminal.Gui/Resources/config.json index ec8cca508e..ca6ab6a842 100644 --- a/Terminal.Gui/Resources/config.json +++ b/Terminal.Gui/Resources/config.json @@ -21,7 +21,7 @@ "Application.ArrangeKey": "Ctrl+F5", "Application.Force16Colors": false, - "Application.ForceDriver": "", + //"Application.ForceDriver": "", // TODO: ForceDriver should be nullable "Application.IsMouseDisabled": false, "Application.NextTabGroupKey": "F6", "Application.NextTabKey": "Tab", diff --git a/Terminal.Gui/View/Layout/DimAuto.cs b/Terminal.Gui/View/Layout/DimAuto.cs index a2061416dc..bd4f3d212f 100644 --- a/Terminal.Gui/View/Layout/DimAuto.cs +++ b/Terminal.Gui/View/Layout/DimAuto.cs @@ -35,7 +35,7 @@ internal override int Calculate (int location, int superviewContentSize, View us int screenX4 = dimension == Dimension.Width ? Application.Screen.Width * 4 : Application.Screen.Height * 4; int autoMax = MaximumContentDim?.GetAnchor (superviewContentSize) ?? screenX4; - Debug.Assert (autoMin <= autoMax, "MinimumContentDim must be less than or equal to MaximumContentDim."); + Debug.WriteLineIf (autoMin <= autoMax, "MinimumContentDim must be less than or equal to MaximumContentDim."); if (Style.FastHasFlags (DimAutoStyle.Text)) { From 3aa3598883e51151b936f14011165fd4817e0921 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 11:09:24 -0600 Subject: [PATCH 12/75] added BUGBUG --- Terminal.Gui/Application/Application.Driver.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Terminal.Gui/Application/Application.Driver.cs b/Terminal.Gui/Application/Application.Driver.cs index 6509babfbf..017a14df7a 100644 --- a/Terminal.Gui/Application/Application.Driver.cs +++ b/Terminal.Gui/Application/Application.Driver.cs @@ -8,6 +8,7 @@ public static partial class Application // Driver abstractions /// Gets the that has been selected. See also . public static ConsoleDriver? Driver { get; internal set; } + // BUGBUG: Force16Colors should be nullable. /// /// Gets or sets whether will be forced to output only the 16 colors defined in /// . The default is , meaning 24-bit (TrueColor) colors will be output From ae4b17251863b01d5b45f35054e4d4391ee0b166 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 11:52:24 -0600 Subject: [PATCH 13/75] Made Position/ScreenPosition clear --- Terminal.Gui/Application/Application.Mouse.cs | 45 +++++----- Terminal.Gui/View/View.Mouse.cs | 2 +- .../Application/ApplicationPopoverTests.cs | 84 +++++++++---------- UnitTests/Application/ApplicationTests.cs | 2 +- .../Mouse/ApplicationMouseTests.cs | 20 ++--- UnitTests/UICatalog/ScenarioTests.cs | 27 ++++++ .../View/Mouse/GetViewsUnderMouseTests.cs | 53 ++++++++++++ UnitTests/View/Mouse/MouseTests.cs | 4 +- UnitTests/View/SubviewTests.cs | 7 +- UnitTests/Views/ColorPickerTests.cs | 4 +- UnitTests/Views/ContextMenuTests.cs | 22 ++--- UnitTests/Views/LabelTests.cs | 6 +- UnitTests/Views/ListViewTests.cs | 10 +-- UnitTests/Views/MenuBarTests.cs | 10 +-- UnitTests/Views/ScrollBarViewTests.cs | 4 +- UnitTests/Views/ShortcutTests.cs | 4 +- UnitTests/Views/TabViewTests.cs | 18 ++-- UnitTests/Views/ToplevelTests.cs | 44 +++++----- 18 files changed, 226 insertions(+), 140 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 806424c6c6..ad35fa8f07 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -137,7 +137,10 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } - List currentViewsUnderMouse = View.GetViewsUnderMouse (mouseEvent.Position); + // The position of the mouse is the same as the screen position at the application level. + mouseEvent.Position = mouseEvent.ScreenPosition; + + List currentViewsUnderMouse = View.GetViewsUnderMouse (mouseEvent.ScreenPosition); View? deepestViewUnderMouse = currentViewsUnderMouse.LastOrDefault (); @@ -168,10 +171,10 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) // avoid one or two of these checks in the process, as well. WantContinuousButtonPressedView = deepestViewUnderMouse switch - { - { WantContinuousButtonPressed: true } => deepestViewUnderMouse, - _ => null - }; + { + { WantContinuousButtonPressed: true } => deepestViewUnderMouse, + _ => null + }; if (Popover is { Visible: true } @@ -190,45 +193,45 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } - // TODO: Move this after call to RaiseMouseEnterLeaveEvents once MouseEnter/Leave don't use MouseEvent anymore. - MouseEvent? me; + // Create a view-relative mouse event to send to the view that is under the mouse. + MouseEvent? viewMouseEvent; if (deepestViewUnderMouse is Adornment adornment) { - Point frameLoc = adornment.ScreenToFrame (mouseEvent.Position); + Point frameLoc = adornment.ScreenToFrame (mouseEvent.ScreenPosition); - me = new () + viewMouseEvent = new () { Position = frameLoc, Flags = mouseEvent.Flags, - ScreenPosition = mouseEvent.Position, + ScreenPosition = mouseEvent.ScreenPosition, View = deepestViewUnderMouse }; } else if (deepestViewUnderMouse.ViewportToScreen (Rectangle.Empty with { Size = deepestViewUnderMouse.Viewport.Size }).Contains (mouseEvent.Position)) { - Point viewportLocation = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position); + Point viewportLocation = deepestViewUnderMouse.ScreenToViewport (mouseEvent.ScreenPosition); - me = new () + viewMouseEvent = new () { Position = viewportLocation, Flags = mouseEvent.Flags, - ScreenPosition = mouseEvent.Position, + ScreenPosition = mouseEvent.ScreenPosition, View = deepestViewUnderMouse }; } else { - Debug.Fail ("This should never happen"); + Debug.Fail("The mouse was outside of any View. But this makes no sense as deepestViewUnderMouse is not null."); return; } - RaiseMouseEnterLeaveEvents (me.ScreenPosition, currentViewsUnderMouse); + RaiseMouseEnterLeaveEvents (viewMouseEvent.ScreenPosition, currentViewsUnderMouse); WantContinuousButtonPressedView = deepestViewUnderMouse.WantContinuousButtonPressed ? deepestViewUnderMouse : null; - while (deepestViewUnderMouse.NewMouseEvent (me) is not true && MouseGrabView is not { }) + while (deepestViewUnderMouse.NewMouseEvent (viewMouseEvent) is not true && MouseGrabView is not { }) { if (deepestViewUnderMouse is Adornment adornmentView) { @@ -244,13 +247,13 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) break; } - Point boundsPoint = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position); + Point boundsPoint = deepestViewUnderMouse.ScreenToViewport (mouseEvent.ScreenPosition); - me = new () + viewMouseEvent = new () { Position = boundsPoint, Flags = mouseEvent.Flags, - ScreenPosition = mouseEvent.Position, + ScreenPosition = mouseEvent.ScreenPosition, View = deepestViewUnderMouse }; } @@ -269,13 +272,13 @@ internal static bool GrabMouse (View? deepestViewUnderMouse, MouseEvent mouseEve // If the mouse is grabbed, send the event to the view that grabbed it. // The coordinates are relative to the Bounds of the view that grabbed the mouse. - Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position); + Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.ScreenPosition); var viewRelativeMouseEvent = new MouseEvent { Position = frameLoc, Flags = mouseEvent.Flags, - ScreenPosition = mouseEvent.Position, + ScreenPosition = mouseEvent.ScreenPosition, View = deepestViewUnderMouse ?? MouseGrabView }; diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 2c555e4796..7e840cab4f 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -648,7 +648,7 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (Application.Popover?.Visible == true) { - if (Application.Top is { }) + if (Application.Top?.Contains (location) ?? false) { viewsUnderMouse.Add (Application.Top); } diff --git a/UnitTests/Application/ApplicationPopoverTests.cs b/UnitTests/Application/ApplicationPopoverTests.cs index 00c1c3fde2..64fde7cd0c 100644 --- a/UnitTests/Application/ApplicationPopoverTests.cs +++ b/UnitTests/Application/ApplicationPopoverTests.cs @@ -144,46 +144,46 @@ public void Popover_Quit_Command_Hides () Assert.False (popover.HasFocus); } - //[Theory] - //[InlineData (0, 0, false)] - //[InlineData (5, 5, true)] - //[InlineData (10, 10, false)] - //[InlineData (5, 10, false)] - //[InlineData (9, 9, false)] - - - //public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) - //{ - // // Arrange - // Application.Top = new Toplevel () - // { - // Id = "top", - // Height = 10, - // Width = 10, - // }; - // var popover = new View () - // { - // Id = "popover", - // X = 5, - // Y = 5, - // Width = 1, - // Height = 1, - // Visible = false, - // CanFocus = true - // }; - - // Application.Popover = popover; - // popover.Visible = true; - // Assert.True (popover.Visible); - // Assert.True (popover.HasFocus); - - // // Act - // Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Clicked, ScreenPosition = new (mouseX, mouseY) }); - - // // Assert - // Assert.Equal (expectedVisible, popover.Visible); - - // Application.Top.Dispose (); - // Application.ResetState (ignoreDisposed: true); - //} + [Theory] + [InlineData (0, 0, false)] + [InlineData (5, 5, true)] + [InlineData (10, 10, false)] + [InlineData (5, 10, false)] + [InlineData (9, 9, false)] + + + public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) + { + // Arrange + Application.Top = new Toplevel () + { + Id = "top", + Height = 10, + Width = 10, + }; + var popover = new View () + { + Id = "popover", + X = 5, + Y = 5, + Width = 1, + Height = 1, + Visible = false, + CanFocus = true + }; + + Application.Popover = popover; + popover.Visible = true; + Assert.True (popover.Visible); + Assert.True (popover.HasFocus); + + // Act + Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (mouseX, mouseY) }); + + // Assert + Assert.Equal (expectedVisible, popover.Visible); + + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); + } } diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 9153cd2906..a97ed51553 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -896,7 +896,7 @@ public void Run_A_Modal_Toplevel_Refresh_Background_On_Moving () Assert.Equal (new (0, 0), w.Frame.Location); // Move down and to the right. - Application.OnMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Assert.Equal (new (1, 1), w.Frame.Location); Application.End (rs); diff --git a/UnitTests/Application/Mouse/ApplicationMouseTests.cs b/UnitTests/Application/Mouse/ApplicationMouseTests.cs index df508b452c..88a44d5d12 100644 --- a/UnitTests/Application/Mouse/ApplicationMouseTests.cs +++ b/UnitTests/Application/Mouse/ApplicationMouseTests.cs @@ -42,13 +42,13 @@ public void MouseEventCoordinatesAreScreenRelative ( bool expectedClicked ) { - var mouseEvent = new MouseEvent { Position = new (clickX, clickY), Flags = MouseFlags.Button1Pressed }; + var mouseEvent = new MouseEvent { ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Pressed }; var clicked = false; void OnApplicationOnMouseEvent (object s, MouseEvent e) { - Assert.Equal (expectedX, e.Position.X); - Assert.Equal (expectedY, e.Position.Y); + Assert.Equal (expectedX, e.ScreenPosition.X); + Assert.Equal (expectedY, e.ScreenPosition.Y); clicked = true; } @@ -116,7 +116,7 @@ bool expectedClicked Height = size.Height }; - var mouseEvent = new MouseEvent { Position = new (clickX, clickY), Flags = MouseFlags.Button1Clicked }; + var mouseEvent = new MouseEvent { ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Clicked }; view.MouseClick += (s, e) => { @@ -218,7 +218,7 @@ bool expectedClicked Application.Top.Add (view); - var mouseEvent = new MouseEvent { Position = new (clickX, clickY), Flags = MouseFlags.Button1Clicked }; + var mouseEvent = new MouseEvent { ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Clicked }; view.MouseClick += (s, e) => { @@ -261,7 +261,7 @@ public void MouseGrabView_WithNullMouseEventView () Assert.True (tf.HasFocus); Assert.Null (Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (5, 5), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (5, 5), Flags = MouseFlags.ReportMousePosition }); Assert.Equal (sv, Application.MouseGrabView); @@ -275,15 +275,15 @@ public void MouseGrabView_WithNullMouseEventView () // another toplevel (Dialog) was opened Assert.Null (Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (5, 5), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (5, 5), Flags = MouseFlags.ReportMousePosition }); Assert.Null (Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (40, 12), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (40, 12), Flags = MouseFlags.ReportMousePosition }); Assert.Null (Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed }); Assert.Null (Application.MouseGrabView); @@ -402,7 +402,7 @@ public void View_Is_Responsible_For_Calling_UnGrabMouse_Before_Being_Disposed () Assert.True (view.WasDisposed); #endif - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed }); Assert.Null (Application.MouseGrabView); Assert.Equal (0, count); top.Dispose (); diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index c179ef0128..b422abbe0b 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -30,6 +30,10 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) Assert.Null (_timeoutLock); _timeoutLock = new (); + // Disable any UIConfig settings + ConfigurationManager.ConfigLocations savedConfigLocations = ConfigurationManager.Locations; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; + // If a previous test failed, this will ensure that the Application is in a clean state Application.ResetState (true); @@ -72,6 +76,9 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) _timeoutLock = null; } + // Restore the configuration locations + ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Reset (); return; void OnApplicationOnInitializedChanged (object s, EventArgs a) @@ -109,6 +116,10 @@ bool ForceCloseCallback () Assert.Fail ( $"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey} after {abortTime}ms and {iterationCount} iterations. Force quit."); + // Restore the configuration locations + ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Reset (); + Application.ResetState (true); return false; @@ -135,6 +146,10 @@ void OnApplicationOnIteration (object s, IterationEventArgs a) [Fact] public void Run_All_Views_Tester_Scenario () { + // Disable any UIConfig settings + ConfigurationManager.ConfigLocations savedConfigLocations = ConfigurationManager.Locations; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; + Window _leftPane; ListView _classListView; FrameView _hostPane; @@ -370,6 +385,10 @@ public void Run_All_Views_Tester_Scenario () top.Dispose (); Application.Shutdown (); + // Restore the configuration locations + ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Reset (); + void DimPosChanged (View view) { if (view == null) @@ -586,6 +605,10 @@ View CreateClass (Type type) [Fact] public void Run_Generic () { + // Disable any UIConfig settings + ConfigurationManager.ConfigLocations savedConfigLocations = ConfigurationManager.Locations; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; + ObservableCollection scenarios = Scenario.GetScenarios (); Assert.NotEmpty (scenarios); @@ -650,6 +673,10 @@ public void Run_Generic () // Shutdown must be called to safely clean up Application if Init has been called Application.Shutdown (); + // Restore the configuration locations + ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Reset (); + #if DEBUG_IDISPOSABLE Assert.Empty (Responder.Instances); #endif diff --git a/UnitTests/View/Mouse/GetViewsUnderMouseTests.cs b/UnitTests/View/Mouse/GetViewsUnderMouseTests.cs index 474dfa687f..6b9d65922c 100644 --- a/UnitTests/View/Mouse/GetViewsUnderMouseTests.cs +++ b/UnitTests/View/Mouse/GetViewsUnderMouseTests.cs @@ -802,4 +802,57 @@ public void GetViewsUnderMouse_Tiled_Subviews (int mouseX, int mouseY, string [] Application.Top.Dispose (); Application.ResetState (true); } + + [Theory] + [InlineData (0, 0, new [] { "top" })] + [InlineData (9, 9, new [] { "top" })] + [InlineData (10, 10, new string [] { })] + [InlineData (-1, -1, new string [] { })] + [InlineData (1, 1, new [] { "top", "view" })] + [InlineData (1, 2, new [] { "top", "view" })] + [InlineData (2, 1, new [] { "top", "view" })] + [InlineData (2, 2, new [] { "top", "view", "popover" })] + [InlineData (3, 3, new [] { "top" })] // clipped + [InlineData (2, 3, new [] { "top" })] // clipped + public void GetViewsUnderMouse_Popover (int mouseX, int mouseY, string [] viewIdStrings) + { + // Arrange + Application.Top = new () + { + Frame = new (0, 0, 10, 10), + Id = "top" + }; + + var view = new View + { + Id = "view", + X = 1, + Y = 1, + Width = 2, + Height = 2, + Arrangement = ViewArrangement.Overlapped + }; // at 1,1 to 3,2 (screen) + + var popOver = new View + { + Id = "popover", + X = 1, + Y = 1, + Width = 2, + Height = 2, + Arrangement = ViewArrangement.Overlapped + }; // at 2,2 to 4,3 (screen) + + view.Add (popOver); + Application.Top.Add (view); + + List found = View.GetViewsUnderMouse (new (mouseX, mouseY)); + + string [] foundIds = found.Select (v => v!.Id).ToArray (); + + Assert.Equal (viewIdStrings, foundIds); + + Application.Top.Dispose (); + Application.ResetState (true); + } } diff --git a/UnitTests/View/Mouse/MouseTests.cs b/UnitTests/View/Mouse/MouseTests.cs index 28613dadfa..7440f4e982 100644 --- a/UnitTests/View/Mouse/MouseTests.cs +++ b/UnitTests/View/Mouse/MouseTests.cs @@ -74,9 +74,9 @@ public void ButtonPressed_In_Border_Starts_Drag (int marginThickness, int border Application.Begin (top); Assert.Equal (new Point (4, 4), testView.Frame.Location); - Application.OnMouseEvent (new () { Position = new (xy, xy), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (xy, xy), Flags = MouseFlags.Button1Pressed }); - Application.OnMouseEvent (new () { Position = new (xy + 1, xy + 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (xy + 1, xy + 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Assert.Equal (expectedMoved, new Point (5, 5) == testView.Frame.Location); top.Dispose (); diff --git a/UnitTests/View/SubviewTests.cs b/UnitTests/View/SubviewTests.cs index 7d6e779a57..bfd8804a00 100644 --- a/UnitTests/View/SubviewTests.cs +++ b/UnitTests/View/SubviewTests.cs @@ -500,6 +500,9 @@ public void MoveSubviewTowardsEnd () Assert.Equal (subview2, superView.Subviews [^1]); } - //[Fact] - //public void IsInHierachy_ + [Fact] + public void IsInHierarchy_ () + { + + } } diff --git a/UnitTests/Views/ColorPickerTests.cs b/UnitTests/Views/ColorPickerTests.cs index 167872ea35..0b5b050c33 100644 --- a/UnitTests/Views/ColorPickerTests.cs +++ b/UnitTests/Views/ColorPickerTests.cs @@ -437,7 +437,7 @@ public void ColorPicker_ClickingDifferentBars_ChangesFocus () Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Pressed, - Position = new (0, 1) + ScreenPosition = new (0, 1) }); //cp.Subviews.OfType () // .Single () @@ -456,7 +456,7 @@ public void ColorPicker_ClickingDifferentBars_ChangesFocus () Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Pressed, - Position = new (0, 2) + ScreenPosition = new (0, 2) }); //cp.Subviews.OfType () // .Single () diff --git a/UnitTests/Views/ContextMenuTests.cs b/UnitTests/Views/ContextMenuTests.cs index 431def120b..74d408cb9a 100644 --- a/UnitTests/Views/ContextMenuTests.cs +++ b/UnitTests/Views/ContextMenuTests.cs @@ -148,7 +148,7 @@ public void Draw_A_ContextMenu_Over_A_Borderless_Top () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (8, 2), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (8, 2), Flags = MouseFlags.Button3Clicked }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -231,7 +231,7 @@ public void Draw_A_ContextMenu_Over_A_Dialog () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (9, 3), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked }); var firstIteration = false; Application.RunIteration (ref rsDialog, ref firstIteration); @@ -287,7 +287,7 @@ public void Draw_A_ContextMenu_Over_A_Top_Dialog () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (9, 3), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1235,7 +1235,7 @@ public void UseSubMenusSingleFrame_True_By_Mouse () ); // X=5 is the border and so need to use at least one more - Application.OnMouseEvent (new MouseEvent { Position = new (6, 13), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 13), Flags = MouseFlags.Button1Clicked }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1253,7 +1253,7 @@ public void UseSubMenusSingleFrame_True_By_Mouse () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (6, 12), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 12), Flags = MouseFlags.Button1Clicked }); firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1327,7 +1327,7 @@ public void UseSubMenusSingleFrame_False_By_Mouse () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (6, 13), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1344,7 +1344,7 @@ public void UseSubMenusSingleFrame_False_By_Mouse () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (6, 14), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 14), Flags = MouseFlags.ReportMousePosition }); firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1362,7 +1362,7 @@ public void UseSubMenusSingleFrame_False_By_Mouse () output ); - Application.OnMouseEvent (new MouseEvent { Position = new (6, 13), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition }); firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -1399,7 +1399,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.Empty (Application._cachedViewsUnderMouse); // Right click on tf2 to open context menu - Application.OnMouseEvent (new () { Position = new (1, 3), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button3Clicked }); Assert.False (tf1.HasFocus); Assert.False (tf2.HasFocus); Assert.Equal (5, win.Subviews.Count); @@ -1409,7 +1409,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ()); // Click on tf1 to focus it, which cause context menu being closed - Application.OnMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Clicked }); Assert.True (tf1.HasFocus); Assert.False (tf2.HasFocus); Assert.Equal (4, win.Subviews.Count); @@ -1421,7 +1421,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.Equal (tf1, Application._cachedViewsUnderMouse.LastOrDefault ()); // Click on tf2 to focus it - Application.OnMouseEvent (new () { Position = new (1, 3), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked }); Assert.False (tf1.HasFocus); Assert.True (tf2.HasFocus); Assert.Equal (4, win.Subviews.Count); diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index f73a48db25..aa4b76de17 100644 --- a/UnitTests/Views/LabelTests.cs +++ b/UnitTests/Views/LabelTests.cs @@ -1389,7 +1389,7 @@ public void Label_CanFocus_True_Get_Focus_By_Mouse () Assert.True (view.HasFocus); // label can't focus so clicking on it has no effect - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked }); Assert.False (label.HasFocus); Assert.True (view.HasFocus); @@ -1399,12 +1399,12 @@ public void Label_CanFocus_True_Get_Focus_By_Mouse () Assert.True (view.HasFocus); // label can focus, so clicking on it set focus - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked }); Assert.True (label.HasFocus); Assert.False (view.HasFocus); // click on view - Application.OnMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 1), Flags = MouseFlags.Button1Clicked }); Assert.False (label.HasFocus); Assert.True (view.HasFocus); diff --git a/UnitTests/Views/ListViewTests.cs b/UnitTests/Views/ListViewTests.cs index 22fceba091..52fc4881bd 100644 --- a/UnitTests/Views/ListViewTests.cs +++ b/UnitTests/Views/ListViewTests.cs @@ -741,14 +741,14 @@ public void Clicking_On_Border_Is_Ignored () └─────┘", output); - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked }); Assert.Equal ("", selected); Assert.Equal (-1, lv.SelectedItem); Application.OnMouseEvent ( new () { - Position = new (1, 1), Flags = MouseFlags.Button1Clicked + ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Clicked }); Assert.Equal ("One", selected); Assert.Equal (0, lv.SelectedItem); @@ -756,7 +756,7 @@ public void Clicking_On_Border_Is_Ignored () Application.OnMouseEvent ( new () { - Position = new (1, 2), Flags = MouseFlags.Button1Clicked + ScreenPosition = new (1, 2), Flags = MouseFlags.Button1Clicked }); Assert.Equal ("Two", selected); Assert.Equal (1, lv.SelectedItem); @@ -764,7 +764,7 @@ public void Clicking_On_Border_Is_Ignored () Application.OnMouseEvent ( new () { - Position = new (1, 3), Flags = MouseFlags.Button1Clicked + ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked }); Assert.Equal ("Three", selected); Assert.Equal (2, lv.SelectedItem); @@ -772,7 +772,7 @@ public void Clicking_On_Border_Is_Ignored () Application.OnMouseEvent ( new () { - Position = new (1, 4), Flags = MouseFlags.Button1Clicked + ScreenPosition = new (1, 4), Flags = MouseFlags.Button1Clicked }); Assert.Equal ("Three", selected); Assert.Equal (2, lv.SelectedItem); diff --git a/UnitTests/Views/MenuBarTests.cs b/UnitTests/Views/MenuBarTests.cs index dc9db17fc3..deb4c2b525 100644 --- a/UnitTests/Views/MenuBarTests.cs +++ b/UnitTests/Views/MenuBarTests.cs @@ -243,7 +243,7 @@ public void Click_Another_View_Close_An_Open_Menu () top.Add (menu, btn); Application.Begin (top); - Application.OnMouseEvent (new () { Position = new (0, 4), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 4), Flags = MouseFlags.Button1Clicked }); Assert.True (btnClicked); top.Dispose (); } @@ -613,7 +613,7 @@ void ChangeMenuTitle (string title) output ); - Application.OnMouseEvent (new () { Position = new (20, 5), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (20, 5), Flags = MouseFlags.Button1Clicked }); firstIteration = false; @@ -646,7 +646,7 @@ void ChangeMenuTitle (string title) { menu.OpenMenu (); - Application.OnMouseEvent (new () { Position = new (20, 5 + i), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (20, 5 + i), Flags = MouseFlags.Button1Clicked }); firstIteration = false; Application.RunIteration (ref rsDialog, ref firstIteration); @@ -809,7 +809,7 @@ void ChangeMenuTitle (string title) output ); - Application.OnMouseEvent (new () { Position = new (20, 5), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (20, 5), Flags = MouseFlags.Button1Clicked }); firstIteration = false; @@ -831,7 +831,7 @@ void ChangeMenuTitle (string title) { menu.OpenMenu (); - Application.OnMouseEvent (new () { Position = new (20, 5 + i), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (20, 5 + i), Flags = MouseFlags.Button1Clicked }); firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); diff --git a/UnitTests/Views/ScrollBarViewTests.cs b/UnitTests/Views/ScrollBarViewTests.cs index cd02cef99a..5736b0c214 100644 --- a/UnitTests/Views/ScrollBarViewTests.cs +++ b/UnitTests/Views/ScrollBarViewTests.cs @@ -1176,7 +1176,7 @@ This is a test _output ); - Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new MouseEvent { ScreenPosition = new (15, 0), Flags = MouseFlags.Button1Clicked }); Assert.Null (Application.MouseGrabView); Assert.True (clicked); @@ -1200,7 +1200,7 @@ This is a test _output ); - Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new MouseEvent { ScreenPosition = new (15, 0), Flags = MouseFlags.Button1Clicked }); Assert.Null (Application.MouseGrabView); Assert.True (clicked); diff --git a/UnitTests/Views/ShortcutTests.cs b/UnitTests/Views/ShortcutTests.cs index dee6e14052..af172e9324 100644 --- a/UnitTests/Views/ShortcutTests.cs +++ b/UnitTests/Views/ShortcutTests.cs @@ -365,7 +365,7 @@ public void MouseClick_Fires_Accept (int x, int expectedAccept) Application.OnMouseEvent ( new () { - Position = new (x, 0), + ScreenPosition = new (x, 0), Flags = MouseFlags.Button1Clicked }); @@ -420,7 +420,7 @@ public void MouseClick_Button_CommandView_Fires_Accept (int x, int expectedAccep Application.OnMouseEvent ( new () { - Position = new (x, 0), + ScreenPosition = new (x, 0), Flags = MouseFlags.Button1Clicked }); diff --git a/UnitTests/Views/TabViewTests.cs b/UnitTests/Views/TabViewTests.cs index f8f2554346..d56550b35a 100644 --- a/UnitTests/Views/TabViewTests.cs +++ b/UnitTests/Views/TabViewTests.cs @@ -143,21 +143,21 @@ public void MouseClick_ChangesTab () // Waving mouse around does not trigger click for (var i = 0; i < 100; i++) { - args = new () { Position = new (i, 1), Flags = MouseFlags.ReportMousePosition }; + args = new () { ScreenPosition = new (i, 1), Flags = MouseFlags.ReportMousePosition }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Null (clicked); Assert.Equal (tab1, tv.SelectedTab); } - args = new () { Position = new (3, 1), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (3, 1), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Equal (tab1, clicked); Assert.Equal (tab1, tv.SelectedTab); // Click to tab2 - args = new () { Position = new (6, 1), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (6, 1), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Equal (tab2, clicked); @@ -170,7 +170,7 @@ public void MouseClick_ChangesTab () e.MouseEvent.Handled = true; }; - args = new () { Position = new (3, 1), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (3, 1), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); @@ -178,7 +178,7 @@ public void MouseClick_ChangesTab () Assert.Equal (tab1, clicked); Assert.Equal (tab2, tv.SelectedTab); - args = new () { Position = new (12, 1), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (12, 1), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); @@ -233,7 +233,7 @@ public void MouseClick_Right_Left_Arrows_ChangesTab () Application.Begin (top); // Click the right arrow - var args = new MouseEvent { Position = new (6, 2), Flags = MouseFlags.Button1Clicked }; + var args = new MouseEvent { ScreenPosition = new (6, 2), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Null (clicked); @@ -253,7 +253,7 @@ public void MouseClick_Right_Left_Arrows_ChangesTab () ); // Click the left arrow - args = new () { Position = new (0, 2), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (0, 2), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Null (clicked); @@ -324,7 +324,7 @@ public void MouseClick_Right_Left_Arrows_ChangesTab_With_Border () Application.Begin (top); // Click the right arrow - var args = new MouseEvent { Position = new (7, 3), Flags = MouseFlags.Button1Clicked }; + var args = new MouseEvent { ScreenPosition = new (7, 3), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Null (clicked); @@ -346,7 +346,7 @@ public void MouseClick_Right_Left_Arrows_ChangesTab_With_Border () ); // Click the left arrow - args = new () { Position = new (1, 3), Flags = MouseFlags.Button1Clicked }; + args = new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked }; Application.OnMouseEvent (args); Application.Refresh (); Assert.Null (clicked); diff --git a/UnitTests/Views/ToplevelTests.cs b/UnitTests/Views/ToplevelTests.cs index 69d71712a0..c47e15b4fd 100644 --- a/UnitTests/Views/ToplevelTests.cs +++ b/UnitTests/Views/ToplevelTests.cs @@ -440,7 +440,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null () Assert.Null (Application.MouseGrabView); // Grab the mouse - Application.OnMouseEvent (new () { Position = new (3, 2), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (3, 2), Flags = MouseFlags.Button1Pressed }); Assert.Equal (Application.Top!.Border, Application.MouseGrabView); Assert.Equal (new (2, 2, 10, 3), Application.Top.Frame); @@ -453,7 +453,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null () Application.OnMouseEvent ( new () { - Position = new (2, 2), Flags = MouseFlags.Button1Pressed + ScreenPosition = new (2, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -476,7 +476,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null () Application.OnMouseEvent ( new () { - Position = new (2, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (2, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -496,7 +496,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null () Assert.Equal (Application.Top!.Border, Application.MouseGrabView); // Ungrab the mouse - Application.OnMouseEvent (new () { Position = new (2, 1), Flags = MouseFlags.Button1Released }); + Application.OnMouseEvent (new () { ScreenPosition = new (2, 1), Flags = MouseFlags.Button1Released }); Application.Refresh (); Assert.Null (Application.MouseGrabView); @@ -548,7 +548,7 @@ public void Mouse_Drag_On_Top_With_Superview_Not_Null () Application.OnMouseEvent ( new () { - Position = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed + ScreenPosition = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed }); Assert.Equal (win.Border, Application.MouseGrabView); @@ -564,7 +564,7 @@ public void Mouse_Drag_On_Top_With_Superview_Not_Null () Application.OnMouseEvent ( new () { - Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = + ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); @@ -589,7 +589,7 @@ public void Mouse_Drag_On_Top_With_Superview_Not_Null () Application.OnMouseEvent ( new () { - Position = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = + ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); @@ -614,7 +614,7 @@ public void Mouse_Drag_On_Top_With_Superview_Not_Null () Application.OnMouseEvent ( new () { - Position = new (win.Frame.X + movex, win.Frame.Y + movey), + ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags = MouseFlags.Button1Released }); @@ -743,11 +743,11 @@ public void Toplevel_Inside_ScrollView_MouseGrabView () Assert.Equal (new (0, 0, 200, 100), scrollView.Subviews [0].Frame); Assert.Equal (new (3, 3, 194, 94), win.Frame); - Application.OnMouseEvent (new () { Position = new (6, 6), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (6, 6), Flags = MouseFlags.Button1Pressed }); Assert.Equal (win.Border, Application.MouseGrabView); Assert.Equal (new (3, 3, 194, 94), win.Frame); - Application.OnMouseEvent (new () { Position = new (9, 9), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (9, 9), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Assert.Equal (win.Border, Application.MouseGrabView); top.SetNeedsLayout (); top.LayoutSubviews (); @@ -757,7 +757,7 @@ public void Toplevel_Inside_ScrollView_MouseGrabView () Application.OnMouseEvent ( new () { - Position = new (5, 5), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (5, 5), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Assert.Equal (win.Border, Application.MouseGrabView); top.SetNeedsLayout (); @@ -765,12 +765,12 @@ public void Toplevel_Inside_ScrollView_MouseGrabView () Assert.Equal (new (2, 2, 195, 95), win.Frame); Application.Refresh (); - Application.OnMouseEvent (new () { Position = new (5, 5), Flags = MouseFlags.Button1Released }); + Application.OnMouseEvent (new () { ScreenPosition = new (5, 5), Flags = MouseFlags.Button1Released }); // ScrollView always grab the mouse when the container's subview OnMouseEnter don't want grab the mouse Assert.Equal (scrollView, Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (4, 4), Flags = MouseFlags.ReportMousePosition }); + Application.OnMouseEvent (new () { ScreenPosition = new (4, 4), Flags = MouseFlags.ReportMousePosition }); Assert.Equal (scrollView, Application.MouseGrabView); top.Dispose (); } @@ -790,14 +790,14 @@ public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_L Assert.Null (Application.MouseGrabView); - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed }); Assert.Equal (window.Border, Application.MouseGrabView); Application.OnMouseEvent ( new () { - Position = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -810,7 +810,7 @@ public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_L Application.OnMouseEvent ( new () { - Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -823,7 +823,7 @@ public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_L Application.OnMouseEvent ( new () { - Position = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -833,7 +833,7 @@ public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_L Application.OnMouseEvent ( new () { - Position = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -844,7 +844,7 @@ public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_L Application.OnMouseEvent ( new () { - Position = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); Application.Refresh (); @@ -882,7 +882,7 @@ public void Modal_As_Top_Will_Drag_Cleanly () Assert.Null (Application.MouseGrabView); Assert.Equal (new (0, 0, 10, 3), window.Frame); - Application.OnMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed }); + Application.OnMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -893,7 +893,7 @@ public void Modal_As_Top_Will_Drag_Cleanly () Application.OnMouseEvent ( new () { - Position = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition + ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }); firstIteration = false; @@ -984,7 +984,7 @@ void OnDrawContentComplete (object sender, DrawEventArgs e) Assert.Equal (new (2, 1, 15, 10), testWindow.Frame); - Application.OnMouseEvent (new () { Position = new (5, 2), Flags = MouseFlags.Button1Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (5, 2), Flags = MouseFlags.Button1Clicked }); Application.Refresh (); From 4e6bf04a25ce0c143961d0ac74914cb1de152bea Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 12:21:02 -0600 Subject: [PATCH 14/75] Added View.IsInHierarchy tests --- UnitTests/View/SubviewTests.cs | 106 ++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/UnitTests/View/SubviewTests.cs b/UnitTests/View/SubviewTests.cs index bfd8804a00..90e196a9e5 100644 --- a/UnitTests/View/SubviewTests.cs +++ b/UnitTests/View/SubviewTests.cs @@ -501,8 +501,110 @@ public void MoveSubviewTowardsEnd () } [Fact] - public void IsInHierarchy_ () + public void IsInHierarchy_ViewIsNull_ReturnsFalse () { - + // Arrange + var start = new View (); + + // Act + var result = View.IsInHierarchy (start, null); + + // Assert + Assert.False (result); + } + + [Fact] + public void IsInHierarchy_StartIsNull_ReturnsFalse () + { + // Arrange + var view = new View (); + + // Act + var result = View.IsInHierarchy (null, view); + + // Assert + Assert.False (result); + } + + [Fact] + public void IsInHierarchy_ViewIsStart_ReturnsTrue () + { + // Arrange + var start = new View (); + + // Act + var result = View.IsInHierarchy (start, start); + + // Assert + Assert.True (result); + } + + [Fact] + public void IsInHierarchy_ViewIsDirectSubview_ReturnsTrue () + { + // Arrange + var start = new View (); + var subview = new View (); + start.Add (subview); + + // Act + var result = View.IsInHierarchy (start, subview); + + // Assert + Assert.True (result); + } + + [Fact] + public void IsInHierarchy_ViewIsNestedSubview_ReturnsTrue () + { + // Arrange + var start = new View (); + var subview = new View (); + var nestedSubview = new View (); + start.Add (subview); + subview.Add (nestedSubview); + + // Act + var result = View.IsInHierarchy (start, nestedSubview); + + // Assert + Assert.True (result); + } + + [Fact] + public void IsInHierarchy_ViewIsNotInHierarchy_ReturnsFalse () + { + // Arrange + var start = new View (); + var subview = new View (); + + // Act + var result = View.IsInHierarchy (start, subview); + + // Assert + Assert.False (result); + } + + [Theory] + [CombinatorialData] + public void IsInHierarchy_ViewIsInAdornments_ReturnsTrue (bool includeAdornments) + { + // Arrange + var start = new View () + { + Id = "start" + }; + var inPadding = new View () + { + Id = "inPadding" + }; + + start.Padding.Add (inPadding); + + // Act + var result = View.IsInHierarchy (start, inPadding, includeAdornments: includeAdornments); + + // Assert + Assert.Equal(includeAdornments, result); } } From a5044dffe2471040d37a9e52e2cd19187468b11a Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 13:13:34 -0600 Subject: [PATCH 15/75] Added Contextmenuv2 scenario. --- Terminal.Gui/Application/Application.Mouse.cs | 9 + Terminal.Gui/Application/Application.cs | 1 + Terminal.Gui/View/Layout/DimAuto.cs | 2 +- Terminal.Gui/View/View.cs | 30 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 75 ++++ Terminal.Gui/Views/Menu/Menu.cs | 2 +- Terminal.Gui/Views/{ => Menu}/MenuBarv2.cs | 0 Terminal.Gui/Views/{ => Menu}/Menuv2.cs | 10 - UICatalog/Scenarios/ContextMenusv2.cs | 335 ++++++++++++++++++ UnitTests/Application/ApplicationTests.cs | 9 +- 10 files changed, 454 insertions(+), 19 deletions(-) create mode 100644 Terminal.Gui/Views/Menu/ContextMenuv2.cs rename Terminal.Gui/Views/{ => Menu}/MenuBarv2.cs (100%) rename Terminal.Gui/Views/{ => Menu}/Menuv2.cs (93%) create mode 100644 UICatalog/Scenarios/ContextMenusv2.cs diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index ad35fa8f07..040b89fef8 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -6,6 +6,13 @@ namespace Terminal.Gui; public static partial class Application // Mouse handling { + internal static Point _lastMousePosition = Point.Empty; + + /// + /// Gets the most recent position of the mouse. + /// + public static Point GetLastMousePosition () => _lastMousePosition; + /// Disable or enable the mouse. The mouse is enabled by default. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] public static bool IsMouseDisabled { get; set; } @@ -132,6 +139,8 @@ private static void OnUnGrabbedMouse (View? view) /// The mouse event with coordinates relative to the screen. internal static void OnMouseEvent (MouseEvent mouseEvent) { + _lastMousePosition = mouseEvent.ScreenPosition; + if (IsMouseDisabled) { return; diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index 9688838366..e31fcf47c8 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -197,6 +197,7 @@ internal static void ResetState (bool ignoreDisposed = false) IsInitialized = false; // Mouse + _lastMousePosition = Point.Empty; _cachedViewsUnderMouse.Clear (); WantContinuousButtonPressedView = null; MouseEvent = null; diff --git a/Terminal.Gui/View/Layout/DimAuto.cs b/Terminal.Gui/View/Layout/DimAuto.cs index bd4f3d212f..7b52937aca 100644 --- a/Terminal.Gui/View/Layout/DimAuto.cs +++ b/Terminal.Gui/View/Layout/DimAuto.cs @@ -35,7 +35,7 @@ internal override int Calculate (int location, int superviewContentSize, View us int screenX4 = dimension == Dimension.Width ? Application.Screen.Width * 4 : Application.Screen.Height * 4; int autoMax = MaximumContentDim?.GetAnchor (superviewContentSize) ?? screenX4; - Debug.WriteLineIf (autoMin <= autoMax, "MinimumContentDim must be less than or equal to MaximumContentDim."); + Debug.WriteLineIf (autoMin > autoMax, "MinimumContentDim must be less than or equal to MaximumContentDim."); if (Style.FastHasFlags (DimAutoStyle.Text)) { diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index f01c9c78d0..1fecc2226e 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -287,7 +287,7 @@ public virtual void EndInit () Initialized?.Invoke (this, EventArgs.Empty); } -#endregion Constructors and Initialization + #endregion Constructors and Initialization #region Visibility @@ -365,6 +365,18 @@ public virtual bool Visible return; } + if (OnVisibleChanging ()) + { + return; + } + + CancelEventArgs args = new (in _visible, ref value); + VisibleChanging?.Invoke (this, args); + if (args.Cancel) + { + return; + } + _visible = value; if (!_visible) @@ -382,14 +394,22 @@ public virtual bool Visible } OnVisibleChanged (); + VisibleChanged?.Invoke (this, EventArgs.Empty); + SetNeedsDisplay (); } } - /// Method invoked when the property from a view is changed. - public virtual void OnVisibleChanged () { VisibleChanged?.Invoke (this, EventArgs.Empty); } + /// Called when is changing. Can be cancelled by returning . + protected virtual bool OnVisibleChanging () { return false; } + + /// Raised when the value is being changed. Can be cancelled by setting Cancel to . + public event EventHandler>? VisibleChanging; + + /// Called when has changed. + protected virtual void OnVisibleChanged () { } - /// Event fired when the value is being changed. + /// Raised when has changed. public event EventHandler? VisibleChanged; // TODO: This API is a hack. We should make Visible propogate automatically, no? See https://github.com/gui-cs/Terminal.Gui/issues/3703 @@ -416,7 +436,7 @@ internal static bool CanBeVisible (View view) return true; } -#endregion Visibility + #endregion Visibility #region Title diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs new file mode 100644 index 0000000000..27b479464e --- /dev/null +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -0,0 +1,75 @@ +#nullable enable + +namespace Terminal.Gui; + +/// +/// ContextMenu provides a pop-up menu that can be positioned anywhere within a . ContextMenu is +/// analogous to and, once activated, works like a sub-menu of a (but +/// can be positioned anywhere). +/// +/// By default, a ContextMenu with sub-menus is displayed in a cascading manner, where each sub-menu pops out of +/// the ContextMenu frame (either to the right or left, depending on where the ContextMenu is relative to the edge +/// of the screen). By setting to , this behavior can be +/// changed such that all sub-menus are drawn within the ContextMenu frame. +/// +/// +/// ContextMenus can be activated using the Shift-F10 key (by default; use the to change to +/// another key). +/// +/// +/// Callers can cause the ContextMenu to be activated on a right-mouse click (or other interaction) by calling +/// . +/// +/// ContextMenus are located using screen coordinates and appear above all other Views. +/// +public class ContextMenuv2 : Menuv2 +{ + private Key _key = DefaultKey; + + /// Initializes a context menu with no menu items. + public ContextMenuv2 () + { + VisibleChanged += OnVisibleChanged; + } + + private void OnVisibleChanged (object? sender, EventArgs e) + { + if (Visible) + { + + } + else + { + if (Application.MouseGrabView == this) + { + Application.UngrabMouse (); + } + } + } + + /// The default key for activating the context menu. + [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] + public static Key DefaultKey { get; set; } = Key.F10.WithShift; + + /// + /// Sets or gets whether the context menu be forced to the right, ensuring it is not clipped, if the x position is + /// less than zero. The default is which means the context menu will be forced to the right. If + /// set to , the context menu will be clipped on the left if x is less than zero. + /// + public bool ForceMinimumPosToZero { get; set; } = true; + + /// Specifies the key that will activate the context menu. + public Key Key + { + get => _key; + set + { + Key oldKey = _key; + _key = value; + KeyChanged?.Invoke (this, new KeyChangedEventArgs (oldKey, _key)); + } + } + + /// Event raised when the is changed. + public event EventHandler? KeyChanged; +} diff --git a/Terminal.Gui/Views/Menu/Menu.cs b/Terminal.Gui/Views/Menu/Menu.cs index adad33eee7..b5f8b6eaba 100644 --- a/Terminal.Gui/Views/Menu/Menu.cs +++ b/Terminal.Gui/Views/Menu/Menu.cs @@ -330,7 +330,7 @@ private void Current_TerminalResized (object? sender, SizeChangedEventArgs e) } /// - public override void OnVisibleChanged () + protected override void OnVisibleChanged () { base.OnVisibleChanged (); diff --git a/Terminal.Gui/Views/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs similarity index 100% rename from Terminal.Gui/Views/MenuBarv2.cs rename to Terminal.Gui/Views/Menu/MenuBarv2.cs diff --git a/Terminal.Gui/Views/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs similarity index 93% rename from Terminal.Gui/Views/Menuv2.cs rename to Terminal.Gui/Views/Menu/Menuv2.cs index 6289e38275..8f9a809972 100644 --- a/Terminal.Gui/Views/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -21,16 +21,6 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) VisibleChanged += OnVisibleChanged; } - //private void OnMouseEvent (object sender, MouseEventEventArgs e) - //{ - // if (!e.MouseEvent.Flags.HasFlag(MouseFlags.ReportMousePosition)) - // { - // return; - // } - - - //} - private void OnVisibleChanged (object sender, EventArgs e) { if (Visible) diff --git a/UICatalog/Scenarios/ContextMenusv2.cs b/UICatalog/Scenarios/ContextMenusv2.cs new file mode 100644 index 0000000000..a1964cb7b3 --- /dev/null +++ b/UICatalog/Scenarios/ContextMenusv2.cs @@ -0,0 +1,335 @@ +using System.Collections.Generic; +using System.Globalization; +using System.Threading; +using JetBrains.Annotations; +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +[ScenarioMetadata ("ContextMenus v2", "Context Menu v2 Sample.")] +[ScenarioCategory ("Menus")] +public class ContextMenusv2 : Scenario +{ + [CanBeNull] + private ContextMenuv2 _contextMenu; + private bool _forceMinimumPosToZero = true; + private MenuItem _miForceMinimumPosToZero; + private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight; + private bool _useSubMenusSingleFrame; + + public override void Main () + { + // Init + Application.Init (); + + // Setup - Create a top-level application window and configure it. + Window appWindow = new () + { + Title = GetQuitKeyAndName () + }; + + _contextMenu = new ContextMenuv2 () + { + }; + + ConfigureMenu (_contextMenu); + _contextMenu.Key = Key.Space.WithCtrl; + + var text = "Context Menu"; + var width = 20; + + var label = new Label + { + X = Pos.Center (), Y = 1, Text = $"Press '{_contextMenu.Key}' to open the Window context menu." + }; + appWindow.Add (label); + + label = new() + { + X = Pos.Center (), + Y = Pos.Bottom (label), + Text = $"Press '{ContextMenu.DefaultKey}' to open the TextField context menu." + }; + appWindow.Add (label); + + _tfTopLeft = new() { Width = width, Text = text }; + appWindow.Add (_tfTopLeft); + + _tfTopRight = new() { X = Pos.AnchorEnd (width), Width = width, Text = text }; + appWindow.Add (_tfTopRight); + + _tfMiddle = new() { X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; + appWindow.Add (_tfMiddle); + + _tfBottomLeft = new() { Y = Pos.AnchorEnd (1), Width = width, Text = text }; + appWindow.Add (_tfBottomLeft); + + _tfBottomRight = new() { X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; + appWindow.Add (_tfBottomRight); + + Point mousePos = default; + + appWindow.KeyDown += (s, e) => + { + if (e.KeyCode == _contextMenu.Key) + { + Application.Popover = _contextMenu; + _contextMenu.Visible = true; + e.Handled = true; + } + }; + + appWindow.MouseClick += (s, e) => + { + if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) + { + Application.Popover = _contextMenu; + _contextMenu.X = e.MouseEvent.ScreenPosition.X; + _contextMenu.Y = e.MouseEvent.ScreenPosition.Y; + _contextMenu.Visible = true; + e.Handled = true; + } + }; + + appWindow.Closed += (s, e) => + { + Thread.CurrentThread.CurrentUICulture = new ("en-US"); + }; + + + // Run - Start the application. + Application.Run (appWindow); + appWindow.Dispose (); + _contextMenu.Dispose (); + + // Shutdown - Calling Application.Shutdown is required. + Application.Shutdown (); + } + + private void ConfigureMenu (Bar bar) + { + + var shortcut1 = new Shortcut + { + Title = "Z_igzag", + Key = Key.I.WithCtrl, + Text = "Gonna zig zag", + HighlightStyle = HighlightStyle.Hover + }; + + var shortcut2 = new Shortcut + { + Title = "Za_G", + Text = "Gonna zag", + Key = Key.G.WithAlt, + HighlightStyle = HighlightStyle.Hover + }; + + var shortcut3 = new Shortcut + { + Title = "_Three", + Text = "The 3rd item", + Key = Key.D3.WithAlt, + HighlightStyle = HighlightStyle.Hover + }; + + var line = new Line () + { + BorderStyle = LineStyle.Dotted, + Orientation = Orientation.Horizontal, + CanFocus = false, + }; + // HACK: Bug in Line + line.Orientation = Orientation.Vertical; + line.Orientation = Orientation.Horizontal; + + var shortcut4 = new Shortcut + { + Title = "_Four", + Text = "Below the line", + Key = Key.D3.WithAlt, + HighlightStyle = HighlightStyle.Hover + }; + bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); + } + + + //private MenuItem [] GetSupportedCultures () + //{ + // List supportedCultures = new (); + // int index = -1; + + // foreach (CultureInfo c in _cultureInfos) + // { + // var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked }; + + // if (index == -1) + // { + // culture.Title = "_English"; + // culture.Help = "en-US"; + // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US"; + // CreateAction (supportedCultures, culture); + // supportedCultures.Add (culture); + // index++; + // culture = new() { CheckType = MenuItemCheckStyle.Checked }; + // } + + // culture.Title = $"_{c.Parent.EnglishName}"; + // culture.Help = c.Name; + // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name; + // CreateAction (supportedCultures, culture); + // supportedCultures.Add (culture); + // } + + // return supportedCultures.ToArray (); + + // void CreateAction (List supportedCultures, MenuItem culture) + // { + // culture.Action += () => + // { + // Thread.CurrentThread.CurrentUICulture = new (culture.Help); + // culture.Checked = true; + + // foreach (MenuItem item in supportedCultures) + // { + // item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name; + // } + // }; + // } + //} + + //private void ShowContextMenu (int x, int y) + //{ + // _contextMenu = new() + // { + // Position = new (x, y), + // ForceMinimumPosToZero = _forceMinimumPosToZero, + // UseSubMenusSingleFrame = _useSubMenusSingleFrame + // }; + + // MenuBarItem menuItems = new ( + // new [] + // { + // new MenuBarItem ( + // "_Languages", + // GetSupportedCultures () + // ), + // new ( + // "_Configuration", + // "Show configuration", + // () => MessageBox.Query ( + // 50, + // 5, + // "Info", + // "This would open settings dialog", + // "Ok" + // ) + // ), + // new MenuBarItem ( + // "M_ore options", + // new MenuItem [] + // { + // new ( + // "_Setup", + // "Change settings", + // () => MessageBox + // .Query ( + // 50, + // 5, + // "Info", + // "This would open setup dialog", + // "Ok" + // ), + // shortcutKey: KeyCode.T + // | KeyCode + // .CtrlMask + // ), + // new ( + // "_Maintenance", + // "Maintenance mode", + // () => MessageBox + // .Query ( + // 50, + // 5, + // "Info", + // "This would open maintenance dialog", + // "Ok" + // ) + // ) + // } + // ), + // _miForceMinimumPosToZero = + // new ( + // "Fo_rceMinimumPosToZero", + // "", + // () => + // { + // _miForceMinimumPosToZero + // .Checked = + // _forceMinimumPosToZero = + // !_forceMinimumPosToZero; + + // _tfTopLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfTopRight.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfMiddle.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfBottomLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfBottomRight + // .ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + // } + // ) + // { + // CheckType = + // MenuItemCheckStyle + // .Checked, + // Checked = + // _forceMinimumPosToZero + // }, + // _miUseSubMenusSingleFrame = + // new ( + // "Use_SubMenusSingleFrame", + // "", + // () => _contextMenu + // .UseSubMenusSingleFrame = + // (bool) + // (_miUseSubMenusSingleFrame + // .Checked = + // _useSubMenusSingleFrame = + // !_useSubMenusSingleFrame) + // ) + // { + // CheckType = MenuItemCheckStyle + // .Checked, + // Checked = + // _useSubMenusSingleFrame + // }, + // null, + // new ( + // "_Quit", + // "", + // () => Application.RequestStop () + // ) + // } + // ); + // _tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + + // _contextMenu.Show (menuItems); + //} +} diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index a97ed51553..ff47f27cdd 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -290,6 +290,7 @@ void CheckReset () // Public Properties Assert.Null (Application.Top); + Assert.Null (Application.Popover); Assert.Null (Application.MouseGrabView); Assert.Null (Application.WantContinuousButtonPressedView); @@ -318,6 +319,9 @@ void CheckReset () // Keyboard Assert.Empty (Application.GetViewKeyBindings ()); + // Mouse + Assert.Equal (Application._lastMousePosition, Point.Empty); + // Navigation Assert.Null (Application.Navigation); @@ -355,12 +359,13 @@ void CheckReset () Application.QuitKey = Key.C; Application.KeyBindings.Add (Key.D, KeyBindingScope.Application, Command.Cancel); - //ApplicationOverlapped.OverlappedChildren = new List (); - //ApplicationOverlapped.OverlappedTop = Application._cachedViewsUnderMouse.Clear (); //Application.WantContinuousButtonPressedView = new View (); + // Mouse + Application._lastMousePosition = new Point (1, 1); + Application.Navigation = new (); Application.ResetState (); From 06bcefe774b9b92d3517e0bb942f1cbcd13aad6b Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 16:04:40 -0600 Subject: [PATCH 16/75] Implemented CM2 in TextView --- Terminal.Gui/Application/Application.Mouse.cs | 2 +- .../Application/Application.Popover.cs | 8 +- .../Application/ApplicationNavigation.cs | 2 +- Terminal.Gui/Input/KeyBindings.cs | 2 +- Terminal.Gui/View/View.Navigation.cs | 3 +- Terminal.Gui/View/View.cs | 3 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 62 ++++----- Terminal.Gui/Views/Menu/Menuv2.cs | 1 - Terminal.Gui/Views/Shortcut.cs | 24 +++- Terminal.Gui/Views/TextField.cs | 127 +++++++----------- UICatalog/Scenarios/ContextMenus.cs | 50 +++---- UICatalog/Scenarios/ContextMenusv2.cs | 10 +- UnitTests/View/Navigation/NavigationTests.cs | 3 + UnitTests/Views/ContextMenuTests.cs | 72 +++++----- UnitTests/Views/TextFieldTests.cs | 2 +- 15 files changed, 183 insertions(+), 188 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 040b89fef8..e8f23b44d4 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -231,7 +231,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) } else { - Debug.Fail("The mouse was outside of any View. But this makes no sense as deepestViewUnderMouse is not null."); + // The mouse was outside any View's Viewport. return; } diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index b15710c555..a965dcd72c 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -24,7 +24,7 @@ public static View? Popover if (_popover is { }) { _popover.Visible = false; - _popover.VisibleChanged -= PopoverVisibleChanged; + _popover.VisibleChanging -= PopoverVisibleChanging; } _popover = value; @@ -46,19 +46,19 @@ public static View? Popover _popover.SetRelativeLayout (Screen.Size); - _popover.VisibleChanged += PopoverVisibleChanged; + _popover.VisibleChanging += PopoverVisibleChanging; } } } - private static void PopoverVisibleChanged (object? sender, EventArgs e) + private static void PopoverVisibleChanging (object? sender, CancelEventArgs e) { if (Popover is null) { return; } - if (Popover.Visible) + if (e.NewValue) { Popover.Arrangement |= ViewArrangement.Overlapped; diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index 677e066e43..9c14ac9085 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -73,7 +73,7 @@ public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { if (Application.Popover is { Visible: true }) { - Application.Popover.AdvanceFocus (direction, behavior); + return Application.Popover.AdvanceFocus (direction, behavior); } return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index 4df4b6e322..8950af4054 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -289,7 +289,7 @@ public Command [] GetCommands (Key key) /// The set of commands to search. /// The used by a /// If no matching set of commands was found. - public Key GetKeyFromCommands (params Command [] commands) { return Bindings.First (a => a.Value.Commands.SequenceEqual (commands)).Key; } + public Key GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } /// Removes a from the collection. /// diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index 10dacc2acd..a63237de2a 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -292,7 +292,8 @@ public View? MostFocused /// internal bool RestoreFocus () { - View [] indicies = GetFocusChain (NavigationDirection.Forward, TabStop); + // Ignore TabStop + View [] indicies = GetFocusChain (NavigationDirection.Forward, null); if (Focused is null && _previouslyFocused is { } && indicies.Contains (_previouslyFocused)) { diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 1fecc2226e..ae50fc8198 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -354,7 +354,8 @@ public bool Enabled private bool _visible = true; - /// Gets or sets a value indicating whether this and all its child controls are displayed. + // TODO: Remove virtual once Menu/MenuBar are removed. MenuBar is the only override. + /// Gets or sets a value indicating whether this is visible. public virtual bool Visible { get => _visible; diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 27b479464e..8ba6f3fefd 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -1,63 +1,65 @@ #nullable enable +using System.Diagnostics; + namespace Terminal.Gui; /// -/// ContextMenu provides a pop-up menu that can be positioned anywhere within a . ContextMenu is -/// analogous to and, once activated, works like a sub-menu of a (but -/// can be positioned anywhere). +/// ContextMenuv2 provides a Popover menu that can be positioned anywhere within a . +/// +/// To show the ContextMenu, set to the ContextMenu object and set +/// property to . +/// /// -/// By default, a ContextMenu with sub-menus is displayed in a cascading manner, where each sub-menu pops out of -/// the ContextMenu frame (either to the right or left, depending on where the ContextMenu is relative to the edge -/// of the screen). By setting to , this behavior can be -/// changed such that all sub-menus are drawn within the ContextMenu frame. +/// The menu will be hidden when the user clicks outside the menu or when the user presses . /// /// -/// ContextMenus can be activated using the Shift-F10 key (by default; use the to change to -/// another key). +/// To explicitly hide the menu, set property to . /// /// -/// Callers can cause the ContextMenu to be activated on a right-mouse click (or other interaction) by calling -/// . +/// is the key used to activate the ContextMenus (Shift+F10 by default). Callers can use this in +/// their keyboard handling code. /// -/// ContextMenus are located using screen coordinates and appear above all other Views. +/// The menu will be displayed at the current mouse coordinates. /// public class ContextMenuv2 : Menuv2 { private Key _key = DefaultKey; + private MouseFlags _mouseFlags = MouseFlags.Button3Clicked; + + public MouseFlags MouseFlags + { + get => _mouseFlags; + set + { + _mouseFlags = value; + } + } + /// Initializes a context menu with no menu items. - public ContextMenuv2 () + public ContextMenuv2 () : this ([]) { } + + /// + public ContextMenuv2 (IEnumerable shortcuts) : base(shortcuts) { - VisibleChanged += OnVisibleChanged; + Visible = false; + VisibleChanging += OnVisibleChanging; + Key = DefaultKey; } - private void OnVisibleChanged (object? sender, EventArgs e) + private void OnVisibleChanging (object? sender, CancelEventArgs args) { - if (Visible) + if (args.NewValue) { } - else - { - if (Application.MouseGrabView == this) - { - Application.UngrabMouse (); - } - } } /// The default key for activating the context menu. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] public static Key DefaultKey { get; set; } = Key.F10.WithShift; - /// - /// Sets or gets whether the context menu be forced to the right, ensuring it is not clipped, if the x position is - /// less than zero. The default is which means the context menu will be forced to the right. If - /// set to , the context menu will be clipped on the left if x is less than zero. - /// - public bool ForceMinimumPosToZero { get; set; } = true; - /// Specifies the key that will activate the context menu. public Key Key { diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index 8f9a809972..7a51c84947 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -69,7 +69,6 @@ public override View Add (View view) if (view is Shortcut shortcut) { shortcut.CanFocus = true; - shortcut.KeyBindingScope = KeyBindingScope.Application; shortcut.Orientation = Orientation.Vertical; shortcut.HighlightStyle |= HighlightStyle.Hover; diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 802d785d5a..baf748b966 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -38,6 +38,17 @@ /// public class Shortcut : View, IOrientation, IDesignable { + /// + /// Creates a new instance of . + /// + public Shortcut () : this (Key.Empty, string.Empty, null) { } + + public Shortcut (View targetView, Command command, string commandText, string helpText = null) : this (targetView?.KeyBindings.GetKeyFromCommands (command), commandText, null, helpText) + { + _targetView = targetView; + _command = command; + } + /// /// Creates a new instance of . /// @@ -134,10 +145,10 @@ Dim GetWidthDimAuto () } } - /// - /// Creates a new instance of . - /// - public Shortcut () : this (Key.Empty, string.Empty, null) { } + [CanBeNull] + private View _targetView; + + private Command _command; private readonly OrientationHelper _orientationHelper; @@ -755,6 +766,11 @@ private void UpdateKeyBinding (Key oldKey) cancel = true; } + if (_targetView is { }) + { + _targetView.InvokeCommand (_command); + } + return cancel; } diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 64e3d0482e..905a25c9b0 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -319,7 +319,7 @@ public TextField () Command.Context, () => { - ShowContextMenu (); + ShowContextMenu (keyboard: true); return true; } @@ -405,11 +405,11 @@ public TextField () _currentCulture = Thread.CurrentThread.CurrentUICulture; - ContextMenu = new ContextMenu { Host = this }; - ContextMenu.KeyChanged += ContextMenu_KeyChanged; - - KeyBindings.Add (ContextMenu.Key, KeyBindingScope.HotKey, Command.Context); KeyBindings.Add (Key.Enter, Command.Accept); + + ContextMenu = CreateContextMenu (); + KeyBindings.Add (ContextMenu!.Key, KeyBindingScope.HotKey, Command.Context); + } @@ -429,7 +429,8 @@ public TextField () public Color CaptionColor { get; set; } /// Get the for this view. - public ContextMenu ContextMenu { get; } + [CanBeNull] + public ContextMenuv2 ContextMenu { get; private set; } /// Sets or gets the current cursor position. public virtual int CursorPosition @@ -813,7 +814,7 @@ protected internal override bool OnMouseEvent (MouseEvent ev) && !ev.Flags.HasFlag (MouseFlags.Button1Released) && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked) && !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked) - && !ev.Flags.HasFlag (ContextMenu.MouseFlags)) + && !ev.Flags.HasFlag (ContextMenu!.MouseFlags)) { return base.OnMouseEvent (ev); } @@ -913,9 +914,13 @@ protected internal override bool OnMouseEvent (MouseEvent ev) ClearAllSelection (); PrepareSelection (0, _text.Count); } - else if (ev.Flags == ContextMenu.MouseFlags) + else if (ev.Flags == ContextMenu!.MouseFlags) { - ShowContextMenu (); + PositionCursor (ev); + + ContextMenu!.X = ev.ScreenPosition.X; + ContextMenu!.Y = ev.ScreenPosition.Y + 1; + ShowContextMenu (false); } //SetNeedsDisplay (); @@ -1251,69 +1256,23 @@ private void Adjust () } } - private MenuBarItem BuildContextMenuBarItem () - { - return new MenuBarItem ( - new MenuItem [] - { - new ( - Strings.ctxSelectAll, - "", - () => SelectAll (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll) - ), - new ( - Strings.ctxDeleteAll, - "", - () => DeleteAll (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll) - ), - new ( - Strings.ctxCopy, - "", - () => Copy (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy) - ), - new ( - Strings.ctxCut, - "", - () => Cut (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut) - ), - new ( - Strings.ctxPaste, - "", - () => Paste (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste) - ), - new ( - Strings.ctxUndo, - "", - () => Undo (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo) - ), - new ( - Strings.ctxRedo, - "", - () => Redo (), - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo) - ) - } - ); + + private ContextMenuv2 CreateContextMenu () + { + ContextMenuv2 menu = new (new List () + { + new (this, Command.SelectAll, Strings.ctxSelectAll), + new (this, Command.DeleteAll, Strings.ctxDeleteAll), + new (this, Command.Copy, Strings.ctxCopy), + new (this, Command.Cut, Strings.ctxCut), + new (this, Command.Paste, Strings.ctxPaste), + new (this, Command.Undo, Strings.ctxUndo), + new (this, Command.Redo, Strings.ctxRedo), + }); + + menu.KeyChanged += ContextMenu_KeyChanged; + + return menu; } private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) @@ -1844,14 +1803,16 @@ private void SetSelectedStartSelectedLength () private void SetText (List newText) { Text = StringExtensions.ToString (newText); } private void SetText (IEnumerable newText) { SetText (newText.ToList ()); } - private void ShowContextMenu () + private void ShowContextMenu (bool keyboard) { - if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture)) + if (keyboard) { - _currentCulture = Thread.CurrentThread.CurrentUICulture; + Point loc = ViewportToScreen (new Point (_cursorPosition - ScrollOffset, 1)); + ContextMenu!.X = loc.X; + ContextMenu!.Y = loc.Y; } - - ContextMenu.Show (BuildContextMenuBarItem ()); + Application.Popover = ContextMenu; + ContextMenu!.Visible = true; } private void TextField_Added (object sender, SuperViewChangedEventArgs e) @@ -1883,6 +1844,18 @@ private void TextField_Initialized (object sender, EventArgs e) Autocomplete.PopupInsideContainer = false; } } + + /// + protected override void Dispose (bool disposing) + { + if (ContextMenu is { }) + { + ContextMenu.Visible = false; + ContextMenu.Dispose (); + ContextMenu = null; + } + base.Dispose (disposing); + } } /// diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 01c4e340e6..e4b589a1d8 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -229,26 +229,26 @@ private void ShowContextMenu (int x, int y) _forceMinimumPosToZero = !_forceMinimumPosToZero; - _tfTopLeft.ContextMenu - .ForceMinimumPosToZero = - _forceMinimumPosToZero; - - _tfTopRight.ContextMenu - .ForceMinimumPosToZero = - _forceMinimumPosToZero; - - _tfMiddle.ContextMenu - .ForceMinimumPosToZero = - _forceMinimumPosToZero; - - _tfBottomLeft.ContextMenu - .ForceMinimumPosToZero = - _forceMinimumPosToZero; - - _tfBottomRight - .ContextMenu - .ForceMinimumPosToZero = - _forceMinimumPosToZero; + //_tfTopLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + //_tfTopRight.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + //_tfMiddle.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + //_tfBottomLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + //_tfBottomRight + // .ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; } ) { @@ -284,11 +284,11 @@ private void ShowContextMenu (int x, int y) ) } ); - _tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - _tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - _tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - _tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - _tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + //_tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + //_tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + //_tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + //_tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + //_tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; _contextMenu.Show (menuItems); } diff --git a/UICatalog/Scenarios/ContextMenusv2.cs b/UICatalog/Scenarios/ContextMenusv2.cs index a1964cb7b3..b724f20591 100644 --- a/UICatalog/Scenarios/ContextMenusv2.cs +++ b/UICatalog/Scenarios/ContextMenusv2.cs @@ -52,19 +52,19 @@ public override void Main () }; appWindow.Add (label); - _tfTopLeft = new() { Width = width, Text = text }; + _tfTopLeft = new() { Id = "_tfTopLeft", Width = width, Text = text }; appWindow.Add (_tfTopLeft); - _tfTopRight = new() { X = Pos.AnchorEnd (width), Width = width, Text = text }; + _tfTopRight = new() { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text }; appWindow.Add (_tfTopRight); - _tfMiddle = new() { X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; + _tfMiddle = new() { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; appWindow.Add (_tfMiddle); - _tfBottomLeft = new() { Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomLeft = new() { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomLeft); - _tfBottomRight = new() { X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomRight = new() { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomRight); Point mousePos = default; diff --git a/UnitTests/View/Navigation/NavigationTests.cs b/UnitTests/View/Navigation/NavigationTests.cs index 763219d76e..85d9c1638b 100644 --- a/UnitTests/View/Navigation/NavigationTests.cs +++ b/UnitTests/View/Navigation/NavigationTests.cs @@ -144,6 +144,9 @@ public void AllViews_HasFocus_Changed_Event (Type viewType) Assert.False (view.HasFocus); Assert.False (otherView.HasFocus); + // Ensure the view is Visible + view.Visible = true; + Application.Top.SetFocus (); Assert.True (Application.Top!.HasFocus); Assert.True (top.HasFocus); diff --git a/UnitTests/Views/ContextMenuTests.cs b/UnitTests/Views/ContextMenuTests.cs index 74d408cb9a..6a9608513c 100644 --- a/UnitTests/Views/ContextMenuTests.cs +++ b/UnitTests/Views/ContextMenuTests.cs @@ -5,7 +5,7 @@ namespace Terminal.Gui.ViewsTests; public class ContextMenuTests (ITestOutputHelper output) { - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void ContextMenu_Constructors () { @@ -60,7 +60,7 @@ public void ContextMenu_Constructors () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void ContextMenu_Is_Closed_If_Another_MenuBar_Is_Open_Or_Vice_Versa () { @@ -126,7 +126,7 @@ public void ContextMenu_Is_Closed_If_Another_MenuBar_Is_Open_Or_Vice_Versa () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Draw_A_ContextMenu_Over_A_Borderless_Top () { @@ -172,7 +172,7 @@ public void Draw_A_ContextMenu_Over_A_Borderless_Top () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Draw_A_ContextMenu_Over_A_Dialog () { @@ -261,7 +261,7 @@ public void Draw_A_ContextMenu_Over_A_Dialog () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Draw_A_ContextMenu_Over_A_Top_Dialog () { @@ -312,7 +312,7 @@ public void Draw_A_ContextMenu_Over_A_Top_Dialog () dialog.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void ForceMinimumPosToZero_True_False () { @@ -362,7 +362,7 @@ public void ForceMinimumPosToZero_True_False () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Hide_Is_Invoke_At_Container_Closing () { @@ -391,7 +391,7 @@ public void Hide_Is_Invoke_At_Container_Closing () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Key_Open_And_Close_The_ContextMenu () { @@ -401,15 +401,15 @@ public void Key_Open_And_Close_The_ContextMenu () Application.Begin (top); Assert.True (Application.OnKeyDown (ContextMenu.DefaultKey)); - Assert.True (tf.ContextMenu.MenuBar!.IsMenuOpen); + Assert.True (tf.ContextMenu!.Visible); Assert.True (Application.OnKeyDown (ContextMenu.DefaultKey)); // The last context menu bar opened is always preserved - Assert.NotNull (tf.ContextMenu.MenuBar); + Assert.False (tf.ContextMenu.Visible); top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void KeyChanged_Event () { @@ -423,7 +423,7 @@ public void KeyChanged_Event () Assert.Equal (ContextMenu.DefaultKey, oldKey); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void MenuItens_Changing () { @@ -475,7 +475,7 @@ public void MenuItens_Changing () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Menus_And_SubMenus_Always_Try_To_Be_On_Screen () { @@ -737,7 +737,7 @@ top.Subviews [0] top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void MouseFlags_Changing () { @@ -768,7 +768,7 @@ public void MouseFlags_Changing () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] public void MouseFlagsChanged_Event () { var oldMouseFlags = new MouseFlags (); @@ -781,7 +781,7 @@ public void MouseFlagsChanged_Event () Assert.Equal (MouseFlags.Button3Clicked, oldMouseFlags); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Position_Changing () { @@ -826,7 +826,7 @@ public void Position_Changing () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws () { @@ -911,7 +911,7 @@ public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height () { @@ -949,7 +949,7 @@ public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_He top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width () { @@ -988,7 +988,7 @@ public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Wid top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space () { @@ -1062,7 +1062,7 @@ public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Ensures_Display_Inside_The_Container_But_Preserves_Position () { @@ -1100,7 +1100,7 @@ public void Show_Ensures_Display_Inside_The_Container_But_Preserves_Position () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Ensures_Display_Inside_The_Container_Without_Overlap_The_Host () { @@ -1151,7 +1151,7 @@ public void Show_Ensures_Display_Inside_The_Container_Without_Overlap_The_Host ( top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Show_Hide_IsShow () { @@ -1190,7 +1190,7 @@ public void Show_Hide_IsShow () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void UseSubMenusSingleFrame_True_By_Mouse () { @@ -1273,7 +1273,7 @@ public void UseSubMenusSingleFrame_True_By_Mouse () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void UseSubMenusSingleFrame_False_By_Mouse () { @@ -1383,7 +1383,7 @@ public void UseSubMenusSingleFrame_False_By_Mouse () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () { @@ -1403,7 +1403,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.False (tf1.HasFocus); Assert.False (tf2.HasFocus); Assert.Equal (5, win.Subviews.Count); - Assert.True (tf2.ContextMenu.MenuBar.IsMenuOpen); + Assert.True (tf2.ContextMenu!.Visible); Assert.True (win.Focused is Menu); Assert.True (Application.MouseGrabView is MenuBar); Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ()); @@ -1415,7 +1415,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.Equal (4, win.Subviews.Count); // The last context menu bar opened is always preserved - Assert.NotNull (tf2.ContextMenu.MenuBar); + Assert.NotNull (tf2.ContextMenu); Assert.Equal (win.Focused, tf1); Assert.Null (Application.MouseGrabView); Assert.Equal (tf1, Application._cachedViewsUnderMouse.LastOrDefault ()); @@ -1427,7 +1427,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () Assert.Equal (4, win.Subviews.Count); // The last context menu bar opened is always preserved - Assert.NotNull (tf2.ContextMenu.MenuBar); + Assert.NotNull (tf2.ContextMenu); Assert.Equal (win.Focused, tf2); Assert.Null (Application.MouseGrabView); Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ()); @@ -1436,7 +1436,7 @@ public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus () win.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Empty_Menus_Items_Children_Does_Not_Open_The_Menu () { @@ -1452,7 +1452,7 @@ public void Empty_Menus_Items_Children_Does_Not_Open_The_Menu () top.Dispose (); } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void KeyBindings_Removed_On_Close_ContextMenu () { @@ -1523,7 +1523,7 @@ public void KeyBindings_Removed_On_Close_ContextMenu () void Delete () { deleteFile = true; } } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void KeyBindings_With_ContextMenu_And_MenuBar () { @@ -1602,7 +1602,7 @@ public void KeyBindings_With_ContextMenu_And_MenuBar () void Rename () { renameFile = true; } } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar () { @@ -1672,7 +1672,7 @@ public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar () void NewContextMenu () { newContextMenu = true; } } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void HotKeys_Removed_On_Close_ContextMenu () { @@ -1758,7 +1758,7 @@ public void HotKeys_Removed_On_Close_ContextMenu () void Delete () { deleteFile = true; } } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void HotKeys_With_ContextMenu_And_MenuBar () { @@ -1890,7 +1890,7 @@ public void HotKeys_With_ContextMenu_And_MenuBar () void Rename () { renameFile = true; } } - [Fact] + [Fact (Skip = "Redo for CMv2")] [AutoInitShutdown] public void Opened_MenuBar_Is_Closed_When_Another_MenuBar_Is_Opening_Also_By_HotKey () { diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index a5c6fe4e1e..4ff91d88a6 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -192,7 +192,7 @@ public void CaptionedTextField_DoesNotOverspillViewport_Unicode () } - [Theory] + [Theory (Skip = "Broke with ContextMenuv2")] [AutoInitShutdown] [InlineData ("blah")] [InlineData (" ")] From a708648082c3c72962525d1836d0a47176c9eebb Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 16:21:36 -0600 Subject: [PATCH 17/75] Removed unneeded CM stuff from testhelpers --- Terminal.Gui/Application/Application.cs | 2 ++ UnitTests/TestHelpers.cs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index e31fcf47c8..9bcdce9920 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -167,6 +167,8 @@ internal static void ResetState (bool ignoreDisposed = false) Top = null; _cachedRunStateToplevel = null; + Popover = null; + // MainLoop stuff MainLoop?.Dispose (); MainLoop = null; diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index 62cad2635f..17b735669d 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -102,9 +102,9 @@ public override void After (MethodInfo methodUnderTest) } } - // Force the use of the default config file - Locations = ConfigLocations.DefaultOnly; - Reset (); + //// Force the use of the default config file + //Locations = ConfigLocations.DefaultOnly; + //Reset (); // Enable subsequent tests that call Init to get all config files (the default). Locations = ConfigLocations.All; @@ -118,7 +118,7 @@ public override void Before (MethodInfo methodUnderTest) { // Force the use of the default config file Locations = ConfigLocations.DefaultOnly; - Reset (); + //Reset (); // Init will do this. #if DEBUG_IDISPOSABLE @@ -157,10 +157,10 @@ public override void After (MethodInfo methodUnderTest) { Debug.WriteLine ($"After: {methodUnderTest.Name}"); base.After (methodUnderTest); - + // Reset the to default All Locations = ConfigLocations.All; - Reset (); + //Reset (); #if DEBUG_IDISPOSABLE Assert.Empty (Responder.Instances); @@ -209,7 +209,7 @@ public override void After (MethodInfo methodUnderTest) // Reset the to default All Locations = ConfigLocations.All; - Reset (); + //Reset (); } public override void Before (MethodInfo methodUnderTest) @@ -731,11 +731,11 @@ private static string ReplaceNewLinesToPlatformSpecific (string toReplace) string replaced = toReplace; replaced = Environment.NewLine.Length switch - { - 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), - 1 => replaced.Replace ("\r\n", Environment.NewLine), - var _ => replaced - }; + { + 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), + 1 => replaced.Replace ("\r\n", Environment.NewLine), + var _ => replaced + }; return replaced; } From d888de8f87ea08e759eab52143841a203e47c7ba Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 16:26:16 -0600 Subject: [PATCH 18/75] Shortcut API docs --- Terminal.Gui/Views/Shortcut.cs | 38 ++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index baf748b966..f6c387f2f4 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -43,7 +43,26 @@ public class Shortcut : View, IOrientation, IDesignable /// public Shortcut () : this (Key.Empty, string.Empty, null) { } - public Shortcut (View targetView, Command command, string commandText, string helpText = null) : this (targetView?.KeyBindings.GetKeyFromCommands (command), commandText, null, helpText) + /// + /// Creates a new instance of , binding it to and + /// . The Key + /// has bound to will be used as . + /// + /// + /// The View that will be invoked on when is + /// raised. + /// + /// + /// The Command to invoke on . The Key + /// has bound to will be used as + /// + /// The text to display for the command. + /// The help text to display. + public Shortcut (View targetView, Command command, string commandText, string helpText = null) : this ( + targetView?.KeyBindings.GetKeyFromCommands (command), + commandText, + null, + helpText) { _targetView = targetView; _command = command; @@ -58,9 +77,9 @@ public Shortcut (View targetView, Command command, string commandText, string he /// /// /// - /// + /// The text to display for the command. /// - /// + /// The help text to display. public Shortcut (Key key, string commandText, Action action, string helpText = null) { Id = "_shortcut"; @@ -84,7 +103,7 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n CommandView = new () { Width = Dim.Auto (), - Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1) + Height = Dim.Auto (1) }; HelpView.Id = "_helpView"; @@ -146,9 +165,9 @@ Dim GetWidthDimAuto () } [CanBeNull] - private View _targetView; + private readonly View _targetView; - private Command _command; + private readonly Command _command; private readonly OrientationHelper _orientationHelper; @@ -157,7 +176,7 @@ Dim GetWidthDimAuto () // This is used to calculate the minimum width of the Shortcut when the width is NOT Dim.Auto private int? _minimumDimAutoWidth; - /// + /// protected override bool OnHighlight (CancelEventArgs args) { if (args.NewValue.HasFlag (HighlightStyle.Hover)) @@ -840,10 +859,7 @@ internal void SetColors (bool highlight = false) } /// - protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view) - { - SetColors (); - } + protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view) { SetColors (); } #endregion Focus } From 8954fec18f12b3b021761d0e4e49dee23f38ed2c Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 19:57:45 -0600 Subject: [PATCH 19/75] Fixed keybinding unit tests --- Terminal.Gui/Input/KeyBindings.cs | 6 ++---- Terminal.Gui/Views/Shortcut.cs | 2 +- UnitTests/Input/KeyBindingTests.cs | 14 +++++--------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index 8950af4054..1b9b226dc5 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -285,11 +285,9 @@ public Command [] GetCommands (Key key) } /// Gets the Key used by a set of commands. - /// /// The set of commands to search. - /// The used by a - /// If no matching set of commands was found. - public Key GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } + /// The used by a . if the set of caommands was not found. + public Key? GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } /// Removes a from the collection. /// diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index f6c387f2f4..a9e53abdab 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -103,7 +103,7 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n CommandView = new () { Width = Dim.Auto (), - Height = Dim.Auto (1) + Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1) }; HelpView.Id = "_helpView"; diff --git a/UnitTests/Input/KeyBindingTests.cs b/UnitTests/Input/KeyBindingTests.cs index fe3d137e79..e5628da5ad 100644 --- a/UnitTests/Input/KeyBindingTests.cs +++ b/UnitTests/Input/KeyBindingTests.cs @@ -103,7 +103,9 @@ public void Clear_Clears () public void Defaults () { var keyBindings = new KeyBindings (); - Assert.Throws (() => keyBindings.GetKeyFromCommands (Command.Accept)); + Assert.Empty (keyBindings.Bindings); + Assert.Null (keyBindings.GetKeyFromCommands (Command.Accept)); + Assert.Null (keyBindings.BoundView); } [Fact] @@ -173,9 +175,6 @@ public void GetKeyFromCommands_MultipleCommands () key = keyBindings.GetKeyFromCommands (commands2); Assert.Equal (Key.B, key); - - // Negative case - Assert.Throws (() => key = keyBindings.GetKeyFromCommands (Command.RightEnd)); } [Fact] @@ -186,17 +185,14 @@ public void GetKeyFromCommands_OneCommand () Key key = keyBindings.GetKeyFromCommands (Command.Right); Assert.Equal (Key.A, key); - - // Negative case - Assert.Throws (() => key = keyBindings.GetKeyFromCommands (Command.Left)); } // GetKeyFromCommands [Fact] - public void GetKeyFromCommands_Unknown_Throws_InvalidOperationException () + public void GetKeyFromCommands_Unknown_Returns_Key_Empty () { var keyBindings = new KeyBindings (); - Assert.Throws (() => keyBindings.GetKeyFromCommands (Command.Accept)); + Assert.Null (keyBindings.GetKeyFromCommands (Command.Accept)); } [Fact] From 47a9ef7de0cc84a336ed80bb448a47413492942d Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 20:55:35 -0600 Subject: [PATCH 20/75] Fixed mouse handling --- Terminal.Gui/Application/Application.Mouse.cs | 5 + Terminal.Gui/Views/Bar.cs | 6 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 23 +- UICatalog/Scenarios/ContextMenus.cs | 448 ++++++++++-------- UICatalog/Scenarios/ContextMenusv2.cs | 335 ------------- .../Application/ApplicationPopoverTests.cs | 66 ++- 6 files changed, 334 insertions(+), 549 deletions(-) delete mode 100644 UICatalog/Scenarios/ContextMenusv2.cs diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index e8f23b44d4..caef59c4a1 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -193,6 +193,11 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) { Popover.Visible = false; + + // Recurse once + OnMouseEvent (mouseEvent); + + return; } // May be null before the prior condition or the condition may set it as null. diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 105359adcc..e7f9e5eb94 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -47,7 +47,7 @@ public Bar (IEnumerable shortcuts) private void OnMouseEvent (object? sender, MouseEventEventArgs e) { - NavigationDirection direction = NavigationDirection.Forward; + NavigationDirection direction = NavigationDirection.Backward; if (e.MouseEvent.Flags == MouseFlags.WheeledDown) { @@ -56,7 +56,7 @@ private void OnMouseEvent (object? sender, MouseEventEventArgs e) if (e.MouseEvent.Flags == MouseFlags.WheeledUp) { - direction = NavigationDirection.Backward; + direction = NavigationDirection.Forward; e.Handled = true; } @@ -67,7 +67,7 @@ private void OnMouseEvent (object? sender, MouseEventEventArgs e) if (e.MouseEvent.Flags == MouseFlags.WheeledLeft) { - direction = NavigationDirection.Backward; + direction = NavigationDirection.Forward; e.Handled = true; } diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 8ba6f3fefd..e9289414b9 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -44,15 +44,15 @@ public ContextMenuv2 () : this ([]) { } public ContextMenuv2 (IEnumerable shortcuts) : base(shortcuts) { Visible = false; - VisibleChanging += OnVisibleChanging; + VisibleChanged += OnVisibleChanged; Key = DefaultKey; } - private void OnVisibleChanging (object? sender, CancelEventArgs args) + private void OnVisibleChanged (object? sender, EventArgs _) { - if (args.NewValue) + if (Visible && Subviews.Count > 0) { - + Subviews [0].SetFocus (); } } @@ -74,4 +74,19 @@ public Key Key /// Event raised when the is changed. public event EventHandler? KeyChanged; + + /// + /// Sets the position of the ContextMenu. The actual position of the menu will be adjusted to + /// ensure the menu fully fits on the screen, and the mouse cursor is over the first call of the + /// first Shortcut. + /// + /// + public void SetPosition (Point screenPosition) + { + Frame = Frame with + { + X = screenPosition.X - GetViewportOffsetFromFrame ().X, + Y = screenPosition.Y - GetViewportOffsetFromFrame ().Y, + }; + } } diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index e4b589a1d8..21a9b7b9fa 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Globalization; using System.Threading; +using JetBrains.Annotations; using Terminal.Gui; namespace UICatalog.Scenarios; @@ -9,11 +10,10 @@ namespace UICatalog.Scenarios; [ScenarioCategory ("Menus")] public class ContextMenus : Scenario { - private readonly List _cultureInfos = Application.SupportedCultures; - private ContextMenu _contextMenu = new (); + [CanBeNull] + private ContextMenuv2 _contextMenu; private bool _forceMinimumPosToZero = true; private MenuItem _miForceMinimumPosToZero; - private MenuItem _miUseSubMenusSingleFrame; private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight; private bool _useSubMenusSingleFrame; @@ -28,13 +28,19 @@ public override void Main () Title = GetQuitKeyAndName () }; + _contextMenu = new ContextMenuv2 () + { + }; + + ConfigureMenu (_contextMenu); + _contextMenu.Key = Key.Space.WithCtrl; + var text = "Context Menu"; var width = 20; - var winContextMenuKey = (KeyCode)Key.Space.WithCtrl; var label = new Label { - X = Pos.Center (), Y = 1, Text = $"Press '{winContextMenuKey}' to open the Window context menu." + X = Pos.Center (), Y = 1, Text = $"Press '{_contextMenu.Key}' to open the Window context menu." }; appWindow.Add (label); @@ -46,250 +52,284 @@ public override void Main () }; appWindow.Add (label); - _tfTopLeft = new() { Width = width, Text = text }; + _tfTopLeft = new() { Id = "_tfTopLeft", Width = width, Text = text }; appWindow.Add (_tfTopLeft); - _tfTopRight = new() { X = Pos.AnchorEnd (width), Width = width, Text = text }; + _tfTopRight = new() { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text }; appWindow.Add (_tfTopRight); - _tfMiddle = new() { X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; + _tfMiddle = new() { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; appWindow.Add (_tfMiddle); - _tfBottomLeft = new() { Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomLeft = new() { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomLeft); - _tfBottomRight = new() { X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomRight = new() { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomRight); Point mousePos = default; appWindow.KeyDown += (s, e) => { - if (e.KeyCode == winContextMenuKey) + if (e.KeyCode == _contextMenu.Key) { - ShowContextMenu (mousePos.X, mousePos.Y); + Application.Popover = _contextMenu; + _contextMenu.Visible = true; e.Handled = true; } }; appWindow.MouseClick += (s, e) => { - if (e.MouseEvent.Flags == _contextMenu.MouseFlags) + if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) { - ShowContextMenu (e.MouseEvent.Position.X, e.MouseEvent.Position.Y); + Application.Popover = _contextMenu; + + _contextMenu.SetPosition (e.MouseEvent.ScreenPosition); + _contextMenu.Visible = true; e.Handled = true; } }; - Application.MouseEvent += ApplicationMouseEvent; - - void ApplicationMouseEvent (object sender, MouseEvent a) { mousePos = a.Position; } - - appWindow.WantMousePositionReports = true; - appWindow.Closed += (s, e) => { Thread.CurrentThread.CurrentUICulture = new ("en-US"); - Application.MouseEvent -= ApplicationMouseEvent; }; - var menu = new MenuBar - { - Menus = - [ - new ( - "_File", - new MenuItem [] { new ("_Quit", "", () => Application.RequestStop (), null, null, Application.QuitKey) }) - ] - }; - - var top = new Toplevel (); - top.Add (appWindow, menu); // Run - Start the application. - Application.Run (top); - top.Dispose (); + Application.Run (appWindow); + appWindow.Dispose (); + _contextMenu.Dispose (); // Shutdown - Calling Application.Shutdown is required. Application.Shutdown (); } - private MenuItem [] GetSupportedCultures () + private void ConfigureMenu (Bar bar) { - List supportedCultures = new (); - int index = -1; - foreach (CultureInfo c in _cultureInfos) + var shortcut1 = new Shortcut { - var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked }; - - if (index == -1) - { - culture.Title = "_English"; - culture.Help = "en-US"; - culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US"; - CreateAction (supportedCultures, culture); - supportedCultures.Add (culture); - index++; - culture = new() { CheckType = MenuItemCheckStyle.Checked }; - } - - culture.Title = $"_{c.Parent.EnglishName}"; - culture.Help = c.Name; - culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name; - CreateAction (supportedCultures, culture); - supportedCultures.Add (culture); - } - - return supportedCultures.ToArray (); - - void CreateAction (List supportedCultures, MenuItem culture) + Title = "Z_igzag", + Key = Key.I.WithCtrl, + Text = "Gonna zig zag", + HighlightStyle = HighlightStyle.Hover + }; + + var shortcut2 = new Shortcut { - culture.Action += () => - { - Thread.CurrentThread.CurrentUICulture = new (culture.Help); - culture.Checked = true; - - foreach (MenuItem item in supportedCultures) - { - item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name; - } - }; - } - } + Title = "Za_G", + Text = "Gonna zag", + Key = Key.G.WithAlt, + HighlightStyle = HighlightStyle.Hover + }; - private void ShowContextMenu (int x, int y) - { - _contextMenu = new() + var shortcut3 = new Shortcut { - Position = new (x, y), - ForceMinimumPosToZero = _forceMinimumPosToZero, - UseSubMenusSingleFrame = _useSubMenusSingleFrame + Title = "_Three", + Text = "The 3rd item", + Key = Key.D3.WithAlt, + HighlightStyle = HighlightStyle.Hover }; - MenuBarItem menuItems = new ( - new [] - { - new MenuBarItem ( - "_Languages", - GetSupportedCultures () - ), - new ( - "_Configuration", - "Show configuration", - () => MessageBox.Query ( - 50, - 5, - "Info", - "This would open settings dialog", - "Ok" - ) - ), - new MenuBarItem ( - "M_ore options", - new MenuItem [] - { - new ( - "_Setup", - "Change settings", - () => MessageBox - .Query ( - 50, - 5, - "Info", - "This would open setup dialog", - "Ok" - ), - shortcutKey: KeyCode.T - | KeyCode - .CtrlMask - ), - new ( - "_Maintenance", - "Maintenance mode", - () => MessageBox - .Query ( - 50, - 5, - "Info", - "This would open maintenance dialog", - "Ok" - ) - ) - } - ), - _miForceMinimumPosToZero = - new ( - "Fo_rceMinimumPosToZero", - "", - () => - { - _miForceMinimumPosToZero - .Checked = - _forceMinimumPosToZero = - !_forceMinimumPosToZero; - - //_tfTopLeft.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - //_tfTopRight.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - //_tfMiddle.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - //_tfBottomLeft.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - //_tfBottomRight - // .ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - } - ) - { - CheckType = - MenuItemCheckStyle - .Checked, - Checked = - _forceMinimumPosToZero - }, - _miUseSubMenusSingleFrame = - new ( - "Use_SubMenusSingleFrame", - "", - () => _contextMenu - .UseSubMenusSingleFrame = - (bool) - (_miUseSubMenusSingleFrame - .Checked = - _useSubMenusSingleFrame = - !_useSubMenusSingleFrame) - ) - { - CheckType = MenuItemCheckStyle - .Checked, - Checked = - _useSubMenusSingleFrame - }, - null, - new ( - "_Quit", - "", - () => Application.RequestStop () - ) - } - ); - //_tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - //_tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - //_tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - //_tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - //_tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - - _contextMenu.Show (menuItems); + var line = new Line () + { + BorderStyle = LineStyle.Dotted, + Orientation = Orientation.Horizontal, + CanFocus = false, + }; + // HACK: Bug in Line + line.Orientation = Orientation.Vertical; + line.Orientation = Orientation.Horizontal; + + var shortcut4 = new Shortcut + { + Title = "_Four", + Text = "Below the line", + Key = Key.D3.WithAlt, + HighlightStyle = HighlightStyle.Hover + }; + bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); } + + + //private MenuItem [] GetSupportedCultures () + //{ + // List supportedCultures = new (); + // int index = -1; + + // foreach (CultureInfo c in _cultureInfos) + // { + // var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked }; + + // if (index == -1) + // { + // culture.Title = "_English"; + // culture.Help = "en-US"; + // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US"; + // CreateAction (supportedCultures, culture); + // supportedCultures.Add (culture); + // index++; + // culture = new() { CheckType = MenuItemCheckStyle.Checked }; + // } + + // culture.Title = $"_{c.Parent.EnglishName}"; + // culture.Help = c.Name; + // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name; + // CreateAction (supportedCultures, culture); + // supportedCultures.Add (culture); + // } + + // return supportedCultures.ToArray (); + + // void CreateAction (List supportedCultures, MenuItem culture) + // { + // culture.Action += () => + // { + // Thread.CurrentThread.CurrentUICulture = new (culture.Help); + // culture.Checked = true; + + // foreach (MenuItem item in supportedCultures) + // { + // item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name; + // } + // }; + // } + //} + + //private void ShowContextMenu (int x, int y) + //{ + // _contextMenu = new() + // { + // Position = new (x, y), + // ForceMinimumPosToZero = _forceMinimumPosToZero, + // UseSubMenusSingleFrame = _useSubMenusSingleFrame + // }; + + // MenuBarItem menuItems = new ( + // new [] + // { + // new MenuBarItem ( + // "_Languages", + // GetSupportedCultures () + // ), + // new ( + // "_Configuration", + // "Show configuration", + // () => MessageBox.Query ( + // 50, + // 5, + // "Info", + // "This would open settings dialog", + // "Ok" + // ) + // ), + // new MenuBarItem ( + // "M_ore options", + // new MenuItem [] + // { + // new ( + // "_Setup", + // "Change settings", + // () => MessageBox + // .Query ( + // 50, + // 5, + // "Info", + // "This would open setup dialog", + // "Ok" + // ), + // shortcutKey: KeyCode.T + // | KeyCode + // .CtrlMask + // ), + // new ( + // "_Maintenance", + // "Maintenance mode", + // () => MessageBox + // .Query ( + // 50, + // 5, + // "Info", + // "This would open maintenance dialog", + // "Ok" + // ) + // ) + // } + // ), + // _miForceMinimumPosToZero = + // new ( + // "Fo_rceMinimumPosToZero", + // "", + // () => + // { + // _miForceMinimumPosToZero + // .Checked = + // _forceMinimumPosToZero = + // !_forceMinimumPosToZero; + + // _tfTopLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfTopRight.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfMiddle.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfBottomLeft.ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + + // _tfBottomRight + // .ContextMenu + // .ForceMinimumPosToZero = + // _forceMinimumPosToZero; + // } + // ) + // { + // CheckType = + // MenuItemCheckStyle + // .Checked, + // Checked = + // _forceMinimumPosToZero + // }, + // _miUseSubMenusSingleFrame = + // new ( + // "Use_SubMenusSingleFrame", + // "", + // () => _contextMenu + // .UseSubMenusSingleFrame = + // (bool) + // (_miUseSubMenusSingleFrame + // .Checked = + // _useSubMenusSingleFrame = + // !_useSubMenusSingleFrame) + // ) + // { + // CheckType = MenuItemCheckStyle + // .Checked, + // Checked = + // _useSubMenusSingleFrame + // }, + // null, + // new ( + // "_Quit", + // "", + // () => Application.RequestStop () + // ) + // } + // ); + // _tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + // _tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; + + // _contextMenu.Show (menuItems); + //} } diff --git a/UICatalog/Scenarios/ContextMenusv2.cs b/UICatalog/Scenarios/ContextMenusv2.cs deleted file mode 100644 index b724f20591..0000000000 --- a/UICatalog/Scenarios/ContextMenusv2.cs +++ /dev/null @@ -1,335 +0,0 @@ -using System.Collections.Generic; -using System.Globalization; -using System.Threading; -using JetBrains.Annotations; -using Terminal.Gui; - -namespace UICatalog.Scenarios; - -[ScenarioMetadata ("ContextMenus v2", "Context Menu v2 Sample.")] -[ScenarioCategory ("Menus")] -public class ContextMenusv2 : Scenario -{ - [CanBeNull] - private ContextMenuv2 _contextMenu; - private bool _forceMinimumPosToZero = true; - private MenuItem _miForceMinimumPosToZero; - private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight; - private bool _useSubMenusSingleFrame; - - public override void Main () - { - // Init - Application.Init (); - - // Setup - Create a top-level application window and configure it. - Window appWindow = new () - { - Title = GetQuitKeyAndName () - }; - - _contextMenu = new ContextMenuv2 () - { - }; - - ConfigureMenu (_contextMenu); - _contextMenu.Key = Key.Space.WithCtrl; - - var text = "Context Menu"; - var width = 20; - - var label = new Label - { - X = Pos.Center (), Y = 1, Text = $"Press '{_contextMenu.Key}' to open the Window context menu." - }; - appWindow.Add (label); - - label = new() - { - X = Pos.Center (), - Y = Pos.Bottom (label), - Text = $"Press '{ContextMenu.DefaultKey}' to open the TextField context menu." - }; - appWindow.Add (label); - - _tfTopLeft = new() { Id = "_tfTopLeft", Width = width, Text = text }; - appWindow.Add (_tfTopLeft); - - _tfTopRight = new() { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text }; - appWindow.Add (_tfTopRight); - - _tfMiddle = new() { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; - appWindow.Add (_tfMiddle); - - _tfBottomLeft = new() { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text }; - appWindow.Add (_tfBottomLeft); - - _tfBottomRight = new() { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; - appWindow.Add (_tfBottomRight); - - Point mousePos = default; - - appWindow.KeyDown += (s, e) => - { - if (e.KeyCode == _contextMenu.Key) - { - Application.Popover = _contextMenu; - _contextMenu.Visible = true; - e.Handled = true; - } - }; - - appWindow.MouseClick += (s, e) => - { - if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) - { - Application.Popover = _contextMenu; - _contextMenu.X = e.MouseEvent.ScreenPosition.X; - _contextMenu.Y = e.MouseEvent.ScreenPosition.Y; - _contextMenu.Visible = true; - e.Handled = true; - } - }; - - appWindow.Closed += (s, e) => - { - Thread.CurrentThread.CurrentUICulture = new ("en-US"); - }; - - - // Run - Start the application. - Application.Run (appWindow); - appWindow.Dispose (); - _contextMenu.Dispose (); - - // Shutdown - Calling Application.Shutdown is required. - Application.Shutdown (); - } - - private void ConfigureMenu (Bar bar) - { - - var shortcut1 = new Shortcut - { - Title = "Z_igzag", - Key = Key.I.WithCtrl, - Text = "Gonna zig zag", - HighlightStyle = HighlightStyle.Hover - }; - - var shortcut2 = new Shortcut - { - Title = "Za_G", - Text = "Gonna zag", - Key = Key.G.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - - var shortcut3 = new Shortcut - { - Title = "_Three", - Text = "The 3rd item", - Key = Key.D3.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - - var line = new Line () - { - BorderStyle = LineStyle.Dotted, - Orientation = Orientation.Horizontal, - CanFocus = false, - }; - // HACK: Bug in Line - line.Orientation = Orientation.Vertical; - line.Orientation = Orientation.Horizontal; - - var shortcut4 = new Shortcut - { - Title = "_Four", - Text = "Below the line", - Key = Key.D3.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); - } - - - //private MenuItem [] GetSupportedCultures () - //{ - // List supportedCultures = new (); - // int index = -1; - - // foreach (CultureInfo c in _cultureInfos) - // { - // var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked }; - - // if (index == -1) - // { - // culture.Title = "_English"; - // culture.Help = "en-US"; - // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US"; - // CreateAction (supportedCultures, culture); - // supportedCultures.Add (culture); - // index++; - // culture = new() { CheckType = MenuItemCheckStyle.Checked }; - // } - - // culture.Title = $"_{c.Parent.EnglishName}"; - // culture.Help = c.Name; - // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name; - // CreateAction (supportedCultures, culture); - // supportedCultures.Add (culture); - // } - - // return supportedCultures.ToArray (); - - // void CreateAction (List supportedCultures, MenuItem culture) - // { - // culture.Action += () => - // { - // Thread.CurrentThread.CurrentUICulture = new (culture.Help); - // culture.Checked = true; - - // foreach (MenuItem item in supportedCultures) - // { - // item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name; - // } - // }; - // } - //} - - //private void ShowContextMenu (int x, int y) - //{ - // _contextMenu = new() - // { - // Position = new (x, y), - // ForceMinimumPosToZero = _forceMinimumPosToZero, - // UseSubMenusSingleFrame = _useSubMenusSingleFrame - // }; - - // MenuBarItem menuItems = new ( - // new [] - // { - // new MenuBarItem ( - // "_Languages", - // GetSupportedCultures () - // ), - // new ( - // "_Configuration", - // "Show configuration", - // () => MessageBox.Query ( - // 50, - // 5, - // "Info", - // "This would open settings dialog", - // "Ok" - // ) - // ), - // new MenuBarItem ( - // "M_ore options", - // new MenuItem [] - // { - // new ( - // "_Setup", - // "Change settings", - // () => MessageBox - // .Query ( - // 50, - // 5, - // "Info", - // "This would open setup dialog", - // "Ok" - // ), - // shortcutKey: KeyCode.T - // | KeyCode - // .CtrlMask - // ), - // new ( - // "_Maintenance", - // "Maintenance mode", - // () => MessageBox - // .Query ( - // 50, - // 5, - // "Info", - // "This would open maintenance dialog", - // "Ok" - // ) - // ) - // } - // ), - // _miForceMinimumPosToZero = - // new ( - // "Fo_rceMinimumPosToZero", - // "", - // () => - // { - // _miForceMinimumPosToZero - // .Checked = - // _forceMinimumPosToZero = - // !_forceMinimumPosToZero; - - // _tfTopLeft.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - // _tfTopRight.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - // _tfMiddle.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - // _tfBottomLeft.ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - - // _tfBottomRight - // .ContextMenu - // .ForceMinimumPosToZero = - // _forceMinimumPosToZero; - // } - // ) - // { - // CheckType = - // MenuItemCheckStyle - // .Checked, - // Checked = - // _forceMinimumPosToZero - // }, - // _miUseSubMenusSingleFrame = - // new ( - // "Use_SubMenusSingleFrame", - // "", - // () => _contextMenu - // .UseSubMenusSingleFrame = - // (bool) - // (_miUseSubMenusSingleFrame - // .Checked = - // _useSubMenusSingleFrame = - // !_useSubMenusSingleFrame) - // ) - // { - // CheckType = MenuItemCheckStyle - // .Checked, - // Checked = - // _useSubMenusSingleFrame - // }, - // null, - // new ( - // "_Quit", - // "", - // () => Application.RequestStop () - // ) - // } - // ); - // _tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - // _tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - // _tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - // _tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - // _tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero; - - // _contextMenu.Show (menuItems); - //} -} diff --git a/UnitTests/Application/ApplicationPopoverTests.cs b/UnitTests/Application/ApplicationPopoverTests.cs index 64fde7cd0c..00976e6a4f 100644 --- a/UnitTests/Application/ApplicationPopoverTests.cs +++ b/UnitTests/Application/ApplicationPopoverTests.cs @@ -1,4 +1,5 @@ -using Xunit.Abstractions; +using System.ComponentModel; +using Xunit.Abstractions; namespace Terminal.Gui.ApplicationTests; @@ -144,14 +145,73 @@ public void Popover_Quit_Command_Hides () Assert.False (popover.HasFocus); } + + [Fact] + public void Popover_MouseClick_Outside_Hides_Passes_Event_On () + { + // Arrange + Application.Top = new Toplevel () + { + Id = "top", + Height = 10, + Width = 10, + }; + + View otherView = new () + { + X = 1, + Y = 1, + Height = 1, + Width = 1, + Id = "otherView", + }; + + bool otherViewPressed = false; + otherView.MouseEvent += (sender, e) => + { + otherViewPressed = e.MouseEvent.Flags.HasFlag(MouseFlags.Button1Pressed); + }; + + Application.Top.Add (otherView); + + var popover = new View () + { + Id = "popover", + X = 5, + Y = 5, + Width = 1, + Height = 1, + Visible = false, + CanFocus = true + }; + + Application.Popover = popover; + popover.Visible = true; + Assert.True (popover.Visible); + Assert.True (popover.HasFocus); + + // Act + // Click on popover + Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (5, 5) }); + Assert.True (popover.Visible); + + // Click outside popover (on button) + Application.OnMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (1, 1) }); + + // Assert + Assert.True (otherViewPressed); + Assert.False (popover.Visible); + + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); + } + [Theory] [InlineData (0, 0, false)] [InlineData (5, 5, true)] [InlineData (10, 10, false)] [InlineData (5, 10, false)] [InlineData (9, 9, false)] - - public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) { // Arrange From 23c3ec86ce950f3887260edec95af18de92c8955 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 26 Sep 2024 21:16:28 -0600 Subject: [PATCH 21/75] Fighting with CM related unit test failures --- UnitTests/TestHelpers.cs | 25 +++++++------------------ UnitTests/Views/RuneCellTests.cs | 23 ++++++++++++++--------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index 17b735669d..b527b00625 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -73,10 +73,8 @@ public override void After (MethodInfo methodUnderTest) if (AutoInit) { - try + // try { - // TODO: This Dispose call is here until all unit tests that don't correctly dispose Toplevel's they create are fixed. - //Application.Top?.Dispose (); Application.Shutdown (); #if DEBUG_IDISPOSABLE if (Responder.Instances.Count == 0) @@ -89,11 +87,11 @@ public override void After (MethodInfo methodUnderTest) } #endif } - catch (Exception e) - { - Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}"); - } - finally + //catch (Exception e) + //{ + // Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}"); + //} + //finally { #if DEBUG_IDISPOSABLE Responder.Instances.Clear (); @@ -102,10 +100,6 @@ public override void After (MethodInfo methodUnderTest) } } - //// Force the use of the default config file - //Locations = ConfigLocations.DefaultOnly; - //Reset (); - // Enable subsequent tests that call Init to get all config files (the default). Locations = ConfigLocations.All; } @@ -116,10 +110,6 @@ public override void Before (MethodInfo methodUnderTest) if (AutoInit) { - // Force the use of the default config file - Locations = ConfigLocations.DefaultOnly; - //Reset (); // Init will do this. - #if DEBUG_IDISPOSABLE // Clear out any lingering Responder instances from previous tests @@ -160,7 +150,6 @@ public override void After (MethodInfo methodUnderTest) // Reset the to default All Locations = ConfigLocations.All; - //Reset (); #if DEBUG_IDISPOSABLE Assert.Empty (Responder.Instances); @@ -209,12 +198,12 @@ public override void After (MethodInfo methodUnderTest) // Reset the to default All Locations = ConfigLocations.All; - //Reset (); } public override void Before (MethodInfo methodUnderTest) { Debug.WriteLine ($"Before: {methodUnderTest.Name}"); + // Force the use of the default config file Locations = ConfigLocations.DefaultOnly; Reset (); diff --git a/UnitTests/Views/RuneCellTests.cs b/UnitTests/Views/RuneCellTests.cs index 9b09b249d5..215f383a70 100644 --- a/UnitTests/Views/RuneCellTests.cs +++ b/UnitTests/Views/RuneCellTests.cs @@ -50,7 +50,7 @@ public void Equals_True () } [Fact] - [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)] + [SetupFakeDriver] public void RuneCell_LoadRuneCells_InheritsPreviousColorScheme () { List runeCells = new (); @@ -69,9 +69,13 @@ public void RuneCell_LoadRuneCells_InheritsPreviousColorScheme () TextView tv = CreateTextView (); tv.Load (runeCells); - var top = new Toplevel (); - top.Add (tv); - RunState rs = Application.Begin (top); + Application.Top = new Toplevel (); + Application.Top.Add (tv); + Application.Top.BeginInit(); + Application.Top.EndInit(); + + Application.Top.Draw (); + Assert.True (tv.InheritsPreviousColorScheme); var expectedText = @" @@ -109,7 +113,7 @@ Colors.ColorSchemes ["Error"].Focus TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes); tv.WordWrap = true; - Application.Refresh (); + Application.Top.Draw (); TestHelpers.AssertDriverContentsWithFrameAre (expectedText, output); TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes); @@ -121,7 +125,8 @@ Colors.ColorSchemes ["Error"].Focus tv.Selecting = false; tv.CursorPosition = new (2, 4); tv.Paste (); - Application.Refresh (); + + Application.Top.Draw (); expectedText = @" TopLevel @@ -156,7 +161,7 @@ Colors.ColorSchemes ["Error"].Focus tv.Selecting = false; tv.CursorPosition = new (2, 4); tv.Paste (); - Application.Refresh (); + Application.Top.Draw (); expectedText = @" TopLevel @@ -180,8 +185,8 @@ Colors.ColorSchemes ["Error"].Focus 4440000000"; TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes); - Application.End (rs); - top.Dispose (); + Application.Top.Dispose (); + Application.ResetState (true); } [Fact] From 493ece7612902f1d2a3d1a264a513b9328993183 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 27 Sep 2024 06:48:13 -0600 Subject: [PATCH 22/75] Unit tests pass. I think. --- Terminal.Gui/Application/Application.cs | 3 ++- Terminal.Gui/Views/Shortcut.cs | 12 ++++++++---- UnitTests/Views/RuneCellTests.cs | 2 ++ UnitTests/Views/ShortcutTests.cs | 13 ++++++------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index 9bcdce9920..174fcb8062 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -217,7 +217,8 @@ internal static void ResetState (bool ignoreDisposed = false) AddApplicationKeyBindings (); - Colors.Reset (); + // BUGBUG: This should not be here as it conflics with CM + //Colors.Reset (); // Reset synchronization context to allow the user to run async/await, // as the main loop has been ended, the synchronization context from diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index a9e53abdab..934d5c1b2a 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -15,9 +15,9 @@ /// - Pressing the HotKey specified by . /// /// -/// If is , will invoked +/// If is , will invoke /// -/// command regardless of what View has focus, enabling an application-wide keyboard shortcut. +/// regardless of what View has focus, enabling an application-wide keyboard shortcut. /// /// /// By default, a Shortcut displays the command text on the left side, the help text in the middle, and the key @@ -48,9 +48,13 @@ public Shortcut () : this (Key.Empty, string.Empty, null) { } /// . The Key /// has bound to will be used as . /// + /// + /// + /// This is a helper API that simplifies creation of multiple Shortcuts when adding them to -based objects, like . + /// + /// /// - /// The View that will be invoked on when is - /// raised. + /// The View that will be invoked when user does something that causes the Shortcut's Accept event to be raised. /// /// /// The Command to invoke on . The Key diff --git a/UnitTests/Views/RuneCellTests.cs b/UnitTests/Views/RuneCellTests.cs index 215f383a70..8e0726c9e3 100644 --- a/UnitTests/Views/RuneCellTests.cs +++ b/UnitTests/Views/RuneCellTests.cs @@ -53,6 +53,8 @@ public void Equals_True () [SetupFakeDriver] public void RuneCell_LoadRuneCells_InheritsPreviousColorScheme () { + // NOTE: This test relies on CM loading the default color schemes. It will not work if the default color schemes are not loaded. + // NOTE: SetupFakeDriver DOES setup CM correctly. List runeCells = new (); foreach (KeyValuePair color in Colors.ColorSchemes) diff --git a/UnitTests/Views/ShortcutTests.cs b/UnitTests/Views/ShortcutTests.cs index af172e9324..39a1296746 100644 --- a/UnitTests/Views/ShortcutTests.cs +++ b/UnitTests/Views/ShortcutTests.cs @@ -477,11 +477,10 @@ public void KeyDown_Invokes_Accept (bool canFocus, KeyCode key, int expectedAcce [InlineData (KeyCode.Enter, 1)] [InlineData (KeyCode.Space, 0)] [InlineData (KeyCode.F1, 0)] - [AutoInitShutdown] public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept) { - var current = new Toplevel (); - + Application.Top = new Toplevel (); + var shortcut = new Shortcut { Key = Key.A, @@ -489,9 +488,8 @@ public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept) Text = "0", Title = "_C" }; - current.Add (shortcut); - - Application.Begin (current); + Application.Top.Add (shortcut); + Application.Top.SetFocus (); var accepted = 0; shortcut.Accept += (s, e) => accepted++; @@ -500,7 +498,8 @@ public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept) Assert.Equal (expectedAccept, accepted); - current.Dispose (); + Application.Top.Dispose (); + Application.ResetState (true); } [Theory] From 16e76895c7d456431e0370096fdbce74006f7914 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 27 Sep 2024 06:49:54 -0600 Subject: [PATCH 23/75] Shortcut code cleanup --- Terminal.Gui/Views/Shortcut.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 934d5c1b2a..81b59eb9df 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -50,11 +50,13 @@ public Shortcut () : this (Key.Empty, string.Empty, null) { } /// /// /// - /// This is a helper API that simplifies creation of multiple Shortcuts when adding them to -based objects, like . + /// This is a helper API that simplifies creation of multiple Shortcuts when adding them to -based + /// objects, like . /// /// /// - /// The View that will be invoked when user does something that causes the Shortcut's Accept event to be raised. + /// The View that will be invoked when user does something that causes the Shortcut's Accept + /// event to be raised. /// /// /// The Command to invoke on . The Key @@ -107,7 +109,7 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n CommandView = new () { Width = Dim.Auto (), - Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1) + Height = Dim.Auto (1) }; HelpView.Id = "_helpView"; @@ -169,9 +171,9 @@ Dim GetWidthDimAuto () } [CanBeNull] - private readonly View _targetView; + private readonly View _targetView; // If set, _command will be invoked - private readonly Command _command; + private readonly Command _command; // Used when _targetView is set private readonly OrientationHelper _orientationHelper; From 5d9e0d15f97d074abe4eecbe1d240edede145858 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 27 Sep 2024 07:13:42 -0600 Subject: [PATCH 24/75] TextView uses new CM2 --- Terminal.Gui/Views/Shortcut.cs | 2 +- Terminal.Gui/Views/TextField.cs | 1 - Terminal.Gui/Views/TextView.cs | 133 ++++++++++++++------------------ UICatalog/Scenarios/Editor.cs | 10 +-- 4 files changed, 63 insertions(+), 83 deletions(-) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 81b59eb9df..84390f1c40 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -109,7 +109,7 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n CommandView = new () { Width = Dim.Auto (), - Height = Dim.Auto (1) + Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1) }; HelpView.Id = "_helpView"; diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 905a25c9b0..ef3969b171 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -409,7 +409,6 @@ public TextField () ContextMenu = CreateContextMenu (); KeyBindings.Add (ContextMenu!.Key, KeyBindingScope.HotKey, Command.Context); - } diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b81877b531..b0b7562d17 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -2393,11 +2393,8 @@ public TextView () Command.Context, () => { - ContextMenu!.Position = new ( - CursorPosition.X - _leftColumn + 2, - CursorPosition.Y - _topRow + 2 - ); - ShowContextMenu (); + ContextMenu!.SetPosition (new (CursorPosition.X - _leftColumn + 2, CursorPosition.Y - _topRow + 2)); + ShowContextMenu (true); return true; } @@ -2498,10 +2495,9 @@ public TextView () _currentCulture = Thread.CurrentThread.CurrentUICulture; - ContextMenu = new (); + ContextMenu = CreateContextMenu (); + KeyBindings.Add (ContextMenu!.Key, KeyBindingScope.HotKey, Command.Context); ContextMenu.KeyChanged += ContextMenu_KeyChanged!; - - KeyBindings.Add ((KeyCode)ContextMenu.Key, KeyBindingScope.HotKey, Command.Context); } private void TextView_Added1 (object? sender, SuperViewChangedEventArgs e) @@ -2586,8 +2582,8 @@ public bool AllowsTab /// public IAutocomplete Autocomplete { get; protected set; } = new TextViewAutocomplete (); - /// Get the for this view. - public ContextMenu? ContextMenu { get; } + /// Get the for this view. + public ContextMenuv2? ContextMenu { get; private set; } /// Gets the cursor column. /// The cursor column. @@ -3487,8 +3483,12 @@ protected internal override bool OnMouseEvent (MouseEvent ev) } else if (ev.Flags == ContextMenu!.MouseFlags) { - ContextMenu.Position = ViewportToScreen ((Viewport with { X = ev.Position.X, Y = ev.Position.Y }).Location); - ShowContextMenu (); + ContextMenu!.X = ev.ScreenPosition.X; + ContextMenu!.Y = ev.ScreenPosition.Y; + + ShowContextMenu (false); + //ContextMenu.Position = ViewportToScreen ((Viewport with { X = ev.Position.X, Y = ev.Position.Y }).Location); + //ShowContextMenu (); } return true; @@ -4124,69 +4124,22 @@ private void Adjust () private void AppendClipboard (string text) { Clipboard.Contents += text; } - private MenuBarItem? BuildContextMenuBarItem () + private ContextMenuv2 CreateContextMenu () { - return new ( - new MenuItem [] - { - new ( - Strings.ctxSelectAll, - "", - SelectAll, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll) - ), - new ( - Strings.ctxDeleteAll, - "", - DeleteAll, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll) - ), - new ( - Strings.ctxCopy, - "", - Copy, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy) - ), - new ( - Strings.ctxCut, - "", - Cut, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut) - ), - new ( - Strings.ctxPaste, - "", - Paste, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste) - ), - new ( - Strings.ctxUndo, - "", - Undo, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo) - ), - new ( - Strings.ctxRedo, - "", - Redo, - null, - null, - (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo) - ) - } - ); + ContextMenuv2 menu = new (new List () + { + new (this, Command.SelectAll, Strings.ctxSelectAll), + new (this, Command.DeleteAll, Strings.ctxDeleteAll), + new (this, Command.Copy, Strings.ctxCopy), + new (this, Command.Cut, Strings.ctxCut), + new (this, Command.Paste, Strings.ctxPaste), + new (this, Command.Undo, Strings.ctxUndo), + new (this, Command.Redo, Strings.ctxRedo), + }); + + menu.KeyChanged += ContextMenu_KeyChanged; + + return menu; } private void ClearRegion (int left, int top, int right, int bottom) @@ -6280,14 +6233,30 @@ private void SetWrapModel ([CallerMemberName] string? caller = null) } } - private void ShowContextMenu () + private void ShowContextMenu (bool keyboard) { if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture)) { _currentCulture = Thread.CurrentThread.CurrentUICulture; + + if (ContextMenu is { }) + { + Point currentLoc = ContextMenu.Frame.Location; + ContextMenu.Dispose (); + ContextMenu = CreateContextMenu (); + ContextMenu.Frame = ContextMenu.Frame with { Location = currentLoc }; + } } - ContextMenu!.Show (BuildContextMenuBarItem ()); + if (keyboard) + { + Point loc = new Point (CursorPosition.X - _leftColumn, CursorPosition.Y - _topRow + 2); + ContextMenu!.X = loc.X; + ContextMenu!.Y = loc.Y; + } + + Application.Popover = ContextMenu; + ContextMenu!.Visible = true; } private void StartSelecting () @@ -6453,6 +6422,18 @@ private void WrapTextModel () SetNeedsDisplay (); } } + + /// + protected override void Dispose (bool disposing) + { + if (ContextMenu is { }) + { + ContextMenu.Visible = false; + ContextMenu.Dispose (); + ContextMenu = null; + } + base.Dispose (disposing); + } } /// diff --git a/UICatalog/Scenarios/Editor.cs b/UICatalog/Scenarios/Editor.cs index 65d2506709..2b8a9e9280 100644 --- a/UICatalog/Scenarios/Editor.cs +++ b/UICatalog/Scenarios/Editor.cs @@ -218,12 +218,12 @@ public override void Main () "", () => { - _miForceMinimumPosToZero.Checked = - _forceMinimumPosToZero = - !_forceMinimumPosToZero; + //_miForceMinimumPosToZero.Checked = + // _forceMinimumPosToZero = + // !_forceMinimumPosToZero; - _textView.ContextMenu.ForceMinimumPosToZero = - _forceMinimumPosToZero; + //_textView.ContextMenu.ForceMinimumPosToZero = + // _forceMinimumPosToZero; } ) { From fb85d44e18e2fb43d00e30ac6a8134d1a23b089f Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 28 Sep 2024 16:24:09 -0700 Subject: [PATCH 25/75] Starting on OnSelect etc... --- Terminal.Gui/Input/KeyBindings.cs | 10 +- Terminal.Gui/View/View.Keyboard.cs | 25 ++-- Terminal.Gui/View/View.cs | 24 ++++ Terminal.Gui/Views/Button.cs | 20 ++- Terminal.Gui/Views/CheckBox.cs | 38 ++++-- Terminal.Gui/Views/ListView.cs | 2 +- Terminal.Gui/Views/RadioGroup.cs | 23 +++- Terminal.Gui/Views/Shortcut.cs | 136 +++++++++++++------ Terminal.Gui/Views/TextField.cs | 15 +++ Terminal.Gui/Views/TextView.cs | 5 +- UICatalog/Scenarios/ContextMenus.cs | 157 ++++++++++++---------- UICatalog/Scenarios/FileDialogExamples.cs | 2 +- UICatalog/Scenarios/Shortcuts.cs | 30 +++-- UnitTests/Views/TextViewTests.cs | 2 +- docfx/docs/navigation.md | 14 +- 15 files changed, 340 insertions(+), 163 deletions(-) diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index 1b9b226dc5..6e1d86a136 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -284,10 +284,14 @@ public Command [] GetCommands (Key key) return Array.Empty (); } - /// Gets the Key used by a set of commands. + // TODO: Consider having this return all keys bound to the commands + /// Gets the first Key bound to the set of commands specified by . /// The set of commands to search. - /// The used by a . if the set of caommands was not found. - public Key? GetKeyFromCommands (params Command [] commands) { return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; } + /// The first bound to the set of commands specified by . if the set of caommands was not found. + public Key? GetKeyFromCommands (params Command [] commands) + { + return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key; + } /// Removes a from the collection. /// diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index 9561b6a5ca..3cf6540057 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -15,7 +15,14 @@ private void SetupKeyboard () TitleTextFormatter.HotKeyChanged += TitleTextFormatter_HotKeyChanged; // By default, the HotKey command sets the focus - AddCommand (Command.HotKey, OnHotKey); + AddCommand (Command.Select, () => + { + SetFocus (); + return OnSelect (); + }); + + // By default, the HotKey command sets the focus + AddCommand (Command.HotKey, () => SetFocus ()); // By default, the Accept command raises the Accept event AddCommand (Command.Accept, OnAccept); @@ -28,22 +35,6 @@ private void SetupKeyboard () #region HotKey Support - /// - /// Called when the HotKey command () is invoked. Causes this view to be focused. - /// - /// If the command was canceled. - private bool? OnHotKey () - { - if (CanFocus) - { - SetFocus (); - - return true; - } - - return false; - } - /// Invoked when the is changed. public event EventHandler? HotKeyChanged; diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index ae50fc8198..7cc4b8732f 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -108,6 +108,13 @@ namespace Terminal.Gui; public partial class View : Responder, ISupportInitializeNotification { + /// + /// Cancelable event fired when the command is invoked. Set + /// + /// to cancel the event. + /// + public event EventHandler? Select; + /// /// Cancelable event fired when the command is invoked. Set /// @@ -163,6 +170,23 @@ protected override void Dispose (bool disposing) return Accept is null ? null : args.Handled; } + + /// + /// Called when the command is invoked. Raises + /// event. + /// + /// + /// If the event was canceled. If the event was raised but not canceled. + /// If no event was raised. + /// + protected bool? OnSelect () + { + var args = new HandledEventArgs (); + Select?.Invoke (this, args); + + return Select is null ? null : args.Handled; + } + #region Constructors and Initialization /// diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 742c341510..a54c7a58f2 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -61,6 +61,17 @@ public Button () CanFocus = true; + // By default, the HotKey command sets the focus + AddCommand (Command.Select, () => + { + if (!OnAccept () == true) + { + SetFocus (); + } + + return false; + }); + // Override default behavior of View AddCommand (Command.HotKey, () => { @@ -106,7 +117,14 @@ public override bool WantContinuousButtonPressed private void Button_MouseClick (object sender, MouseEventEventArgs e) { - e.Handled = InvokeCommand (Command.HotKey) == true; + if (CanFocus) + { + e.Handled = InvokeCommand (Command.HotKey) == true; + } + else + { + e.Handled = InvokeCommand (Command.Select) == true; + } } private void Button_TitleChanged (object sender, EventArgs e) diff --git a/Terminal.Gui/Views/CheckBox.cs b/Terminal.Gui/Views/CheckBox.cs index f94a6b4f5a..9a3bb229a4 100644 --- a/Terminal.Gui/Views/CheckBox.cs +++ b/Terminal.Gui/Views/CheckBox.cs @@ -21,11 +21,25 @@ public CheckBox () CanFocus = true; // Things this view knows how to do - AddCommand (Command.Accept, AdvanceCheckState); - AddCommand (Command.HotKey, AdvanceCheckState); + + AddCommand (Command.Accept, OnAccept); + AddCommand (Command.HotKey, () => + { + //AdvanceCheckState (); + + return OnAccept (); + }); + AddCommand (Command.Select, () => + { + OnSelect (); + AdvanceCheckState (); + + return false; + }); // Default keybindings for this view - KeyBindings.Add (Key.Space, Command.Accept); + KeyBindings.Add (Key.Space, Command.Select); + KeyBindings.Add (Key.Enter, Command.Accept); TitleChanged += Checkbox_TitleChanged; @@ -35,7 +49,17 @@ public CheckBox () private void CheckBox_MouseClick (object? sender, MouseEventEventArgs e) { - e.Handled = AdvanceCheckState () == true; + //e.Handled = AdvanceCheckState () == true; + + //if (CanFocus) + { + e.Handled = InvokeCommand (Command.Select) == true; + } + //else + //{ + // e.Handled = InvokeCommand (Command.Accept) == true; + //} + } private void Checkbox_TitleChanged (object? sender, EventArgs e) @@ -164,11 +188,7 @@ public CheckState CheckedState return e.Cancel; } - // By default, Command.Accept calls OnAccept, so we need to call it here to ensure that the Accept event is fired. - if (OnAccept () == true) - { - return true; - } + SetFocus (); CheckedState = e.NewValue; diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index c887879b9b..21a39746bd 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -133,9 +133,9 @@ public ListView () AddCommand (Command.PageDown, () => MovePageDown ()); AddCommand (Command.Start, () => MoveHome ()); AddCommand (Command.End, () => MoveEnd ()); - AddCommand (Command.Accept, () => OnOpenSelectedItem ()); AddCommand (Command.Open, () => OnOpenSelectedItem ()); AddCommand (Command.Select, () => MarkUnmarkRow ()); + AddCommand (Command.Accept, () => OnOpenSelectedItem ()); AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1)); AddCommand (Command.ScrollRight, () => ScrollHorizontal (1)); diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 5655df26c9..fd6e334e03 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -82,10 +82,26 @@ public RadioGroup () { SelectedItem = _cursor; - return OnAccept () is true or null; + return OnAccept () is false; } ); + AddCommand ( + Command.Select, + () => + { + if (SelectedItem == _cursor) + { + if (!MoveDownRight ()) + { + MoveHome (); + } + } + + SelectedItem = _cursor; + + return true; + }); AddCommand ( Command.HotKey, ctx => @@ -96,7 +112,7 @@ public RadioGroup () { SelectedItem = (int)ctx.KeyBinding?.Context!; - return OnAccept () is true or null; + return OnSelect () is true or null; } return true; @@ -136,7 +152,8 @@ private void SetupKeyBindings () KeyBindings.Add (Key.Home, Command.Start); KeyBindings.Add (Key.End, Command.End); - KeyBindings.Add (Key.Space, Command.Accept); + KeyBindings.Add (Key.Enter, Command.Accept); + KeyBindings.Add (Key.Space, Command.Select); } private void RadioGroup_MouseClick (object sender, MouseEventEventArgs e) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 84390f1c40..d941bc9fe9 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -1,4 +1,6 @@ -namespace Terminal.Gui; +using System.ComponentModel; + +namespace Terminal.Gui; /// /// Displays a command, help text, and a key binding. When the key specified by is pressed, the @@ -98,8 +100,8 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e); _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e); - AddCommand (Command.HotKey, ctx => OnAccept (ctx)); - AddCommand (Command.Accept, ctx => OnAccept (ctx)); + AddCommand (Command.HotKey, ctx => DispatchAcceptCommand (ctx)); + AddCommand (Command.Accept, ctx => DispatchAcceptCommand (ctx)); AddCommand (Command.Select, ctx => OnSelect (ctx)); KeyBindings.Add (KeyCode.Enter, Command.Accept); KeyBindings.Add (KeyCode.Space, Command.Select); @@ -376,7 +378,9 @@ private void Shortcut_MouseClick (object sender, MouseEventEventArgs e) if (!e.Handled) { // If the subview (likely CommandView) didn't handle the mouse click, invoke the command. - e.Handled = InvokeCommand (Command.Accept) == true; + KeyBinding binding = new ([Command.Select], KeyBindingScope.Focused, _commandView, null); + e.Handled = DispatchAcceptCommand (new CommandContext (Command.Select, null, binding)) == true; + //e.Handled = InvokeCommand (Command.Accept) == true; } if (CanFocus) @@ -485,6 +489,7 @@ public View CommandView if (_commandView is { }) { + _commandView.Accept -= CommandViewOnAccept; Remove (_commandView); _commandView?.Dispose (); } @@ -509,6 +514,22 @@ public View CommandView Title = _commandView.Text; + _commandView.Accept += CommandViewOnAccept; + + void CommandViewOnAccept (object sender, HandledEventArgs e) + { + KeyBinding binding = new ([Command.Select], KeyBindingScope.Focused, _commandView, null); + e.Handled = DispatchAcceptCommand (new CommandContext (Command.Select, null, binding)) == true; + } + + _commandView.Select += CommandViewOnSelect; + + void CommandViewOnSelect (object sender, HandledEventArgs e) + { + SetFocus (); + //OnAccept (); + } + SetCommandViewDefaultLayout (); SetHelpViewDefaultLayout (); SetKeyViewDefaultLayout (); @@ -701,9 +722,12 @@ private void UpdateKeyBinding (Key oldKey) { if (Key != null && Key.IsValid) { - // Disable the command view key bindings + // Disable the command view HotKey bindings CommandView.KeyBindings.Remove (Key); CommandView.KeyBindings.Remove (CommandView.HotKey); + CommandView.KeyBindings.Remove (CommandView.HotKey.WithShift); + CommandView.KeyBindings.Remove (CommandView.HotKey.WithAlt); + CommandView.KeyBindings.Remove (CommandView.HotKey.WithShift.WithAlt); if (KeyBindingScope.FastHasFlags (KeyBindingScope.Application)) { @@ -740,60 +764,90 @@ private void UpdateKeyBinding (Key oldKey) /// - if the user presses the HotKey specified by CommandView /// - if HasFocus and the user presses Space or Enter (or any other key bound to Command.Accept). /// - protected bool? OnAccept (CommandContext ctx) + protected bool? DispatchAcceptCommand (CommandContext ctx) { var cancel = false; - switch (ctx.KeyBinding?.Scope) + if (ctx.Command == Command.HotKey) + { + var ret = CommandView.InvokeCommand (Command.Accept, ctx.Key, ctx.KeyBinding); + + return ret; + } + + if (ctx.Command == Command.Select) { - case KeyBindingScope.Application: - cancel = base.OnAccept () == true; + var ret = CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding); - break; + if (ret == false) + { + SetFocus (); - case KeyBindingScope.Focused: - base.OnAccept (); + return OnAccept (); + } - // cancel if we're focused - cancel = true; + return ret; + } - break; + //if (ctx.Command == Command.Accept) + { - case KeyBindingScope.HotKey: - //if (!CanBeVisible(this)) - //{ - // return true; - //} - cancel = base.OnAccept () == true; - if (CanFocus) - { - SetFocus (); + switch (ctx.KeyBinding?.Scope) + { + case KeyBindingScope.Application: + cancel = base.OnAccept () == true; + + break; + + case KeyBindingScope.Focused: + cancel = base.OnAccept () == true; + + // cancel if we're focused cancel = true; - } - break; + break; - default: - // Mouse - cancel = base.OnAccept () == true; + case KeyBindingScope.HotKey: + //if (!CanBeVisible(this)) + //{ + // return true; + //} + cancel = base.OnAccept () == true; - break; - } + if (CanFocus) + { + SetFocus (); + cancel = true; + } - CommandView.InvokeCommand (Command.Accept, ctx.Key, ctx.KeyBinding); + break; - if (Action is { }) - { - Action.Invoke (); + default: + // Mouse + //cancel = true; - // Assume if there's a subscriber to Action, it's handled. - cancel = true; - } + break; + } - if (_targetView is { }) - { - _targetView.InvokeCommand (_command); + + if (!cancel && ctx.KeyBinding?.BoundView != CommandView) + { + CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding); + } + + if (Action is { }) + { + Action.Invoke (); + + // Assume if there's a subscriber to Action, it's handled. + cancel = true; + } + + if (_targetView is { }) + { + _targetView.InvokeCommand (_command); + } } return cancel; diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index ef3969b171..4f56593d08 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1804,6 +1804,21 @@ private void SetSelectedStartSelectedLength () private void ShowContextMenu (bool keyboard) { + + if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture)) + { + _currentCulture = Thread.CurrentThread.CurrentUICulture; + + if (ContextMenu is { }) + { + Point currentLoc = ContextMenu.Frame.Location; + ContextMenu.Dispose (); + ContextMenu = CreateContextMenu (); + ContextMenu!.X = currentLoc.X; + ContextMenu!.Y = currentLoc.Y; + } + } + if (keyboard) { Point loc = ViewportToScreen (new Point (_cursorPosition - ScrollOffset, 1)); diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b0b7562d17..4ca738a29e 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -2460,9 +2460,9 @@ public TextView () KeyBindings.Add (Key.C.WithAlt, Command.Copy); KeyBindings.Add (Key.C.WithCtrl, Command.Copy); + KeyBindings.Add (Key.X.WithCtrl, Command.Cut); KeyBindings.Add (Key.W.WithAlt, Command.Cut); KeyBindings.Add (Key.W.WithCtrl, Command.Cut); - KeyBindings.Add (Key.X.WithCtrl, Command.Cut); KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.WordLeft); KeyBindings.Add (Key.B.WithAlt, Command.WordLeft); @@ -6244,7 +6244,8 @@ private void ShowContextMenu (bool keyboard) Point currentLoc = ContextMenu.Frame.Location; ContextMenu.Dispose (); ContextMenu = CreateContextMenu (); - ContextMenu.Frame = ContextMenu.Frame with { Location = currentLoc }; + ContextMenu.X = currentLoc.X; + ContextMenu.Y = currentLoc.Y; } } diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 21a9b7b9fa..a87ab9cb9a 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Globalization; using System.Threading; using JetBrains.Annotations; @@ -11,12 +12,14 @@ namespace UICatalog.Scenarios; public class ContextMenus : Scenario { [CanBeNull] - private ContextMenuv2 _contextMenu; + private ContextMenuv2 _winContextMenu; private bool _forceMinimumPosToZero = true; private MenuItem _miForceMinimumPosToZero; private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight; private bool _useSubMenusSingleFrame; + private readonly List _cultureInfos = Application.SupportedCultures; + public override void Main () { // Init @@ -28,23 +31,23 @@ public override void Main () Title = GetQuitKeyAndName () }; - _contextMenu = new ContextMenuv2 () + _winContextMenu = new ContextMenuv2 () { }; - ConfigureMenu (_contextMenu); - _contextMenu.Key = Key.Space.WithCtrl; + ConfigureMenu (_winContextMenu); + _winContextMenu.Key = Key.Space.WithCtrl; var text = "Context Menu"; var width = 20; var label = new Label { - X = Pos.Center (), Y = 1, Text = $"Press '{_contextMenu.Key}' to open the Window context menu." + X = Pos.Center (), Y = 1, Text = $"Press '{_winContextMenu.Key}' to open the Window context menu." }; appWindow.Add (label); - label = new() + label = new () { X = Pos.Center (), Y = Pos.Bottom (label), @@ -52,29 +55,28 @@ public override void Main () }; appWindow.Add (label); - _tfTopLeft = new() { Id = "_tfTopLeft", Width = width, Text = text }; + _tfTopLeft = new () { Id = "_tfTopLeft", Width = width, Text = text }; appWindow.Add (_tfTopLeft); - _tfTopRight = new() { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text }; + _tfTopRight = new () { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text }; appWindow.Add (_tfTopRight); - _tfMiddle = new() { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; + _tfMiddle = new () { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text }; appWindow.Add (_tfMiddle); - _tfBottomLeft = new() { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomLeft = new () { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomLeft); - _tfBottomRight = new() { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; + _tfBottomRight = new () { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomRight); Point mousePos = default; appWindow.KeyDown += (s, e) => { - if (e.KeyCode == _contextMenu.Key) + if (e.KeyCode == _winContextMenu.Key) { - Application.Popover = _contextMenu; - _contextMenu.Visible = true; + ShowContextMenu (Application.GetLastMousePosition ()); e.Handled = true; } }; @@ -83,10 +85,7 @@ public override void Main () { if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) { - Application.Popover = _contextMenu; - - _contextMenu.SetPosition (e.MouseEvent.ScreenPosition); - _contextMenu.Visible = true; + ShowContextMenu (e.MouseEvent.ScreenPosition); e.Handled = true; } }; @@ -100,7 +99,7 @@ public override void Main () // Run - Start the application. Application.Run (appWindow); appWindow.Dispose (); - _contextMenu.Dispose (); + _winContextMenu.Dispose (); // Shutdown - Calling Application.Shutdown is required. Application.Shutdown (); @@ -152,60 +151,78 @@ private void ConfigureMenu (Bar bar) }; bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); } + private Shortcut [] GetSupportedCultures () + { + List supportedCultures = new (); + int index = -1; + foreach (CultureInfo c in _cultureInfos) + { + Shortcut culture = new (); - //private MenuItem [] GetSupportedCultures () - //{ - // List supportedCultures = new (); - // int index = -1; - - // foreach (CultureInfo c in _cultureInfos) - // { - // var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked }; - - // if (index == -1) - // { - // culture.Title = "_English"; - // culture.Help = "en-US"; - // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US"; - // CreateAction (supportedCultures, culture); - // supportedCultures.Add (culture); - // index++; - // culture = new() { CheckType = MenuItemCheckStyle.Checked }; - // } - - // culture.Title = $"_{c.Parent.EnglishName}"; - // culture.Help = c.Name; - // culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name; - // CreateAction (supportedCultures, culture); - // supportedCultures.Add (culture); - // } - - // return supportedCultures.ToArray (); - - // void CreateAction (List supportedCultures, MenuItem culture) - // { - // culture.Action += () => - // { - // Thread.CurrentThread.CurrentUICulture = new (culture.Help); - // culture.Checked = true; - - // foreach (MenuItem item in supportedCultures) - // { - // item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name; - // } - // }; - // } - //} + culture.CommandView = new CheckBox () { HighlightStyle = HighlightStyle.None }; + + if (index == -1) + { + culture.Title = "_English"; + culture.HelpText = "en-US"; + ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? CheckState.Checked : CheckState.UnChecked; + + CreateAction (supportedCultures, culture); + supportedCultures.Add (culture); + index++; + culture = new (); + culture.CommandView = new CheckBox () { HighlightStyle = HighlightStyle.None}; + } + + culture.Title = $"_{c.Parent.EnglishName}"; + culture.HelpText = c.Name; + ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked; + CreateAction (supportedCultures, culture); + supportedCultures.Add (culture); + } + + return supportedCultures.ToArray (); + + void CreateAction (List cultures, Shortcut culture) + { + culture.Action += () => + { + Thread.CurrentThread.CurrentUICulture = new (culture.HelpText); + ((CheckBox)culture.CommandView).CheckedState = CheckState.Checked; + + foreach (Shortcut item in cultures) + { + ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked; + } + }; + } + } + + private void ShowContextMenu (Point screenPosition) + { + if (_winContextMenu is { }) + { + if (Application.Popover == _winContextMenu) + { + Application.Popover = null; + } - //private void ShowContextMenu (int x, int y) - //{ - // _contextMenu = new() - // { - // Position = new (x, y), - // ForceMinimumPosToZero = _forceMinimumPosToZero, - // UseSubMenusSingleFrame = _useSubMenusSingleFrame - // }; + _winContextMenu.Dispose (); + _winContextMenu = null; + } + + _winContextMenu = new (GetSupportedCultures()) + { + //Position = new (x, y), + //ForceMinimumPosToZero = _forceMinimumPosToZero, + //UseSubMenusSingleFrame = _useSubMenusSingleFrame + }; + + _winContextMenu.SetPosition(screenPosition); + Application.Popover = _winContextMenu; + _winContextMenu.Visible = true; + } // MenuBarItem menuItems = new ( // new [] diff --git a/UICatalog/Scenarios/FileDialogExamples.cs b/UICatalog/Scenarios/FileDialogExamples.cs index 094f210e73..314d917e42 100644 --- a/UICatalog/Scenarios/FileDialogExamples.cs +++ b/UICatalog/Scenarios/FileDialogExamples.cs @@ -122,7 +122,7 @@ public override void Main () _cbFlipButtonOrder = new CheckBox { X = x, Y = y++, Text = "Flip Order" }; win.Add (_cbFlipButtonOrder); - var btn = new Button { X = 1, Y = 9, Text = "Run Dialog" }; + var btn = new Button { X = 1, Y = 9, Text = "_Run Dialog" , IsDefault = true}; SetupHandler (btn); win.Add (btn); diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index 9e8e2a78f6..f39627a687 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/UICatalog/Scenarios/Shortcuts.cs @@ -72,7 +72,8 @@ private void App_Loaded (object sender, EventArgs e) CommandView = new RadioGroup { Orientation = Orientation.Vertical, - RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"] + RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"], + CanFocus = false }, }; @@ -101,7 +102,7 @@ private void App_Loaded (object sender, EventArgs e) Orientation = Orientation.Vertical, X = 0, Y = Pos.Bottom (vShortcut2), - CommandView = new CheckBox { Text = "_Align" }, + CommandView = new CheckBox { Text = "_Align", CanFocus = false, HighlightStyle = HighlightStyle.None}, Key = Key.F5.WithCtrl.WithAlt.WithShift, HelpText = "Width is Fill", Width = Dim.Fill () - Dim.Width (eventLog), @@ -144,13 +145,15 @@ private void App_Loaded (object sender, EventArgs e) CommandView = new Button { Title = "_Button", + ShadowStyle = ShadowStyle.None, + HighlightStyle = HighlightStyle.None }, HelpText = "Width is Fill", Key = Key.K, KeyBindingScope = KeyBindingScope.HotKey, }; Button button = (Button)vShortcut4.CommandView; - vShortcut4.CommandView.Accept += Button_Clicked; + vShortcut4.Accept += Button_Clicked; Application.Top.Add (vShortcut4); @@ -174,13 +177,13 @@ private void App_Loaded (object sender, EventArgs e) eventSource.Add ($"Toggle: {cb.Text}"); eventLog.MoveDown (); - foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!) - { - if (peer.CanFocus) - { - peer.CommandView.CanFocus = e.NewValue == CheckState.Checked; - } - } + //foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!) + //{ + // if (peer.CanFocus) + // { + // peer.CommandView.CanFocus = e.NewValue == CheckState.Checked; + // } + //} } }; Application.Top.Add (vShortcut5); @@ -360,7 +363,7 @@ private void App_Loaded (object sender, EventArgs e) { shortcut.Accept += (o, args) => { - eventSource.Add ($"Accept: {shortcut!.CommandView.Text}"); + eventSource.Add ($"Accept: {shortcut!.CommandView.Text} ({shortcut!.Id})"); eventLog.MoveDown (); args.Handled = true; }; @@ -378,7 +381,8 @@ private void App_Loaded (object sender, EventArgs e) private void Button_Clicked (object sender, HandledEventArgs e) { - //e.Cancel = true; - MessageBox.Query ("Hi", $"You clicked {sender}"); + e.Handled = true; + View view = sender as View; + MessageBox.Query ("Hi", $"You clicked {view!.Text}", "_Ok"); } } diff --git a/UnitTests/Views/TextViewTests.cs b/UnitTests/Views/TextViewTests.cs index 2ab55e7aea..91a43cb8f2 100644 --- a/UnitTests/Views/TextViewTests.cs +++ b/UnitTests/Views/TextViewTests.cs @@ -5437,7 +5437,7 @@ public void KeyBindings_Command () Assert.False (tv.NewKeyDownEvent (Application.PrevTabGroupKey)); Assert.True (tv.NewKeyDownEvent (ContextMenu.DefaultKey)); - Assert.True (tv.ContextMenu != null && tv.ContextMenu.MenuBar.Visible); + Assert.True (tv.ContextMenu != null && tv.ContextMenu.Visible); top.Dispose (); } diff --git a/docfx/docs/navigation.md b/docfx/docs/navigation.md index c68b56d619..b8f76e5310 100644 --- a/docfx/docs/navigation.md +++ b/docfx/docs/navigation.md @@ -6,6 +6,7 @@ - What are the visual cues that help the user know what keystrokes will change the focus? - What are the visual cues that help the user know what keystrokes will cause action in elements of the application that don't currently have focus? - What is the order in which UI elements are traversed when using keyboard navigation? +- What are the default actions for standard key/mouse input (e.g. Hotkey, `Space`, `Enter`, `MouseClick`)? ## Lexicon & Taxonomy @@ -208,7 +209,18 @@ These could also be named `Gain/Lose`. They could also be combined into a single QUESTION: Should we retain the same names as in v1 to simplify porting? Or, given the semantics of `Handled` v. `Cancel` are reversed would it be better to rename and/or combine? -## `TabIndex` and `TabIndexes` +## Built-In Views Interactivity + +| | | | | **Keyboard** | | | | **Mouse** | | | | | +|----------------|-------------------------|------------|---------------|--------------|-----------------------|------------------------------|---------------------------|------------------------------|------------------------------|------------------------------|----------------|---------------| +| | **Number
of States** | **Static** | **IsDefault** | **Hotkeys** | **Select
Command** | **Accept
Command** | **Hotkey
Command** | **CanFocus
Click** | **CanFocus
DblCLick** | **!CanFocus
Click** | **RightClick** | **GrabMouse** | +| **View** | 1 | Yes | No | 1 | OnSelect | OnAccept | Focus | Focus | | | | No | +| **Label** | 1 | Yes | No | 1 | OnSelect | OnAccept | FocusNext | Focus | | FocusNext | | No | +| **Button** | 1 | No | Yes | 1 | OnSelect | Focus
OnAccept | Focus
OnAccept | HotKey | | Select | | No | +| **Checkbox** | 3 | No | No | 1 | OnSelect
Advance | OnAccept | OnAccept | Select | | Select | | No | +| **RadioGroup** | > 1 | No | No | 2+ | Advance | Set SelectedItem
OnAccept | Focus
Set SelectedItem | SetFocus
Set _cursor | | SetFocus
Set _cursor | | No | +| **Slider** | > 1 | No | No | 1 | SetFocusedOption | SetFocusedOption
OnAccept | Focus | SetFocus
SetFocusedOption | | SetFocus
SetFocusedOption | | Yes | +| **ListView** | > 1 | No | No | 1 | MarkUnMarkRow | OpenSelectedItem
OnAccept | OnAccept | SetMark
OnSelectedChanged | OpenSelectedItem
OnAccept | | | No | ### v1 Behavior From bdc947ecace4f244d134ca9240f32d14182447cd Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 28 Sep 2024 16:27:21 -0700 Subject: [PATCH 26/75] Starting on OnSelect etc... --- Terminal.Gui/Views/RadioGroup.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index fd6e334e03..125941d4e4 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -75,17 +75,6 @@ public RadioGroup () return true; } ); - - AddCommand ( - Command.Accept, - () => - { - SelectedItem = _cursor; - - return OnAccept () is false; - } - ); - AddCommand ( Command.Select, () => @@ -102,6 +91,15 @@ public RadioGroup () return true; }); + AddCommand ( + Command.Accept, + () => + { + SelectedItem = _cursor; + + return OnAccept () is false; + } + ); AddCommand ( Command.HotKey, ctx => From a68dec296040d14cbaf69bb66b5358d4e7df5711 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 7 Oct 2024 10:45:24 -0400 Subject: [PATCH 27/75] Fixed ContextMenuv2 --- Terminal.Gui/Views/Menu/Menuv2.cs | 6 +++--- UICatalog/Scenarios/Bars.cs | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index e8d371e22d..eed92af196 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -76,14 +76,14 @@ public override View Add (View view) // TODO: instead, add a property (a style enum?) to Shortcut to control this //shortcut.AlignmentModes = AlignmentModes.EndToStart; - shortcut.Accepting += ShortcutOnAccept; + shortcut.Accepting += ShortcutOnAccepting; - void ShortcutOnAccept (object sender, CommandEventArgs e) + void ShortcutOnAccepting (object sender, CommandEventArgs e) { if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible) { Visible = false; - e.Cancel = true; +// e.Cancel = true; return; } diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index bf455f0da7..4af84828bf 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -172,13 +172,23 @@ private void App_Loaded (object sender, EventArgs e) void PopoverMenuOnAccepting (object o, CommandEventArgs args) { - if (_popoverMenu.Visible) + eventSource.Add ($"Accepting: {_popoverMenu!.Id}"); + eventLog.MoveDown (); + var cbShortcuts = _popoverMenu.Subviews.Where ( + v => + { + if (v is Shortcut sh) + { + return sh.CommandView is CheckBox; + } + + return false; + }).Cast (); + + foreach (Shortcut sh in cbShortcuts) { - _popoverMenu.Visible = false; - } - else - { - _popoverMenu.Visible = true; + eventSource.Add ($" {sh.Id} - {((CheckBox)sh.CommandView).CheckedState}"); + eventLog.MoveDown (); } } @@ -186,9 +196,8 @@ void PopoverMenuOnAccepting (object o, CommandEventArgs args) { sh.Accepting += (o, args) => { - eventSource.Add ($"Accepting: {sh!.SuperView.Id} {sh!.CommandView.Text}"); + eventSource.Add ($"shortcut.Accepting: {sh!.SuperView.Id} {sh!.CommandView.Text}"); eventLog.MoveDown (); - //args.Handled = true; }; } @@ -261,7 +270,6 @@ void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e) { eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}"); eventLog.MoveDown (); - //args.Handled = true; }; } } From 834634880b06ad0c85347065caf50805841c2d07 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 7 Oct 2024 22:04:54 -0400 Subject: [PATCH 28/75] ContextMenu is working again. --- .../Application/Application.Keyboard.cs | 7 +- .../Application/Application.Popover.cs | 2 +- Terminal.Gui/Input/KeyBindings.cs | 110 ++++++++++++++---- Terminal.Gui/View/View.Keyboard.cs | 3 +- Terminal.Gui/View/View.Mouse.cs | 2 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 40 ++++++- Terminal.Gui/Views/Shortcut.cs | 6 +- Terminal.Gui/Views/TextField.cs | 49 +++++--- Terminal.Gui/Views/TextView.cs | 15 +-- UICatalog/Scenarios/ContextMenus.cs | 90 ++++---------- UICatalog/Scenarios/KeyBindings.cs | 6 +- UICatalog/UICatalog.cs | 1 - UnitTests/Input/KeyBindingTests.cs | 58 +++++---- UnitTests/Views/ContextMenuTests.cs | 4 +- UnitTests/Views/ViewDisposalTest.cs | 5 +- 15 files changed, 242 insertions(+), 156 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 885f000b3a..bdee8b18ae 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -131,6 +131,11 @@ public static bool OnKeyDown (Key keyEvent) { if (binding.Value.BoundView is { }) { + if (!binding.Value.BoundView.Enabled) + { + return false; + } + bool? handled = binding.Value.BoundView?.InvokeCommands (binding.Value.Commands, binding.Key, binding.Value); if (handled != null && (bool)handled) @@ -140,7 +145,7 @@ public static bool OnKeyDown (Key keyEvent) } else { - if (!KeyBindings.TryGet (keyEvent, KeyBindingScope.Application, out KeyBinding appBinding)) + if (!KeyBindings.TryGet (keyEvent, KeyBindingScope.Application, null, out KeyBinding appBinding)) { continue; } diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index a965dcd72c..abca11fd35 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -83,7 +83,7 @@ out StatusBar? sb Top.HasFocus = false; } - Popover.SetFocus (); + Popover?.SetFocus (); } } } diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index 04b7b9f3cc..86703b919e 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -1,5 +1,7 @@ #nullable enable +using static System.Formats.Asn1.AsnWriter; + namespace Terminal.Gui; /// @@ -14,7 +16,7 @@ public class KeyBindings public KeyBindings () { } /// Initializes a new instance bound to . - public KeyBindings (View boundView) { BoundView = boundView; } + public KeyBindings (View? boundView) { BoundView = boundView; } /// Adds a to the collection. /// @@ -24,24 +26,22 @@ public void Add (Key key, KeyBinding binding, View? boundViewForAppScope = null) { if (BoundView is { } && binding.Scope.FastHasFlags (KeyBindingScope.Application)) { - throw new ArgumentException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add"); + throw new InvalidOperationException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add"); } - if (TryGet (key, out KeyBinding _)) + if (BoundView is { } && boundViewForAppScope is null) + { + boundViewForAppScope = BoundView; + } + + if (TryGet (key, binding.Scope, boundViewForAppScope, out KeyBinding _)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); //Bindings [key] = binding; } - if (BoundView is { }) - { - binding.BoundView = BoundView; - } - else - { - binding.BoundView = boundViewForAppScope; - } + binding.BoundView = boundViewForAppScope; Bindings.Add (key, binding); } @@ -71,6 +71,10 @@ public void Add (Key key, KeyBindingScope scope, View? boundViewForAppScope = nu { throw new ArgumentException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add"); } + else + { + // boundViewForAppScope = BoundView; + } if (key is null || !key.IsValid) { @@ -83,14 +87,12 @@ public void Add (Key key, KeyBindingScope scope, View? boundViewForAppScope = nu throw new ArgumentException (@"At least one command must be specified", nameof (commands)); } - if (TryGet (key, out KeyBinding binding)) + if (TryGet (key, scope, boundViewForAppScope, out KeyBinding binding)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); - - //Bindings [key] = new (commands, scope, BoundView); } - Add (key, new KeyBinding (commands, scope, BoundView), boundViewForAppScope); + Add (key, new KeyBinding (commands, scope, boundViewForAppScope), boundViewForAppScope); } /// @@ -113,9 +115,14 @@ public void Add (Key key, KeyBindingScope scope, View? boundViewForAppScope = nu /// public void Add (Key key, KeyBindingScope scope, params Command [] commands) { + if (BoundView is null && !scope.FastHasFlags (KeyBindingScope.Application)) + { + throw new InvalidOperationException ("BoundView cannot be null."); + } + if (BoundView is { } && scope.FastHasFlags (KeyBindingScope.Application)) { - throw new ArgumentException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add"); + throw new InvalidOperationException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add"); } if (key == Key.Empty || !key.IsValid) @@ -128,12 +135,13 @@ public void Add (Key key, KeyBindingScope scope, params Command [] commands) throw new ArgumentException (@"At least one command must be specified", nameof (commands)); } - if (TryGet (key, out KeyBinding binding)) + // if BoundView is null, the right thing will happen + if (TryGet (key, scope, BoundView, out KeyBinding binding)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); } - Add (key, new KeyBinding (commands, scope, BoundView)); + Add (key, new KeyBinding (commands, scope, BoundView), BoundView); } /// @@ -212,14 +220,22 @@ public void Add (Key key, params Command [] commands) /// The collection of objects. public Dictionary Bindings { get; } = new (); + /// + /// Gets the keys that are bound. + /// + /// + public IEnumerable GetBoundKeys () + { + return Bindings.Keys; + } + /// /// The view that the are bound to. /// /// - /// If , the are not bound to a . This is used for - /// Application.KeyBindings. + /// If the KeyBindings object is being used for Application.KeyBindings. /// - public View? BoundView { get; } + internal View? BoundView { get; } /// Removes all objects from the collection. public void Clear () { Bindings.Clear (); } @@ -305,12 +321,25 @@ public IEnumerable GetKeysFromCommands (params Command [] commands) /// Optional View for bindings. public void Remove (Key key, View? boundViewForAppScope = null) { - if (!TryGet (key, out KeyBinding binding)) + KeyBindingScope scope = KeyBindingScope.Disabled; + if (boundViewForAppScope is null) + { + boundViewForAppScope = BoundView; + scope = KeyBindingScope.HotKey | KeyBindingScope.Focused; + } + else + { + scope = KeyBindingScope.Application; + } + if (!TryGet (key, scope, boundViewForAppScope, out KeyBinding binding)) { return; } - Bindings.Remove (key); + if (boundViewForAppScope is { } && binding.BoundView == boundViewForAppScope) + { + Bindings.Remove (key); + } } /// Replaces the commands already bound to a key. @@ -364,6 +393,11 @@ public void ReplaceKey (Key oldKey, Key newKey) /// if the Key is bound; otherwise . public bool TryGet (Key key, out KeyBinding binding) { + //if (BoundView is null) + //{ + // throw new InvalidOperationException ("KeyBindings must be bound to a View to use this method."); + //} + binding = new (Array.Empty (), KeyBindingScope.Disabled, null); if (key.IsValid) @@ -385,6 +419,11 @@ public bool TryGet (Key key, out KeyBinding binding) /// if the Key is bound; otherwise . public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding) { + //if (BoundView is null) + //{ + // throw new InvalidOperationException ("KeyBindings must be bound to a View to use this method."); + //} + binding = new (Array.Empty (), KeyBindingScope.Disabled, null); if (key.IsValid && Bindings.TryGetValue (key, out binding)) @@ -397,4 +436,29 @@ public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding) return false; } + + /// Gets the commands bound with the specified Key that are scoped to a particular scope and bound View. + /// + /// The key to check. + /// the scope to filter on + /// The view the binding is bound to + /// + /// When this method returns, contains the commands bound with the specified Key, if the Key is + /// found; otherwise, null. This parameter is passed uninitialized. + /// + /// if the Key is bound; otherwise . + public bool TryGet (Key key, KeyBindingScope scope, View? boundView, out KeyBinding binding) + { + binding = new (Array.Empty (), KeyBindingScope.Disabled, null); + + if (key.IsValid && Bindings.TryGetValue (key, out binding)) + { + if (binding.BoundView == boundView && scope.HasFlag (binding.Scope)) + { + return true; + } + } + + return false; + } } diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index fad1cdbb06..15611ebdc0 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -703,8 +703,7 @@ public bool IsHotKeyKeyBound (Key key, out View? boundView) #if DEBUG - // TODO: Determine if App scope bindings should be fired first or last (currently last). - if (Application.KeyBindings.TryGet (key, KeyBindingScope.Focused | KeyBindingScope.HotKey, out KeyBinding b)) + if (Application.KeyBindings.TryGet (key, KeyBindingScope.Focused | KeyBindingScope.HotKey, this, out KeyBinding b)) { //var boundView = views [0]; //var commandBinding = boundView.KeyBindings.Get (key); diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 8be3b5a7d2..53619771bb 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -351,7 +351,7 @@ protected bool OnMouseClick (MouseEventEventArgs args) // BUGBUG: This should be named NewMouseClickEvent. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029 // Pre-conditions - if (!Enabled || !CanFocus) + if (!Enabled) { // QUESTION: Is this right? Should a disabled view eat mouse clicks? return args.Handled = false; diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index e9289414b9..37e3b61b4c 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -41,11 +41,39 @@ public MouseFlags MouseFlags public ContextMenuv2 () : this ([]) { } /// - public ContextMenuv2 (IEnumerable shortcuts) : base(shortcuts) + public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) { Visible = false; VisibleChanged += OnVisibleChanged; Key = DefaultKey; + + KeyChanged += OnKeyChanged; + + AddCommand (Command.Context, + () => + { + if (!Enabled) + { + return false; + } + Application.Popover = this; + SetPosition (Application.GetLastMousePosition ()); + Visible = !Visible; + + return true; + }); + + //Application.KeyBindings.Remove (Key, this); + //Application.KeyBindings.Add (Key, this, Command.Context); + + return; + + void OnKeyChanged (object? sender, KeyChangedEventArgs e) + { + //Application.KeyBindings.Remove (e.OldKey, this); + //Application.KeyBindings.Remove (e.NewKey, this); + //Application.KeyBindings.Add (e.NewKey, this, Command.Context); + } } private void OnVisibleChanged (object? sender, EventArgs _) @@ -89,4 +117,14 @@ public void SetPosition (Point screenPosition) Y = screenPosition.Y - GetViewportOffsetFromFrame ().Y, }; } + + /// + protected override void Dispose (bool disposing) + { + if (disposing) + { + Application.KeyBindings.Remove (Key, this); + } + base.Dispose (disposing); + } } diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 0987111d3a..7d5f4c3c80 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -642,7 +642,7 @@ public KeyBindingScope KeyBindingScope if (_keyBindingScope == KeyBindingScope.Application) { - Application.KeyBindings.Remove (Key); + Application.KeyBindings.Remove (Key, this); } if (_keyBindingScope is KeyBindingScope.HotKey or KeyBindingScope.Focused) @@ -713,10 +713,10 @@ private void UpdateKeyBindings (Key oldKey) { if (oldKey != Key.Empty) { - Application.KeyBindings.Remove (oldKey); + Application.KeyBindings.Remove (oldKey, this); } - Application.KeyBindings.Remove (Key); + Application.KeyBindings.Remove (Key, this); Application.KeyBindings.Add (Key, this, Command.HotKey); } else diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 1ce09fcb0f..a4e27eb8f6 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -398,12 +398,11 @@ public TextField () KeyBindings.Add (Key.R.WithCtrl, Command.DeleteAll); KeyBindings.Add (Key.D.WithCtrl.WithShift, Command.DeleteAll); - _currentCulture = Thread.CurrentThread.CurrentUICulture; + KeyBindings.Remove (Key.Space); - ContextMenu = CreateContextMenu (); - KeyBindings.Add (ContextMenu!.Key, KeyBindingScope.HotKey, Command.Context); + _currentCulture = Thread.CurrentThread.CurrentUICulture; - KeyBindings.Remove (Key.Space); + CreateContextMenu (); } /// @@ -540,12 +539,12 @@ public string SelectedText if (!Secret && !_historyText.IsFromHistory) { _historyText.Add ( - new() { TextModel.ToRuneCellList (oldText) }, + new () { TextModel.ToRuneCellList (oldText) }, new (_cursorPosition, 0) ); _historyText.Add ( - new() { TextModel.ToRuneCells (_text) }, + new () { TextModel.ToRuneCells (_text) }, new (_cursorPosition, 0), HistoryText.LineStatus.Replaced ); @@ -642,7 +641,7 @@ public virtual void DeleteCharLeft (bool usePreTextChangedCursorPos) } _historyText.Add ( - new() { TextModel.ToRuneCells (_text) }, + new () { TextModel.ToRuneCells (_text) }, new (_cursorPosition, 0) ); @@ -696,7 +695,7 @@ public virtual void DeleteCharRight () } _historyText.Add ( - new() { TextModel.ToRuneCells (_text) }, + new () { TextModel.ToRuneCells (_text) }, new (_cursorPosition, 0) ); @@ -904,7 +903,7 @@ protected internal override bool OnMouseEvent (MouseEvent ev) ClearAllSelection (); PrepareSelection (0, _text.Count); } - else if (ev.Flags == ContextMenu!.MouseFlags) + else if (ev.Flags == ContextMenu?.MouseFlags) { PositionCursor (ev); @@ -1245,9 +1244,9 @@ private void Adjust () } } - - private ContextMenuv2 CreateContextMenu () + private void CreateContextMenu () { + DisposeContextMenu (); ContextMenuv2 menu = new (new List () { new (this, Command.SelectAll, Strings.ctxSelectAll), @@ -1259,12 +1258,17 @@ private ContextMenuv2 CreateContextMenu () new (this, Command.Redo, Strings.ctxRedo), }); + KeyBindings.Remove (menu.Key); + KeyBindings.Add (menu.Key, KeyBindingScope.HotKey, Command.Context); menu.KeyChanged += ContextMenu_KeyChanged; - return menu; + ContextMenu = menu; } - private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode); } + private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) + { + KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode); + } private List DeleteSelectedText () { @@ -1343,7 +1347,7 @@ private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItemE private void InsertText (Key a, bool usePreTextChangedCursorPos) { _historyText.Add ( - new() { TextModel.ToRuneCells (_text) }, + new () { TextModel.ToRuneCells (_text) }, new (_cursorPosition, 0) ); @@ -1794,8 +1798,8 @@ private void ShowContextMenu (bool keyboard) if (ContextMenu is { }) { Point currentLoc = ContextMenu.Frame.Location; - ContextMenu.Dispose (); - ContextMenu = CreateContextMenu (); + + CreateContextMenu (); ContextMenu!.X = currentLoc.X; ContextMenu!.Y = currentLoc.Y; } @@ -1838,15 +1842,24 @@ private void TextField_Initialized (object sender, EventArgs e) } } - /// - protected override void Dispose (bool disposing) + private void DisposeContextMenu () { if (ContextMenu is { }) { ContextMenu.Visible = false; + ContextMenu.KeyChanged -= ContextMenu_KeyChanged; ContextMenu.Dispose (); ContextMenu = null; } + } + + /// + protected override void Dispose (bool disposing) + { + if (disposing) + { + DisposeContextMenu (); + } base.Dispose (disposing); } } diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 780c305597..1f673557df 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -2504,7 +2504,7 @@ public TextView () ContextMenu = CreateContextMenu (); KeyBindings.Add (ContextMenu!.Key, KeyBindingScope.HotKey, Command.Context); - ContextMenu.KeyChanged += ContextMenu_KeyChanged!; + //ContextMenu.KeyChanged += ContextMenu_KeyChanged!; } private void TextView_Added1 (object? sender, SuperViewChangedEventArgs e) @@ -4137,7 +4137,7 @@ private void Adjust () private ContextMenuv2 CreateContextMenu () { ContextMenuv2 menu = new (new List () - { + { new (this, Command.SelectAll, Strings.ctxSelectAll), new (this, Command.DeleteAll, Strings.ctxDeleteAll), new (this, Command.Copy, Strings.ctxCopy), @@ -6248,15 +6248,6 @@ private void ShowContextMenu (bool keyboard) if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture)) { _currentCulture = Thread.CurrentThread.CurrentUICulture; - - if (ContextMenu is { }) - { - Point currentLoc = ContextMenu.Frame.Location; - ContextMenu.Dispose (); - ContextMenu = CreateContextMenu (); - ContextMenu.X = currentLoc.X; - ContextMenu.Y = currentLoc.Y; - } } if (keyboard) @@ -6437,7 +6428,7 @@ private void WrapTextModel () /// protected override void Dispose (bool disposing) { - if (ContextMenu is { }) + if (disposing && ContextMenu is { }) { ContextMenu.Visible = false; ContextMenu.Dispose (); diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 4bb37362f5..5e645d1982 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -20,6 +20,8 @@ public class ContextMenus : Scenario private readonly List _cultureInfos = Application.SupportedCultures; + private readonly Key _winContextMenuKey = Key.Space.WithCtrl; + public override void Main () { // Init @@ -31,19 +33,14 @@ public override void Main () Title = GetQuitKeyAndName () }; - _winContextMenu = new ContextMenuv2 () - { - }; - - ConfigureMenu (_winContextMenu); - _winContextMenu.Key = Key.Space.WithCtrl; - var text = "Context Menu"; var width = 20; + CreateWinContextMenu (); + var label = new Label { - X = Pos.Center (), Y = 1, Text = $"Press '{_winContextMenu.Key}' to open the Window context menu." + X = Pos.Center (), Y = 1, Text = $"Press '{_winContextMenuKey}' to open the Window context menu." }; appWindow.Add (label); @@ -74,9 +71,9 @@ public override void Main () appWindow.KeyDown += (s, e) => { - if (e.KeyCode == _winContextMenu.Key) + if (e.KeyCode == _winContextMenuKey) { - ShowContextMenu (Application.GetLastMousePosition ()); + ShowWinContextMenu (Application.GetLastMousePosition ()); e.Handled = true; } }; @@ -85,72 +82,25 @@ public override void Main () { if (e.MouseEvent.Flags == MouseFlags.Button3Clicked) { - ShowContextMenu (e.MouseEvent.ScreenPosition); + ShowWinContextMenu (e.MouseEvent.ScreenPosition); e.Handled = true; } }; + var originalCulture = Thread.CurrentThread.CurrentUICulture; appWindow.Closed += (s, e) => { - Thread.CurrentThread.CurrentUICulture = new ("en-US"); + Thread.CurrentThread.CurrentUICulture = originalCulture; }; - // Run - Start the application. Application.Run (appWindow); appWindow.Dispose (); - _winContextMenu.Dispose (); + _winContextMenu?.Dispose (); // Shutdown - Calling Application.Shutdown is required. Application.Shutdown (); } - - private void ConfigureMenu (Bar bar) - { - - var shortcut1 = new Shortcut - { - Title = "Z_igzag", - Key = Key.I.WithCtrl, - Text = "Gonna zig zag", - HighlightStyle = HighlightStyle.Hover - }; - - var shortcut2 = new Shortcut - { - Title = "Za_G", - Text = "Gonna zag", - Key = Key.G.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - - var shortcut3 = new Shortcut - { - Title = "_Three", - Text = "The 3rd item", - Key = Key.D3.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - - var line = new Line () - { - BorderStyle = LineStyle.Dotted, - Orientation = Orientation.Horizontal, - CanFocus = false, - }; - // HACK: Bug in Line - line.Orientation = Orientation.Vertical; - line.Orientation = Orientation.Horizontal; - - var shortcut4 = new Shortcut - { - Title = "_Four", - Text = "Below the line", - Key = Key.D3.WithAlt, - HighlightStyle = HighlightStyle.Hover - }; - bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); - } private Shortcut [] GetSupportedCultures () { List supportedCultures = new (); @@ -164,6 +114,7 @@ private Shortcut [] GetSupportedCultures () if (index == -1) { + culture.Id = "_English"; culture.Title = "_English"; culture.HelpText = "en-US"; ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? CheckState.Checked : CheckState.UnChecked; @@ -175,6 +126,7 @@ private Shortcut [] GetSupportedCultures () culture.CommandView = new CheckBox () { CanFocus = false, HighlightStyle = HighlightStyle.None}; } + culture.Id= $"_{c.Parent.EnglishName}"; culture.Title = $"_{c.Parent.EnglishName}"; culture.HelpText = c.Name; ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked; @@ -189,17 +141,16 @@ void CreateAction (List cultures, Shortcut culture) culture.Action += () => { Thread.CurrentThread.CurrentUICulture = new (culture.HelpText); - ((CheckBox)culture.CommandView).CheckedState = CheckState.Checked; - + foreach (Shortcut item in cultures) { - ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked; + ((CheckBox)item.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == item.HelpText ? CheckState.Checked : CheckState.UnChecked; } }; } } - private void ShowContextMenu (Point screenPosition) + private void CreateWinContextMenu () { if (_winContextMenu is { }) { @@ -212,13 +163,20 @@ private void ShowContextMenu (Point screenPosition) _winContextMenu = null; } - _winContextMenu = new (GetSupportedCultures()) + _winContextMenu = new (GetSupportedCultures ()) { + Key = _winContextMenuKey, + //Position = new (x, y), //ForceMinimumPosToZero = _forceMinimumPosToZero, //UseSubMenusSingleFrame = _useSubMenusSingleFrame }; + //_winContextMenu.KeyBindings.Add (_winContextMenuKey, Command.Context); + } + + private void ShowWinContextMenu (Point screenPosition) + { _winContextMenu.SetPosition(screenPosition); Application.Popover = _winContextMenu; _winContextMenu.Visible = true; diff --git a/UICatalog/Scenarios/KeyBindings.cs b/UICatalog/Scenarios/KeyBindings.cs index b51fb81fcd..42c68bbaa3 100644 --- a/UICatalog/Scenarios/KeyBindings.cs +++ b/UICatalog/Scenarios/KeyBindings.cs @@ -80,10 +80,10 @@ Pressing Esc or {Application.QuitKey} will cause it to quit the app. }; appWindow.Add (appBindingsListView); - foreach (var appBinding in Application.KeyBindings.Bindings) + foreach (var key in Application.KeyBindings.GetBoundKeys()) { - var commands = Application.KeyBindings.GetCommands (appBinding.Key); - appBindings.Add ($"{appBinding.Key} -> {appBinding.Value.BoundView?.GetType ().Name} - {commands [0]}"); + var binding = Application.KeyBindings.Get (key); + appBindings.Add ($"{key} -> {binding.BoundView?.GetType ().Name} - {binding.Commands [0]}"); } ObservableCollection hotkeyBindings = new (); diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 0ca6560557..310dd62eac 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -389,7 +389,6 @@ private static void VerifyObjectsWereDisposed () // 'app' closed cleanly. foreach (Responder? inst in Responder.Instances) { - Debug.Assert (inst.WasDisposed); } diff --git a/UnitTests/Input/KeyBindingTests.cs b/UnitTests/Input/KeyBindingTests.cs index e5628da5ad..be328e8bba 100644 --- a/UnitTests/Input/KeyBindingTests.cs +++ b/UnitTests/Input/KeyBindingTests.cs @@ -1,4 +1,5 @@ -using Xunit.Abstractions; +using Terminal.Gui.EnumExtensions; +using Xunit.Abstractions; namespace Terminal.Gui.InputTests; @@ -10,11 +11,19 @@ public class KeyBindingTests [Fact] public void Add_Invalid_Key_Throws () { - var keyBindings = new KeyBindings (); + var keyBindings = new KeyBindings (new View ()); List commands = new (); Assert.Throws (() => keyBindings.Add (Key.Empty, KeyBindingScope.HotKey, Command.Accept)); } + [Fact] + public void Add_BoundView_Null_Non_AppScope_Throws () + { + var keyBindings = new KeyBindings (); + List commands = new (); + Assert.Throws (() => keyBindings.Add (Key.Empty, KeyBindingScope.HotKey, Command.Accept)); + } + [Fact] public void Add_Multiple_Adds () { @@ -53,32 +62,41 @@ public void Add_Single_Adds () Assert.Contains (Command.HotKey, resultCommands); } + // Add should not allow duplicates [Fact] - public void Add_Throws_If_Exists () + public void Add_With_Bound_View_Throws_If_App_Scope () { - var keyBindings = new KeyBindings (); - keyBindings.Add (Key.A, KeyBindingScope.Application, Command.HotKey); + var keyBindings = new KeyBindings (new View ()); Assert.Throws (() => keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept)); + } + + // Add should not allow duplicates + [Fact] + public void Add_With_Bound_View_Throws_If_Exists () + { + var keyBindings = new KeyBindings (new View ()); + keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey); + Assert.Throws (() => keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.Accept)); Command [] resultCommands = keyBindings.GetCommands (Key.A); Assert.Contains (Command.HotKey, resultCommands); - keyBindings = new (); + keyBindings = new (new View ()); keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey); Assert.Throws (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept)); resultCommands = keyBindings.GetCommands (Key.A); Assert.Contains (Command.HotKey, resultCommands); - keyBindings = new (); + keyBindings = new (new View ()); keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey); - Assert.Throws (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept)); + Assert.Throws (() => keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.Accept)); resultCommands = keyBindings.GetCommands (Key.A); Assert.Contains (Command.HotKey, resultCommands); - keyBindings = new (); + keyBindings = new (new View ()); keyBindings.Add (Key.A, new KeyBinding (new [] { Command.HotKey }, KeyBindingScope.HotKey)); Assert.Throws (() => keyBindings.Add (Key.A, new KeyBinding (new [] { Command.Accept }, KeyBindingScope.HotKey))); @@ -207,11 +225,11 @@ public void GetKeyFromCommands_WithCommands_ReturnsKey () [Fact] public void ReplaceKey_Replaces () { - var keyBindings = new KeyBindings (); - keyBindings.Add (Key.A, KeyBindingScope.Application, Command.HotKey); - keyBindings.Add (Key.B, KeyBindingScope.Application, Command.HotKey); - keyBindings.Add (Key.C, KeyBindingScope.Application, Command.HotKey); - keyBindings.Add (Key.D, KeyBindingScope.Application, Command.HotKey); + var keyBindings = new KeyBindings (new ()); + keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey); + keyBindings.Add (Key.B, KeyBindingScope.Focused, Command.HotKey); + keyBindings.Add (Key.C, KeyBindingScope.Focused, Command.HotKey); + keyBindings.Add (Key.D, KeyBindingScope.Focused, Command.HotKey); keyBindings.ReplaceKey (Key.A, Key.E); Assert.Empty (keyBindings.GetCommands (Key.A)); @@ -233,9 +251,9 @@ public void ReplaceKey_Replaces () [Fact] public void ReplaceKey_Replaces_Leaves_Old_Binding () { - var keyBindings = new KeyBindings (); - keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept); - keyBindings.Add (Key.B, KeyBindingScope.Application, Command.HotKey); + var keyBindings = new KeyBindings (new ()); + keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept); + keyBindings.Add (Key.B, KeyBindingScope.Focused, Command.HotKey); keyBindings.ReplaceKey (keyBindings.GetKeyFromCommands (Command.Accept), Key.C); Assert.Empty (keyBindings.GetCommands (Key.A)); @@ -264,7 +282,7 @@ public void ReplaceKey_Throws_If_New_Is_Empty () [InlineData (KeyBindingScope.Application)] public void Scope_Add_Adds (KeyBindingScope scope) { - var keyBindings = new KeyBindings (); + var keyBindings = new KeyBindings (scope.FastHasFlags(KeyBindingScope.Application) ? null : new ()); Command [] commands = { Command.Right, Command.Left }; var key = new Key (Key.A); @@ -288,7 +306,7 @@ public void Scope_Add_Adds (KeyBindingScope scope) [InlineData (KeyBindingScope.Application)] public void Scope_Get_Filters (KeyBindingScope scope) { - var keyBindings = new KeyBindings (); + var keyBindings = new KeyBindings (scope.FastHasFlags (KeyBindingScope.Application) ? null : new ()); Command [] commands = { Command.Right, Command.Left }; var key = new Key (Key.A); @@ -308,7 +326,7 @@ public void Scope_Get_Filters (KeyBindingScope scope) [InlineData (KeyBindingScope.Application)] public void Scope_TryGet_Filters (KeyBindingScope scope) { - var keyBindings = new KeyBindings (); + var keyBindings = new KeyBindings (scope.FastHasFlags (KeyBindingScope.Application) ? null : new ()); Command [] commands = { Command.Right, Command.Left }; var key = new Key (Key.A); diff --git a/UnitTests/Views/ContextMenuTests.cs b/UnitTests/Views/ContextMenuTests.cs index 0ca5dd8cb4..6a9608513c 100644 --- a/UnitTests/Views/ContextMenuTests.cs +++ b/UnitTests/Views/ContextMenuTests.cs @@ -148,7 +148,7 @@ public void Draw_A_ContextMenu_Over_A_Borderless_Top () output ); - Application.OnMouseEvent (new () { ScreenScreenPosition = new (8, 2), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (8, 2), Flags = MouseFlags.Button3Clicked }); var firstIteration = false; Application.RunIteration (ref rs, ref firstIteration); @@ -231,7 +231,7 @@ public void Draw_A_ContextMenu_Over_A_Dialog () output ); - Application.OnMouseEvent (new () { ScreenScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked }); + Application.OnMouseEvent (new () { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked }); var firstIteration = false; Application.RunIteration (ref rsDialog, ref firstIteration); diff --git a/UnitTests/Views/ViewDisposalTest.cs b/UnitTests/Views/ViewDisposalTest.cs index 9938415a0f..7f6b913d88 100644 --- a/UnitTests/Views/ViewDisposalTest.cs +++ b/UnitTests/Views/ViewDisposalTest.cs @@ -32,8 +32,8 @@ public void TestViewsDisposeCorrectly () private WeakReference DoTest () { GetSpecialParams (); - var container = new View (); - Toplevel top = new (); + var container = new View () { Id = "container" }; + Toplevel top = new () { Id = "top" }; List views = GetViews (); foreach (Type view in views) @@ -51,6 +51,7 @@ private WeakReference DoTest () } Assert.NotNull (instance); + instance.Id = $"{view.Name}"; container.Add (instance); output.WriteLine ($"Added instance of {view}!"); } From 22dc0759fb62cfec42c8eb6e4fb3aa2b09a6e071 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 7 Oct 2024 22:44:31 -0400 Subject: [PATCH 29/75] Ugh. ANd fixed button api docs --- Terminal.Gui/Application/Application.Mouse.cs | 1 + Terminal.Gui/View/View.Command.cs | 19 ++++++ Terminal.Gui/Views/Button.cs | 62 +++++++++++-------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index b7846df935..c7a7bcfe47 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -238,6 +238,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) else { // The mouse was outside any View's Viewport. + Debug.Fail("this should not happen."); return; } diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 3facab4d0a..a75f368cd3 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -53,7 +53,16 @@ private void SetupCommands () /// event. The default handler calls this method. /// /// + /// /// The event should raised after the state of the View has changed (after is raised). + /// + /// + /// If the Accepting event is not handled, will be invoked on the SuperView, enabling default Accept behavior. + /// + /// + /// If a peer-View raises the Accepting event and the event is not cancelled, the will be invoked on the + /// first Button in the SuperView that has set to . + /// /// /// /// If the event was canceled. If the event was raised but not canceled. @@ -100,6 +109,11 @@ private void SetupCommands () /// Called when the user is accepting the state of the View and the has been invoked. Set to /// and return to stop processing. /// + /// + /// + /// See for more information. + /// + /// /// /// to stop processing. protected virtual bool OnAccepting (CommandEventArgs args) { return false; } @@ -108,6 +122,11 @@ private void SetupCommands () /// Cancelable event raised when the user is accepting the state of the View and the has been invoked. Set /// to cancel the event. /// + /// + /// + /// See for more information. + /// + /// public event EventHandler? Accepting; /// diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index b0ab5408f5..c10e295457 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -1,29 +1,18 @@ -// -// Button.cs: Button control -// -// Authors: -// Miguel de Icaza (miguel@gnome.org) -// - namespace Terminal.Gui; /// -/// A View that raises the event when clicked with the mouse or when the -/// , Enter, or Space key is pressed. +/// A button View that can be pressed with the mouse or keybaord. /// /// /// -/// Provides a button showing text that raises the event when clicked on with a mouse or -/// when the user presses Enter, Space or the . The hot key is the first -/// letter or digit -/// following the first underscore ('_') in the button text. +/// The Button will raise the event when the user presses , +/// Enter, or Space +/// or clicks on the button with the mouse. /// /// Use to change the hot key specifier from the default of ('_'). /// -/// When the button is configured as the default () and the user causes the button to be -/// accepted the 's event will be raised. If the Accept event is not -/// handled, the Accept event on the . will be raised. This enables default Accept -/// behavior. +/// Button can act as the default handler for all peer-Views. See +/// . /// /// /// Set to to have the @@ -142,7 +131,8 @@ private void Button_MouseClick (object sender, MouseEventEventArgs e) { return; } - e.Handled = InvokeCommand (Command.HotKey, ctx: new (Command.HotKey, key: null, data: this)) == true; + + e.Handled = InvokeCommand (Command.HotKey, new (Command.HotKey, null, data: this)) == true; } private void Button_TitleChanged (object sender, EventArgs e) @@ -166,14 +156,27 @@ public override Rune HotKeySpecifier } /// - /// Gets or sets whether the will show an indicator indicating it is the default Button. If - /// - /// will be invoked when the user presses Enter and no other peer- - /// processes the key. - /// If is not handled, the Gets or sets whether the will show an - /// indicator indicating it is the default Button. If - /// command on the will be invoked. + /// Gets or sets whether the will act as the default handler for + /// commands on the . /// + /// + /// + /// If : + /// + /// + /// - the Button will display an indicator that it is the default Button. + /// + /// + /// - when clicked, if the Accepting event is not handled, will be + /// invoked on the SuperView. + /// + /// + /// - If a peer-View receives and does not handle it, the command will be passed to + /// the + /// first Button in the SuperView that has set to . See + /// for more information. + /// + /// public bool IsDefault { get => _isDefault; @@ -191,10 +194,15 @@ public bool IsDefault } } - /// + /// + /// Gets or sets whether the Button will show decorations or not. If the glyphs that normally + /// brakcet the Button Title and the indicator will not be shown. + /// public bool NoDecorations { get; set; } - /// + /// + /// Gets or sets whether the Button will include padding on each side of the Title. + /// public bool NoPadding { get; set; } /// From ed0b1795958528385138977474626f2915fe7ad5 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 09:53:19 -0600 Subject: [PATCH 30/75] Fixed DrawHorizontalShadowTransparent (vertical was already fixed). --- Terminal.Gui/View/Adornment/ShadowView.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Terminal.Gui/View/Adornment/ShadowView.cs b/Terminal.Gui/View/Adornment/ShadowView.cs index 4e16471490..30a5ade0ea 100644 --- a/Terminal.Gui/View/Adornment/ShadowView.cs +++ b/Terminal.Gui/View/Adornment/ShadowView.cs @@ -109,7 +109,7 @@ private void DrawHorizontalShadowTransparent (Rectangle viewport) Rectangle screen = ViewportToScreen (viewport); // Fill the rest of the rectangle - note we skip the last since vertical will draw it - for (int i = screen.X + 1; i < screen.X + screen.Width - 1; i++) + for (int i = Math.Max (0, screen.X + 1); i < screen.X + screen.Width - 1; i++) { Driver.Move (i, screen.Y); From f5ab50fdf477fc48ac46fe6bf6087e0383513581 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 11:19:50 -0600 Subject: [PATCH 31/75] Made Scenarios compatible with #nullable enable --- Terminal.Gui/Application/Application.Mouse.cs | 5 +- Terminal.Gui/Views/Bar.cs | 5 - Terminal.Gui/Views/Menu/Menuv2.cs | 9 -- UICatalog/Scenario.cs | 48 ++++++-- UICatalog/Scenarios/Bars.cs | 107 ++++++++++-------- 5 files changed, 98 insertions(+), 76 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 459a68b8c5..534e3e9694 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -193,6 +193,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) { + Popover.Visible = false; // Recurse once @@ -238,9 +239,9 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) else { // The mouse was outside any View's Viewport. - Debug.Fail("this should not happen."); + Debug.Fail ("this should not happen."); - // Debug.Fail ("This should never happen. If it does please file an Issue!!"); + // Debug.Fail ("This should never happen. If it does please file an Issue!!"); return; } diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index ddaa444365..5b8dbc12c5 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -34,11 +34,6 @@ public Bar (IEnumerable shortcuts) Initialized += Bar_Initialized; MouseEvent += OnMouseEvent; - if (shortcuts is null) - { - return; - } - foreach (Shortcut shortcut in shortcuts) { Add (shortcut); diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index eed92af196..d348e9317d 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -72,10 +72,6 @@ public override View Add (View view) shortcut.Orientation = Orientation.Vertical; shortcut.HighlightStyle |= HighlightStyle.Hover; - // TODO: not happy about using AlignmentModes for this. Too implied. - // TODO: instead, add a property (a style enum?) to Shortcut to control this - //shortcut.AlignmentModes = AlignmentModes.EndToStart; - shortcut.Accepting += ShortcutOnAccepting; void ShortcutOnAccepting (object sender, CommandEventArgs e) @@ -87,11 +83,6 @@ void ShortcutOnAccepting (object sender, CommandEventArgs e) return; } - - //if (!e.Handled) - //{ - // RaiseAcceptEvent (); - //} } } diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index f0a03f5ce4..1f2098a964 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -1,7 +1,9 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Reflection.Metadata; using Terminal.Gui; namespace UICatalog; @@ -114,16 +116,19 @@ public class Scenario : IDisposable /// public static ObservableCollection GetScenarios () { - List objects = new (); + List objects = []; foreach (Type type in typeof (Scenario).Assembly.ExportedTypes .Where ( - myType => myType.IsClass - && !myType.IsAbstract + myType => myType is { IsClass: true, IsAbstract: false } && myType.IsSubclassOf (typeof (Scenario)) )) { - var scenario = (Scenario)Activator.CreateInstance (type); + if (Activator.CreateInstance (type) is not Scenario { } scenario) + { + continue; + } + objects.Add (scenario); _maxScenarioNameLen = Math.Max (_maxScenarioNameLen, scenario.GetName ().Length + 1); } @@ -170,8 +175,7 @@ internal static ObservableCollection GetAllCategories () aCategories = typeof (Scenario).Assembly.GetTypes () .Where ( - myType => myType.IsClass - && !myType.IsAbstract + myType => myType is { IsClass: true, IsAbstract: false } && myType.IsSubclassOf (typeof (Scenario))) .Select (type => System.Attribute.GetCustomAttributes (type).ToList ()) .Aggregate ( @@ -210,7 +214,15 @@ public static List GetCategories (Type t) /// Static helper function to get the Name given a Type /// /// Name of the category - public static string GetName (Type t) { return ((ScenarioCategory)GetCustomAttributes (t) [0]).Name; } + public static string GetName (Type t) + { + if (GetCustomAttributes (t).FirstOrDefault (a => a is ScenarioMetadata) is ScenarioMetadata { } metadata) + { + return metadata.Name; + } + + return string.Empty; + } /// Category Name public string Name { get; set; } = name; @@ -226,12 +238,28 @@ public class ScenarioMetadata (string name, string description) : System.Attribu /// Static helper function to get the Description given a Type /// /// - public static string GetDescription (Type t) { return ((ScenarioMetadata)GetCustomAttributes (t) [0]).Description; } + public static string GetDescription (Type t) + { + if (GetCustomAttributes (t).FirstOrDefault (a => a is ScenarioMetadata) is ScenarioMetadata { } metadata) + { + return metadata.Description; + } + + return string.Empty; + } /// Static helper function to get the Name given a Type /// /// - public static string GetName (Type t) { return ((ScenarioMetadata)GetCustomAttributes (t) [0]).Name; } + public static string GetName (Type t) + { + if (GetCustomAttributes (t).FirstOrDefault (a => a is ScenarioMetadata) is ScenarioMetadata { } metadata) + { + return metadata.Name; + } + + return string.Empty; + } /// Name public string Name { get; set; } = name; diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 4af84828bf..22c02b199d 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Collections.ObjectModel; using System.ComponentModel; @@ -9,9 +11,10 @@ namespace UICatalog.Scenarios; [ScenarioMetadata ("Bars", "Illustrates Bar views (e.g. StatusBar)")] [ScenarioCategory ("Controls")] +[ScenarioCategory ("Shortcuts")] public class Bars : Scenario { - private Menuv2 _popoverMenu; + private Menuv2? _popoverMenu; public override void Main () { @@ -34,7 +37,7 @@ public override void Main () // Setting everything up in Loaded handler because we change the // QuitKey and it only sticks if changed after init - private void App_Loaded (object sender, EventArgs e) + private void App_Loaded (object? sender, EventArgs e) { Application.Top!.Title = GetQuitKeyAndName (); @@ -56,8 +59,8 @@ private void App_Loaded (object sender, EventArgs e) Title = "MenuBar-Like Examples", X = 0, Y = 0, - Width = Dim.Fill () - Dim.Width (eventLog), - Height = Dim.Percent(33), + Width = Dim.Fill ()! - Dim.Width (eventLog), + Height = Dim.Percent (33), }; Application.Top.Add (menuBarLikeExamples); @@ -69,7 +72,7 @@ private void App_Loaded (object sender, EventArgs e) }; menuBarLikeExamples.Add (label); - Bar bar = new Bar + var bar = new Bar { Id = "menuBar-like", X = Pos.Right (label), @@ -103,7 +106,7 @@ private void App_Loaded (object sender, EventArgs e) Title = "Menu-Like Examples", X = 0, Y = Pos.Center (), - Width = Dim.Fill () - Dim.Width (eventLog), + Width = Dim.Fill ()! - Dim.Width (eventLog), Height = Dim.Percent (33), }; Application.Top.Add (menuLikeExamples); @@ -120,7 +123,7 @@ private void App_Loaded (object sender, EventArgs e) { Id = "menu-like", X = 0, - Y = Pos.Bottom(label), + Y = Pos.Bottom (label), //Width = Dim.Percent (40), Orientation = Orientation.Vertical, }; @@ -131,7 +134,7 @@ private void App_Loaded (object sender, EventArgs e) label = new Label () { Title = "Menu:", - X = Pos.Right(bar) + 1, + X = Pos.Right (bar) + 1, Y = Pos.Top (label), }; menuLikeExamples.Add (label); @@ -154,9 +157,9 @@ private void App_Loaded (object sender, EventArgs e) }; menuLikeExamples.Add (label); - ConfigureMenu (_popoverMenu); + ConfigureMenu (_popoverMenu!); - _popoverMenu.ColorScheme = Colors.ColorSchemes ["Menu"]; + _popoverMenu!.ColorScheme = Colors.ColorSchemes ["Menu"]; _popoverMenu.Visible = false; var toggleShortcut = new Shortcut @@ -170,7 +173,7 @@ private void App_Loaded (object sender, EventArgs e) _popoverMenu.Accepting += PopoverMenuOnAccepting; - void PopoverMenuOnAccepting (object o, CommandEventArgs args) + void PopoverMenuOnAccepting (object? o, CommandEventArgs args) { eventSource.Add ($"Accepting: {_popoverMenu!.Id}"); eventLog.MoveDown (); @@ -192,18 +195,20 @@ void PopoverMenuOnAccepting (object o, CommandEventArgs args) } } - foreach (Shortcut sh in _popoverMenu.Subviews.Where (s => s is Shortcut)!) + foreach (var view in _popoverMenu.Subviews.Where (s => s is Shortcut)!) { + var sh = (Shortcut)view; + sh.Accepting += (o, args) => - { - eventSource.Add ($"shortcut.Accepting: {sh!.SuperView.Id} {sh!.CommandView.Text}"); - eventLog.MoveDown (); - }; + { + eventSource.Add ($"shortcut.Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + }; } menuLikeExamples.MouseClick += MenuLikeExamplesMouseClick; - void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e) + void MenuLikeExamplesMouseClick (object? sender, MouseEventEventArgs e) { if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)) { @@ -260,17 +265,23 @@ void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e) ConfigStatusBar (bar); statusBarLikeExamples.Add (bar); - foreach (FrameView frameView in Application.Top.Subviews.Where (f => f is FrameView)!) + foreach (var view in Application.Top.Subviews.Where (f => f is FrameView)!) { - foreach (Bar barView in frameView.Subviews.Where (b => b is Bar)!) + var frameView = (FrameView)view; + + foreach (var view1 in frameView.Subviews.Where (b => b is Bar)!) { - foreach (Shortcut sh in barView.Subviews.Where (s => s is Shortcut)!) + var barView = (Bar)view1; + + foreach (var view2 in barView.Subviews.Where (s => s is Shortcut)!) { + var sh = (Shortcut)view2; + sh.Accepting += (o, args) => - { - eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}"); - eventLog.MoveDown (); - }; + { + eventSource.Add ($"Accept: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + }; } } } @@ -420,36 +431,32 @@ void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e) private void ConfigMenuBar (Bar bar) { - var fileMenuBarItem = new Shortcut + Menuv2? fileMenu = new ContextMenuv2 + { + Id = "fileMenu", + }; + ConfigureMenu (fileMenu); + + fileMenu.Accepting += (sender, args) => + { + + }; + + var fileMenuBarItem = new Shortcut (fileMenu, Command.Copy, "_File", "File Menu") { - Title = "_File", - HelpText = "File Menu", Key = Key.D0.WithAlt, - HighlightStyle = HighlightStyle.Hover }; + fileMenuBarItem.Disposing += (sender, args) => fileMenu?.Dispose (); + fileMenuBarItem.Accepting += (sender, args) => - { - var fileMenu = new Menuv2 - { - Id = "fileMenu", - }; - ConfigureMenu (fileMenu); - fileMenu.VisibleChanged += (sender, args) => - { - if (Application.Popover is { Visible: false }) - { - Application.Popover?.Dispose (); - Application.Popover = null; - } - }; - Application.Popover = fileMenu; - Rectangle screen = fileMenuBarItem.FrameToScreen (); - fileMenu.X = screen.X; - fileMenu.Y = screen.Y + screen.Height; - fileMenu.Visible = true; - - }; + { + Application.Popover = fileMenu; + Rectangle screen = fileMenuBarItem.FrameToScreen (); + fileMenu.X = screen.X; + fileMenu.Y = screen.Y + screen.Height; + fileMenu.Visible = true; + }; @@ -591,7 +598,7 @@ public void ConfigStatusBar (Bar bar) return; - void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); } + void Button_Clicked (object? sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); } } From e81ab446aa6f096fb2a446ae20de2f3a701e66d2 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 14:50:16 -0600 Subject: [PATCH 32/75] Undid some keybinding stuff --- .../Application/Application.Keyboard.cs | 2 +- Terminal.Gui/Input/KeyBindings.cs | 42 ++++--------------- Terminal.Gui/View/View.Keyboard.cs | 2 +- Terminal.Gui/Views/Bar.cs | 10 +++-- UICatalog/Scenarios/Bars.cs | 8 +++- UnitTests/Application/KeyboardTests.cs | 11 +++-- UnitTests/Input/KeyBindingTests.cs | 2 +- 7 files changed, 29 insertions(+), 48 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 737adcb7fe..0e687e5c73 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -145,7 +145,7 @@ public static bool OnKeyDown (Key keyEvent) } else { - if (!KeyBindings.TryGet (keyEvent, KeyBindingScope.Application, null, out KeyBinding appBinding)) + if (!KeyBindings.TryGet (keyEvent, out KeyBinding appBinding)) { continue; } diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index a95b8bb3d2..c45a6fd436 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -37,7 +37,7 @@ public void Add (Key key, KeyBinding binding, View? boundViewForAppScope = null) boundViewForAppScope = BoundView; } - if (TryGet (key, binding.Scope, boundViewForAppScope, out KeyBinding _)) + if (TryGet (key, out KeyBinding _)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); @@ -90,7 +90,7 @@ public void Add (Key key, KeyBindingScope scope, View? boundViewForAppScope = nu throw new ArgumentException (@"At least one command must be specified", nameof (commands)); } - if (TryGet (key, scope, boundViewForAppScope, out KeyBinding binding)) + if (TryGet (key, out KeyBinding binding)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); } @@ -138,8 +138,7 @@ public void Add (Key key, KeyBindingScope scope, params Command [] commands) throw new ArgumentException (@"At least one command must be specified", nameof (commands)); } - // if BoundView is null, the right thing will happen - if (TryGet (key, scope, BoundView, out KeyBinding binding)) + if (TryGet (key, out KeyBinding binding)) { throw new InvalidOperationException (@$"A key binding for {key} exists ({binding})."); } @@ -325,7 +324,7 @@ public IEnumerable GetKeysFromCommands (params Command [] commands) public void Remove (Key key, View? boundViewForAppScope = null) { KeyBindingScope scope = KeyBindingScope.Disabled; - if (boundViewForAppScope is null) + if (boundViewForAppScope is null && BoundView is { }) { boundViewForAppScope = BoundView; scope = KeyBindingScope.HotKey | KeyBindingScope.Focused; @@ -334,15 +333,13 @@ public void Remove (Key key, View? boundViewForAppScope = null) { scope = KeyBindingScope.Application; } - if (!TryGet (key, scope, boundViewForAppScope, out KeyBinding binding)) + + if (!TryGet (key, out KeyBinding binding)) { return; } - if (boundViewForAppScope is { } && binding.BoundView == boundViewForAppScope) - { - Bindings.Remove (key); - } + Bindings.Remove (key); } /// Replaces the commands already bound to a key. @@ -439,29 +436,4 @@ public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding) return false; } - - /// Gets the commands bound with the specified Key that are scoped to a particular scope and bound View. - /// - /// The key to check. - /// the scope to filter on - /// The view the binding is bound to - /// - /// When this method returns, contains the commands bound with the specified Key, if the Key is - /// found; otherwise, null. This parameter is passed uninitialized. - /// - /// if the Key is bound; otherwise . - public bool TryGet (Key key, KeyBindingScope scope, View? boundView, out KeyBinding binding) - { - binding = new (Array.Empty (), KeyBindingScope.Disabled, null); - - if (key.IsValid && Bindings.TryGetValue (key, out binding)) - { - if (binding.BoundView == boundView && scope.HasFlag (binding.Scope)) - { - return true; - } - } - - return false; - } } diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index 6437e8a4ef..5189155500 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -704,7 +704,7 @@ public bool IsHotKeyKeyBound (Key key, out View? boundView) #if DEBUG - if (Application.KeyBindings.TryGet (key, KeyBindingScope.Focused | KeyBindingScope.HotKey, this, out KeyBinding b)) + if (Application.KeyBindings.TryGet (key, out KeyBinding b)) { //var boundView = views [0]; //var commandBinding = boundView.KeyBindings.Get (key); diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 5b8dbc12c5..4075ed6639 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -20,7 +20,7 @@ public class Bar : View, IOrientation, IDesignable public Bar () : this ([]) { } /// - public Bar (IEnumerable shortcuts) + public Bar (IEnumerable? shortcuts) { CanFocus = true; @@ -34,10 +34,14 @@ public Bar (IEnumerable shortcuts) Initialized += Bar_Initialized; MouseEvent += OnMouseEvent; - foreach (Shortcut shortcut in shortcuts) + if (shortcuts is { }) { - Add (shortcut); + foreach (Shortcut shortcut in shortcuts) + { + Add (shortcut); + } } + } private void OnMouseEvent (object? sender, MouseEventEventArgs e) diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 22c02b199d..bdc2988806 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -279,8 +279,9 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventEventArgs e) sh.Accepting += (o, args) => { - eventSource.Add ($"Accept: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); eventLog.MoveDown (); + args.Cancel = true; }; } } @@ -530,6 +531,9 @@ private void ConfigureMenu (Bar bar) HighlightStyle = HighlightStyle.None, CanFocus = false }; + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + shortcut4.Accepting += (sender, args) => args.Cancel = true; + bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); } @@ -563,6 +567,8 @@ public void ConfigStatusBar (Bar bar) Text = "_Show/Hide" }, }; + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + shortcut.Accepting += (sender, args) => args.Cancel = true; bar.Add (shortcut); diff --git a/UnitTests/Application/KeyboardTests.cs b/UnitTests/Application/KeyboardTests.cs index 09b1bf7a3f..251368686d 100644 --- a/UnitTests/Application/KeyboardTests.cs +++ b/UnitTests/Application/KeyboardTests.cs @@ -159,16 +159,14 @@ public void KeyBinding_Application_RemoveKeyBinding_Removes () } [Fact] - [AutoInitShutdown] public void KeyBinding_OnKeyDown () { + Application.Top = new Toplevel (); var view = new ScopedKeyBindingView (); var invoked = false; view.InvokingKeyBindings += (s, e) => invoked = true; - var top = new Toplevel (); - top.Add (view); - Application.Begin (top); + Application.Top.Add (view); Application.OnKeyDown (Key.A); Assert.False (invoked); @@ -176,7 +174,7 @@ public void KeyBinding_OnKeyDown () invoked = false; view.ApplicationCommand = false; - Application.KeyBindings.Remove (KeyCode.A); + Application.KeyBindings.Remove (Key.A); Application.OnKeyDown (Key.A); // old Assert.False (invoked); Assert.False (view.ApplicationCommand); @@ -200,7 +198,8 @@ public void KeyBinding_OnKeyDown () Assert.True (view.ApplicationCommand); Assert.True (view.HotKeyCommand); Assert.False (view.FocusedCommand); - top.Dispose (); + Application.Top.Dispose (); + Application.ResetState (true); } [Fact] diff --git a/UnitTests/Input/KeyBindingTests.cs b/UnitTests/Input/KeyBindingTests.cs index be328e8bba..e81662f38c 100644 --- a/UnitTests/Input/KeyBindingTests.cs +++ b/UnitTests/Input/KeyBindingTests.cs @@ -73,7 +73,7 @@ public void Add_With_Bound_View_Throws_If_App_Scope () // Add should not allow duplicates [Fact] - public void Add_With_Bound_View_Throws_If_Exists () + public void Add_With_Throws_If_Exists () { var keyBindings = new KeyBindings (new View ()); keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey); From 3d5d101a1fed0e011239e668a83aa0317d9e5e84 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 16:00:09 -0600 Subject: [PATCH 33/75] Fixed stuff --- Terminal.Gui/Application/Application.Mouse.cs | 2 +- Terminal.Gui/Input/KeyBindings.cs | 13 +----- Terminal.Gui/View/View.Drawing.cs | 9 ++-- Terminal.Gui/View/View.Keyboard.cs | 24 ----------- Terminal.Gui/View/View.Layout.cs | 25 ----------- Terminal.Gui/View/View.Mouse.cs | 18 ++++---- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 27 ++---------- Terminal.Gui/Views/TextView.cs | 2 +- UICatalog/Scenarios/ContextMenus.cs | 12 ++---- UICatalog/Scenarios/Shortcuts.cs | 42 ++++++++----------- 10 files changed, 42 insertions(+), 132 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 534e3e9694..1195cf8ae9 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -224,7 +224,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) View = deepestViewUnderMouse }; } - else if (deepestViewUnderMouse.ViewportToScreen (Rectangle.Empty with { Size = deepestViewUnderMouse.Viewport.Size }).Contains (mouseEvent.Position)) + else if (deepestViewUnderMouse.ViewportToScreen (Rectangle.Empty with { Size = deepestViewUnderMouse.Viewport.Size }).Contains (mouseEvent.ScreenPosition)) { Point viewportLocation = deepestViewUnderMouse.ScreenToViewport (mouseEvent.ScreenPosition); diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index c45a6fd436..368228f39f 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -323,18 +323,7 @@ public IEnumerable GetKeysFromCommands (params Command [] commands) /// Optional View for bindings. public void Remove (Key key, View? boundViewForAppScope = null) { - KeyBindingScope scope = KeyBindingScope.Disabled; - if (boundViewForAppScope is null && BoundView is { }) - { - boundViewForAppScope = BoundView; - scope = KeyBindingScope.HotKey | KeyBindingScope.Focused; - } - else - { - scope = KeyBindingScope.Application; - } - - if (!TryGet (key, out KeyBinding binding)) + if (!TryGet (key, out KeyBinding _)) { return; } diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index 4401d29580..7e2bf6622d 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -528,24 +528,21 @@ public virtual void OnDrawContent (Rectangle viewport) } // BUGBUG: this clears way too frequently. Need to optimize this. - if (SuperView is { } || Arrangement.HasFlag (ViewArrangement.Overlapped)) + if (SuperView is { } || Arrangement.HasFlag (ViewArrangement.Overlapped) || IsCurrentTop) { Clear (); } if (!string.IsNullOrEmpty (TextFormatter.Text)) { - if (TextFormatter is { }) - { - TextFormatter.NeedsFormat = true; - } + TextFormatter.NeedsFormat = true; } // This should NOT clear // TODO: If the output is not in the Viewport, do nothing var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ()); - TextFormatter?.Draw ( + TextFormatter.Draw ( drawRect, HasFocus ? GetFocusColor () : GetNormalColor (), HasFocus ? GetHotFocusColor () : GetHotNormalColor (), diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index 5189155500..8c0dac57ed 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -722,30 +722,6 @@ public bool IsHotKeyKeyBound (Key key, out View? boundView) #endif return InvokeCommands (binding.Commands, key, binding); - - foreach (Command command in binding.Commands) - { - if (!CommandImplementations.ContainsKey (command)) - { - throw new NotSupportedException ( - @$"A KeyBinding was set up for the command {command} ({key}) but that command is not supported by this View ({GetType ().Name})" - ); - } - - // each command has its own return value - bool? thisReturn = InvokeCommand (command, key, binding); - - // if we haven't got anything yet, the current command result should be used - toReturn ??= thisReturn; - - // if ever see a true then that's what we will return - if (thisReturn ?? false) - { - toReturn = true; - } - } - - return toReturn; } #endregion Key Bindings diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 1edd4daca2..e4d04d1710 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -28,7 +28,6 @@ public partial class View // Layout APIs /// The target y location. /// The new x location that will ensure will be fully visible. /// The new y location that will ensure will be fully visible. - /// The new top most statusBar /// /// Either (if does not have a Super View) or /// 's SuperView. This can be used to ensure LayoutSubviews is called on the correct View. @@ -39,13 +38,10 @@ public partial class View // Layout APIs int targetY, out int nx, out int ny - //, - // out StatusBar? statusBar ) { int maxDimension; View? superView; - //statusBar = null!; if (Application.Driver is null) { @@ -120,27 +116,6 @@ out int ny ny = Math.Max (targetY, maxDimension); - //if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) - //{ - // statusVisible = Application.Top?.StatusBar?.Visible == true; - // statusBar = Application.Top?.StatusBar!; - //} - //else - //{ - // View? t = viewToMove!.SuperView; - - // while (t is { } and not Toplevel) - // { - // t = t.SuperView; - // } - - // if (t is Toplevel topLevel) - // { - // statusVisible = topLevel.StatusBar?.Visible == true; - // statusBar = topLevel.StatusBar!; - // } - //} - if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { maxDimension = statusVisible ? Driver.Rows - 1 : Driver.Rows; diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 53619771bb..17895abfdb 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -645,16 +645,20 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) View? start = Application.Top; - if (Application.Popover?.Visible == true) - { - if (Application.Top?.Contains (location) ?? false) - { - viewsUnderMouse.Add (Application.Top); - } + viewsUnderMouse = GetViewsUnderMouse (Application.Top, location); - start = Application.Popover; + if (Application.Popover is { Visible: true }) + { + viewsUnderMouse.AddRange (GetViewsUnderMouse (Application.Popover, location)); } + return viewsUnderMouse; + + } + + internal static List GetViewsUnderMouse (View? start, in Point location) + { + List viewsUnderMouse = new (); Point currentLocation = location; while (start is { Visible: true } && start.Contains (currentLocation)) diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 33ee4f7e6f..4fbdfeba31 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -26,16 +26,10 @@ public class ContextMenuv2 : Menuv2 { private Key _key = DefaultKey; - private MouseFlags _mouseFlags = MouseFlags.Button3Clicked; - - public MouseFlags MouseFlags - { - get => _mouseFlags; - set - { - _mouseFlags = value; - } - } + /// + /// The mouse flags that will trigger the context menu. The default is which is typically the right mouse button. + /// + public MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked; /// Initializes a context menu with no menu items. public ContextMenuv2 () : this ([]) { } @@ -46,9 +40,6 @@ public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) Visible = false; VisibleChanged += OnVisibleChanged; Key = DefaultKey; - - KeyChanged += OnKeyChanged; - AddCommand (Command.Context, () => { @@ -62,18 +53,8 @@ public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) return true; }); - - //Application.KeyBindings.Remove (Key, this); - //Application.KeyBindings.Add (Key, this, Command.Context); - return; - void OnKeyChanged (object? sender, KeyChangedEventArgs e) - { - //Application.KeyBindings.Remove (e.OldKey, this); - //Application.KeyBindings.Remove (e.NewKey, this); - //Application.KeyBindings.Add (e.NewKey, this, Command.Context); - } } private void OnVisibleChanged (object? sender, EventArgs _) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b02c4ba7f6..f889e699d4 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -4294,7 +4294,7 @@ private void ClearSelectedRegion () DoNeededAction (); } - private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey, e.NewKey); } + private void ContextMenu_KeyChanged (object? sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey, e.NewKey); } private bool DeleteTextBackwards () { diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 2473574f81..fbfd9945a9 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -68,8 +68,6 @@ public override void Main () _tfBottomRight = new () { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text }; appWindow.Add (_tfBottomRight); - Point mousePos = default; - appWindow.KeyDown += (s, e) => { if (e.KeyCode == _winContextMenuKey) @@ -124,10 +122,10 @@ private Shortcut [] GetSupportedCultures () supportedCultures.Add (culture); index++; culture = new (); - culture.CommandView = new CheckBox () { CanFocus = false, HighlightStyle = HighlightStyle.None}; + culture.CommandView = new CheckBox () { CanFocus = false, HighlightStyle = HighlightStyle.None }; } - culture.Id= $"_{c.Parent.EnglishName}"; + culture.Id = $"_{c.Parent.EnglishName}"; culture.Title = $"_{c.Parent.EnglishName}"; culture.HelpText = c.Name; ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked; @@ -142,7 +140,7 @@ void CreateAction (List cultures, Shortcut culture) culture.Action += () => { Thread.CurrentThread.CurrentUICulture = new (culture.HelpText); - + foreach (Shortcut item in cultures) { ((CheckBox)item.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == item.HelpText ? CheckState.Checked : CheckState.UnChecked; @@ -172,13 +170,11 @@ private void CreateWinContextMenu () //ForceMinimumPosToZero = _forceMinimumPosToZero, //UseSubMenusSingleFrame = _useSubMenusSingleFrame }; - - //_winContextMenu.KeyBindings.Add (_winContextMenuKey, Command.Context); } private void ShowWinContextMenu (Point? screenPosition) { - _winContextMenu!.SetPosition(screenPosition); + _winContextMenu!.SetPosition (screenPosition); Application.Popover = _winContextMenu; _winContextMenu.Visible = true; } diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index b0a3803e9c..9d88d3ec29 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/UICatalog/Scenarios/Shortcuts.cs @@ -1,11 +1,9 @@ +#nullable enable + using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Diagnostics; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Timers; using Terminal.Gui; @@ -29,10 +27,10 @@ public override void Main () // Setting everything up in Loaded handler because we change the // QuitKey and it only sticks if changed after init - private void App_Loaded (object sender, EventArgs e) + private void App_Loaded (object? sender, EventArgs e) { Application.QuitKey = Key.F4.WithCtrl; - Application.Top.Title = GetQuitKeyAndName (); + Application.Top!.Title = GetQuitKeyAndName (); ObservableCollection eventSource = new (); @@ -45,7 +43,7 @@ private void App_Loaded (object sender, EventArgs e) BorderStyle = LineStyle.Double, Title = "E_vents" }; - eventLog.Width = Dim.Func (() => Math.Min (Application.Top.Viewport.Width / 2, eventLog?.MaxLength + eventLog.GetAdornmentsThickness ().Horizontal ?? 0)); + eventLog.Width = Dim.Func (() => Math.Min (Application.Top.Viewport.Width / 2, eventLog?.MaxLength + eventLog!.GetAdornmentsThickness ().Horizontal ?? 0)); Application.Top.Add (eventLog); var vShortcut1 = new Shortcut @@ -80,7 +78,7 @@ private void App_Loaded (object sender, EventArgs e) ((RadioGroup)vShortcut2.CommandView).SelectedItemChanged += (o, args) => { - eventSource.Add ($"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}"); + eventSource.Add ($"SelectedItemChanged: {o?.GetType ().Name} - {args.SelectedItem}"); eventLog.MoveDown (); }; @@ -99,11 +97,11 @@ private void App_Loaded (object sender, EventArgs e) }, Key = Key.F5.WithCtrl.WithAlt.WithShift, HelpText = "Width is Fill", - Width = Dim.Fill () - Dim.Width (eventLog), + Width = Dim.Fill ()! - Dim.Width (eventLog), KeyBindingScope = KeyBindingScope.HotKey, }; - ((CheckBox)vShortcut3.CommandView).CheckedStateChanging += (s, e) => + ((CheckBox)vShortcut3.CommandView).CheckedStateChanging += (_, args) => { if (vShortcut3.CommandView is CheckBox cb) { @@ -114,18 +112,12 @@ private void App_Loaded (object sender, EventArgs e) IEnumerable toAlign = Application.Top.Subviews.Where (v => v is Shortcut { Orientation: Orientation.Vertical, Width: not DimAbsolute }); IEnumerable enumerable = toAlign as View [] ?? toAlign.ToArray (); - if (e.NewValue == CheckState.Checked) + if (args.NewValue == CheckState.Checked) { - foreach (var view in enumerable) - { - var peer = (Shortcut)view; - - // DANGER: KeyView is internal so we can't access it. So we assume this is how it works. - max = Math.Max (max, peer.Key.ToString ().GetColumns ()); - } + max = (from Shortcut? peer in enumerable select peer.Key.ToString ().GetColumns ()).Prepend (max).Max (); } - foreach (var view in enumerable) + foreach (View view in enumerable) { var peer = (Shortcut)view; peer.MinimumKeyTextSize = max; @@ -203,12 +195,12 @@ private void App_Loaded (object sender, EventArgs e) Key = Key.F5, }; - ((Slider)vShortcutSlider.CommandView).Options = new () { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } }; + ((Slider)vShortcutSlider.CommandView).Options = [new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" }]; ((Slider)vShortcutSlider.CommandView).SetOption (0); ((Slider)vShortcutSlider.CommandView).OptionsChanged += (o, args) => { - eventSource.Add ($"OptionsChanged: {o.GetType ().Name} - {string.Join (",", ((Slider)o).GetSetOptions ())}"); + eventSource.Add ($"OptionsChanged: {o?.GetType ().Name} - {string.Join (",", ((Slider)o!)!.GetSetOptions ())}"); eventLog.MoveDown (); }; @@ -330,7 +322,7 @@ private void App_Loaded (object sender, EventArgs e) { Application.Top.ColorScheme = new ColorScheme (Application.Top.ColorScheme) { - Normal = new Attribute (Application.Top.ColorScheme.Normal.Foreground, args.CurrentValue), + Normal = new (Application.Top!.GetNormalColor().Foreground, args.CurrentValue), }; }; hShortcutBG.CommandView = bgColor; @@ -399,10 +391,10 @@ private void App_Loaded (object sender, EventArgs e) //((CheckBox)vShortcut5.CommandView).OnToggle (); } - private void Button_Clicked (object sender, CommandEventArgs e) + private void Button_Clicked (object? sender, CommandEventArgs e) { e.Cancel = true; - View view = sender as View; - MessageBox.Query ("Hi", $"You clicked {view!.Text}", "_Ok"); + View? view = sender as View; + MessageBox.Query ("Hi", $"You clicked {view?.Text}", "_Ok"); } } From a243cc89e4b06348997e2ce918222d8a7ac2d8ca Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 16:32:05 -0600 Subject: [PATCH 34/75] Sped up unit tests --- UnitTests/Application/SynchronizatonContextTests.cs | 4 ++-- UnitTests/TestHelpers.cs | 3 --- UnitTests/UICatalog/ScenarioTests.cs | 7 +++---- UnitTests/Views/DateFieldTests.cs | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/UnitTests/Application/SynchronizatonContextTests.cs b/UnitTests/Application/SynchronizatonContextTests.cs index fce4a3250d..ce099fd100 100644 --- a/UnitTests/Application/SynchronizatonContextTests.cs +++ b/UnitTests/Application/SynchronizatonContextTests.cs @@ -33,7 +33,7 @@ public void SynchronizationContext_Post (Type driverType) Task.Run ( () => { - Thread.Sleep (1_000); + Thread.Sleep (500); // non blocking context.Post ( @@ -68,7 +68,7 @@ public void SynchronizationContext_Send () Task.Run ( () => { - Thread.Sleep (1_000); + Thread.Sleep (500); // blocking context.Send ( diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index acc367e9a9..8ed9820da7 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -112,9 +112,6 @@ public override void After (MethodInfo methodUnderTest) // Reset to defaults Locations = ConfigLocations.DefaultOnly; Reset(); - - // Enable subsequent tests that call Init to get all config files (the default). - //Locations = ConfigLocations.All; } public override void Before (MethodInfo methodUnderTest) diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index d7549b594c..9d0fe5c26b 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -31,7 +31,6 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) _timeoutLock = new (); // Disable any UIConfig settings - ConfigurationManager.ConfigLocations savedConfigLocations = ConfigurationManager.Locations; ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; // If a previous test failed, this will ensure that the Application is in a clean state @@ -40,7 +39,7 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) _output.WriteLine ($"Running Scenario '{scenarioType}'"); var scenario = (Scenario)Activator.CreateInstance (scenarioType); - uint abortTime = 1500; + uint abortTime = 500; var initialized = false; var shutdown = false; object timeout = null; @@ -77,7 +76,7 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) } // Restore the configuration locations - ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; ConfigurationManager.Reset (); return; @@ -117,7 +116,7 @@ bool ForceCloseCallback () $"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey} after {abortTime}ms and {iterationCount} iterations. Force quit."); // Restore the configuration locations - ConfigurationManager.Locations = savedConfigLocations; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.DefaultOnly; ConfigurationManager.Reset (); Application.ResetState (true); diff --git a/UnitTests/Views/DateFieldTests.cs b/UnitTests/Views/DateFieldTests.cs index 3f17e89500..bcb88fb329 100644 --- a/UnitTests/Views/DateFieldTests.cs +++ b/UnitTests/Views/DateFieldTests.cs @@ -185,7 +185,7 @@ public void Using_All_Culture_StandardizeDateFormat () DateTime date = DateTime.Parse ("1/1/1971"); - foreach (CultureInfo culture in CultureInfo.GetCultures (CultureTypes.AllCultures)) + foreach (CultureInfo culture in CultureInfo.GetCultures (CultureTypes.NeutralCultures)) { CultureInfo.CurrentCulture = culture; string separator = culture.DateTimeFormat.DateSeparator.Trim (); From e63fb7c34c6b3f70f9343e195df67741d25e8c61 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 16:44:04 -0600 Subject: [PATCH 35/75] Sped up unit tests 2 --- UnitTests/UICatalog/ScenarioTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index 9d0fe5c26b..8c34f8d131 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -39,7 +39,7 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType) _output.WriteLine ($"Running Scenario '{scenarioType}'"); var scenario = (Scenario)Activator.CreateInstance (scenarioType); - uint abortTime = 500; + uint abortTime = 1500; var initialized = false; var shutdown = false; object timeout = null; From 2f3dbe7570ba45ab81b231f56ec6849c282a608e Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 11 Oct 2024 17:36:24 -0600 Subject: [PATCH 36/75] Sped up unit tests 3 --- Terminal.Gui/Input/Responder.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Input/Responder.cs b/Terminal.Gui/Input/Responder.cs index 43cc082403..622c179d5c 100644 --- a/Terminal.Gui/Input/Responder.cs +++ b/Terminal.Gui/Input/Responder.cs @@ -50,7 +50,7 @@ protected virtual void Dispose (bool disposing) } // TODO: v2 - nuke this - /// Utilty function to determine is overridden in the . + /// Utility function to determine is overridden in the . /// The view. /// The method name. /// if it's overridden, otherwise. @@ -76,10 +76,7 @@ internal static bool IsOverridden (Responder subclass, string method) #if DEBUG_IDISPOSABLE /// For debug purposes to verify objects are being disposed properly public bool WasDisposed; - - /// For debug purposes to verify objects are being disposed properly - public int DisposedCount = 0; - + /// For debug purposes public static List Instances = new (); From 883ab6739a7e3524ad2273741e0187909bd9f8d4 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 13 Oct 2024 07:47:47 -0600 Subject: [PATCH 37/75] Messing with menus --- Terminal.Gui/Application/Application.Mouse.cs | 29 +++---- Terminal.Gui/Views/Bar.cs | 1 - Terminal.Gui/Views/Menu/ContextMenuv2.cs | 9 ++ Terminal.Gui/Views/Menu/MenuBarv2.cs | 49 ++++++++--- Terminal.Gui/Views/Menu/Menuv2.cs | 48 ++++++++++ Terminal.Gui/Views/Shortcut.cs | 23 +++-- UICatalog/Scenarios/Bars.cs | 87 ++++++++++++++----- 7 files changed, 189 insertions(+), 57 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 1195cf8ae9..d02f767128 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -172,21 +172,6 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } - if (HandleMouseGrab (deepestViewUnderMouse, mouseEvent)) - { - return; - } - - // We can combine this into the switch expression to reduce cognitive complexity even more and likely - // avoid one or two of these checks in the process, as well. - - WantContinuousButtonPressedView = deepestViewUnderMouse switch - { - { WantContinuousButtonPressed: true } => deepestViewUnderMouse, - _ => null - }; - - if (Popover is { Visible: true } && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) @@ -202,6 +187,20 @@ internal static void OnMouseEvent (MouseEvent mouseEvent) return; } + if (HandleMouseGrab (deepestViewUnderMouse, mouseEvent)) + { + return; + } + + // We can combine this into the switch expression to reduce cognitive complexity even more and likely + // avoid one or two of these checks in the process, as well. + + WantContinuousButtonPressedView = deepestViewUnderMouse switch + { + { WantContinuousButtonPressed: true } => deepestViewUnderMouse, + _ => null + }; + // May be null before the prior condition or the condition may set it as null. // So, the checking must be outside the prior condition. if (deepestViewUnderMouse is null) diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 4075ed6639..fe815da6f4 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -41,7 +41,6 @@ public Bar (IEnumerable? shortcuts) Add (shortcut); } } - } private void OnMouseEvent (object? sender, MouseEventEventArgs e) diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 4fbdfeba31..6176854d90 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -53,6 +53,15 @@ public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) return true; }); + + foreach (var sc in shortcuts) + { + AddCommand(Command.Accept, + (ctx) => + { + return sc.TargetView?.InvokeCommand (sc.Command, ctx); + }); + } return; } diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index d4c598dfed..0fd357e3cf 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Reflection; namespace Terminal.Gui; @@ -22,15 +23,35 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) ColorScheme = Colors.ColorSchemes ["Menu"]; Orientation = Orientation.Horizontal; - LayoutStarted += MenuBarv2_LayoutStarted; + AddCommand (Command.Context, + (ctx) => + { + if (ctx.Data is Shortcut shortcut) + { + Rectangle screen = shortcut.FrameToScreen (); + Application.Popover = shortcut.TargetView; + shortcut.TargetView.X = screen.X; + shortcut.TargetView.Y = screen.Y + screen.Height; + shortcut.TargetView.Visible = true; + + return true; + } + + return false; + }); } - // MenuBarv2 arranges the items horizontally. - // The first item has no left border, the last item has no right border. - // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart). - private void MenuBarv2_LayoutStarted (object sender, LayoutEventArgs e) + /// + protected override bool OnHighlight (CancelEventArgs args) { - + if (args.NewValue.HasFlag (HighlightStyle.Hover)) + { + if (Application.Popover is { Visible: true } && View.IsInHierarchy (this, Application.Popover)) + { + + } + } + return base.OnHighlight (args); } /// @@ -43,12 +64,18 @@ public override View Add (View view) if (view is Shortcut shortcut) { - // TODO: not happy about using AlignmentModes for this. Too implied. - // TODO: instead, add a property (a style enum?) to Shortcut to control this - //shortcut.AlignmentModes = AlignmentModes.EndToStart; - shortcut.KeyView.Visible = false; shortcut.HelpView.Visible = false; + + shortcut.Selecting += (sender, args) => + { + args.Cancel = InvokeCommand (Command.Context, args.Context) == true; + }; + + shortcut.Accepting += (sender, args) => + { + args.Cancel = InvokeCommand (Command.Context, args.Context) == true; + }; } return view; diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index d348e9317d..78088c7966 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -74,6 +74,12 @@ public override View Add (View view) shortcut.Accepting += ShortcutOnAccepting; + AddCommand (shortcut.Command, (ctx) => + { + return RaiseShortcutCommandInvoked (ctx); + }); + + void ShortcutOnAccepting (object sender, CommandEventArgs e) { if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible) @@ -88,4 +94,46 @@ void ShortcutOnAccepting (object sender, CommandEventArgs e) return view; } + + + protected bool? RaiseShortcutCommandInvoked (CommandContext ctx) + { + CommandEventArgs args = new () { Context = ctx }; + + // Best practice is to invoke the virtual method first. + // This allows derived classes to handle the event and potentially cancel it. + args.Cancel = OnShortcutCommandInvoked (args) || args.Cancel; + + if (!args.Cancel) + { + // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. + ShortcutCommandInvoked?.Invoke (this, args); + } + + return ShortcutCommandInvoked is null ? null : args.Cancel; + } + + /// + /// Called when the user is accepting the state of the View and the has been invoked. Set CommandEventArgs.Cancel to + /// and return to stop processing. + /// + /// + /// + /// See for more information. + /// + /// + /// + /// to stop processing. + protected virtual bool OnShortcutCommandInvoked (CommandEventArgs args) { return false; } + + /// + /// Cancelable event raised when the user is accepting the state of the View and the has been invoked. Set + /// CommandEventArgs.Cancel to cancel the event. + /// + /// + /// + /// See for more information. + /// + /// + public event EventHandler? ShortcutCommandInvoked; } diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 66eef77e5c..c27ae6426f 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -75,7 +75,7 @@ public Shortcut (View targetView, Command command, string commandText, string? h helpText) { _targetView = targetView; - _command = command; + Command = command; } /// @@ -92,7 +92,7 @@ public Shortcut (View targetView, Command command, string commandText, string? h /// The help text to display. public Shortcut (Key key, string? commandText, Action? action, string? helpText = null) { - Id = "_shortcut"; + Id = $"shortcut:{commandText}"; HighlightStyle = HighlightStyle.None; CanFocus = true; @@ -312,7 +312,9 @@ private void OnLayoutStarted (object? sender, LayoutEventArgs e) private readonly View? _targetView; // If set, _command will be invoked - private readonly Command _command; // Used when _targetView is set + public View? TargetView => _targetView; + + public Command Command { get; } private void AddCommands () { @@ -351,6 +353,11 @@ private void AddCommands () return true; } + if (ctx.Command != Command.Accept) + { + // return false; + } + if (Action is { }) { Action.Invoke (); @@ -361,7 +368,7 @@ private void AddCommands () if (_targetView is { }) { - _targetView.InvokeCommand (_command); + _targetView.InvokeCommand (Command); } return cancel; @@ -526,10 +533,12 @@ void CommandViewOnSelecting (object? sender, CommandEventArgs e) if (e.Context.Data != this) { // Forward command to ourselves - InvokeCommand (Command.Select, new (Command.Select, null, null, this)); + e.Cancel = InvokeCommand (Command.Select, new (Command.Select, null, null, this)) == true; + } + else + { + e.Cancel = true; } - - e.Cancel = true; } } } diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index bdc2988806..99f8700746 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -5,6 +5,7 @@ using System.ComponentModel; using System.Linq; using System.Text; +using System.Threading; using Terminal.Gui; namespace UICatalog.Scenarios; @@ -79,9 +80,8 @@ private void App_Loaded (object? sender, EventArgs e) Y = Pos.Top (label), Width = Dim.Fill (), }; - - ConfigMenuBar (bar); menuBarLikeExamples.Add (bar); + ConfigMenuBar (bar); label = new Label () { @@ -98,8 +98,8 @@ private void App_Loaded (object? sender, EventArgs e) Y = Pos.Top (label), }; - ConfigMenuBar (bar); menuBarLikeExamples.Add (bar); + ConfigMenuBar (bar); FrameView menuLikeExamples = new () { @@ -268,10 +268,36 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventEventArgs e) foreach (var view in Application.Top.Subviews.Where (f => f is FrameView)!) { var frameView = (FrameView)view; - - foreach (var view1 in frameView.Subviews.Where (b => b is Bar)!) + frameView.Accepting += (o, args) => + { + eventSource.Add ($"Accepting: {frameView?.Id}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + foreach (var view1 in frameView.Subviews.Where (b => b is Bar || b is MenuBarv2 || b is Menuv2)!) { var barView = (Bar)view1; + barView.Accepting += (o, args) => + { + eventSource.Add ($"Accepting: {barView!.Id} {args.Context.Command}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + if (barView is Menuv2 menuv2) + { + menuv2.ShortcutCommandInvoked += (o, args) => + { + Shortcut? sc = args.Context.Data as Shortcut; + + eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); + eventLog.MoveDown (); + //args.Cancel = true; + + }; + + } foreach (var view2 in barView.Subviews.Where (s => s is Shortcut)!) { @@ -432,43 +458,58 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventEventArgs e) private void ConfigMenuBar (Bar bar) { - Menuv2? fileMenu = new ContextMenuv2 + Menuv2? fileMenu = new ContextMenuv2 ([ + new (bar, Command.Open, "_Open...", "Open a file") + ]) { Id = "fileMenu", }; - ConfigureMenu (fileMenu); - - fileMenu.Accepting += (sender, args) => - { - }; + //ConfigureMenu (fileMenu); - var fileMenuBarItem = new Shortcut (fileMenu, Command.Copy, "_File", "File Menu") + var fileMenuBarItem = new Shortcut (fileMenu, Command.Context, "_File", "File Menu") { + Id = "fileMenuBarItem", Key = Key.D0.WithAlt, + HighlightStyle = HighlightStyle.Hover }; fileMenuBarItem.Disposing += (sender, args) => fileMenu?.Dispose (); - fileMenuBarItem.Accepting += (sender, args) => - { - Application.Popover = fileMenu; - Rectangle screen = fileMenuBarItem.FrameToScreen (); - fileMenu.X = screen.X; - fileMenu.Y = screen.Y + screen.Height; - fileMenu.Visible = true; - }; + //fileMenuBarItem.Accepting += (sender, args) => + // { + // Application.Popover = fileMenu; + // Rectangle screen = fileMenuBarItem.FrameToScreen (); + // fileMenu.X = screen.X; + // fileMenu.Y = screen.Y + screen.Height; + // fileMenu.Visible = true; + // }; + Menuv2? editMenu = new ContextMenuv2 + { + Id = "editMenu", + }; + ConfigureMenu (editMenu); - var editMenuBarItem = new Shortcut + var editMenuBarItem = new Shortcut (editMenu, Command.Edit, "_Edit", "Edit Menu") { Title = "_Edit", - HelpText = "Edit Menu", - Key = Key.D1.WithAlt, HighlightStyle = HighlightStyle.Hover }; + editMenuBarItem.Disposing += (sender, args) => editMenu?.Dispose (); + + editMenuBarItem.Accepting += (sender, args) => + { + Application.Popover = editMenu; + Rectangle screen = editMenuBarItem.FrameToScreen (); + editMenu.X = screen.X; + editMenu.Y = screen.Y + screen.Height; + editMenu.Visible = true; + }; + + var helpMenuBarItem = new Shortcut { Title = "_Help", From bc2348265ce3f7293f305671f0820841c8dc8293 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 10 Nov 2024 08:30:49 -0700 Subject: [PATCH 38/75] merged latest v2_develop --- Terminal.Gui/Views/Shortcut.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index fa8edb13d6..251ec2c73f 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -490,12 +490,19 @@ void CommandViewOnSelecting (object? sender, CommandEventArgs e) if (e.Context.Data != this) { // Forward command to ourselves - e.Cancel = InvokeCommand (Command.Select, new (Command.Select, null, null, this)) == true; - } - else - { - e.Cancel = true; + InvokeCommand (Command.Select, new (Command.Select, null, null, this)); } + + e.Cancel = true; + //if (e.Context.Data != this) + //{ + // // Forward command to ourselves + // e.Cancel = InvokeCommand (Command.Select, new (Command.Select, null, null, this)) == true; + //} + //else + //{ + // e.Cancel = true; + //} } } } From b1e1da44f97e25175acec5a000de94546f8ae43b Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 24 Nov 2024 12:10:40 -0700 Subject: [PATCH 39/75] Added more Popover unit tests --- Terminal.Gui/View/View.Layout.cs | 12 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 7 +- .../Application/ApplicationPopoverTests.cs | 112 ++++++++++++++++++ 3 files changed, 120 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 1945b5383e..53d526b578 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -1041,12 +1041,12 @@ out int ny View? superView; - if (Application.Driver is null) - { - nx = targetX; - ny = targetY; - return null; - } + //if (Application.Driver is null) + //{ + // nx = targetX; + // ny = targetY; + // return null; + //} if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 315d3bb796..0d77e0de9e 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -105,11 +105,8 @@ public void SetPosition (Point? screenPosition) { if (screenPosition is { }) { - Frame = Frame with - { - X = screenPosition.Value.X - GetViewportOffsetFromFrame ().X, - Y = screenPosition.Value.Y - GetViewportOffsetFromFrame ().Y, - }; + X = screenPosition.Value.X - GetViewportOffsetFromFrame ().X; + Y = screenPosition.Value.Y - GetViewportOffsetFromFrame ().Y; } } diff --git a/UnitTests/Application/ApplicationPopoverTests.cs b/UnitTests/Application/ApplicationPopoverTests.cs index bb9e2455e6..d0b412bf51 100644 --- a/UnitTests/Application/ApplicationPopoverTests.cs +++ b/UnitTests/Application/ApplicationPopoverTests.cs @@ -20,6 +20,8 @@ public void Popover_SetAndGet () // Assert Assert.Equal (popover, Application.Popover); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -34,6 +36,8 @@ public void Popover_SetToNull () // Assert Assert.Null (Application.Popover); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -54,6 +58,8 @@ public void Popover_VisibleChangedEvent () // Assert Assert.True (eventTriggered); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -67,6 +73,8 @@ public void Popover_InitializesCorrectly () // Assert Assert.True (popover.IsInitialized); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -82,6 +90,8 @@ public void Popover_SetsColorScheme () // Assert Assert.Equal (topColorScheme, popover.ColorScheme); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -101,6 +111,37 @@ public void Popover_VisibleChangedToTrue_SetsFocus () // Assert Assert.True (popover.Visible); Assert.True (popover.HasFocus); + + Application.ResetState (ignoreDisposed: true); + } + + [Theory] + [InlineData(-1, -1)] + [InlineData (0, 0)] + [InlineData (2048, 2048)] + [InlineData (2049, 2049)] + public void Popover_VisibleChangedToTrue_Locates_In_Visible_Position (int x, int y) + { + // Arrange + var popover = new View () + { + X = x, + Y = y, + Visible = false, + CanFocus = true, + Width = 1, + Height = 1 + }; + Application.Popover = popover; + + // Act + popover.Visible = true; + Application.LayoutAndDraw(); + + // Assert + Assert.True (Application.Screen.Contains (popover.Frame)); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -121,6 +162,8 @@ public void Popover_VisibleChangedToFalse_Hides_And_Removes_Focus () // Assert Assert.False (popover.Visible); Assert.False (popover.HasFocus); + + Application.ResetState (ignoreDisposed: true); } [Fact] @@ -143,6 +186,8 @@ public void Popover_Quit_Command_Hides () // Assert Assert.False (popover.Visible); Assert.False (popover.HasFocus); + + Application.ResetState (ignoreDisposed: true); } @@ -246,4 +291,71 @@ public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expec Application.Top.Dispose (); Application.ResetState (ignoreDisposed: true); } + + [Fact] + public void Popover_SetAndGet_ReturnsCorrectValue () + { + // Arrange + var view = new View (); + + // Act + Application.Popover = view; + + // Assert + Assert.Equal (view, Application.Popover); + + Application.ResetState (ignoreDisposed: true); + } + + [Fact] + public void Popover_SetToNull_HidesPreviousPopover () + { + // Arrange + var view = new View { Visible = true }; + Application.Popover = view; + + // Act + Application.Popover = null; + + // Assert + Assert.False (view.Visible); + Assert.Null (Application.Popover); + + Application.ResetState (ignoreDisposed: true); + } + + [Fact] + public void Popover_SetNewPopover_HidesPreviousPopover () + { + // Arrange + var oldView = new View { Visible = true }; + var newView = new View (); + Application.Popover = oldView; + + // Act + Application.Popover = newView; + + // Assert + Assert.False (oldView.Visible); + Assert.Equal (newView, Application.Popover); + + Application.ResetState (ignoreDisposed: true); + } + + [Fact] + public void Popover_SetNewPopover_InitializesAndSetsProperties () + { + // Arrange + var view = new View (); + + // Act + Application.Popover = view; + + // Assert + Assert.True (view.IsInitialized); + Assert.True (view.Arrangement.HasFlag (ViewArrangement.Overlapped)); + Assert.Equal (Application.Top?.ColorScheme, view.ColorScheme); + + Application.ResetState (ignoreDisposed: true); + } } From 2cc729741f4204300807b6ce5af9bcd5ebba8c16 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 24 Nov 2024 12:20:43 -0700 Subject: [PATCH 40/75] Added more Popover unit tests2 --- Terminal.Gui/Views/Menu/Menuv2.cs | 24 +++++++++++++++++------- UICatalog/Scenarios/Bars.cs | 10 +++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index b2bef1d7ba..a716c86085 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.ComponentModel; using System.Reflection; @@ -21,7 +22,7 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) VisibleChanged += OnVisibleChanged; } - private void OnVisibleChanged (object sender, EventArgs e) + private void OnVisibleChanged (object? sender, EventArgs e) { if (Visible) { @@ -36,15 +37,19 @@ private void OnVisibleChanged (object sender, EventArgs e) } } - private void Menuv2_Initialized (object sender, EventArgs e) + private void Menuv2_Initialized (object? sender, EventArgs e) { - Border.Thickness = new Thickness (1, 1, 1, 1); - Border.LineStyle = LineStyle.Single; + if (Border is { }) + { + Border.Thickness = new Thickness (1, 1, 1, 1); + Border.LineStyle = LineStyle.Single; + } + ColorScheme = Colors.ColorSchemes ["Menu"]; } /// - public override View Add (View view) + public override View? Add (View? view) { base.Add (view); @@ -62,7 +67,7 @@ public override View Add (View view) }); - void ShortcutOnAccepting (object sender, CommandEventArgs e) + void ShortcutOnAccepting (object? sender, CommandEventArgs e) { if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible) { @@ -78,6 +83,11 @@ void ShortcutOnAccepting (object sender, CommandEventArgs e) } + /// + /// + /// + /// + /// protected bool? RaiseShortcutCommandInvoked (CommandContext ctx) { CommandEventArgs args = new () { Context = ctx }; diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 556207dc39..fb5806f11a 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -52,7 +52,7 @@ private void App_Loaded (object? sender, EventArgs e) ColorScheme = Colors.ColorSchemes ["Toplevel"], Source = new ListWrapper (eventSource) }; - eventLog.Border.Thickness = new (0, 1, 0, 0); + eventLog.Border!.Thickness = new (0, 1, 0, 0); Application.Top.Add (eventLog); FrameView menuBarLikeExamples = new () @@ -290,12 +290,12 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) { menuv2.ShortcutCommandInvoked += (o, args) => { - Shortcut? sc = args.Context.Data as Shortcut; + if (args.Context.Data is Shortcut { } sc) + { + eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); + } - eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); eventLog.MoveDown (); - //args.Cancel = true; - }; } From 9f2635d8447271c57c5ce343e356bcedfabe9a1f Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 24 Nov 2024 12:27:53 -0700 Subject: [PATCH 41/75] Fixed positioning bug --- Terminal.Gui/Application/Application.Popover.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index eab3fc6263..b00c52c974 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -64,6 +64,11 @@ private static void PopoverVisibleChanging (object? sender, CancelEventArgs Date: Sun, 24 Nov 2024 12:41:48 -0700 Subject: [PATCH 42/75] Fixed mouse bug --- Terminal.Gui/Application/Application.Mouse.cs | 9 +++++---- Terminal.Gui/Application/Application.Run.cs | 15 ++++++--------- Terminal.Gui/View/View.Mouse.cs | 2 +- Terminal.Gui/Views/Menu/MenuBarv2.cs | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 6b939e40df..9507f386b3 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -166,6 +166,7 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent) return; } + // Dismiss the Popover if the user clicked outside of it if (Popover is { Visible: true } && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) @@ -187,10 +188,10 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent) } WantContinuousButtonPressedView = deepestViewUnderMouse switch - { - { WantContinuousButtonPressed: true } => deepestViewUnderMouse, - _ => null - }; + { + { WantContinuousButtonPressed: true } => deepestViewUnderMouse, + _ => null + }; // May be null before the prior condition or the condition may set it as null. // So, the checking must be outside the prior condition. diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 04de6e1629..d0592ef9ef 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -502,20 +502,17 @@ public static void Invoke (Action action) /// If the entire View hierarchy will be redrawn. The default is and should only be overriden for testing. public static void LayoutAndDraw (bool forceDraw = false) { - bool neededLayout = View.Layout (TopLevels.Reverse (), Screen.Size); + List tops = new (TopLevels); - if (Popover is { }) + if (Popover is { Visible: true }) { - neededLayout = View.Layout ([Popover], Screen.Size); + tops.Insert(0, Popover); } + bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size); + View.SetClipToScreen (); - View.Draw (TopLevels, neededLayout || forceDraw); - if (Popover is { }) - { - View.SetClipToScreen (); - View.Draw ([Popover], neededLayout || forceDraw); - } + View.Draw (tops, neededLayout || forceDraw); View.SetClipToScreen (); if (forceDraw) diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 3249781ea4..53f5a8b362 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -660,7 +660,7 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (Application.Popover is { Visible: true }) { - viewsUnderMouse.AddRange (GetViewsUnderMouse (Application.Popover, location)); + viewsUnderMouse= GetViewsUnderMouse (Application.Popover, location); } return viewsUnderMouse; diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index 0fd357e3cf..9a0abe62a4 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -26,7 +26,7 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) AddCommand (Command.Context, (ctx) => { - if (ctx.Data is Shortcut shortcut) + if (ctx.Data is Shortcut { TargetView: { } } shortcut) { Rectangle screen = shortcut.FrameToScreen (); Application.Popover = shortcut.TargetView; From 10711c725ed3a887c10aa20e98148f845c3721d1 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 24 Nov 2024 14:52:32 -0700 Subject: [PATCH 43/75] Fixed Bar draw issue --- Terminal.Gui/View/View.Drawing.cs | 7 ++++ Terminal.Gui/Views/Bar.cs | 14 +++++--- Terminal.Gui/Views/Line.cs | 51 ++++++++-------------------- Terminal.Gui/Views/Menu/MenuBarv2.cs | 2 +- UICatalog/Scenarios/Bars.cs | 8 ++--- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index 79ab737f7c..c9ef07aae8 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -55,6 +55,13 @@ public void Draw () // Draw the Border and Padding. // We clip to the frame to prevent drawing outside the frame. saved = ClipFrame (); + + if (SubViewNeedsDraw) + { + // A Subview may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen. + Border?.SetNeedsDraw (); + Padding?.SetNeedsDraw(); + } DoDrawBorderAndPadding (); SetClip (saved); diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 2bd361b028..05bcc11014 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -28,8 +28,6 @@ public Bar (IEnumerable? shortcuts) Height = Dim.Auto (); _orientationHelper = new (this); - _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e); - _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e); // Initialized += Bar_Initialized; MouseEvent += OnMouseEvent; @@ -241,7 +239,6 @@ private void LayoutBarItems (Size contentSize) { View barItem = Subviews [index]; - barItem.X = 0; barItem.ColorScheme = ColorScheme; @@ -252,6 +249,7 @@ private void LayoutBarItems (Size contentSize) if (barItem is Shortcut scBarItem) { + barItem.X = 0; scBarItem.MinimumKeyTextSize = minKeyWidth; scBarItem.Width = scBarItem.GetWidthDimAuto (); barItem.Layout (Application.Screen.Size); @@ -276,14 +274,20 @@ private void LayoutBarItems (Size contentSize) foreach (var subView in Subviews) { - subView.Width = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: _maxBarItemWidth); + if (subView is not Line) + { + subView.Width = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: _maxBarItemWidth); + } } } else { foreach (var subView in Subviews) { - subView.Width = Dim.Fill(); + if (subView is not Line) + { + subView.Width = Dim.Fill (); + } } } diff --git a/Terminal.Gui/Views/Line.cs b/Terminal.Gui/Views/Line.cs index 82b7499e19..9d28893733 100644 --- a/Terminal.Gui/Views/Line.cs +++ b/Terminal.Gui/Views/Line.cs @@ -1,6 +1,10 @@ namespace Terminal.Gui; -/// Draws a single line using the specified by . +/// +/// Draws a single line using the specified by . +/// +/// +/// public class Line : View, IOrientation { private readonly OrientationHelper _orientationHelper; @@ -8,14 +12,13 @@ public class Line : View, IOrientation /// Constructs a Line object. public Line () { - BorderStyle = LineStyle.Single; - Border.Thickness = new Thickness (0); - SuperViewRendersLineCanvas = true; + CanFocus = false; + + base.SuperViewRendersLineCanvas = true; _orientationHelper = new (this); _orientationHelper.Orientation = Orientation.Horizontal; - _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e); - _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e); + OnOrientationChanged(Orientation); } @@ -45,10 +48,12 @@ public void OnOrientationChanged (Orientation newOrientation) { case Orientation.Horizontal: Height = 1; + Width = Dim.Fill (); break; case Orientation.Vertical: Width = 1; + Height = Dim.Fill (); break; @@ -56,48 +61,20 @@ public void OnOrientationChanged (Orientation newOrientation) } #endregion - /// - public override void SetBorderStyle (LineStyle value) - { - // The default changes the thickness. We don't want that. We just set the style. - Border.LineStyle = value; - } - /// protected override bool OnDrawingContent () { - LineCanvas lc = LineCanvas; - - if (SuperViewRendersLineCanvas) - { - lc = SuperView?.LineCanvas; - } - - if (SuperView is Adornment adornment) - { - lc = adornment.Parent?.LineCanvas; - } - Point pos = ViewportToScreen (Viewport).Location; int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height; - if (SuperView is {} && SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal) - { - pos.Offset (-SuperView.Border.Thickness.Left, 0); - length += SuperView.Border.Thickness.Horizontal; - } - - if (SuperView is { } && SuperViewRendersLineCanvas && Orientation == Orientation.Vertical) - { - pos.Offset (0, -SuperView.Border.Thickness.Top); - length += SuperView.Border.Thickness.Vertical; - } - lc?.AddLine ( + LineCanvas?.AddLine ( pos, length, Orientation, BorderStyle ); + + //SuperView?.SetNeedsDraw (); return true; } } diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index 9a0abe62a4..d0f76d7a62 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -20,7 +20,7 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) Width = Dim.Fill (); Height = Dim.Auto (DimAutoStyle.Content, 1); BorderStyle = LineStyle.Dashed; - ColorScheme = Colors.ColorSchemes ["Menu"]; + base.ColorScheme = Colors.ColorSchemes ["Menu"]; Orientation = Orientation.Horizontal; AddCommand (Command.Context, diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index fb5806f11a..2b61f56c2b 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -551,13 +551,9 @@ private void ConfigureMenu (Bar bar) var line = new Line () { - BorderStyle = LineStyle.Dotted, - Orientation = Orientation.Horizontal, - CanFocus = false, + X = -1, + Width = Dim.Fill() + 1 }; - // HACK: Bug in Line - line.Orientation = Orientation.Vertical; - line.Orientation = Orientation.Horizontal; var shortcut4 = new Shortcut { From 15d9dfd3af8afe611d1f8ce52b108558a234cd1c Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 24 Nov 2024 15:30:24 -0700 Subject: [PATCH 44/75] WIP --- Terminal.Gui/Application/Application.Mouse.cs | 2 +- Terminal.Gui/View/View.Drawing.cs | 15 ++++++++------- Terminal.Gui/View/View.Hierarchy.cs | 2 +- Terminal.Gui/View/View.Mouse.cs | 2 +- Terminal.Gui/Views/Menu/MenuBarv2.cs | 14 ++++++++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 9507f386b3..aaa95a32cc 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -230,7 +230,7 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent) else { // The mouse was outside any View's Viewport. - Debug.Fail ("this should not happen."); + //Debug.Fail ("this should not happen."); // Debug.Fail ("This should never happen. If it does please file an Issue!!"); diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index c9ef07aae8..f571893a21 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -56,12 +56,6 @@ public void Draw () // We clip to the frame to prevent drawing outside the frame. saved = ClipFrame (); - if (SubViewNeedsDraw) - { - // A Subview may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen. - Border?.SetNeedsDraw (); - Padding?.SetNeedsDraw(); - } DoDrawBorderAndPadding (); SetClip (saved); @@ -78,7 +72,7 @@ public void Draw () DoSetAttribute (); DoClearViewport (); - // Draw the subviews + // Draw the subviews only if needed if (SubViewNeedsDraw) { DoSetAttribute (); @@ -173,6 +167,13 @@ private void DoDrawBorderAndPaddingSubViews () private void DoDrawBorderAndPadding () { + if (SubViewNeedsDraw) + { + // A Subview may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen. + Border?.SetNeedsDraw (); + Padding?.SetNeedsDraw (); + } + if (OnDrawingBorderAndPadding ()) { return; diff --git a/Terminal.Gui/View/View.Hierarchy.cs b/Terminal.Gui/View/View.Hierarchy.cs index 5a0705adae..2e5b41ce2d 100644 --- a/Terminal.Gui/View/View.Hierarchy.cs +++ b/Terminal.Gui/View/View.Hierarchy.cs @@ -26,7 +26,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, public virtual View? SuperView { get => _superView!; - set => throw new NotImplementedException (); + set => _superView = value;// throw new NotImplementedException (); } #region AddRemove diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 53f5a8b362..5db289f3d7 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -660,7 +660,7 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (Application.Popover is { Visible: true }) { - viewsUnderMouse= GetViewsUnderMouse (Application.Popover, location); + viewsUnderMouse.AddRange(GetViewsUnderMouse (Application.Popover, location)); } return viewsUnderMouse; diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index d0f76d7a62..3e81994f88 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -28,6 +28,20 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) { if (ctx.Data is Shortcut { TargetView: { } } shortcut) { + //MenuBarv2? clone = MemberwiseClone () as MenuBarv2; + //clone!.SuperView = null; + //clone.Visible = false; + + //Rectangle screen = FrameToScreen (); + //Application.Popover = clone; + //clone.X = screen.X; + //clone.Y = screen.Y; + //clone.Width = Dim.Fill (1); + + //clone.Visible = true; + //clone.SetSubViewNeedsDraw (); + + Rectangle screen = shortcut.FrameToScreen (); Application.Popover = shortcut.TargetView; shortcut.TargetView.X = screen.X; From b735b48a2ba580e7f9325b7550be9bad166a5aa5 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 10 Dec 2024 08:30:54 -0800 Subject: [PATCH 45/75] merge v2_develop --- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 2 +- Terminal.Gui/Views/Menu/MenuBarv2.cs | 2 +- Terminal.Gui/Views/Menu/Menuv2.cs | 2 +- Terminal.Gui/Views/TextField.cs | 6 +++--- Terminal.Gui/Views/TextView.cs | 2 +- UICatalog/Scenarios/Bars.cs | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index 0d77e0de9e..c35c67134e 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -115,7 +115,7 @@ protected override void Dispose (bool disposing) { if (disposing) { - Application.KeyBindings.Remove (Key, this); + Application.KeyBindings.Remove (Key); } base.Dispose (disposing); } diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index 7301ed9081..bf2a95a8d3 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -26,7 +26,7 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) AddCommand (Command.Context, (ctx) => { - if (ctx.Data is Shortcut { TargetView: { } } shortcut) + if (ctx is CommandContext commandContext && commandContext.Binding.Data is Shortcut { TargetView: { } } shortcut) { //MenuBarv2? clone = MemberwiseClone () as MenuBarv2; //clone!.SuperView = null; diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index a716c86085..e49e1cb3bc 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -88,7 +88,7 @@ void ShortcutOnAccepting (object? sender, CommandEventArgs e) /// /// /// - protected bool? RaiseShortcutCommandInvoked (CommandContext ctx) + protected bool? RaiseShortcutCommandInvoked (ICommandContext? ctx) { CommandEventArgs args = new () { Context = ctx }; diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 17ca8f49ec..7852784f81 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1243,8 +1243,8 @@ private void CreateContextMenu () new (this, Command.Redo, Strings.ctxRedo), }); - KeyBindings.Remove (menu.Key); - KeyBindings.Add (menu.Key, KeyBindingScope.HotKey, Command.Context); + HotKeyBindings.Remove (menu.Key); + HotKeyBindings.Add (menu.Key, Command.Context); menu.KeyChanged += ContextMenu_KeyChanged; ContextMenu = menu; @@ -1252,7 +1252,7 @@ private void CreateContextMenu () private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { - KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode); + KeyBindings.Replace (e.OldKey.KeyCode, e.NewKey.KeyCode); } private List DeleteSelectedText () diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 751e1c7f16..8d98a3cdff 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -4277,7 +4277,7 @@ private void ClearSelectedRegion () DoNeededAction (); } - private void ContextMenu_KeyChanged (object? sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey, e.NewKey); } + private void ContextMenu_KeyChanged (object? sender, KeyChangedEventArgs e) { KeyBindings.Replace (e.OldKey, e.NewKey); } private bool DeleteTextBackwards () { diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index b99fc6869d..be93c14775 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -290,7 +290,7 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) { menuv2.ShortcutCommandInvoked += (o, args) => { - if (args.Context.Data is Shortcut { } sc) + if (args.Context is CommandContext { Binding.Data: Shortcut { } sc }) { eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); } From 9f7d358f4743a800f8583f44adf0b94fb13deda5 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 10 Dec 2024 08:38:36 -0800 Subject: [PATCH 46/75] CM2 sorta works --- Terminal.Gui/Application/Application.Run.cs | 37 ++++++++++++++++++--- Terminal.Gui/Views/TextField.cs | 3 +- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 9e243be870..c5dc161fb6 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -507,15 +507,11 @@ public static void LayoutAndDraw (bool forceDraw = false) if (Popover is { Visible: true }) { - tops.Insert(0, Popover); + tops.Insert (0, Popover); } bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size); - View.SetClipToScreen (); - View.Draw (tops, neededLayout || forceDraw); - View.SetClipToScreen (); - if (ClearScreenNextIteration) { forceDraw = true; @@ -526,7 +522,38 @@ public static void LayoutAndDraw (bool forceDraw = false) Driver?.ClearContents (); } + View.SetClipToScreen (); + View.Draw (tops, neededLayout || forceDraw); + View.SetClipToScreen (); + Driver?.Refresh (); + + return; + + //List tops = new (TopLevels); + + //if (Popover is { Visible: true }) + //{ + // tops.Insert(0, Popover); + //} + + //bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size); + + //View.SetClipToScreen (); + //View.Draw (tops, neededLayout || forceDraw); + //View.SetClipToScreen (); + + //if (ClearScreenNextIteration) + //{ + // forceDraw = true; + // ClearScreenNextIteration = false; + //} + //if (forceDraw) + //{ + // Driver?.ClearContents (); + //} + + //Driver?.Refresh (); } /// This event is raised on each iteration of the main loop. diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 7852784f81..b38959832a 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -399,11 +399,10 @@ public TextField () KeyBindings.Remove (Key.Space); - KeyBindings.Add (ContextMenu.Key, Command.Context); - _currentCulture = Thread.CurrentThread.CurrentUICulture; CreateContextMenu (); + KeyBindings.Add (ContextMenu.Key, Command.Context); } /// From e0014fae7cd1cddcfa6a1e8cdcadd6cf1b0f6474 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 10 Dec 2024 08:54:22 -0800 Subject: [PATCH 47/75] Enabled Bar subclasses to have IDesignable --- Terminal.Gui/Views/Bar.cs | 2 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 44 +++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 05bcc11014..40bc2411b9 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -296,7 +296,7 @@ private void LayoutBarItems (Size contentSize) } /// - public bool EnableForDesign () + public virtual bool EnableForDesign () { var shortcut = new Shortcut { diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index c35c67134e..d30db67cda 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -22,7 +22,7 @@ namespace Terminal.Gui; /// /// The menu will be displayed at the current mouse coordinates. /// -public class ContextMenuv2 : Menuv2 +public class ContextMenuv2 : Menuv2, IDesignable { private Key _key = DefaultKey; @@ -119,4 +119,46 @@ protected override void Dispose (bool disposing) } base.Dispose (disposing); } + + + /// + public override bool EnableForDesign () + { + var shortcut = new Shortcut + { + Text = "Quit", + Title = "Q_uit", + Key = Key.Z.WithCtrl, + }; + + Add (shortcut); + + shortcut = new Shortcut + { + Text = "Help Text", + Title = "Help", + Key = Key.F1, + }; + + Add (shortcut); + + shortcut = new Shortcut + { + Text = "Czech", + CommandView = new CheckBox () + { + Title = "_Check" + }, + Key = Key.F9, + CanFocus = false + }; + + Add (shortcut); + + // HACK: This enables All Views Tester to show the CM if DefaultKey is pressed + AddCommand (Command.Context, () => Visible = true); + HotKeyBindings.Add (DefaultKey, Command.Context); + + return true; + } } From 3880cc72c9907b5a18fc10abaedd416a73606d13 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 14 Dec 2024 09:01:29 -0700 Subject: [PATCH 48/75] Added ViewportSettings.Transparent --- .../Application/Application.Keyboard.cs | 18 +- Terminal.Gui/Application/Application.Mouse.cs | 6 +- .../Application/Application.Popover.cs | 129 ++-- Terminal.Gui/Application/Application.Run.cs | 42 +- Terminal.Gui/Application/Application.cs | 9 +- .../Application/ApplicationNavigation.cs | 8 +- Terminal.Gui/Drawing/Thickness.cs | 13 + Terminal.Gui/View/View.Arrangement.cs | 11 +- Terminal.Gui/View/View.Drawing.Clipping.cs | 4 +- Terminal.Gui/View/View.Drawing.Primitives.cs | 4 +- Terminal.Gui/View/View.Drawing.cs | 84 ++- Terminal.Gui/View/View.Mouse.cs | 5 +- Terminal.Gui/View/View.Navigation.cs | 2 +- Terminal.Gui/View/ViewArrangement.cs | 7 +- Terminal.Gui/View/ViewportSettings.cs | 8 +- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 4 +- Terminal.Gui/Views/Menu/MenuBarv2.cs | 8 +- Terminal.Gui/Views/TextField.cs | 2 +- Terminal.Gui/Views/TextView.cs | 2 +- UICatalog/Scenarios/Arrangement.cs | 28 + UICatalog/Scenarios/Bars.cs | 11 +- UICatalog/Scenarios/ContextMenus.cs | 10 +- UICatalog/Scenarios/ViewExperiments.cs | 10 +- UICatalog/UICatalog.cs | 4 +- .../Application/ApplicationPopoverTests.cs | 698 +++++++++--------- UnitTests/Application/ApplicationTests.cs | 2 +- UnitTests/View/Draw/DrawTests.cs | 4 +- UnitTests/View/ViewTests.cs | 4 +- 28 files changed, 635 insertions(+), 502 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 635c334705..de8864df2a 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -22,10 +22,10 @@ public static bool RaiseKeyDownEvent (Key key) View? top = Top; - if (Popover is { Visible: true }) - { - top = Popover; - } + //if (Popover is { Visible: true }) + //{ + // top = Popover; + //} if (top is null) { @@ -179,12 +179,12 @@ internal static void AddKeyBindings () Command.Quit, static () => { - if (Popover is {Visible: true}) - { - Popover.Visible = false; + //if (Popover is {Visible: true}) + //{ + // Popover.Visible = false; - return true; - } + // return true; + //} RequestStop (); return true; diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index e226362e90..863c3be1a0 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -167,14 +167,14 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent) } // Dismiss the Popover if the user clicked outside of it - if (Popover is { Visible: true } - && View.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true) is false + if (PopoverHost is { Visible: true } + && View.IsInHierarchy (PopoverHost, deepestViewUnderMouse, includeAdornments: true) is false && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed) || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed))) { - Popover.Visible = false; + PopoverHost.Visible = false; // Recurse once RaiseMouseEvent (mouseEvent); diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index b00c52c974..be6312f25f 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -1,9 +1,11 @@ #nullable enable +using static Unix.Terminal.Curses; + namespace Terminal.Gui; public static partial class Application // Popover handling { - private static View? _popover; + private static PopoverHost? _popoverHost; /// Gets or sets the Application Popover View. /// @@ -11,82 +13,117 @@ public static partial class Application // Popover handling /// To show or hide the Popover, set it's property. /// /// - public static View? Popover + public static PopoverHost? PopoverHost { - get => _popover; - set + get { - if (_popover == value) - { - return; - } - - if (_popover is { }) + if (_popoverHost is null) { - _popover.Visible = false; - _popover.VisibleChanging -= PopoverVisibleChanging; - } - - _popover = value; - - if (_popover is { }) - { - if (!_popover.IsInitialized) - { - _popover.BeginInit (); - _popover.EndInit (); - } - - _popover.Arrangement |= ViewArrangement.Overlapped; - - if (_popover.ColorScheme is null) - { - _popover.ColorScheme = Top?.ColorScheme; - } - - _popover.SetRelativeLayout (Screen.Size); - - _popover.VisibleChanging += PopoverVisibleChanging; + _popoverHost = new PopoverHost (); } + return _popoverHost; } + internal set => _popoverHost = value; + //{ + // if (_popoverHost == value) + // { + // return; + // } + + // if (_popoverHost is { }) + // { + // _popoverHost.Visible = false; + // _popoverHost.VisibleChanging -= PopoverVisibleChanging; + // } + + // _popoverHost = value; + + // if (_popoverHost is { }) + // { + // if (!_popoverHost.IsInitialized) + // { + // _popoverHost.BeginInit (); + // _popoverHost.EndInit (); + // } + + // _popoverHost.Arrangement |= ViewArrangement.Overlapped; + + // if (_popoverHost.ColorScheme is null) + // { + // _popoverHost.ColorScheme = Top?.ColorScheme; + // } + + // _popoverHost.SetRelativeLayout (Screen.Size); + + // _popoverHost.VisibleChanging += PopoverVisibleChanging; + // } + //} } private static void PopoverVisibleChanging (object? sender, CancelEventArgs e) { - if (Popover is null) + if (PopoverHost is null) { return; } if (e.NewValue) { - Popover.Arrangement |= ViewArrangement.Overlapped; + PopoverHost.Arrangement |= ViewArrangement.Overlapped; - Popover.ColorScheme ??= Top?.ColorScheme; + PopoverHost.ColorScheme ??= Top?.ColorScheme; - if (Popover.NeedsLayout) + if (PopoverHost.NeedsLayout) { - Popover.SetRelativeLayout (Screen.Size); + PopoverHost.SetRelativeLayout (Screen.Size); } View.GetLocationEnsuringFullVisibility ( - Popover, - Popover.Frame.X, - Popover.Frame.Y, + PopoverHost, + PopoverHost.Frame.X, + PopoverHost.Frame.Y, out int nx, out int ny); - Popover.X = nx; - Popover.Y = ny; + PopoverHost.X = nx; + PopoverHost.Y = ny; - Popover.SetRelativeLayout (Screen.Size); + PopoverHost.SetRelativeLayout (Screen.Size); if (Top is { }) { Top.HasFocus = false; } - Popover?.SetFocus (); + PopoverHost?.SetFocus (); } } } + +public class PopoverHost : View +{ + public PopoverHost() + { + Id = "popoverHost"; + Width = Dim.Fill (); + Height = Dim.Fill (); + Visible = false; + } + + /// + protected override bool OnClearingViewport () { return true; } + + /// + protected override bool OnVisibleChanging () + { + if (!Visible) + { + ColorScheme ??= Application.Top?.ColorScheme; + Frame = Application.Screen; + + SetRelativeLayout (Application.Screen.Size); + } + + return false; + } +} diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index c5dc161fb6..41b1e32b69 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -505,9 +505,9 @@ public static void LayoutAndDraw (bool forceDraw = false) { List tops = new (TopLevels); - if (Popover is { Visible: true }) + if (PopoverHost is { Visible: true }) { - tops.Insert (0, Popover); + tops.Insert (0, PopoverHost); } bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size); @@ -527,33 +527,6 @@ public static void LayoutAndDraw (bool forceDraw = false) View.SetClipToScreen (); Driver?.Refresh (); - - return; - - //List tops = new (TopLevels); - - //if (Popover is { Visible: true }) - //{ - // tops.Insert(0, Popover); - //} - - //bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size); - - //View.SetClipToScreen (); - //View.Draw (tops, neededLayout || forceDraw); - //View.SetClipToScreen (); - - //if (ClearScreenNextIteration) - //{ - // forceDraw = true; - // ClearScreenNextIteration = false; - //} - //if (forceDraw) - //{ - // Driver?.ClearContents (); - //} - - //Driver?.Refresh (); } /// This event is raised on each iteration of the main loop. @@ -687,6 +660,12 @@ public static void End (RunState runState) { ArgumentNullException.ThrowIfNull (runState); + if (PopoverHost is { }) + { + PopoverHost?.Dispose (); + PopoverHost = null; + } + runState.Toplevel.OnUnloaded (); // End the RunState.Toplevel @@ -724,11 +703,6 @@ public static void End (RunState runState) _cachedRunStateToplevel = runState.Toplevel; - if (Popover is { }) - { - Popover = null; - } - runState.Toplevel = null; runState.Dispose (); diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index f3ee635772..5b08e7cfb8 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -148,6 +148,13 @@ internal static void ResetState (bool ignoreDisposed = false) t!.Running = false; } + //Popover = null; + if (PopoverHost is { }) + { + PopoverHost.Dispose (); + PopoverHost = null; + } + TopLevels.Clear (); #if DEBUG_IDISPOSABLE @@ -167,8 +174,6 @@ internal static void ResetState (bool ignoreDisposed = false) Top = null; _cachedRunStateToplevel = null; - Popover = null; - // MainLoop stuff MainLoop?.Dispose (); MainLoop = null; diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index c7ad73aff9..5317878f7a 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -108,10 +108,10 @@ internal void SetFocused (View? value) /// public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { - if (Application.Popover is { Visible: true }) - { - return Application.Popover.AdvanceFocus (direction, behavior); - } + //if (Application.Popover is { Visible: true }) + //{ + // return Application.Popover.AdvanceFocus (direction, behavior); + //} return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } } diff --git a/Terminal.Gui/Drawing/Thickness.cs b/Terminal.Gui/Drawing/Thickness.cs index 6aa2088e5a..ed73b3913c 100644 --- a/Terminal.Gui/Drawing/Thickness.cs +++ b/Terminal.Gui/Drawing/Thickness.cs @@ -235,6 +235,19 @@ public Rectangle GetInside (Rectangle rect) return new (x, y, width, height); } + /// + /// Returns a region describing the thickness. + /// + /// The source rectangle + /// + public Region AsRegion (Rectangle rect) + { + Region region = new Region (rect); + region.Exclude (GetInside (rect)); + + return region; + } + /// /// Gets the total width of the left and right sides of the rectangle. Sets the width of the left and right sides /// of the rectangle to half the specified value. diff --git a/Terminal.Gui/View/View.Arrangement.cs b/Terminal.Gui/View/View.Arrangement.cs index e11b1d3894..204082da59 100644 --- a/Terminal.Gui/View/View.Arrangement.cs +++ b/Terminal.Gui/View/View.Arrangement.cs @@ -3,6 +3,8 @@ namespace Terminal.Gui; public partial class View { + private ViewArrangement _arrangement; + /// /// Gets or sets the user actions that are enabled for the arranging this view within it's . /// @@ -11,5 +13,12 @@ public partial class View /// See the View Arrangement Deep Dive for more information: /// /// - public ViewArrangement Arrangement { get; set; } + public ViewArrangement Arrangement + { + get => _arrangement; + set + { + _arrangement = value; + } + } } diff --git a/Terminal.Gui/View/View.Drawing.Clipping.cs b/Terminal.Gui/View/View.Drawing.Clipping.cs index 30f8b703c6..509854f99a 100644 --- a/Terminal.Gui/View/View.Drawing.Clipping.cs +++ b/Terminal.Gui/View/View.Drawing.Clipping.cs @@ -86,7 +86,7 @@ public static void SetClip (Region? region) /// /// The current Clip, which can be then re-applied /// - internal Region? ClipFrame () + internal Region? AddFrameToClip () { if (Driver is null) { @@ -133,7 +133,7 @@ public static void SetClip (Region? region) /// /// The current Clip, which can be then re-applied /// - public Region? ClipViewport () + public Region? AddViewportToClip () { if (Driver is null) { diff --git a/Terminal.Gui/View/View.Drawing.Primitives.cs b/Terminal.Gui/View/View.Drawing.Primitives.cs index a7f1c8522d..153f12e644 100644 --- a/Terminal.Gui/View/View.Drawing.Primitives.cs +++ b/Terminal.Gui/View/View.Drawing.Primitives.cs @@ -137,7 +137,7 @@ public void FillRect (Rectangle rect, Color? color = null) return; } - Region prevClip = ClipViewport (); + Region prevClip = AddViewportToClip (); Rectangle toClear = ViewportToScreen (rect); Attribute prev = SetAttribute (new (color ?? GetNormalColor ().Background)); Driver.FillRect (toClear); @@ -155,7 +155,7 @@ public void FillRect (Rectangle rect, Rune rune) return; } - Region prevClip = ClipViewport (); + Region prevClip = AddViewportToClip (); Rectangle toClear = ViewportToScreen (rect); Driver.FillRect (toClear, rune); SetClip (prevClip); diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index adea35c36a..1add0b57cb 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -1,5 +1,6 @@ #nullable enable using System.ComponentModel; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Terminal.Gui; @@ -47,27 +48,36 @@ public void Draw () return; } - Region? saved = GetClip (); + Region? originalClip = GetClip (); + + Region? contentClip = null; // TODO: This can be further optimized by checking NeedsDraw below and only clearing, drawing text, drawing content, etc. if it is true. if (NeedsDraw || SubViewNeedsDraw) { // Draw the Border and Padding. - // We clip to the frame to prevent drawing outside the frame. - saved = ClipFrame (); - + if (this is Adornment) + { + AddFrameToClip (); + } + else + { + // Set the clip to be just the thicknesses + Region? clipAdornments = Border!.Thickness.AsRegion (Border!.FrameToScreen ()); + clipAdornments?.Union (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ())); + clipAdornments?.Intersect (originalClip); + SetClip (clipAdornments); + } DoDrawBorderAndPadding (); - SetClip (saved); + SetClip (originalClip); - // Draw the content within the Viewport + // Clear the Viewport // By default, we clip to the viewport preventing drawing outside the viewport // We also clip to the content, but if a developer wants to draw outside the viewport, they can do // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag. // Get our Viewport in screen coordinates + originalClip = AddViewportToClip (); - saved = ClipViewport (); - - // Clear the viewport // TODO: Simplify/optimize SetAttribute system. DoSetAttribute (); DoClearViewport (); @@ -87,11 +97,28 @@ public void Draw () DoSetAttribute (); DoDrawContent (); + // TODO: This flag may not be needed. Just returning true from OnClearViewport may be sufficient. + if (ViewportSettings.HasFlag (ViewportSettings.Transparent) && _subviews is { Count: > 0 }) + { + contentClip = new Region (); + + contentClip.Union(ViewportToScreen (new Rectangle(Viewport.Location, TextFormatter.FormatAndGetSize()))); + // TODO: Move this into DrawSubviews somehow + foreach (View view in _subviews?.Where (view => view.Visible).Reverse ()) + { + contentClip.Union (view.FrameToScreen ()); + } + } + else + { + contentClip = new (ViewportToScreen (new Rectangle (Point.Empty, Viewport.Size))); + } + // Restore the clip before rendering the line canvas and adornment subviews // because they may draw outside the viewport. - SetClip (saved); + SetClip (originalClip); - saved = ClipFrame (); + originalClip = AddFrameToClip (); // Draw the line canvas DoRenderLineCanvas (); @@ -113,9 +140,6 @@ public void Draw () // We're done drawing DoDrawComplete (); - // QUESTION: Should this go before DoDrawComplete? What is more correct? - SetClip (saved); - // Exclude this view (not including Margin) from the Clip if (this is not Adornment) { @@ -126,7 +150,25 @@ public void Draw () borderFrame = Border.FrameToScreen (); } - ExcludeFromClip (borderFrame); + // TODO: This flag may not be needed. Just returning true from OnClearViewport may be sufficient. + if (ViewportSettings.HasFlag (ViewportSettings.Transparent) && contentClip is { }) + { + Region? saved = originalClip.Clone (); + + saved.Exclude (Border!.Thickness.AsRegion (Border!.FrameToScreen ())); + saved.Exclude (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ())); + saved.Exclude (contentClip); + SetClip (saved); + } + else + { + SetClip (originalClip); + ExcludeFromClip (borderFrame); + } + } + else + { + SetClip (originalClip); } } @@ -144,10 +186,10 @@ private void DoDrawBorderAndPaddingSubViews () subview.SetNeedsDraw (); } - LineCanvas.Exclude (new (subview.FrameToScreen())); + LineCanvas.Exclude (new (subview.FrameToScreen ())); } - Region? saved = Border?.ClipFrame (); + Region? saved = Border?.AddFrameToClip (); Border?.DoDrawSubviews (); SetClip (saved); } @@ -159,7 +201,7 @@ private void DoDrawBorderAndPaddingSubViews () subview.SetNeedsDraw (); } - Region? saved = Padding?.ClipFrame (); + Region? saved = Padding?.AddFrameToClip (); Padding?.DoDrawSubviews (); SetClip (saved); } @@ -170,6 +212,7 @@ private void DoDrawBorderAndPadding () if (Margin?.NeedsLayout == true) { Margin.NeedsLayout = false; + // BUGBUG: This should not use ClearFrame as that clears the insides too Margin?.ClearFrame (); Margin?.Parent?.SetSubViewNeedsDraw (); } @@ -293,6 +336,11 @@ public void SetNormalAttribute () private void DoClearViewport () { + if (ViewportSettings.HasFlag (ViewportSettings.Transparent)) + { + return; + } + if (OnClearingViewport ()) { return; diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 840600d493..d1e9ca1ec6 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -770,9 +770,10 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) viewsUnderMouse = GetViewsUnderMouse (Application.Top, location); - if (Application.Popover is { Visible: true }) + if (Application.PopoverHost is { Visible: true }) { - viewsUnderMouse.AddRange(GetViewsUnderMouse (Application.Popover, location)); + viewsUnderMouse.AddRange (GetViewsUnderMouse (Application.PopoverHost, location)); + viewsUnderMouse.Remove (Application.PopoverHost); } return viewsUnderMouse; diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index cd5103bc02..a8933166dd 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -840,7 +840,7 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false if (appFocused is { } || appFocused == this) { - Application.Navigation.SetFocused (newFocusedView ?? superViewOrParent ?? Application.Popover); + Application.Navigation.SetFocused (newFocusedView ?? superViewOrParent/* ?? Application.Popover*/); } } diff --git a/Terminal.Gui/View/ViewArrangement.cs b/Terminal.Gui/View/ViewArrangement.cs index b4843f7dd5..a1d4d2de96 100644 --- a/Terminal.Gui/View/ViewArrangement.cs +++ b/Terminal.Gui/View/ViewArrangement.cs @@ -70,5 +70,10 @@ public enum ViewArrangement /// Use Ctrl-Tab (Ctrl-PageDown) / Ctrl-Shift-Tab (Ctrl-PageUp) to move between overlapped views. /// /// - Overlapped = 32 + Overlapped = 32, + + /// + /// The view overlaps other views and prevents any subviews without this flag set to gain input. + /// + Popover = 64, } diff --git a/Terminal.Gui/View/ViewportSettings.cs b/Terminal.Gui/View/ViewportSettings.cs index db2c23f4fe..8b4c43031f 100644 --- a/Terminal.Gui/View/ViewportSettings.cs +++ b/Terminal.Gui/View/ViewportSettings.cs @@ -138,5 +138,11 @@ public enum ViewportSettings /// must be set for this setting to work (clipping beyond the visible area must be /// disabled). /// - ClearContentOnly = 128 + ClearContentOnly = 128, + + /// + /// If set, any will not be cleared when the View is drawn and the clip region + /// will be set to clip the View's and . + /// + Transparent = 256, } diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index d30db67cda..2d667046cd 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -7,7 +7,7 @@ namespace Terminal.Gui; /// /// ContextMenuv2 provides a Popover menu that can be positioned anywhere within a . /// -/// To show the ContextMenu, set to the ContextMenu object and set +/// To show the ContextMenu, set to the ContextMenu object and set /// property to . /// /// @@ -47,7 +47,7 @@ public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) { return false; } - Application.Popover = this; + //Application.Popover = this; SetPosition (Application.GetLastMousePosition ()); Visible = !Visible; diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index bf2a95a8d3..1a267371c4 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -43,7 +43,7 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) Rectangle screen = shortcut.FrameToScreen (); - Application.Popover = shortcut.TargetView; + //Application.Popover = shortcut.TargetView; shortcut.TargetView.X = screen.X; shortcut.TargetView.Y = screen.Y + screen.Height; shortcut.TargetView.Visible = true; @@ -60,10 +60,10 @@ protected override bool OnHighlight (CancelEventArgs args) { if (args.NewValue.HasFlag (HighlightStyle.Hover)) { - if (Application.Popover is { Visible: true } && View.IsInHierarchy (this, Application.Popover)) - { + //if (Application.Popover is { Visible: true } && View.IsInHierarchy (this, Application.Popover)) + //{ - } + //} } return base.OnHighlight (args); } diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index b38959832a..0ac1806f8a 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1795,7 +1795,7 @@ private void ShowContextMenu (bool keyboard) ContextMenu!.X = loc.X; ContextMenu!.Y = loc.Y; } - Application.Popover = ContextMenu; + //Application.Popover = ContextMenu; ContextMenu!.Visible = true; } diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 8d98a3cdff..a1f495d6ce 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -6347,7 +6347,7 @@ private void ShowContextMenu (bool keyboard) ContextMenu!.Y = loc.Y; } - Application.Popover = ContextMenu; + //Application.Popover = ContextMenu; ContextMenu!.Visible = true; } diff --git a/UICatalog/Scenarios/Arrangement.cs b/UICatalog/Scenarios/Arrangement.cs index 33ac9ee84c..df768a49a7 100644 --- a/UICatalog/Scenarios/Arrangement.cs +++ b/UICatalog/Scenarios/Arrangement.cs @@ -185,6 +185,9 @@ public override void Main () }; testFrame.Add (datePicker); + + testFrame.Add (new TransparentView ()); + adornmentsEditor.AutoSelectSuperView = testFrame; arrangementEditor.AutoSelectSuperView = testFrame; @@ -298,4 +301,29 @@ public override List GetDemoKeyStrokes () return keys; } + + public class TransparentView : FrameView + { + public TransparentView() + { + Title = "Transparent"; + Text = "Text"; + X = 0; + Y = 0; + Width = 30; + Height = 10; + Arrangement = ViewArrangement.Overlapped | ViewArrangement.Resizable | ViewArrangement.Movable; + ViewportSettings |= Terminal.Gui.ViewportSettings.Transparent; + + Padding!.Thickness = new Thickness (1); + + Add ( + new Button () + { + Title = "_Hi", + X = Pos.Center (), + Y = Pos.Center () + }); + } + } } diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index be93c14775..9eecc8f6bb 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -27,9 +27,13 @@ public override void Main () _popoverMenu = new Menuv2 { Id = "popoverMenu", + Arrangement = ViewArrangement.Popover }; + Application.PopoverHost.Add (_popoverMenu); + Application.Run (app); + Application.PopoverHost.Remove(_popoverMenu); _popoverMenu.Dispose (); app.Dispose (); Application.Shutdown (); @@ -213,11 +217,14 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) { if (e.Flags.HasFlag (MouseFlags.Button3Clicked)) { - Application.Popover = _popoverMenu; + //Application.Popover = _popoverMenu; + _popoverMenu.Arrangement = ViewArrangement.Overlapped; _popoverMenu.X = e.ScreenPosition.X; _popoverMenu.Y = e.ScreenPosition.Y; _popoverMenu.Visible = true; + + Application.PopoverHost.Visible = true; } } @@ -503,7 +510,7 @@ private void ConfigMenuBar (Bar bar) editMenuBarItem.Accepting += (sender, args) => { - Application.Popover = editMenu; + //Application.Popover = editMenu; Rectangle screen = editMenuBarItem.FrameToScreen (); editMenu.X = screen.X; editMenu.Y = screen.Y + screen.Height; diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 25ff3b30c7..4b643af934 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -153,10 +153,10 @@ private void CreateWinContextMenu () { if (_winContextMenu is { }) { - if (Application.Popover == _winContextMenu) - { - Application.Popover = null; - } + //if (Application.Popover == _winContextMenu) + //{ + // Application.Popover = null; + //} _winContextMenu.Dispose (); _winContextMenu = null; @@ -175,7 +175,7 @@ private void CreateWinContextMenu () private void ShowWinContextMenu (Point? screenPosition) { _winContextMenu!.SetPosition (screenPosition); - Application.Popover = _winContextMenu; + //Application.Popover = _winContextMenu; _winContextMenu.Visible = true; } diff --git a/UICatalog/Scenarios/ViewExperiments.cs b/UICatalog/Scenarios/ViewExperiments.cs index 1d7d156932..c510195725 100644 --- a/UICatalog/Scenarios/ViewExperiments.cs +++ b/UICatalog/Scenarios/ViewExperiments.cs @@ -76,15 +76,15 @@ public override void Main () Y = Pos.Center (), Title = $"_Close", }; - popoverButton.Accepting += (sender, e) => Application.Popover!.Visible = false; + //popoverButton.Accepting += (sender, e) => Application.Popover!.Visible = false; popoverView.Add (popoverButton); button.Accepting += ButtonAccepting; void ButtonAccepting (object sender, CommandEventArgs e) { - Application.Popover = popoverView; - Application.Popover!.Visible = true; + //Application.Popover = popoverView; + //Application.Popover!.Visible = true; } testFrame.MouseClick += TestFrameOnMouseClick; @@ -95,8 +95,8 @@ void TestFrameOnMouseClick (object sender, MouseEventArgs e) { popoverView.X = e.ScreenPosition.X; popoverView.Y = e.ScreenPosition.Y; - Application.Popover = popoverView; - Application.Popover!.Visible = true; + //Application.Popover = popoverView; + //Application.Popover!.Visible = true; } } diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index ff453b3743..17bbc23bc0 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -615,7 +615,7 @@ private static void VerifyObjectsWereDisposed () { #if DEBUG_IDISPOSABLE - // Validate there are no outstanding Responder-based instances + // Validate there are no outstanding View instances // after a scenario was selected to run. This proves the main UI Catalog // 'app' closed cleanly. foreach (View? inst in View.Instances) @@ -796,7 +796,7 @@ public UICatalogTopLevel () { if (_statusBar.NeedsLayout) { - throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout."); + throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout."); //_statusBar.Layout (); } return _statusBar.Frame.Height; diff --git a/UnitTests/Application/ApplicationPopoverTests.cs b/UnitTests/Application/ApplicationPopoverTests.cs index d0b412bf51..ff1738cca3 100644 --- a/UnitTests/Application/ApplicationPopoverTests.cs +++ b/UnitTests/Application/ApplicationPopoverTests.cs @@ -9,353 +9,353 @@ namespace Terminal.Gui.ApplicationTests; public class ApplicationPopoverTests { - [Fact] - public void Popover_SetAndGet () - { - // Arrange - var popover = new View (); - - // Act - Application.Popover = popover; - - // Assert - Assert.Equal (popover, Application.Popover); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetToNull () - { - // Arrange - var popover = new View (); - Application.Popover = popover; - - // Act - Application.Popover = null; - - // Assert - Assert.Null (Application.Popover); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_VisibleChangedEvent () - { - // Arrange - var popover = new View () - { - Visible = false - }; - Application.Popover = popover; - bool eventTriggered = false; - - popover.VisibleChanged += (sender, e) => eventTriggered = true; - - // Act - popover.Visible = true; - - // Assert - Assert.True (eventTriggered); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_InitializesCorrectly () - { - // Arrange - var popover = new View (); - - // Act - Application.Popover = popover; - - // Assert - Assert.True (popover.IsInitialized); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetsColorScheme () - { - // Arrange - var popover = new View (); - var topColorScheme = new ColorScheme (); - Application.Top = new Toplevel { ColorScheme = topColorScheme }; - - // Act - Application.Popover = popover; - - // Assert - Assert.Equal (topColorScheme, popover.ColorScheme); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_VisibleChangedToTrue_SetsFocus () - { - // Arrange - var popover = new View () - { - Visible = false, - CanFocus = true - }; - Application.Popover = popover; - - // Act - popover.Visible = true; - - // Assert - Assert.True (popover.Visible); - Assert.True (popover.HasFocus); - - Application.ResetState (ignoreDisposed: true); - } - - [Theory] - [InlineData(-1, -1)] - [InlineData (0, 0)] - [InlineData (2048, 2048)] - [InlineData (2049, 2049)] - public void Popover_VisibleChangedToTrue_Locates_In_Visible_Position (int x, int y) - { - // Arrange - var popover = new View () - { - X = x, - Y = y, - Visible = false, - CanFocus = true, - Width = 1, - Height = 1 - }; - Application.Popover = popover; - - // Act - popover.Visible = true; - Application.LayoutAndDraw(); - - // Assert - Assert.True (Application.Screen.Contains (popover.Frame)); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_VisibleChangedToFalse_Hides_And_Removes_Focus () - { - // Arrange - var popover = new View () - { - Visible = false, - CanFocus = true - }; - Application.Popover = popover; - popover.Visible = true; - - // Act - popover.Visible = false; - - // Assert - Assert.False (popover.Visible); - Assert.False (popover.HasFocus); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_Quit_Command_Hides () - { - // Arrange - var popover = new View () - { - Visible = false, - CanFocus = true - }; - Application.Popover = popover; - popover.Visible = true; - Assert.True (popover.Visible); - Assert.True (popover.HasFocus); - - // Act - Application.RaiseKeyDownEvent (Application.QuitKey); - - // Assert - Assert.False (popover.Visible); - Assert.False (popover.HasFocus); - - Application.ResetState (ignoreDisposed: true); - } - - - [Fact] - public void Popover_MouseClick_Outside_Hides_Passes_Event_On () - { - // Arrange - Application.Top = new Toplevel () - { - Id = "top", - Height = 10, - Width = 10, - }; - - View otherView = new () - { - X = 1, - Y = 1, - Height = 1, - Width = 1, - Id = "otherView", - }; - - bool otherViewPressed = false; - otherView.MouseEvent += (sender, e) => - { - otherViewPressed = e.Flags.HasFlag(MouseFlags.Button1Pressed); - }; - - Application.Top.Add (otherView); - - var popover = new View () - { - Id = "popover", - X = 5, - Y = 5, - Width = 1, - Height = 1, - Visible = false, - CanFocus = true - }; - - Application.Popover = popover; - popover.Visible = true; - Assert.True (popover.Visible); - Assert.True (popover.HasFocus); - - // Act - // Click on popover - Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (5, 5) }); - Assert.True (popover.Visible); - - // Click outside popover (on button) - Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (1, 1) }); - - // Assert - Assert.True (otherViewPressed); - Assert.False (popover.Visible); - - Application.Top.Dispose (); - Application.ResetState (ignoreDisposed: true); - } - - [Theory] - [InlineData (0, 0, false)] - [InlineData (5, 5, true)] - [InlineData (10, 10, false)] - [InlineData (5, 10, false)] - [InlineData (9, 9, false)] - public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) - { - // Arrange - Application.Top = new Toplevel () - { - Id = "top", - Height = 10, - Width = 10, - }; - var popover = new View () - { - Id = "popover", - X = 5, - Y = 5, - Width = 1, - Height = 1, - Visible = false, - CanFocus = true - }; - - Application.Popover = popover; - popover.Visible = true; - Assert.True (popover.Visible); - Assert.True (popover.HasFocus); - - // Act - Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (mouseX, mouseY) }); - - // Assert - Assert.Equal (expectedVisible, popover.Visible); - - Application.Top.Dispose (); - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetAndGet_ReturnsCorrectValue () - { - // Arrange - var view = new View (); - - // Act - Application.Popover = view; - - // Assert - Assert.Equal (view, Application.Popover); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetToNull_HidesPreviousPopover () - { - // Arrange - var view = new View { Visible = true }; - Application.Popover = view; - - // Act - Application.Popover = null; - - // Assert - Assert.False (view.Visible); - Assert.Null (Application.Popover); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetNewPopover_HidesPreviousPopover () - { - // Arrange - var oldView = new View { Visible = true }; - var newView = new View (); - Application.Popover = oldView; - - // Act - Application.Popover = newView; - - // Assert - Assert.False (oldView.Visible); - Assert.Equal (newView, Application.Popover); - - Application.ResetState (ignoreDisposed: true); - } - - [Fact] - public void Popover_SetNewPopover_InitializesAndSetsProperties () - { - // Arrange - var view = new View (); - - // Act - Application.Popover = view; - - // Assert - Assert.True (view.IsInitialized); - Assert.True (view.Arrangement.HasFlag (ViewArrangement.Overlapped)); - Assert.Equal (Application.Top?.ColorScheme, view.ColorScheme); - - Application.ResetState (ignoreDisposed: true); - } + //[Fact] + //public void Popover_SetAndGet () + //{ + // // Arrange + // var popover = new View (); + + // // Act + // Application.PopoverHost = popover; + + // // Assert + // Assert.Equal (popover, Application.PopoverHost); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetToNull () + //{ + // // Arrange + // var popover = new View (); + // Application.PopoverHost = popover; + + // // Act + // Application.PopoverHost = null; + + // // Assert + // Assert.Null (Application.PopoverHost); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_VisibleChangedEvent () + //{ + // // Arrange + // var popover = new View () + // { + // Visible = false + // }; + // Application.PopoverHost = popover; + // bool eventTriggered = false; + + // popover.VisibleChanged += (sender, e) => eventTriggered = true; + + // // Act + // popover.Visible = true; + + // // Assert + // Assert.True (eventTriggered); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_InitializesCorrectly () + //{ + // // Arrange + // var popover = new View (); + + // // Act + // Application.PopoverHost = popover; + + // // Assert + // Assert.True (popover.IsInitialized); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetsColorScheme () + //{ + // // Arrange + // var popover = new View (); + // var topColorScheme = new ColorScheme (); + // Application.Top = new Toplevel { ColorScheme = topColorScheme }; + + // // Act + // Application.PopoverHost = popover; + + // // Assert + // Assert.Equal (topColorScheme, popover.ColorScheme); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_VisibleChangedToTrue_SetsFocus () + //{ + // // Arrange + // var popover = new View () + // { + // Visible = false, + // CanFocus = true + // }; + // Application.PopoverHost = popover; + + // // Act + // popover.Visible = true; + + // // Assert + // Assert.True (popover.Visible); + // Assert.True (popover.HasFocus); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Theory] + //[InlineData(-1, -1)] + //[InlineData (0, 0)] + //[InlineData (2048, 2048)] + //[InlineData (2049, 2049)] + //public void Popover_VisibleChangedToTrue_Locates_In_Visible_Position (int x, int y) + //{ + // // Arrange + // var popover = new View () + // { + // X = x, + // Y = y, + // Visible = false, + // CanFocus = true, + // Width = 1, + // Height = 1 + // }; + // Application.PopoverHost = popover; + + // // Act + // popover.Visible = true; + // Application.LayoutAndDraw(); + + // // Assert + // Assert.True (Application.Screen.Contains (popover.Frame)); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_VisibleChangedToFalse_Hides_And_Removes_Focus () + //{ + // // Arrange + // var popover = new View () + // { + // Visible = false, + // CanFocus = true + // }; + // Application.PopoverHost = popover; + // popover.Visible = true; + + // // Act + // popover.Visible = false; + + // // Assert + // Assert.False (popover.Visible); + // Assert.False (popover.HasFocus); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_Quit_Command_Hides () + //{ + // // Arrange + // var popover = new View () + // { + // Visible = false, + // CanFocus = true + // }; + // Application.PopoverHost = popover; + // popover.Visible = true; + // Assert.True (popover.Visible); + // Assert.True (popover.HasFocus); + + // // Act + // Application.RaiseKeyDownEvent (Application.QuitKey); + + // // Assert + // Assert.False (popover.Visible); + // Assert.False (popover.HasFocus); + + // Application.ResetState (ignoreDisposed: true); + //} + + + //[Fact] + //public void Popover_MouseClick_Outside_Hides_Passes_Event_On () + //{ + // // Arrange + // Application.Top = new Toplevel () + // { + // Id = "top", + // Height = 10, + // Width = 10, + // }; + + // View otherView = new () + // { + // X = 1, + // Y = 1, + // Height = 1, + // Width = 1, + // Id = "otherView", + // }; + + // bool otherViewPressed = false; + // otherView.MouseEvent += (sender, e) => + // { + // otherViewPressed = e.Flags.HasFlag(MouseFlags.Button1Pressed); + // }; + + // Application.Top.Add (otherView); + + // var popover = new View () + // { + // Id = "popover", + // X = 5, + // Y = 5, + // Width = 1, + // Height = 1, + // Visible = false, + // CanFocus = true + // }; + + // Application.PopoverHost = popover; + // popover.Visible = true; + // Assert.True (popover.Visible); + // Assert.True (popover.HasFocus); + + // // Act + // // Click on popover + // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (5, 5) }); + // Assert.True (popover.Visible); + + // // Click outside popover (on button) + // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (1, 1) }); + + // // Assert + // Assert.True (otherViewPressed); + // Assert.False (popover.Visible); + + // Application.Top.Dispose (); + // Application.ResetState (ignoreDisposed: true); + //} + + //[Theory] + //[InlineData (0, 0, false)] + //[InlineData (5, 5, true)] + //[InlineData (10, 10, false)] + //[InlineData (5, 10, false)] + //[InlineData (9, 9, false)] + //public void Popover_MouseClick_Outside_Hides (int mouseX, int mouseY, bool expectedVisible) + //{ + // // Arrange + // Application.Top = new Toplevel () + // { + // Id = "top", + // Height = 10, + // Width = 10, + // }; + // var popover = new View () + // { + // Id = "popover", + // X = 5, + // Y = 5, + // Width = 1, + // Height = 1, + // Visible = false, + // CanFocus = true + // }; + + // Application.PopoverHost = popover; + // popover.Visible = true; + // Assert.True (popover.Visible); + // Assert.True (popover.HasFocus); + + // // Act + // Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed, ScreenPosition = new (mouseX, mouseY) }); + + // // Assert + // Assert.Equal (expectedVisible, popover.Visible); + + // Application.Top.Dispose (); + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetAndGet_ReturnsCorrectValue () + //{ + // // Arrange + // var view = new View (); + + // // Act + // Application.PopoverHost = view; + + // // Assert + // Assert.Equal (view, Application.PopoverHost); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetToNull_HidesPreviousPopover () + //{ + // // Arrange + // var view = new View { Visible = true }; + // Application.PopoverHost = view; + + // // Act + // Application.PopoverHost = null; + + // // Assert + // Assert.False (view.Visible); + // Assert.Null (Application.PopoverHost); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetNewPopover_HidesPreviousPopover () + //{ + // // Arrange + // var oldView = new View { Visible = true }; + // var newView = new View (); + // Application.PopoverHost = oldView; + + // // Act + // Application.PopoverHost = newView; + + // // Assert + // Assert.False (oldView.Visible); + // Assert.Equal (newView, Application.PopoverHost); + + // Application.ResetState (ignoreDisposed: true); + //} + + //[Fact] + //public void Popover_SetNewPopover_InitializesAndSetsProperties () + //{ + // // Arrange + // var view = new View (); + + // // Act + // Application.PopoverHost = view; + + // // Assert + // Assert.True (view.IsInitialized); + // Assert.True (view.Arrangement.HasFlag (ViewArrangement.Overlapped)); + // Assert.Equal (Application.Top?.ColorScheme, view.ColorScheme); + + // Application.ResetState (ignoreDisposed: true); + //} } diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 56fc5e12d0..c6f363b855 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -292,7 +292,7 @@ void CheckReset () // Public Properties Assert.Null (Application.Top); - Assert.Null (Application.Popover); + Assert.Null (Application.PopoverHost); Assert.Null (Application.MouseGrabView); Assert.Null (Application.WantContinuousButtonPressedView); diff --git a/UnitTests/View/Draw/DrawTests.cs b/UnitTests/View/Draw/DrawTests.cs index 90d2ecb520..666e5b8adc 100644 --- a/UnitTests/View/Draw/DrawTests.cs +++ b/UnitTests/View/Draw/DrawTests.cs @@ -968,7 +968,7 @@ public void SetClip_ClipVisibleContentOnly_VisibleContentIsClipped () Assert.Equal (view.Frame, View.GetClip ()!.GetBounds ()); // Act - view.ClipViewport (); + view.AddViewportToClip (); // Assert Assert.Equal (expectedClip, View.GetClip ()!.GetBounds ()); @@ -1002,7 +1002,7 @@ public void SetClip_Default_ClipsToViewport () view.Viewport = view.Viewport with { X = 1, Y = 1 }; // Act - view.ClipViewport (); + view.AddViewportToClip (); // Assert Assert.Equal (expectedClip, View.GetClip ()!.GetBounds ()); diff --git a/UnitTests/View/ViewTests.cs b/UnitTests/View/ViewTests.cs index c169013e99..9a76f8e83e 100644 --- a/UnitTests/View/ViewTests.cs +++ b/UnitTests/View/ViewTests.cs @@ -163,7 +163,7 @@ public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods () view.DrawingContent += (s, e) => { - Region savedClip = view.ClipViewport (); + Region savedClip = view.AddViewportToClip (); for (var row = 0; row < view.Viewport.Height; row++) { @@ -226,7 +226,7 @@ public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods () view.DrawingContent += (s, e) => { - Region savedClip = view.ClipViewport (); + Region savedClip = view.AddViewportToClip (); for (var row = 0; row < view.Viewport.Height; row++) { From 4d6913b11b4f198a9ed356e9ba3c11c3e023010c Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 14 Dec 2024 09:17:29 -0700 Subject: [PATCH 49/75] Region -> nullable enable --- Terminal.Gui/Drawing/Region.cs | 79 +++++++++++++---------- Terminal.Gui/View/ViewportSettings.cs | 20 +++--- UnitTests/Application/ApplicationTests.cs | 2 +- 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/Terminal.Gui/Drawing/Region.cs b/Terminal.Gui/Drawing/Region.cs index c3ef1d61b2..abd06dcbae 100644 --- a/Terminal.Gui/Drawing/Region.cs +++ b/Terminal.Gui/Drawing/Region.cs @@ -1,21 +1,25 @@ -/// +#nullable enable + +namespace Terminal.Gui; + +/// /// Represents a region composed of one or more rectangles, providing methods for union, intersection, exclusion, and /// complement operations. /// public class Region : IDisposable { - private List _rectangles; + private List? _rectangles; /// /// Initializes a new instance of the class. /// - public Region () { _rectangles = new (); } + public Region () { _rectangles = []; } /// /// Initializes a new instance of the class with the specified rectangle. /// /// The initial rectangle for the region. - public Region (Rectangle rectangle) { _rectangles = new () { rectangle }; } + public Region (Rectangle rectangle) { _rectangles = [rectangle]; } /// /// Adds the specified rectangle to the region. @@ -23,7 +27,7 @@ public class Region : IDisposable /// The rectangle to add to the region. public void Union (Rectangle rectangle) { - _rectangles.Add (rectangle); + _rectangles!.Add (rectangle); _rectangles = MergeRectangles (_rectangles); } @@ -31,10 +35,13 @@ public void Union (Rectangle rectangle) /// Adds the specified region to this region. /// /// The region to add to this region. - public void Union (Region region) + public void Union (Region? region) { - _rectangles.AddRange (region._rectangles); - _rectangles = MergeRectangles (_rectangles); + if (region is { }) + { + _rectangles!.AddRange (region._rectangles!); + _rectangles = MergeRectangles (_rectangles); + } } /// @@ -43,20 +50,23 @@ public void Union (Region region) /// The rectangle to intersect with the region. public void Intersect (Rectangle rectangle) { - _rectangles = _rectangles.Select (r => Rectangle.Intersect (r, rectangle)).Where (r => !r.IsEmpty).ToList (); + _rectangles = _rectangles!.Select (r => Rectangle.Intersect (r, rectangle)).Where (r => !r.IsEmpty).ToList (); } /// /// Updates the region to be the intersection of itself with the specified region. /// /// The region to intersect with this region. - public void Intersect (Region region) + public void Intersect (Region? region) { - List intersections = new List (); + List intersections = []; + + // Null is same as empty region + region ??= new (); - foreach (Rectangle rect1 in _rectangles) + foreach (Rectangle rect1 in _rectangles!) { - foreach (Rectangle rect2 in region._rectangles) + foreach (Rectangle rect2 in region!._rectangles!) { Rectangle intersected = Rectangle.Intersect (rect1, rect2); @@ -74,17 +84,20 @@ public void Intersect (Region region) /// Removes the specified rectangle from the region. /// /// The rectangle to exclude from the region. - public void Exclude (Rectangle rectangle) { _rectangles = _rectangles.SelectMany (r => SubtractRectangle (r, rectangle)).ToList (); } + public void Exclude (Rectangle rectangle) { _rectangles = _rectangles!.SelectMany (r => SubtractRectangle (r, rectangle)).ToList (); } /// /// Removes the portion of the specified region from this region. /// /// The region to exclude from this region. - public void Exclude (Region region) + public void Exclude (Region? region) { - foreach (Rectangle rect in region._rectangles) + // Null is same as empty region + region ??= new (); + + foreach (Rectangle rect in region._rectangles!) { - _rectangles = _rectangles.SelectMany (r => SubtractRectangle (r, rect)).ToList (); + _rectangles = _rectangles!.SelectMany (r => SubtractRectangle (r, rect)).ToList (); } } @@ -94,14 +107,14 @@ public void Exclude (Region region) /// The bounding rectangle to use for complementing the region. public void Complement (Rectangle bounds) { - if (bounds.IsEmpty || _rectangles.Count == 0) + if (bounds.IsEmpty || _rectangles!.Count == 0) { - _rectangles.Clear (); + _rectangles!.Clear (); return; } - List complementRectangles = new List { bounds }; + List complementRectangles = [bounds]; foreach (Rectangle rect in _rectangles) { @@ -118,7 +131,7 @@ public void Complement (Rectangle bounds) public Region Clone () { var clone = new Region (); - clone._rectangles = new (_rectangles); + clone._rectangles = [.. _rectangles!]; return clone; } @@ -129,7 +142,7 @@ public Region Clone () /// A that bounds the region. public Rectangle GetBounds () { - if (_rectangles.Count == 0) + if (_rectangles!.Count == 0) { return Rectangle.Empty; } @@ -146,7 +159,7 @@ public Rectangle GetBounds () /// Determines whether the region is empty. /// /// true if the region is empty; otherwise, false. - public bool IsEmpty () { return !_rectangles.Any (); } + public bool IsEmpty () { return !_rectangles!.Any (); } /// /// Determines whether the specified point is contained within the region. @@ -154,20 +167,20 @@ public Rectangle GetBounds () /// The x-coordinate of the point. /// The y-coordinate of the point. /// true if the point is contained within the region; otherwise, false. - public bool Contains (int x, int y) { return _rectangles.Any (r => r.Contains (x, y)); } + public bool Contains (int x, int y) { return _rectangles!.Any (r => r.Contains (x, y)); } /// /// Determines whether the specified rectangle is contained within the region. /// /// The rectangle to check for containment. /// true if the rectangle is contained within the region; otherwise, false. - public bool Contains (Rectangle rectangle) { return _rectangles.Any (r => r.Contains (rectangle)); } + public bool Contains (Rectangle rectangle) { return _rectangles!.Any (r => r.Contains (rectangle)); } /// /// Returns an array of rectangles that represent the region. /// /// An array of objects that make up the region. - public Rectangle [] GetRegionScans () { return _rectangles.ToArray (); } + public Rectangle [] GetRegionScans () { return _rectangles!.ToArray (); } /// /// Offsets all rectangles in the region by the specified amounts. @@ -176,10 +189,10 @@ public Rectangle GetBounds () /// The amount to offset along the y-axis. public void Offset (int offsetX, int offsetY) { - for (int i = 0; i < _rectangles.Count; i++) + for (var i = 0; i < _rectangles!.Count; i++) { - var rect = _rectangles [i]; - _rectangles [i] = new Rectangle (rect.Left + offsetX, rect.Top + offsetY, rect.Width, rect.Height); + Rectangle rect = _rectangles [i]; + _rectangles [i] = new (rect.Left + offsetX, rect.Top + offsetY, rect.Width, rect.Height); } } @@ -188,11 +201,11 @@ public void Offset (int offsetX, int offsetY) /// /// The list of rectangles to merge. /// A list of merged rectangles. - private List MergeRectangles (List rectangles) + private static List MergeRectangles (List rectangles) { // Simplified merging logic: this does not handle all edge cases for merging overlapping rectangles. // For a full implementation, a plane sweep algorithm or similar would be needed. - List merged = new List (rectangles); + List merged = [.. rectangles]; bool mergedAny; do @@ -230,7 +243,7 @@ private List MergeRectangles (List rectangles) /// The original rectangle. /// The rectangle to subtract from the original. /// An enumerable collection of resulting rectangles after subtraction. - private IEnumerable SubtractRectangle (Rectangle original, Rectangle subtract) + private static IEnumerable SubtractRectangle (Rectangle original, Rectangle subtract) { if (!original.IntersectsWith (subtract)) { @@ -279,5 +292,5 @@ private IEnumerable SubtractRectangle (Rectangle original, Rectangle /// /// Releases all resources used by the . /// - public void Dispose () { _rectangles.Clear (); } + public void Dispose () { _rectangles!.Clear (); } } diff --git a/Terminal.Gui/View/ViewportSettings.cs b/Terminal.Gui/View/ViewportSettings.cs index 8b4c43031f..c687da7513 100644 --- a/Terminal.Gui/View/ViewportSettings.cs +++ b/Terminal.Gui/View/ViewportSettings.cs @@ -13,7 +13,7 @@ public enum ViewportSettings /// /// No settings. /// - None = 0, + None = 0b_0000, /// /// If set, .X can be set to negative values enabling scrolling beyond the left of @@ -23,7 +23,7 @@ public enum ViewportSettings /// When not set, .X is constrained to positive values. /// /// - AllowNegativeX = 1, + AllowNegativeX = 0b_0001, /// /// If set, .Y can be set to negative values enabling scrolling beyond the top of the @@ -32,7 +32,7 @@ public enum ViewportSettings /// When not set, .Y is constrained to positive values. /// /// - AllowNegativeY = 2, + AllowNegativeY = 0b_0010, /// /// If set, .Size can be set to negative coordinates enabling scrolling beyond the @@ -58,7 +58,7 @@ public enum ViewportSettings /// The practical effect of this is that the last column of the content will always be visible. /// /// - AllowXGreaterThanContentWidth = 4, + AllowXGreaterThanContentWidth = 0b_0100, /// /// If set, .Y can be set values greater than @@ -74,7 +74,7 @@ public enum ViewportSettings /// The practical effect of this is that the last row of the content will always be visible. /// /// - AllowYGreaterThanContentHeight = 8, + AllowYGreaterThanContentHeight = 0b_1000, /// /// If set, .Location can be set values greater than @@ -102,7 +102,7 @@ public enum ViewportSettings /// This can be useful in infinite scrolling scenarios. /// /// - AllowNegativeXWhenWidthGreaterThanContentWidth = 16, + AllowNegativeXWhenWidthGreaterThanContentWidth = 0b_0001_0000, /// /// If set and .Height is greater than @@ -117,7 +117,7 @@ public enum ViewportSettings /// This can be useful in infinite scrolling scenarios. /// /// - AllowNegativeYWhenHeightGreaterThanContentHeight = 32, + AllowNegativeYWhenHeightGreaterThanContentHeight = 0b_0010_0000, /// /// The combination of and @@ -129,7 +129,7 @@ public enum ViewportSettings /// By default, clipping is applied to the . Setting this flag will cause clipping to be /// applied to the visible content area. /// - ClipContentOnly = 64, + ClipContentOnly = 0b_0100_0000, /// /// If set will clear only the portion of the content @@ -138,11 +138,11 @@ public enum ViewportSettings /// must be set for this setting to work (clipping beyond the visible area must be /// disabled). /// - ClearContentOnly = 128, + ClearContentOnly = 0b_1000_0000, /// /// If set, any will not be cleared when the View is drawn and the clip region /// will be set to clip the View's and . /// - Transparent = 256, + Transparent = 0b_0001_0000_0000, } diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index c6f363b855..9d0d4f6b32 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -292,7 +292,7 @@ void CheckReset () // Public Properties Assert.Null (Application.Top); - Assert.Null (Application.PopoverHost); + //Assert.Null (Application.PopoverHost); Assert.Null (Application.MouseGrabView); Assert.Null (Application.WantContinuousButtonPressedView); From b639b2c71e5a80efd38e08fe7ff4c12714c2f4f5 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 14 Dec 2024 10:45:40 -0700 Subject: [PATCH 50/75] Added ViewportSettigs Editor --- Terminal.Gui/View/View.Drawing.cs | 9 +- Terminal.Gui/Views/CharMap/CharMap.cs | 4 +- UICatalog/Scenarios/AllViewsTester.cs | 27 +- .../Editors/ViewportSettingsEditor.cs | 371 ++++++++++++++++++ UICatalog/Scenarios/ViewportSettings.cs | 2 +- 5 files changed, 400 insertions(+), 13 deletions(-) create mode 100644 UICatalog/Scenarios/Editors/ViewportSettingsEditor.cs diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index 1add0b57cb..ee7dccd871 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -62,8 +62,10 @@ public void Draw () } else { - // Set the clip to be just the thicknesses - Region? clipAdornments = Border!.Thickness.AsRegion (Border!.FrameToScreen ()); + // Set the clip to be just the thicknesses of the adornments + // TODO: Put this union logic in a method on View? + Region? clipAdornments = Margin!.Thickness.AsRegion (Margin!.FrameToScreen ()); + clipAdornments?.Union (Border!.Thickness.AsRegion (Border!.FrameToScreen ())); clipAdornments?.Union (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ())); clipAdornments?.Intersect (originalClip); SetClip (clipAdornments); @@ -150,10 +152,9 @@ public void Draw () borderFrame = Border.FrameToScreen (); } - // TODO: This flag may not be needed. Just returning true from OnClearViewport may be sufficient. if (ViewportSettings.HasFlag (ViewportSettings.Transparent) && contentClip is { }) { - Region? saved = originalClip.Clone (); + Region? saved = originalClip!.Clone (); saved.Exclude (Border!.Thickness.AsRegion (Border!.FrameToScreen ())); saved.Exclude (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ())); diff --git a/Terminal.Gui/Views/CharMap/CharMap.cs b/Terminal.Gui/Views/CharMap/CharMap.cs index b49c8c4e6f..a42d39c69e 100644 --- a/Terminal.Gui/Views/CharMap/CharMap.cs +++ b/Terminal.Gui/Views/CharMap/CharMap.cs @@ -97,8 +97,8 @@ public CharMap () }; // Set up the vertical scrollbar. Turn off AutoShow since it's always visible. - VerticalScrollBar.AutoShow = false; - VerticalScrollBar.Visible = true; // Force always visible + VerticalScrollBar.AutoShow = true; + VerticalScrollBar.Visible = false; // Force always visible VerticalScrollBar.X = Pos.AnchorEnd (); VerticalScrollBar.Y = HEADER_HEIGHT; // Header } diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index d982b2b899..c6f9136796 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -18,10 +18,9 @@ public class AllViewsTester : Scenario private Dictionary? _viewClasses; private ListView? _classListView; private AdornmentsEditor? _adornmentsEditor; - private ArrangementEditor? _arrangementEditor; - private LayoutEditor? _layoutEditor; + private ViewportSettingsEditor? _viewportSettingsEditor; private FrameView? _settingsPane; private RadioGroup? _orientation; private string _demoText = "This, that, and the other thing."; @@ -133,13 +132,27 @@ public override void Main () AutoSelectAdornments = false, SuperViewRendersLineCanvas = true }; - _layoutEditor.Border!.Thickness = new (1); + _layoutEditor.Border!.Thickness = new (1, 1, 1, 0); + + _viewportSettingsEditor = new () + { + Title = "ViewportSettings [_5]", + X = Pos.Right (_arrangementEditor) - 1, + Y = Pos.Bottom (_layoutEditor) - Pos.Func (() => _layoutEditor.Frame.Height == 1 ? 0 : 1), + Width = Dim.Width (_layoutEditor), + Height = Dim.Auto (), + CanFocus = true, + AutoSelectViewToEdit = false, + AutoSelectAdornments = false, + SuperViewRendersLineCanvas = true + }; + _viewportSettingsEditor.Border!.Thickness = new (1, 1, 1, 1); _settingsPane = new () { - Title = "Settings [_5]", + Title = "Misc Settings [_6]", X = Pos.Right (_adornmentsEditor) - 1, - Y = Pos.Bottom (_layoutEditor) - Pos.Func (() => _layoutEditor.Frame.Height == 1 ? 0 : 1), + Y = Pos.Bottom (_viewportSettingsEditor) - Pos.Func (() => _viewportSettingsEditor.Frame.Height == 1 ? 0 : 1), Width = Dim.Width (_layoutEditor), Height = Dim.Auto (), CanFocus = true, @@ -237,7 +250,7 @@ public override void Main () _hostPane.Padding.Diagnostics = ViewDiagnosticFlags.Ruler; _hostPane.Padding.ColorScheme = app.ColorScheme; - app.Add (_classListView, _adornmentsEditor, _arrangementEditor, _layoutEditor, _settingsPane, _eventLog, _hostPane); + app.Add (_classListView, _adornmentsEditor, _arrangementEditor, _layoutEditor, _viewportSettingsEditor, _settingsPane, _eventLog, _hostPane); app.Initialized += App_Initialized; @@ -306,6 +319,7 @@ private void CreateCurrentView (Type type) _hostPane!.Add (_curView); _layoutEditor!.ViewToEdit = _curView; + _viewportSettingsEditor!.ViewToEdit = _curView; _arrangementEditor!.ViewToEdit = _curView; _curView.SetNeedsLayout (); } @@ -318,6 +332,7 @@ private void DisposeCurrentView () _curView.SubviewsLaidOut -= CurrentView_LayoutComplete; _hostPane!.Remove (_curView); _layoutEditor!.ViewToEdit = null; + _viewportSettingsEditor!.ViewToEdit = null; _arrangementEditor!.ViewToEdit = null; _curView.Dispose (); diff --git a/UICatalog/Scenarios/Editors/ViewportSettingsEditor.cs b/UICatalog/Scenarios/Editors/ViewportSettingsEditor.cs new file mode 100644 index 0000000000..ab77fe4a26 --- /dev/null +++ b/UICatalog/Scenarios/Editors/ViewportSettingsEditor.cs @@ -0,0 +1,371 @@ +#nullable enable +using System; +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +/// +/// Provides an editor UI for View.ViewportSettings. +/// +public sealed class ViewportSettingsEditor : EditorBase +{ + public ViewportSettingsEditor () + { + Title = "ViewportSettingsEditor"; + TabStop = TabBehavior.TabGroup; + + Initialized += ViewportSettingsEditor_Initialized; + } + + protected override void OnUpdateSettings () + { + foreach (View subview in Subviews) + { + subview.Enabled = ViewToEdit is not Adornment; + } + + if (ViewToEdit is null) + { } + } + + protected override void OnViewToEditChanged () + { + if (ViewToEdit is { } and not Adornment) + { + //ViewToEdit.VerticalScrollBar.AutoShow = true; + //ViewToEdit.HorizontalScrollBar.AutoShow = true; + + _contentSizeWidth!.Value = ViewToEdit.GetContentSize ().Width; + _contentSizeHeight!.Value = ViewToEdit.GetContentSize ().Height; + + _cbAllowNegativeX!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowNegativeX) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbAllowNegativeY!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowNegativeY) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbAllowXGreaterThanContentWidth!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbAllowYGreaterThanContentHeight!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbClearContentOnly!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.ClearContentOnly) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbClipContentOnly!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.ClipContentOnly) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbTransparent!.CheckedState = ViewToEdit.ViewportSettings.HasFlag(Terminal.Gui.ViewportSettings.Transparent) + ? CheckState.Checked + : CheckState.UnChecked; + + _cbVerticalScrollBar!.CheckedState = ViewToEdit.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked; + _cbAutoShowVerticalScrollBar!.CheckedState = ViewToEdit.VerticalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked; + _cbHorizontalScrollBar!.CheckedState = ViewToEdit.HorizontalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked; + _cbAutoShowHorizontalScrollBar!.CheckedState = ViewToEdit.HorizontalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked; + } + } + + private CheckBox? _cbAllowNegativeX; + private CheckBox? _cbAllowNegativeY; + private CheckBox? _cbAllowXGreaterThanContentWidth; + private CheckBox? _cbAllowYGreaterThanContentHeight; + private NumericUpDown? _contentSizeWidth; + private NumericUpDown? _contentSizeHeight; + private CheckBox? _cbClearContentOnly; + private CheckBox? _cbClipContentOnly; + private CheckBox? _cbTransparent; + private CheckBox? _cbVerticalScrollBar; + private CheckBox? _cbAutoShowVerticalScrollBar; + private CheckBox? _cbHorizontalScrollBar; + private CheckBox? _cbAutoShowHorizontalScrollBar; + + private void ViewportSettingsEditor_Initialized (object? s, EventArgs e) + { + _cbAllowNegativeX = new() + { + Title = "Allow X < 0", + CanFocus = true + }; + + Add (_cbAllowNegativeX); + + _cbAllowNegativeY = new() + { + Title = "Allow Y < 0", + CanFocus = true + }; + + Add (_cbAllowNegativeY); + + _cbAllowXGreaterThanContentWidth = new() + { + Title = "Allow X > Content Width", + Y = Pos.Bottom (_cbAllowNegativeX), + CanFocus = true + }; + + _cbAllowNegativeX.CheckedStateChanging += AllowNegativeXToggle; + _cbAllowXGreaterThanContentWidth.CheckedStateChanging += AllowXGreaterThanContentWidthToggle; + + Add (_cbAllowXGreaterThanContentWidth); + + void AllowNegativeXToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowNegativeX; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowNegativeX; + } + } + + void AllowXGreaterThanContentWidthToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth; + } + } + + _cbAllowYGreaterThanContentHeight = new() + { + Title = "Allow Y > Content Height", + X = Pos.Right (_cbAllowXGreaterThanContentWidth) + 1, + Y = Pos.Bottom (_cbAllowNegativeX), + CanFocus = true + }; + + _cbAllowNegativeY.CheckedStateChanging += AllowNegativeYToggle; + + _cbAllowYGreaterThanContentHeight.CheckedStateChanging += AllowYGreaterThanContentHeightToggle; + + Add (_cbAllowYGreaterThanContentHeight); + + void AllowNegativeYToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowNegativeY; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowNegativeY; + } + } + + void AllowYGreaterThanContentHeightToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight; + } + } + + _cbAllowNegativeX.X = Pos.Left (_cbAllowYGreaterThanContentHeight); + + var labelContentSize = new Label + { + Title = "ContentSize:", + Y = Pos.Bottom (_cbAllowYGreaterThanContentHeight) + }; + + _contentSizeWidth = new() + { + X = Pos.Right (labelContentSize) + 1, + Y = Pos.Top (labelContentSize), + CanFocus = true + }; + _contentSizeWidth.ValueChanging += ContentSizeWidthValueChanged; + + void ContentSizeWidthValueChanged (object? sender, CancelEventArgs e) + { + if (e.NewValue < 0) + { + e.Cancel = true; + + return; + } + + // BUGBUG: set_ContentSize is supposed to be `protected`. + ViewToEdit!.SetContentSize (ViewToEdit.GetContentSize () with { Width = e.NewValue }); + } + + var labelComma = new Label + { + Title = ",", + X = Pos.Right (_contentSizeWidth), + Y = Pos.Top (labelContentSize) + }; + + _contentSizeHeight = new() + { + X = Pos.Right (labelComma) + 1, + Y = Pos.Top (labelContentSize), + CanFocus = true + }; + _contentSizeHeight.ValueChanging += ContentSizeHeightValueChanged; + + void ContentSizeHeightValueChanged (object? sender, CancelEventArgs e) + { + if (e.NewValue < 0) + { + e.Cancel = true; + + return; + } + + // BUGBUG: set_ContentSize is supposed to be `protected`. + ViewToEdit?.SetContentSize (ViewToEdit.GetContentSize () with { Height = e.NewValue }); + } + + _cbClearContentOnly = new() + { + Title = "ClearContentOnly", + X = Pos.Right (_contentSizeHeight) + 1, + Y = Pos.Top (labelContentSize), + CanFocus = true + }; + _cbClearContentOnly.CheckedStateChanging += ClearContentOnlyToggle; + + void ClearContentOnlyToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.ClearContentOnly; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.ClearContentOnly; + } + } + + _cbClipContentOnly = new() + { + Title = "ClipContentOnly", + X = Pos.Right (_cbClearContentOnly) + 1, + Y = Pos.Top (labelContentSize), + CanFocus = true + }; + _cbClipContentOnly.CheckedStateChanging += ClipContentOnlyToggle; + + void ClipContentOnlyToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.ClipContentOnly; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.ClipContentOnly; + } + } + + _cbTransparent = new () + { + Title = "Transparent", + X = Pos.Right (_cbClipContentOnly) + 1, + Y = Pos.Top (labelContentSize), + CanFocus = true + }; + _cbTransparent.CheckedStateChanging += TransparentToggle; + + void TransparentToggle (object? sender, CancelEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.Transparent; + } + else + { + ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.Transparent; + } + } + + _cbVerticalScrollBar = new() + { + Title = "VerticalScrollBar.Visible", + X = 0, + Y = Pos.Bottom (labelContentSize), + CanFocus = false + }; + _cbVerticalScrollBar.CheckedStateChanging += VerticalScrollBarToggle; + + void VerticalScrollBarToggle (object? sender, CancelEventArgs e) + { + ViewToEdit!.VerticalScrollBar.Visible = e.NewValue == CheckState.Checked; + } + + _cbAutoShowVerticalScrollBar = new() + { + Title = "VerticalScrollBar.AutoShow", + X = Pos.Right (_cbVerticalScrollBar) + 1, + Y = Pos.Top (_cbVerticalScrollBar), + CanFocus = false + }; + _cbAutoShowVerticalScrollBar.CheckedStateChanging += AutoShowVerticalScrollBarToggle; + + void AutoShowVerticalScrollBarToggle (object? sender, CancelEventArgs e) + { + ViewToEdit!.VerticalScrollBar.AutoShow = e.NewValue == CheckState.Checked; + } + + _cbHorizontalScrollBar = new() + { + Title = "HorizontalScrollBar.Visible", + X = 0, + Y = Pos.Bottom (_cbVerticalScrollBar), + CanFocus = false + }; + _cbHorizontalScrollBar.CheckedStateChanging += HorizontalScrollBarToggle; + + void HorizontalScrollBarToggle (object? sender, CancelEventArgs e) + { + ViewToEdit!.HorizontalScrollBar.Visible = e.NewValue == CheckState.Checked; + } + + _cbAutoShowHorizontalScrollBar = new() + { + Title = "HorizontalScrollBar.AutoShow ", + X = Pos.Right (_cbHorizontalScrollBar) + 1, + Y = Pos.Top (_cbHorizontalScrollBar), + CanFocus = false + }; + _cbAutoShowHorizontalScrollBar.CheckedStateChanging += AutoShowHorizontalScrollBarToggle; + + void AutoShowHorizontalScrollBarToggle (object? sender, CancelEventArgs e) + { + ViewToEdit!.HorizontalScrollBar.AutoShow = e.NewValue == CheckState.Checked; + } + + Add ( + labelContentSize, + _contentSizeWidth, + labelComma, + _contentSizeHeight, + _cbClearContentOnly, + _cbClipContentOnly, + _cbTransparent, + _cbVerticalScrollBar, + _cbHorizontalScrollBar, + _cbAutoShowVerticalScrollBar, + _cbAutoShowHorizontalScrollBar); + } +} diff --git a/UICatalog/Scenarios/ViewportSettings.cs b/UICatalog/Scenarios/ViewportSettings.cs index 4842f1529f..d4929f020f 100644 --- a/UICatalog/Scenarios/ViewportSettings.cs +++ b/UICatalog/Scenarios/ViewportSettings.cs @@ -123,7 +123,7 @@ public override void Main () app.Add (view); // Add Scroll Setting UI to Padding - view.Padding!.Thickness = view.Padding.Thickness with { Top = view.Padding.Thickness.Top + 6 }; + view.Padding!.Thickness = view.Padding.Thickness with { Top = view.Padding.Thickness.Top + 7 }; view.Padding.CanFocus = true; Label frameLabel = new () From be8210a5ac55a3ea308b38dc8ba16c1061cd4ca2 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 6 Mar 2025 14:16:05 -0700 Subject: [PATCH 51/75] merged v2_develop part 2 --- Terminal.Gui/View/View.Mouse.cs | 15 --------------- Terminal.Gui/View/View.cs | 2 -- 2 files changed, 17 deletions(-) diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 85b2f4fd02..bdce9b5e29 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -770,21 +770,6 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) View? start = Application.Top; - viewsUnderMouse = GetViewsUnderMouse (Application.Top, location); - - if (Application.PopoverHost is { Visible: true }) - { - viewsUnderMouse.AddRange (GetViewsUnderMouse (Application.PopoverHost, location)); - viewsUnderMouse.Remove (Application.PopoverHost); - } - - return viewsUnderMouse; - - } - - internal static List GetViewsUnderMouse (View? start, in Point location) - { - List viewsUnderMouse = new (); Point currentLocation = location; while (start is { Visible: true } && start.Contains (currentLocation)) diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index b3d2aea008..bf4fc0d8cb 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -112,8 +112,6 @@ protected virtual void Dispose (bool disposing) /// The id should be unique across all Views that share a SuperView. public string Id { get; set; } = ""; - #region Constructors and Initialization - /// /// Points to the current driver in use by the view, it is a convenience property for simplifying the development /// of new views. From 3b89515ac3672e1eb46c7c370af57df05ffde7c2 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 6 Mar 2025 14:31:24 -0700 Subject: [PATCH 52/75] merged v2_develop part 3 --- .../Application/Application.Keyboard.cs | 17 ++--------------- .../Application/ApplicationNavigation.cs | 8 -------- Terminal.Gui/Drawing/Region.cs | 6 +++--- Terminal.Gui/View/Adornment/Adornment.cs | 1 + Terminal.Gui/View/View.Arrangement.cs | 11 +---------- Terminal.Gui/View/View.Drawing.cs | 1 - Terminal.Gui/View/View.Layout.cs | 11 +---------- Terminal.Gui/View/View.Navigation.cs | 10 ---------- Tests/UnitTests/Application/ApplicationTests.cs | 2 -- UICatalog/Scenario.cs | 1 - 10 files changed, 8 insertions(+), 60 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 6a52acf876..873dc0af0f 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -20,14 +20,7 @@ public static bool RaiseKeyDownEvent (Key key) return true; } - View? top = Top; - - //if (Popover is { Visible: true }) - //{ - // top = Popover; - //} - - if (top is null) + if (Top is null) { foreach (Toplevel topLevel in TopLevels.ToList ()) { @@ -44,7 +37,7 @@ public static bool RaiseKeyDownEvent (Key key) } else { - if (top.NewKeyDownEvent (key)) + if (Top.NewKeyDownEvent (key)) { return true; } @@ -179,12 +172,6 @@ internal static void AddKeyBindings () Command.Quit, static () => { - //if (Popover is {Visible: true}) - //{ - // Popover.Visible = false; - - // return true; - //} RequestStop (); return true; diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index 5317878f7a..1fe6972eae 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -76,10 +76,6 @@ public static bool IsInHierarchy (View? start, View? view) /// internal void SetFocused (View? value) { - if (value is null) - { - - } if (_focused == value) { return; @@ -108,10 +104,6 @@ internal void SetFocused (View? value) /// public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { - //if (Application.Popover is { Visible: true }) - //{ - // return Application.Popover.AdvanceFocus (direction, behavior); - //} return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } } diff --git a/Terminal.Gui/Drawing/Region.cs b/Terminal.Gui/Drawing/Region.cs index 0f07cf393d..2e51319758 100644 --- a/Terminal.Gui/Drawing/Region.cs +++ b/Terminal.Gui/Drawing/Region.cs @@ -249,9 +249,9 @@ private void CombineInternal(Region? region, RegionOp operation) /// The bounding rectangle to use for complementing the region. public void Complement (Rectangle bounds) { - if (bounds.IsEmpty || _rectangles!.Count == 0) + if (bounds.IsEmpty || _rectangles.Count == 0) { - _rectangles!.Clear (); + _rectangles.Clear (); return; } @@ -410,7 +410,7 @@ public bool Equals (Region? other) /// A that bounds the region. public Rectangle GetBounds () { - if (_rectangles!.Count == 0) + if (_rectangles.Count == 0) { return Rectangle.Empty; } diff --git a/Terminal.Gui/View/Adornment/Adornment.cs b/Terminal.Gui/View/Adornment/Adornment.cs index 18da30ad59..d872db769c 100644 --- a/Terminal.Gui/View/Adornment/Adornment.cs +++ b/Terminal.Gui/View/Adornment/Adornment.cs @@ -229,6 +229,7 @@ public override bool Contains (in Point location) return Thickness.Contains (outside, location); } + #endregion View Overrides /// diff --git a/Terminal.Gui/View/View.Arrangement.cs b/Terminal.Gui/View/View.Arrangement.cs index 204082da59..e11b1d3894 100644 --- a/Terminal.Gui/View/View.Arrangement.cs +++ b/Terminal.Gui/View/View.Arrangement.cs @@ -3,8 +3,6 @@ namespace Terminal.Gui; public partial class View { - private ViewArrangement _arrangement; - /// /// Gets or sets the user actions that are enabled for the arranging this view within it's . /// @@ -13,12 +11,5 @@ public partial class View /// See the View Arrangement Deep Dive for more information: /// /// - public ViewArrangement Arrangement - { - get => _arrangement; - set - { - _arrangement = value; - } - } + public ViewArrangement Arrangement { get; set; } } diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index 349f4c693b..11887b9df0 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -1,6 +1,5 @@ #nullable enable using System.ComponentModel; -using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Terminal.Gui; diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 2bcad98665..67c604319a 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -5,7 +5,7 @@ namespace Terminal.Gui; public partial class View // Layout APIs { - #region Frame + #region Frame/Position/Dimension /// /// Indicates whether the specified SuperView-relative coordinates are within the View's . @@ -1048,15 +1048,6 @@ out int ny int maxDimension; View? superView; - - //if (Application.Driver is null) - //{ - // nx = targetX; - // ny = targetY; - // return null; - //} - - if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { maxDimension = Application.Screen.Width; diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index a8933166dd..6799edb9d1 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -834,16 +834,6 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false // Set HasFocus false _hasFocus = false; - if (Application.Navigation is { }) - { - View? appFocused = Application.Navigation.GetFocused (); - - if (appFocused is { } || appFocused == this) - { - Application.Navigation.SetFocused (newFocusedView ?? superViewOrParent/* ?? Application.Popover*/); - } - } - RaiseFocusChanged (HasFocus, this, newFocusedView); if (_hasFocus) diff --git a/Tests/UnitTests/Application/ApplicationTests.cs b/Tests/UnitTests/Application/ApplicationTests.cs index 9ea2c45a94..c98bbe3947 100644 --- a/Tests/UnitTests/Application/ApplicationTests.cs +++ b/Tests/UnitTests/Application/ApplicationTests.cs @@ -305,7 +305,6 @@ void CheckReset () // Public Properties Assert.Null (Application.Top); - //Assert.Null (Application.PopoverHost); Assert.Null (Application.MouseGrabView); Assert.Null (Application.WantContinuousButtonPressedView); @@ -334,7 +333,6 @@ void CheckReset () // Mouse Assert.Null (Application._lastMousePosition); - // Navigation Assert.Null (Application.Navigation); diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index 4c1f0b4473..860ef27207 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -4,7 +4,6 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; -using System.Reflection.Metadata; using Terminal.Gui; namespace UICatalog; From 6fff834851c97e12b76d6cb0470d1e2266a671f5 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 9 Mar 2025 12:47:51 -0600 Subject: [PATCH 53/75] WIP: GetViewsUnderMouse --- .../Application/Application.Initialization.cs | 2 + .../Application/Application.Popover.cs | 95 ++++++++----------- Terminal.Gui/Application/Application.Run.cs | 7 +- Terminal.Gui/Application/Application.cs | 7 +- Terminal.Gui/View/View.Mouse.cs | 11 ++- UICatalog/Scenarios/Bars.cs | 20 ++-- 6 files changed, 62 insertions(+), 80 deletions(-) diff --git a/Terminal.Gui/Application/Application.Initialization.cs b/Terminal.Gui/Application/Application.Initialization.cs index 923b1534d5..ece47663e0 100644 --- a/Terminal.Gui/Application/Application.Initialization.cs +++ b/Terminal.Gui/Application/Application.Initialization.cs @@ -162,6 +162,8 @@ internal static void InternalInit ( SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ()); + PopoverHost.Init (); + MainThreadId = Thread.CurrentThread.ManagedThreadId; bool init = Initialized = true; InitializedChanged?.Invoke (null, new (init)); diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index be6312f25f..8e9e2cdffe 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -1,64 +1,18 @@ #nullable enable +using System.Diagnostics; using static Unix.Terminal.Curses; namespace Terminal.Gui; public static partial class Application // Popover handling { - private static PopoverHost? _popoverHost; - - /// Gets or sets the Application Popover View. + /// Gets or sets the Application Popover Host. /// /// - /// To show or hide the Popover, set it's property. + /// To show or hide a Popover, set it's property. /// /// - public static PopoverHost? PopoverHost - { - get - { - if (_popoverHost is null) - { - _popoverHost = new PopoverHost (); - } - return _popoverHost; - } - internal set => _popoverHost = value; - //{ - // if (_popoverHost == value) - // { - // return; - // } - - // if (_popoverHost is { }) - // { - // _popoverHost.Visible = false; - // _popoverHost.VisibleChanging -= PopoverVisibleChanging; - // } - - // _popoverHost = value; - - // if (_popoverHost is { }) - // { - // if (!_popoverHost.IsInitialized) - // { - // _popoverHost.BeginInit (); - // _popoverHost.EndInit (); - // } - - // _popoverHost.Arrangement |= ViewArrangement.Overlapped; - - // if (_popoverHost.ColorScheme is null) - // { - // _popoverHost.ColorScheme = Top?.ColorScheme; - // } - - // _popoverHost.SetRelativeLayout (Screen.Size); - - // _popoverHost.VisibleChanging += PopoverVisibleChanging; - // } - //} - } + public static PopoverHost? PopoverHost { get; internal set; } private static void PopoverVisibleChanging (object? sender, CancelEventArgs e) { @@ -100,14 +54,38 @@ private static void PopoverVisibleChanging (object? sender, CancelEventArgs +/// +/// public class PopoverHost : View { - public PopoverHost() + public static void Init () + { + // Setup PopoverHost + Debug.Assert (Application.PopoverHost is null); + Application.PopoverHost = new PopoverHost (); + Application.PopoverHost.BeginInit (); + Application.PopoverHost.EndInit (); + } + + public static void Cleanup () + { + Application.PopoverHost?.Dispose (); + Application.PopoverHost = null; + } + + + /// + /// + /// + public PopoverHost () { Id = "popoverHost"; + CanFocus = true; + ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; Width = Dim.Fill (); Height = Dim.Fill (); - Visible = false; + base.Visible = false; } /// @@ -118,12 +96,21 @@ protected override bool OnVisibleChanging () { if (!Visible) { - ColorScheme ??= Application.Top?.ColorScheme; - Frame = Application.Screen; + //ColorScheme ??= Application.Top?.ColorScheme; + //Frame = Application.Screen; SetRelativeLayout (Application.Screen.Size); } return false; } + + /// + protected override void OnVisibleChanged () + { + if (Visible) + { + SetFocus (); + } + } } diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 08f0de4699..47c0371c9a 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -561,12 +561,7 @@ internal static void OnNotifyStopRunState (Toplevel top) public static void End (RunState runState) { ArgumentNullException.ThrowIfNull (runState); - - if (PopoverHost is { }) - { - PopoverHost?.Dispose (); - PopoverHost = null; - } + PopoverHost.Cleanup(); runState.Toplevel.OnUnloaded (); diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index 2fa2ac0be7..ca24bfffe7 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -148,12 +148,7 @@ internal static void ResetState (bool ignoreDisposed = false) t!.Running = false; } - //Popover = null; - if (PopoverHost is { }) - { - PopoverHost.Dispose (); - PopoverHost = null; - } + PopoverHost.Cleanup (); TopLevels.Clear (); #if DEBUG_IDISPOSABLE diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index b0f802af8e..4805acb3df 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -560,7 +560,7 @@ internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent) if (!WantMousePositionReports && Viewport.Contains (mouseEvent.Position)) { - return RaiseMouseClickEvent (mouseEvent); + return RaiseMouseClickEvent (mouseEvent); } return mouseEvent.Handled = true; @@ -770,6 +770,11 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) View? start = Application.Top; + if (Application.PopoverHost?.Visible == true) + { + start = Application.PopoverHost; + } + Point currentLocation = location; while (start is { Visible: true } && start.Contains (currentLocation)) @@ -830,8 +835,8 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) viewsUnderMouse.AddRange (View.GetViewsUnderMouse (location, true)); // De-dupe viewsUnderMouse - HashSet dedupe = [..viewsUnderMouse]; - viewsUnderMouse = [..dedupe]; + HashSet dedupe = [.. viewsUnderMouse]; + viewsUnderMouse = [.. dedupe]; } // No subview was found that's under the mouse, so we're done diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index c969596ab0..ab64e6a04f 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -24,18 +24,9 @@ public override void Main () app.Loaded += App_Loaded; - _popoverMenu = new Menuv2 - { - Id = "popoverMenu", - Arrangement = ViewArrangement.Popover - }; - - Application.PopoverHost.Add (_popoverMenu); - Application.Run (app); - Application.PopoverHost.Remove(_popoverMenu); - _popoverMenu.Dispose (); app.Dispose (); + _popoverMenu?.Dispose (); Application.Shutdown (); } @@ -162,11 +153,19 @@ private void App_Loaded (object? sender, EventArgs e) }; menuLikeExamples.Add (label); + _popoverMenu = new Menuv2 + { + Id = "popoverMenu", + Arrangement = ViewArrangement.Popover + }; + ConfigureMenu (_popoverMenu!); _popoverMenu!.ColorScheme = Colors.ColorSchemes ["Menu"]; _popoverMenu.Visible = false; + Application.PopoverHost.Add (_popoverMenu); + var toggleShortcut = new Shortcut { Title = "Toggle Hide", @@ -217,7 +216,6 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) { if (e.Flags.HasFlag (MouseFlags.Button3Clicked)) { - //Application.Popover = _popoverMenu; _popoverMenu.Arrangement = ViewArrangement.Overlapped; _popoverMenu.X = e.ScreenPosition.X; From 4241cb30f024a24f0b1cb83497789e89ef4fd329 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 9 Mar 2025 13:57:13 -0600 Subject: [PATCH 54/75] WIP: More GetViewsUnderMouse work --- Terminal.Gui/View/View.Mouse.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 4805acb3df..b7b2ffaab5 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -773,6 +773,8 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (Application.PopoverHost?.Visible == true) { start = Application.PopoverHost; + + viewsUnderMouse.Add (Application.Top); } Point currentLocation = location; @@ -830,13 +832,14 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (subview is null) { - if (start.ViewportSettings.HasFlag (ViewportSettings.TransparentMouse)) + // In the case start is transparent, recursively add all it's subviews etc... + if (start.ViewportSettings.HasFlag (ViewportSettings.TransparentMouse) && !viewsUnderMouse.Contains(start)) { viewsUnderMouse.AddRange (View.GetViewsUnderMouse (location, true)); // De-dupe viewsUnderMouse - HashSet dedupe = [.. viewsUnderMouse]; - viewsUnderMouse = [.. dedupe]; + HashSet hashSet = [.. viewsUnderMouse]; + viewsUnderMouse = [.. hashSet]; } // No subview was found that's under the mouse, so we're done From c30742f4302d1c5a221a5c7824bee12b2fe601ea Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 10 Mar 2025 06:20:34 -0600 Subject: [PATCH 55/75] Bars works again --- Terminal.Gui/View/View.Mouse.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index b7b2ffaab5..2bac15f46f 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -770,7 +770,7 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) View? start = Application.Top; - if (Application.PopoverHost?.Visible == true) + if (Application.PopoverHost?.Visible is true && !ignoreTransparent) { start = Application.PopoverHost; @@ -833,7 +833,7 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) if (subview is null) { // In the case start is transparent, recursively add all it's subviews etc... - if (start.ViewportSettings.HasFlag (ViewportSettings.TransparentMouse) && !viewsUnderMouse.Contains(start)) + if (start.ViewportSettings.HasFlag (ViewportSettings.TransparentMouse)) { viewsUnderMouse.AddRange (View.GetViewsUnderMouse (location, true)); From ed44e1657a9ad7bd20f5b88870e6e9c331b5fba0 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 10 Mar 2025 08:03:06 -0600 Subject: [PATCH 56/75] Added unit tests --- .../Application/Application.Popover.cs | 113 ++---------------- Terminal.Gui/Application/PopoverHost.cs | 81 +++++++++++++ Terminal.Gui/View/View.Mouse.cs | 2 + Terminal.Gui/View/ViewArrangement.cs | 5 - ...ests.cs => ApplicationPopoverHostTests.cs} | 87 ++++++++++---- .../ApplicationPopoverHostTests.cs | 15 +++ UICatalog/Scenarios/Bars.cs | 3 +- 7 files changed, 175 insertions(+), 131 deletions(-) create mode 100644 Terminal.Gui/Application/PopoverHost.cs rename Tests/UnitTests/Application/{ApplicationPopoverTests.cs => ApplicationPopoverHostTests.cs} (84%) create mode 100644 Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index 8e9e2cdffe..d4267bfd8d 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -1,116 +1,21 @@ #nullable enable -using System.Diagnostics; using static Unix.Terminal.Curses; namespace Terminal.Gui; public static partial class Application // Popover handling { - /// Gets or sets the Application Popover Host. + /// Gets the Application . /// /// - /// To show or hide a Popover, set it's property. + /// Any View added as a SubView will be a Popover. + /// + /// + /// To show or hide a Popover, set the property of the PopoverHost. + /// + /// + /// If the user clicks anywhere not occulded by a SubView of the PopoverHost, the PopoverHost will be hidden. /// /// public static PopoverHost? PopoverHost { get; internal set; } - - private static void PopoverVisibleChanging (object? sender, CancelEventArgs e) - { - if (PopoverHost is null) - { - return; - } - - if (e.NewValue) - { - PopoverHost.Arrangement |= ViewArrangement.Overlapped; - - PopoverHost.ColorScheme ??= Top?.ColorScheme; - - if (PopoverHost.NeedsLayout) - { - PopoverHost.SetRelativeLayout (Screen.Size); - } - - View.GetLocationEnsuringFullVisibility ( - PopoverHost, - PopoverHost.Frame.X, - PopoverHost.Frame.Y, - out int nx, - out int ny); - - PopoverHost.X = nx; - PopoverHost.Y = ny; - - PopoverHost.SetRelativeLayout (Screen.Size); - - if (Top is { }) - { - Top.HasFocus = false; - } - - PopoverHost?.SetFocus (); - } - } -} - -/// -/// -/// -public class PopoverHost : View -{ - public static void Init () - { - // Setup PopoverHost - Debug.Assert (Application.PopoverHost is null); - Application.PopoverHost = new PopoverHost (); - Application.PopoverHost.BeginInit (); - Application.PopoverHost.EndInit (); - } - - public static void Cleanup () - { - Application.PopoverHost?.Dispose (); - Application.PopoverHost = null; - } - - - /// - /// - /// - public PopoverHost () - { - Id = "popoverHost"; - CanFocus = true; - ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; - Width = Dim.Fill (); - Height = Dim.Fill (); - base.Visible = false; - } - - /// - protected override bool OnClearingViewport () { return true; } - - /// - protected override bool OnVisibleChanging () - { - if (!Visible) - { - //ColorScheme ??= Application.Top?.ColorScheme; - //Frame = Application.Screen; - - SetRelativeLayout (Application.Screen.Size); - } - - return false; - } - - /// - protected override void OnVisibleChanged () - { - if (Visible) - { - SetFocus (); - } - } -} +} \ No newline at end of file diff --git a/Terminal.Gui/Application/PopoverHost.cs b/Terminal.Gui/Application/PopoverHost.cs new file mode 100644 index 0000000000..1aa1ecfaa2 --- /dev/null +++ b/Terminal.Gui/Application/PopoverHost.cs @@ -0,0 +1,81 @@ +#nullable enable +using System.Diagnostics; + +namespace Terminal.Gui; + +/// +/// Singleton View that hosts Views to be shown as Popovers. The host covers the whole screen and is transparent both +/// visually and to the mouse. Set to show or hide the Popovers. +/// +/// +/// +/// If the user clicks anywhere not occulded by a SubView of the PopoverHost, the PopoverHost will be hidden. +/// +/// +public sealed class PopoverHost : View +{ + /// + /// Initializes . + /// + internal static void Init () + { + // Setup PopoverHost + if (Application.PopoverHost is { }) + { + throw new InvalidOperationException (@"PopoverHost is a singleton; Init and Cleanup must be balanced."); + } + + Application.PopoverHost = new PopoverHost (); + Application.PopoverHost.BeginInit (); + Application.PopoverHost.EndInit (); + } + + /// + /// Cleans up . + /// + internal static void Cleanup () + { + Application.PopoverHost?.Dispose (); + Application.PopoverHost = null; + } + + + /// + /// Creates a new instance. + /// + public PopoverHost () + { + Id = "popoverHost"; + CanFocus = true; + ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; + Width = Dim.Fill (); + Height = Dim.Fill (); + base.Visible = false; + } + + /// + protected override bool OnClearingViewport () { return true; } + + /// + protected override bool OnVisibleChanging () + { + if (!Visible) + { + //ColorScheme ??= Application.Top?.ColorScheme; + //Frame = Application.Screen; + + SetRelativeLayout (Application.Screen.Size); + } + + return false; + } + + /// + protected override void OnVisibleChanged () + { + if (Visible) + { + SetFocus (); + } + } +} diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 2bac15f46f..05dab16f22 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -770,10 +770,12 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) View? start = Application.Top; + // PopoverHost - If visible, start with it instead of Top if (Application.PopoverHost?.Visible is true && !ignoreTransparent) { start = Application.PopoverHost; + // Put Top on stack next viewsUnderMouse.Add (Application.Top); } diff --git a/Terminal.Gui/View/ViewArrangement.cs b/Terminal.Gui/View/ViewArrangement.cs index 27eef093af..b43703ec2b 100644 --- a/Terminal.Gui/View/ViewArrangement.cs +++ b/Terminal.Gui/View/ViewArrangement.cs @@ -71,9 +71,4 @@ public enum ViewArrangement /// /// Overlapped = 32, - - /// - /// The view overlaps other views and prevents any subviews without this flag set to gain input. - /// - Popover = 64, } diff --git a/Tests/UnitTests/Application/ApplicationPopoverTests.cs b/Tests/UnitTests/Application/ApplicationPopoverHostTests.cs similarity index 84% rename from Tests/UnitTests/Application/ApplicationPopoverTests.cs rename to Tests/UnitTests/Application/ApplicationPopoverHostTests.cs index ff1738cca3..b65c5159b4 100644 --- a/Tests/UnitTests/Application/ApplicationPopoverTests.cs +++ b/Tests/UnitTests/Application/ApplicationPopoverHostTests.cs @@ -1,28 +1,76 @@ -using System.ComponentModel; -using Xunit.Abstractions; +namespace Terminal.Gui.ApplicationTests; -namespace Terminal.Gui.ApplicationTests; +public class ApplicationPopoverHostTests +{ + [Fact] + public void PopoverHost_ApplicationInit_Inits () + { + // Arrange + Assert.Null (Application.PopoverHost); + Application.Init (new FakeDriver ()); -using System; -using Terminal.Gui; -using Xunit; + // Act + Assert.NotNull (Application.PopoverHost); -public class ApplicationPopoverTests -{ - //[Fact] - //public void Popover_SetAndGet () - //{ - // // Arrange - // var popover = new View (); + Application.ResetState (true); + } - // // Act - // Application.PopoverHost = popover; + [Fact] + public void PopoverHost_ApplicationShutdown_CleansUp () + { + // Arrange + Assert.Null (Application.PopoverHost); + Application.Init (new FakeDriver ()); - // // Assert - // Assert.Equal (popover, Application.PopoverHost); + // Act + Assert.NotNull (Application.PopoverHost); - // Application.ResetState (ignoreDisposed: true); - //} + Application.Shutdown (); + + // Test + Assert.Null (Application.PopoverHost); + } + + [Fact] + public void PopoverHost_CleanUp_CleansUp () + { + // Arrange + Assert.Null (Application.PopoverHost); + PopoverHost.Init (); + + // Act + PopoverHost.Cleanup (); + + // Test + Assert.Null (Application.PopoverHost); + + Application.ResetState (true); + } + + [Fact] + public void PopoverHost_Init_Inits () + { + // Arrange + Assert.Null (Application.PopoverHost); + + // Act + PopoverHost.Init (); + Assert.NotNull (Application.PopoverHost); + + Application.ResetState (true); + } + + [Fact] + public void PopoverHost_Init_WithoutCleanup_Throws () + { + // Arrange + PopoverHost.Init (); + + // Act + Assert.Throws (PopoverHost.Init); + + Application.ResetState (true); + } //[Fact] //public void Popover_SetToNull () @@ -190,7 +238,6 @@ public class ApplicationPopoverTests // Application.ResetState (ignoreDisposed: true); //} - //[Fact] //public void Popover_MouseClick_Outside_Hides_Passes_Event_On () //{ diff --git a/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs b/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs new file mode 100644 index 0000000000..204bd169bd --- /dev/null +++ b/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs @@ -0,0 +1,15 @@ +namespace Terminal.Gui.ApplicationTests; + +public class ApplicationPopoverHostTests +{ + [Fact] + public void PopoverHost_Defaults () + { + var host = new PopoverHost (); + Assert.True (host.CanFocus); + Assert.False (host.Visible); + Assert.Equal (ViewportSettings.Transparent | ViewportSettings.TransparentMouse, host.ViewportSettings); + Assert.True (host.Width!.Has (out _)); + Assert.True (host.Height!.Has (out _)); + } +} diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index ab64e6a04f..bd9604c0b6 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -156,7 +156,6 @@ private void App_Loaded (object? sender, EventArgs e) _popoverMenu = new Menuv2 { Id = "popoverMenu", - Arrangement = ViewArrangement.Popover }; ConfigureMenu (_popoverMenu!); @@ -164,7 +163,7 @@ private void App_Loaded (object? sender, EventArgs e) _popoverMenu!.ColorScheme = Colors.ColorSchemes ["Menu"]; _popoverMenu.Visible = false; - Application.PopoverHost.Add (_popoverMenu); + Application.PopoverHost!.Add (_popoverMenu); var toggleShortcut = new Shortcut { From 804fdbcdf15014bbc96a8863ee9cd4ae7dbb2746 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 10 Mar 2025 13:04:11 -0600 Subject: [PATCH 57/75] CM now works --- .../Application/Application.Keyboard.cs | 8 ++ Terminal.Gui/Application/Application.Run.cs | 1 + Terminal.Gui/Application/Application.cs | 1 + .../Application/ApplicationNavigation.cs | 4 + Terminal.Gui/Application/PopoverHost.cs | 25 ++++++ Terminal.Gui/View/View.Mouse.cs | 5 +- Terminal.Gui/Views/Bar.cs | 54 ++++++++++++ Terminal.Gui/Views/Menu/MenuBarv2.cs | 6 +- .../ApplicationPopoverHostTests.cs | 16 ++++ UICatalog/Scenarios/Bars.cs | 87 ++++++++++++++++--- UICatalog/Scenarios/ContextMenus.cs | 25 ++++-- 11 files changed, 209 insertions(+), 23 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 873dc0af0f..0d68319099 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -20,6 +20,14 @@ public static bool RaiseKeyDownEvent (Key key) return true; } + if (PopoverHost is { Visible: true }) + { + if (PopoverHost.NewKeyDownEvent (key)) + { + return true; + } + } + if (Top is null) { foreach (Toplevel topLevel in TopLevels.ToList ()) diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 47c0371c9a..e0e4521c33 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -430,6 +430,7 @@ internal static void LayoutAndDrawImpl (bool forceDraw = false) if (PopoverHost is { Visible: true }) { + PopoverHost.SetNeedsDraw(); tops.Insert (0, PopoverHost); } diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index ca24bfffe7..2315cebf63 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -103,6 +103,7 @@ internal static List GetAvailableCulturesFromEmbeddedResources () .ToList (); } + // BUGBUG: This does not return en-US even though it's supported by default internal static List GetSupportedCultures () { CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures); diff --git a/Terminal.Gui/Application/ApplicationNavigation.cs b/Terminal.Gui/Application/ApplicationNavigation.cs index f351b515d6..1013d3e08c 100644 --- a/Terminal.Gui/Application/ApplicationNavigation.cs +++ b/Terminal.Gui/Application/ApplicationNavigation.cs @@ -104,6 +104,10 @@ internal void SetFocused (View? value) /// public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { + if (Application.PopoverHost is { Visible: true }) + { + return Application.PopoverHost.AdvanceFocus (direction, behavior); + } return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } } diff --git a/Terminal.Gui/Application/PopoverHost.cs b/Terminal.Gui/Application/PopoverHost.cs index 1aa1ecfaa2..25b0de4cc9 100644 --- a/Terminal.Gui/Application/PopoverHost.cs +++ b/Terminal.Gui/Application/PopoverHost.cs @@ -1,5 +1,7 @@ #nullable enable using System.Diagnostics; +using System.Net.Mime; +using static System.Net.Mime.MediaTypeNames; namespace Terminal.Gui; @@ -26,6 +28,12 @@ internal static void Init () } Application.PopoverHost = new PopoverHost (); + + // TODO: Add a diagnostic setting for this? + Application.PopoverHost.TextFormatter.VerticalAlignment = Alignment.End; + Application.PopoverHost.TextFormatter.Alignment = Alignment.End; + Application.PopoverHost.Text = "popoverHost"; + Application.PopoverHost.BeginInit (); Application.PopoverHost.EndInit (); } @@ -51,6 +59,23 @@ public PopoverHost () Width = Dim.Fill (); Height = Dim.Fill (); base.Visible = false; + + + AddCommand (Command.Quit, Quit); + + bool? Quit (ICommandContext? ctx) + { + if (Visible) + { + Visible = false; + + return true; + } + + return null; + } + + KeyBindings.Add (Application.QuitKey, Command.Quit); } /// diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index 05dab16f22..a310315ab9 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -783,7 +783,10 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle) while (start is { Visible: true } && start.Contains (currentLocation)) { - viewsUnderMouse.Add (start); + if (!start.ViewportSettings.HasFlag(ViewportSettings.TransparentMouse)) + { + viewsUnderMouse.Add (start); + } Adornment? found = null; diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 4c6c0d9ae5..d3328b2d78 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -32,6 +32,59 @@ public Bar (IEnumerable? shortcuts) // Initialized += Bar_Initialized; MouseEvent += OnMouseEvent; + AddCommand (Command.Right, MoveRight); + + bool? MoveRight (ICommandContext? ctx) + { + if (Orientation == Orientation.Vertical) + { + return false; + } + + return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + } + + AddCommand (Command.Left, MoveLeft); + + bool? MoveLeft (ICommandContext? ctx) + { + if (Orientation == Orientation.Vertical) + { + return false; + } + + return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); + } + + AddCommand (Command.Down, MoveDown); + + bool? MoveDown (ICommandContext? ctx) + { + if (Orientation == Orientation.Horizontal) + { + return false; + } + + return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + } + + AddCommand (Command.Up, MoveUp); + + bool? MoveUp (ICommandContext? ctx) + { + if (Orientation == Orientation.Horizontal) + { + return false; + } + + return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); + } + + KeyBindings.Add (Key.CursorRight, Command.Right); + KeyBindings.Add (Key.CursorDown, Command.Down); + KeyBindings.Add (Key.CursorLeft, Command.Left); + KeyBindings.Add (Key.CursorUp, Command.Up); + if (shortcuts is { }) { foreach (Shortcut shortcut in shortcuts) @@ -218,6 +271,7 @@ private void LayoutBarItems (Size contentSize) barItem.X = Pos.Align (Alignment.Start, AlignmentModes); barItem.Y = 0; //Pos.Center (); } + break; case Orientation.Vertical: diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index 55320f6783..90b3a6f26e 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -60,10 +60,10 @@ protected override bool OnHighlight (CancelEventArgs args) { if (args.NewValue.HasFlag (HighlightStyle.Hover)) { - //if (Application.Popover is { Visible: true } && View.IsInHierarchy (this, Application.Popover)) - //{ + if (Application.PopoverHost is { Visible: true } && View.IsInHierarchy (this, Application.PopoverHost)) + { - //} + } } return base.OnHighlight (args); } diff --git a/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs b/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs index 204bd169bd..2ca0110835 100644 --- a/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs +++ b/Tests/UnitTestsParallelizable/Application/ApplicationPopoverHostTests.cs @@ -12,4 +12,20 @@ public void PopoverHost_Defaults () Assert.True (host.Width!.Has (out _)); Assert.True (host.Height!.Has (out _)); } + + [Fact] + public void PopoverHost_Visible_True_SetsFocus () + { + var host = new PopoverHost (); + + Assert.False (host.HasFocus); + + Assert.False (host.Visible); + + host.Visible = true; + + Assert.True (host.HasFocus); + } + + } diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index bd9604c0b6..ee33278e5b 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -161,9 +161,22 @@ private void App_Loaded (object? sender, EventArgs e) ConfigureMenu (_popoverMenu!); _popoverMenu!.ColorScheme = Colors.ColorSchemes ["Menu"]; + + _popoverMenu.HasFocusChanged += (o, args) => + { + _popoverMenu.Visible = args.NewValue; + }; _popoverMenu.Visible = false; + Application.PopoverHost!.Add (_popoverMenu); + Application.PopoverHost.VisibleChanged += (sender, args) => + { + if (!Application.PopoverHost.Visible) + { + _popoverMenu.Visible = false; + } + }; var toggleShortcut = new Shortcut { @@ -290,6 +303,13 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) args.Cancel = true; }; + barView.Selecting += (o, args) => + { + eventSource.Add ($"Selecting: {barView!.Id} {args.Context.Command}"); + eventLog.MoveDown (); + args.Cancel = false; + }; + if (barView is Menuv2 menuv2) { menuv2.ShortcutCommandInvoked += (o, args) => @@ -314,6 +334,13 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) eventLog.MoveDown (); args.Cancel = true; }; + + sh.Selecting += (o, args) => + { + eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + args.Cancel = false; + }; } } } @@ -476,19 +503,38 @@ private void ConfigMenuBar (Bar bar) { Id = "fileMenuBarItem", Key = Key.D0.WithAlt, - HighlightStyle = HighlightStyle.Hover + HighlightStyle = HighlightStyle.Hover, }; + fileMenu.Visible = false; + Application.PopoverHost.Add (fileMenu); + + Application.PopoverHost.VisibleChanged += (sender, args) => + { + if (!Application.PopoverHost.Visible) + { + fileMenu.Visible = false; + } + }; + + fileMenuBarItem.HasFocusChanged += (sender, args) => + { + Rectangle screen = fileMenuBarItem.FrameToScreen (); + fileMenu.X = screen.X; + fileMenu.Y = screen.Y + screen.Height; + fileMenu.Visible = args.NewValue; + }; + fileMenuBarItem.Disposing += (sender, args) => fileMenu?.Dispose (); - //fileMenuBarItem.Accepting += (sender, args) => - // { - // Application.Popover = fileMenu; - // Rectangle screen = fileMenuBarItem.FrameToScreen (); - // fileMenu.X = screen.X; - // fileMenu.Y = screen.Y + screen.Height; - // fileMenu.Visible = true; - // }; + fileMenuBarItem.Accepting += (sender, args) => + { + Rectangle screen = fileMenuBarItem.FrameToScreen (); + fileMenu.X = screen.X; + fileMenu.Y = screen.Y + screen.Height; + fileMenu.Visible = true; + Application.PopoverHost.Visible = true; + }; Menuv2? editMenu = new ContextMenuv2 @@ -502,16 +548,35 @@ private void ConfigMenuBar (Bar bar) Title = "_Edit", HighlightStyle = HighlightStyle.Hover }; + editMenu.Visible = false; + Application.PopoverHost.Add (editMenu); + + Application.PopoverHost.VisibleChanged += (sender, args) => + { + if (!Application.PopoverHost.Visible) + { + editMenu.Visible = false; + } + }; + + editMenuBarItem.HasFocusChanged += (sender, args) => + { + Rectangle screen = editMenuBarItem.FrameToScreen (); + editMenu.X = screen.X; + editMenu.Y = screen.Y + screen.Height; + editMenu.Visible = args.NewValue; + }; + editMenuBarItem.Disposing += (sender, args) => editMenu?.Dispose (); editMenuBarItem.Accepting += (sender, args) => { - //Application.Popover = editMenu; Rectangle screen = editMenuBarItem.FrameToScreen (); editMenu.X = screen.X; editMenu.Y = screen.Y + screen.Height; editMenu.Visible = true; + Application.PopoverHost.Visible = true; }; @@ -556,7 +621,7 @@ private void ConfigureMenu (Bar bar) var line = new Line () { X = -1, - Width = Dim.Fill()! + 1 + Width = Dim.Fill ()! + 1 }; var shortcut4 = new Shortcut diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 4b643af934..1213a91652 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -113,13 +113,14 @@ private Shortcut [] GetSupportedCultures () if (index == -1) { + // Create English because GetSupportedCutures doesn't include it culture.Id = "_English"; culture.Title = "_English"; culture.HelpText = "en-US"; ((CheckBox)culture.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? CheckState.Checked : CheckState.UnChecked; - CreateAction (supportedCultures, culture); supportedCultures.Add (culture); + index++; culture = new (); culture.CommandView = new CheckBox () { CanFocus = false, HighlightStyle = HighlightStyle.None }; @@ -153,16 +154,22 @@ private void CreateWinContextMenu () { if (_winContextMenu is { }) { - //if (Application.Popover == _winContextMenu) - //{ - // Application.Popover = null; - //} - + Application.PopoverHost?.Remove (_winContextMenu); _winContextMenu.Dispose (); _winContextMenu = null; } - _winContextMenu = new (GetSupportedCultures ()) + var cultureShortcuts = GetSupportedCultures (); + foreach (Shortcut shortcut in cultureShortcuts) + { + shortcut.Accepting += (sender, args) => + { + Application.PopoverHost.Visible = false; + _winContextMenu.Visible = false; + args.Cancel = false; + }; + } + _winContextMenu = new (cultureShortcuts) { Key = _winContextMenuKey, @@ -170,13 +177,15 @@ private void CreateWinContextMenu () //ForceMinimumPosToZero = _forceMinimumPosToZero, //UseSubMenusSingleFrame = _useSubMenusSingleFrame }; + + Application.PopoverHost.Add (_winContextMenu); } private void ShowWinContextMenu (Point? screenPosition) { _winContextMenu!.SetPosition (screenPosition); - //Application.Popover = _winContextMenu; _winContextMenu.Visible = true; + Application.PopoverHost.Visible = true; } // MenuBarItem menuItems = new ( From 22e6e9873742d57d164979ed0df934c92b79a00f Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 10 Mar 2025 18:36:39 -0400 Subject: [PATCH 58/75] MenuItemv2 POC --- Terminal.Gui/Views/Menu/ContextMenuv2.cs | 6 +- Terminal.Gui/Views/Menu/MenuBarv2.cs | 7 +- Terminal.Gui/Views/Menu/MenuItemv2.cs | 79 ++++++++ Terminal.Gui/Views/Menu/Menuv2.cs | 28 +-- Terminal.Gui/Views/Shortcut.cs | 56 +----- Terminal.Gui/Views/TextField.cs | 2 +- Terminal.Gui/Views/TextView.cs | 2 +- UICatalog/Scenarios/Bars.cs | 15 +- UICatalog/Scenarios/ContextMenus.cs | 18 +- UICatalog/Scenarios/MenusV2.cs | 220 +++++++++++++++++++++++ UICatalog/UICatalog.cs | 1 + 11 files changed, 349 insertions(+), 85 deletions(-) create mode 100644 Terminal.Gui/Views/Menu/MenuItemv2.cs create mode 100644 UICatalog/Scenarios/MenusV2.cs diff --git a/Terminal.Gui/Views/Menu/ContextMenuv2.cs b/Terminal.Gui/Views/Menu/ContextMenuv2.cs index d4fac4866c..7cac4136eb 100644 --- a/Terminal.Gui/Views/Menu/ContextMenuv2.cs +++ b/Terminal.Gui/Views/Menu/ContextMenuv2.cs @@ -35,7 +35,7 @@ public class ContextMenuv2 : Menuv2, IDesignable public ContextMenuv2 () : this ([]) { } /// - public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) + public ContextMenuv2 (IEnumerable menuItems) : base (menuItems) { Visible = false; VisibleChanged += OnVisibleChanged; @@ -54,9 +54,9 @@ public ContextMenuv2 (IEnumerable shortcuts) : base (shortcuts) return true; }); - if (shortcuts is { }) + if (menuItems is { }) { - foreach (var sc in shortcuts) + foreach (var sc in menuItems) { AddCommand ( Command.Accept, diff --git a/Terminal.Gui/Views/Menu/MenuBarv2.cs b/Terminal.Gui/Views/Menu/MenuBarv2.cs index 90b3a6f26e..a31378b8c9 100644 --- a/Terminal.Gui/Views/Menu/MenuBarv2.cs +++ b/Terminal.Gui/Views/Menu/MenuBarv2.cs @@ -26,7 +26,7 @@ public MenuBarv2 (IEnumerable shortcuts) : base (shortcuts) AddCommand (Command.Context, (ctx) => { - if (ctx is CommandContext commandContext && commandContext.Binding.Data is Shortcut { TargetView: { } } shortcut) + if (ctx is CommandContext commandContext && commandContext.Binding.Data is MenuItemv2 { TargetView: { } } shortcut) { //MenuBarv2? clone = MemberwiseClone () as MenuBarv2; //clone!.SuperView = null; @@ -65,7 +65,8 @@ protected override bool OnHighlight (CancelEventArgs args) } } - return base.OnHighlight (args); + + return false; } /// @@ -80,7 +81,7 @@ protected override bool OnHighlight (CancelEventArgs args) } view.CanFocus = true; - if (view is Shortcut shortcut) + if (view is MenuItemv2 shortcut) { shortcut.KeyView.Visible = false; shortcut.HelpView.Visible = false; diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs new file mode 100644 index 0000000000..515642ff5d --- /dev/null +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -0,0 +1,79 @@ +#nullable enable + +namespace Terminal.Gui; + +/// +/// A has title, an associated help text, and an action to execute on activation. MenuItems +/// can also have a checked indicator (see ). +/// +public class MenuItemv2 : Shortcut +{ + /// + /// Creates a new instance of . + /// + public MenuItemv2 () : base (Key.Empty, null, null, null) + { + HighlightStyle = HighlightStyle.Hover; + } + + /// + /// Creates a new instance of , binding it to and + /// . The Key + /// has bound to will be used as . + /// + /// + /// + /// This is a helper API that simplifies creation of multiple Shortcuts when adding them to -based + /// objects, like . + /// + /// + /// + /// The View that will be invoked on when user does something that causes the Shortcut's Accept + /// event to be raised. + /// + /// + /// The Command to invoke on . The Key + /// has bound to will be used as + /// + /// The text to display for the command. + /// The help text to display. + public MenuItemv2 (View targetView, Command command, string commandText, string? helpText = null) + : base ( + targetView?.HotKeyBindings.GetFirstFromCommands (command)!, + commandText, + null, + helpText) + { + _targetView = targetView; + Command = command; + + if (Command == Command.Context) + { + KeyView.Text = $"{Glyphs.RightArrow}"; + } + } + + private readonly View? _targetView; // If set, _command will be invoked + + /// + /// Gets the target that the will be invoked on. + /// + public View? TargetView => _targetView; + + /// + /// Gets the that will be invoked on when the Shortcut is activated. + /// + public Command Command { get; } + + internal override bool? DispatchCommand (ICommandContext? commandContext) + { + bool? ret = base.DispatchCommand (commandContext); + + if (_targetView is { }) + { + _targetView.InvokeCommand (Command, commandContext); + } + + return ret; + } +} diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index f143c09d57..a1cf926ab4 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -53,21 +53,21 @@ private void Menuv2_Initialized (object? sender, EventArgs e) { base.Add (view); - if (view is Shortcut shortcut) + if (view is MenuItemv2 menuItem) { - shortcut.CanFocus = true; - shortcut.Orientation = Orientation.Vertical; - shortcut.HighlightStyle |= HighlightStyle.Hover; + menuItem.CanFocus = true; + menuItem.Orientation = Orientation.Vertical; + menuItem.HighlightStyle |= HighlightStyle.Hover; - shortcut.Accepting += ShortcutOnAccepting; + menuItem.Accepting += MenuItemtOnAccepting; - AddCommand (shortcut.Command, (ctx) => + AddCommand (menuItem.Command, (ctx) => { - return RaiseShortcutCommandInvoked (ctx); + return RaiseMenuItemCommandInvoked (ctx); }); - void ShortcutOnAccepting (object? sender, CommandEventArgs e) + void MenuItemtOnAccepting (object? sender, CommandEventArgs e) { if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible) { @@ -88,7 +88,7 @@ void ShortcutOnAccepting (object? sender, CommandEventArgs e) /// /// /// - protected bool? RaiseShortcutCommandInvoked (ICommandContext? ctx) + protected bool? RaiseMenuItemCommandInvoked (ICommandContext? ctx) { CommandEventArgs args = new () { Context = ctx }; @@ -99,10 +99,10 @@ void ShortcutOnAccepting (object? sender, CommandEventArgs e) if (!args.Cancel) { // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. - ShortcutCommandInvoked?.Invoke (this, args); + MenuItemCommandInvoked?.Invoke (this, args); } - return ShortcutCommandInvoked is null ? null : args.Cancel; + return MenuItemCommandInvoked is null ? null : args.Cancel; } /// @@ -111,7 +111,7 @@ void ShortcutOnAccepting (object? sender, CommandEventArgs e) /// /// /// - /// See for more information. + /// See for more information. /// /// /// @@ -124,8 +124,8 @@ void ShortcutOnAccepting (object? sender, CommandEventArgs e) /// /// /// - /// See for more information. + /// See for more information. /// /// - public event EventHandler? ShortcutCommandInvoked; + public event EventHandler? MenuItemCommandInvoked; } \ No newline at end of file diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 342d544563..f8284a2069 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -47,37 +47,6 @@ public class Shortcut : View, IOrientation, IDesignable public Shortcut () : this (Key.Empty, null, null, null) { } /// - /// Creates a new instance of , binding it to and - /// . The Key - /// has bound to will be used as . - /// - /// - /// - /// This is a helper API that simplifies creation of multiple Shortcuts when adding them to -based - /// objects, like . - /// - /// - /// - /// The View that will be invoked on when user does something that causes the Shortcut's Accept - /// event to be raised. - /// - /// - /// The Command to invoke on . The Key - /// has bound to will be used as - /// - /// The text to display for the command. - /// The help text to display. - public Shortcut (View targetView, Command command, string commandText, string? helpText = null) - : this ( - targetView?.HotKeyBindings.GetFirstFromCommands (command)!, - commandText, - null, - helpText) - { - _targetView = targetView; - Command = command; - } - /// /// Creates a new instance of . /// @@ -158,10 +127,11 @@ protected override bool OnHighlight (CancelEventArgs args) { if (args.NewValue.HasFlag (HighlightStyle.Hover)) { - HasFocus = true; + SetFocus (); + return true; } - return true; + return false; } /// @@ -204,7 +174,7 @@ internal void ShowHide () SetHelpViewDefaultLayout (); } - if (KeyView.Visible && Key != Key.Empty) + if (KeyView.Visible && (Key != Key.Empty || KeyView.Text != string.Empty)) { Add (KeyView); SetKeyViewDefaultLayout (); @@ -278,18 +248,6 @@ private void OnLayoutStarted (object? sender, LayoutEventArgs e) #region Accept/Select/HotKey Command Handling - private readonly View? _targetView; // If set, _command will be invoked - - /// - /// Gets the target that the will be invoked on. - /// - public View? TargetView => _targetView; - - /// - /// Gets the that will be invoked on when the Shortcut is activated. - /// - public Command Command { get; } - private void AddCommands () { // Accept (Enter key) - @@ -300,7 +258,7 @@ private void AddCommands () AddCommand (Command.Select, DispatchCommand); } - private bool? DispatchCommand (ICommandContext? commandContext) + internal virtual bool? DispatchCommand (ICommandContext? commandContext) { CommandContext? keyCommandContext = commandContext is CommandContext ? (CommandContext)commandContext : default; @@ -342,10 +300,6 @@ private void AddCommands () cancel = true; } - if (_targetView is { }) - { - _targetView.InvokeCommand (Command, commandContext); - } return cancel; } diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index a8ee3486ae..6287178c11 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1229,7 +1229,7 @@ private void Adjust () private void CreateContextMenu () { DisposeContextMenu (); - ContextMenuv2 menu = new (new List () + ContextMenuv2 menu = new (new List () { new (this, Command.SelectAll, Strings.ctxSelectAll), new (this, Command.DeleteAll, Strings.ctxDeleteAll), diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b422451eeb..1f79608a42 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -4151,7 +4151,7 @@ private void Adjust () private ContextMenuv2 CreateContextMenu () { - ContextMenuv2 menu = new (new List () + ContextMenuv2 menu = new (new List () { new (this, Command.SelectAll, Strings.ctxSelectAll), new (this, Command.DeleteAll, Strings.ctxDeleteAll), diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index ee33278e5b..f0cdbaf986 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -141,6 +141,15 @@ private void App_Loaded (object? sender, EventArgs e) Y = Pos.Bottom (label), }; ConfigureMenu (bar); + + var cascadeShortcut = new Shortcut + { + Title = "_Cascade", + Text = "Cascade...", + HighlightStyle = HighlightStyle.Hover + }; + bar.Add (cascadeShortcut); + bar.Arrangement = ViewArrangement.RightResizable; menuLikeExamples.Add (bar); @@ -312,7 +321,7 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) if (barView is Menuv2 menuv2) { - menuv2.ShortcutCommandInvoked += (o, args) => + menuv2.MenuItemCommandInvoked += (o, args) => { if (args.Context is CommandContext { Binding.Data: Shortcut { } sc }) { @@ -499,7 +508,7 @@ private void ConfigMenuBar (Bar bar) //ConfigureMenu (fileMenu); - var fileMenuBarItem = new Shortcut (fileMenu, Command.Context, "_File", "File Menu") + var fileMenuBarItem = new MenuItemv2 (fileMenu, Command.Context, "_File", "File Menu") { Id = "fileMenuBarItem", Key = Key.D0.WithAlt, @@ -543,7 +552,7 @@ private void ConfigMenuBar (Bar bar) }; ConfigureMenu (editMenu); - var editMenuBarItem = new Shortcut (editMenu, Command.Edit, "_Edit", "Edit Menu") + var editMenuBarItem = new MenuItemv2 (editMenu, Command.Edit, "_Edit", "Edit Menu") { Title = "_Edit", HighlightStyle = HighlightStyle.Hover diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 1213a91652..aaf9520d66 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -100,14 +100,14 @@ public override void Main () // Shutdown - Calling Application.Shutdown is required. Application.Shutdown (); } - private Shortcut [] GetSupportedCultures () + private MenuItemv2 [] GetSupportedCultures () { - List supportedCultures = new (); + List supportedCultures = new (); int index = -1; foreach (CultureInfo c in _cultureInfos) { - Shortcut culture = new (); + MenuItemv2 culture = new (); culture.CommandView = new CheckBox () { CanFocus = false, HighlightStyle = HighlightStyle.None }; @@ -136,13 +136,13 @@ private Shortcut [] GetSupportedCultures () return supportedCultures.ToArray (); - void CreateAction (List cultures, Shortcut culture) + void CreateAction (List cultures, MenuItemv2 culture) { culture.Action += () => { Thread.CurrentThread.CurrentUICulture = new (culture.HelpText); - foreach (Shortcut item in cultures) + foreach (MenuItemv2 item in cultures) { ((CheckBox)item.CommandView).CheckedState = Thread.CurrentThread.CurrentUICulture.Name == item.HelpText ? CheckState.Checked : CheckState.UnChecked; } @@ -159,17 +159,17 @@ private void CreateWinContextMenu () _winContextMenu = null; } - var cultureShortcuts = GetSupportedCultures (); - foreach (Shortcut shortcut in cultureShortcuts) + var cultureMenuItems = GetSupportedCultures (); + foreach (MenuItemv2 menuItem in cultureMenuItems) { - shortcut.Accepting += (sender, args) => + menuItem.Accepting += (sender, args) => { Application.PopoverHost.Visible = false; _winContextMenu.Visible = false; args.Cancel = false; }; } - _winContextMenu = new (cultureShortcuts) + _winContextMenu = new (cultureMenuItems) { Key = _winContextMenuKey, diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs new file mode 100644 index 0000000000..48d415bf30 --- /dev/null +++ b/UICatalog/Scenarios/MenusV2.cs @@ -0,0 +1,220 @@ +#nullable enable + +using System.Collections.ObjectModel; +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +[ScenarioMetadata ("MenusV2", "Illustrates MenuV2")] +[ScenarioCategory ("Controls")] +[ScenarioCategory ("Shortcuts")] +public class MenusV2 : Scenario +{ + public override void Main () + { + Application.Init (); + Toplevel app = new (); + app.Title = GetQuitKeyAndName (); + + ObservableCollection eventSource = new (); + + var eventLog = new ListView + { + Title = "Event Log", + X = Pos.AnchorEnd (), + Width = Dim.Auto (), + Height = Dim.Fill (), // Make room for some wide things + ColorScheme = Colors.ColorSchemes ["Toplevel"], + Source = new ListWrapper (eventSource) + }; + eventLog.Border!.Thickness = new (0, 1, 0, 0); + + FrameView frame = new () + { + Title = "Cascading Menu...", + X = 0, + Y = 0, + Width = Dim.Fill ()! - Dim.Width (eventLog), + Height = Dim.Fill () + }; + app.Add (frame); + + var menu = new Menuv2 + { + Id = "menu", + X = 10, + Y = 5 + }; + + menu.MenuItemCommandInvoked += (o, args) => + { + if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) + { + eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); + } + + eventLog.MoveDown (); + }; + + frame.Add (menu); + ConfigureMenu (menu); + + var subMenu = new Menuv2 + { + Id = "menu", + X = 0, + Y = 0, + Visible = false + }; + ConfigureMenu (subMenu); + frame.Add (subMenu); + + var cascadeShortcut = new MenuItemv2 (frame, Command.Context, "_Cascade", "Sub menu..."); + + //cascadeShortcut.Accepting += (o, args) => + // { + // Point loc = cascadeShortcut.Frame.Location; + // subMenu.X = loc.X + menu.Frame.Width - 1; + // subMenu.Y = loc.Y; + // subMenu.Visible = !subMenu.Visible; + // }; + + //cascadeShortcut.Highlight += (o, args) => + // { + + // { + // Point loc = cascadeShortcut.Frame.Location; + // subMenu.X = loc.X + menu.Frame.Width - 1; + // subMenu.Y = loc.Y; + // subMenu.Visible = args.NewValue.HasFlag(HighlightStyle.Hover); + // } + // }; + + //subMenu.HasFocusChanged += (o, args) => + // { + // if (!args.NewValue) + // { + // subMenu.Visible = false; + // } + // }; + + menu.Add (cascadeShortcut); + + menu.SubViews.ElementAt (0).SetFocus (); + + FrameView frameView = frame; + + frameView.Accepting += (o, args) => + { + eventSource.Add ($"Accepting: {frameView?.Id}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + foreach (View view1 in frameView.SubViews.Where (b => b is Bar || b is MenuBarv2 || b is Menuv2)!) + { + var barView = (Bar)view1; + + barView.Accepting += (o, args) => + { + eventSource.Add ($"Accepting: {barView!.Id} {args.Context.Command}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + barView.Selecting += (o, args) => + { + eventSource.Add ($"Selecting: {barView!.Id} {args.Context.Command}"); + eventLog.MoveDown (); + args.Cancel = false; + }; + + if (barView is Menuv2 menuv2) + { + menuv2.MenuItemCommandInvoked += (o, args) => + { + if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) + { + eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); + } + + eventLog.MoveDown (); + }; + } + + foreach (View view2 in barView.SubViews.Where (s => s is Shortcut)!) + { + var sh = (Shortcut)view2; + + sh.Accepting += (o, args) => + { + eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + sh.Selecting += (o, args) => + { + eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + eventLog.MoveDown (); + args.Cancel = false; + }; + } + } + + app.Add (eventLog); + + Application.Run (app); + app.Dispose (); + Application.Shutdown (); + } + + private void ConfigureMenu (Menuv2 menu) + { + var shortcut1 = new MenuItemv2 + { + Title = "Z_igzag", + Key = Key.I.WithCtrl, + Text = "Gonna zig zag" + }; + + var shortcut2 = new MenuItemv2 + { + Title = "Za_G", + Text = "Gonna zag", + Key = Key.G.WithAlt + }; + + var shortcut3 = new MenuItemv2 + { + Title = "_Three", + Text = "The 3rd item", + Key = Key.D3.WithAlt + }; + + var line = new Line + { + X = -1, + Width = Dim.Fill ()! + 1 + }; + + var shortcut4 = new MenuItemv2 + { + Title = "_Four", + Text = "Below the line", + Key = Key.D3.WithAlt + }; + + shortcut4.CommandView = new CheckBox + { + Title = shortcut4.Title, + HighlightStyle = HighlightStyle.None, + CanFocus = false + }; + + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + shortcut4.Accepting += (sender, args) => args.Cancel = true; + + menu.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); + } +} diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 2bd070a270..425d2584e8 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -695,6 +695,7 @@ private static void VerifyObjectsWereDisposed () // 'app' closed cleanly. foreach (View? inst in View.Instances) { + Debug.Assert (inst.WasDisposed); } From b00cee2049874ea35d67ed495c302e4d551bd175 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 03:09:51 +0100 Subject: [PATCH 59/75] SubMenu POC --- Terminal.Gui/Input/Command.cs | 5 + Terminal.Gui/View/View.Command.cs | 68 ++++-- Terminal.Gui/View/View.cs | 2 + Terminal.Gui/Views/Menu/MenuItemv2.cs | 89 +++++++- Terminal.Gui/Views/Menu/Menuv2.cs | 30 ++- Terminal.Gui/Views/Menu/PopoverMenu.cs | 45 ++++ UICatalog/Scenarios/Bars.cs | 2 +- UICatalog/Scenarios/MenusV2.cs | 293 +++++++++++++++---------- 8 files changed, 388 insertions(+), 146 deletions(-) create mode 100644 Terminal.Gui/Views/Menu/PopoverMenu.cs diff --git a/Terminal.Gui/Input/Command.cs b/Terminal.Gui/Input/Command.cs index 0d7be7ea80..a939a4f927 100644 --- a/Terminal.Gui/Input/Command.cs +++ b/Terminal.Gui/Input/Command.cs @@ -14,6 +14,11 @@ namespace Terminal.Gui; /// public enum Command { + /// + /// Indicates the command is not bound or invalid. Will call . + /// + NotBound = 0, + #region Base View Commands /// diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index a273212fce..06cede38e1 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -14,6 +14,9 @@ public partial class View // Command APIs /// private void SetupCommands () { + // NotBound - Invoked if no handler is bound + AddCommand (Command.NotBound, RaiseUnboundCommand); + // Enter - Raise Accepted AddCommand (Command.Accept, RaiseAccepting); @@ -50,6 +53,45 @@ private void SetupCommands () }); } + /// + /// Called when a command that has not been bound is invoked. + /// + /// + /// if no event was raised; input processing should continue. + /// if the event was raised and was not handled (or cancelled); input processing should continue. + /// if the event was raised and handled (or cancelled); input processing should stop. + /// + protected bool? RaiseUnboundCommand (ICommandContext? ctx) + { + CommandEventArgs args = new () { Context = ctx }; + + // Best practice is to invoke the virtual method first. + // This allows derived classes to handle the event and potentially cancel it. + if (OnUnboundCommand (args) || args.Cancel) + { + return true; + } + + // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. + UnboundCommand?.Invoke (this, args); + + return UnboundCommand is null ? null : args.Cancel; + } + + /// + /// Called when a command that has not been bound is invoked. + /// Set CommandEventArgs.Cancel to + /// and return to cancel the event. The default implementation does nothing. + /// + /// The event arguments. + /// to stop processing. + protected virtual bool OnUnboundCommand (CommandEventArgs args) { return false; } + + /// + /// Cancelable event raised when a command that has not been bound is invoked. + /// + public event EventHandler? UnboundCommand; + /// /// Called when the user is accepting the state of the View and the has been invoked. Calls which can be cancelled; if not cancelled raises . /// event. The default handler calls this method. @@ -294,9 +336,7 @@ private void SetupCommands () { if (!_commandImplementations.ContainsKey (command)) { - throw new NotSupportedException ( - @$"A Binding was set up for the command {command} ({binding}) but that command is not supported by this View ({GetType ().Name})" - ); + Logging.Warning (@$"{command} is not supported by this View ({GetType ().Name}). Binding: {binding}."); } // each command has its own return value @@ -327,16 +367,15 @@ private void SetupCommands () /// public bool? InvokeCommand (Command command, TBindingType binding) { - if (_commandImplementations.TryGetValue (command, out CommandImplementation? implementation)) + if (!_commandImplementations.TryGetValue (command, out CommandImplementation? implementation)) { - return implementation (new CommandContext () - { - Command = command, - Binding = binding, - }); + _commandImplementations.TryGetValue (Command.NotBound, out implementation); } - - return null; + return implementation! (new CommandContext () + { + Command = command, + Binding = binding, + }); } /// @@ -350,11 +389,12 @@ private void SetupCommands () /// public bool? InvokeCommand (Command command) { - if (_commandImplementations.TryGetValue (command, out CommandImplementation? implementation)) + if (!_commandImplementations.TryGetValue (command, out CommandImplementation? implementation)) { - return implementation (null); + _commandImplementations.TryGetValue (Command.NotBound, out implementation); } - return null; + return implementation! (null); + } } diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 45f227cf7d..48ed28ccd6 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -337,6 +337,8 @@ public virtual bool Visible if (!_visible) { + // BUGBUG: Ideally we'd reset _previouslyFocused to the first focusable subview + _previouslyFocused = null; if (HasFocus) { HasFocus = false; diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index 515642ff5d..5e72f41ab1 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -37,43 +37,114 @@ public MenuItemv2 () : base (Key.Empty, null, null, null) /// /// The text to display for the command. /// The help text to display. - public MenuItemv2 (View targetView, Command command, string commandText, string? helpText = null) + /// + public MenuItemv2 (View targetView, Command command, string commandText, string? helpText = null, Menuv2? subMenu = null) : base ( targetView?.HotKeyBindings.GetFirstFromCommands (command)!, commandText, null, helpText) { - _targetView = targetView; + TargetView = targetView; Command = command; - if (Command == Command.Context) + if (subMenu is { }) { + // TODO: This is a temporary hack - add a flag or something instead KeyView.Text = $"{Glyphs.RightArrow}"; } - } - private readonly View? _targetView; // If set, _command will be invoked + SubMenu = subMenu; + } /// /// Gets the target that the will be invoked on. /// - public View? TargetView => _targetView; + public View? TargetView { get; set; } /// /// Gets the that will be invoked on when the Shortcut is activated. /// - public Command Command { get; } + public Command Command { get; set; } internal override bool? DispatchCommand (ICommandContext? commandContext) { bool? ret = base.DispatchCommand (commandContext); - if (_targetView is { }) + if (TargetView is { }) + { + ret = TargetView.InvokeCommand (Command, commandContext); + } + + if (SubMenu is { }) { - _targetView.InvokeCommand (Command, commandContext); + RaiseActivateSubMenu (); } return ret; } + + /// + /// + /// + public Menuv2? SubMenu { get; set; } + + /// + protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view) + { + base.OnHasFocusChanged(newHasFocus, previousFocusedView, view); + if (SubMenu is null || view == SubMenu) + { + return; + } + + if (newHasFocus) + { + if (!SubMenu.Visible) + { + RaiseActivateSubMenu (); + } + } + else + { + SubMenu.Visible = false; + } + } + + /// + /// + /// + /// + protected void RaiseActivateSubMenu () + { + if (SubMenu is null) + { + return; + } + + OnActivateSubMenu (); + + // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. + var args = new EventArgs (SubMenu); + ActivateSubMenu?.Invoke (this, args); + } + + /// + /// + protected virtual void OnActivateSubMenu () { } + + /// + /// + public event EventHandler>? ActivateSubMenu; + + /// + protected override void Dispose (bool disposing) + { + if (disposing) + { + SubMenu?.Dispose (); + SubMenu = null; + } + base.Dispose (disposing); + } } diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index a1cf926ab4..3ee9351d15 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -1,6 +1,7 @@ #nullable enable using System; using System.ComponentModel; +using System.Net.Http.Headers; using System.Reflection; namespace Terminal.Gui; @@ -59,23 +60,27 @@ private void Menuv2_Initialized (object? sender, EventArgs e) menuItem.Orientation = Orientation.Vertical; menuItem.HighlightStyle |= HighlightStyle.Hover; + AddCommand (menuItem.Command, RaiseMenuItemCommandInvoked); + menuItem.Accepting += MenuItemtOnAccepting; - AddCommand (menuItem.Command, (ctx) => + menuItem.ActivateSubMenu += MenuItemOnActivateSubMenu; + void MenuItemOnActivateSubMenu (object? sender, EventArgs e) { - return RaiseMenuItemCommandInvoked (ctx); - }); + Logging.Trace ($"MenuItemOnActivateSubMenu: {e}"); + if (e.CurrentValue is { }) + { + SuperView.Add (e.CurrentValue); + e.CurrentValue.X = Frame.X + Frame.Width; + e.CurrentValue.Y = Frame.Y + menuItem.Frame.Y; + e.CurrentValue.Visible = true; + } + } void MenuItemtOnAccepting (object? sender, CommandEventArgs e) { - if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible) - { - Visible = false; - // e.Cancel = true; - - return; - } + //Logging.Trace($"MenuItemtOnAccepting: {e.Context}"); } } @@ -90,11 +95,12 @@ void MenuItemtOnAccepting (object? sender, CommandEventArgs e) /// protected bool? RaiseMenuItemCommandInvoked (ICommandContext? ctx) { + Logging.Trace($"RaiseMenuItemCommandInvoked: {ctx}"); CommandEventArgs args = new () { Context = ctx }; // Best practice is to invoke the virtual method first. // This allows derived classes to handle the event and potentially cancel it. - args.Cancel = OnShortcutCommandInvoked (args) || args.Cancel; + args.Cancel = OnMenuItemCommandInvoked (args) || args.Cancel; if (!args.Cancel) { @@ -116,7 +122,7 @@ void MenuItemtOnAccepting (object? sender, CommandEventArgs e) /// /// /// to stop processing. - protected virtual bool OnShortcutCommandInvoked (CommandEventArgs args) { return false; } + protected virtual bool OnMenuItemCommandInvoked (CommandEventArgs args) { return false; } /// /// Cancelable event raised when the user is accepting the state of the View and the has been invoked. Set diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs new file mode 100644 index 0000000000..f954197f2d --- /dev/null +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -0,0 +1,45 @@ +#nullable enable +namespace Terminal.Gui; + +/// +/// +/// +public class PopoverMenu : View +{ + /// + /// + /// + public PopoverMenu (Menuv2 root) + { + CanFocus = true; + Width = Dim.Fill (); + Height = Dim.Fill (); + ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; + base.Visible = false; + base.ColorScheme = Colors.ColorSchemes ["Menu"]; + + Root = root; + base.Add (root); + + root.Accepting += RootOnAccepting; + root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; + + + return; + + void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) + { + Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); + } + + void RootOnAccepting (object? sender, CommandEventArgs e) + { + Logging.Trace($"RootOnAccepting: {e.Context}"); + } + } + + /// + /// + /// + public Menuv2 Root { get; init; } +} diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index f0cdbaf986..22bf3111ba 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -323,7 +323,7 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) { menuv2.MenuItemCommandInvoked += (o, args) => { - if (args.Context is CommandContext { Binding.Data: Shortcut { } sc }) + if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) { eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index 48d415bf30..d5ebe5a8b0 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -1,7 +1,12 @@ #nullable enable using System.Collections.ObjectModel; +using Microsoft.Extensions.Logging; using Terminal.Gui; +using Serilog; +using Serilog.Core; +using Serilog.Events; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace UICatalog.Scenarios; @@ -12,6 +17,8 @@ public class MenusV2 : Scenario { public override void Main () { + Logging.Logger = CreateLogger (); + Application.Init (); Toplevel app = new (); app.Title = GetQuitKeyAndName (); @@ -31,153 +38,189 @@ public override void Main () FrameView frame = new () { + Id = "frame", Title = "Cascading Menu...", - X = 0, - Y = 0, Width = Dim.Fill ()! - Dim.Width (eventLog), Height = Dim.Fill () }; app.Add (frame); - var menu = new Menuv2 + var rootMenu = new Menuv2 () { - Id = "menu", - X = 10, - Y = 5 + Id = "rootMenu", }; - - menu.MenuItemCommandInvoked += (o, args) => - { - if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) - { - eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); - } - - eventLog.MoveDown (); - }; - - frame.Add (menu); - ConfigureMenu (menu); + ConfigureRootMenu (frame, rootMenu); var subMenu = new Menuv2 { - Id = "menu", - X = 0, - Y = 0, + Id = "subMenu", Visible = false }; - ConfigureMenu (subMenu); - frame.Add (subMenu); - - var cascadeShortcut = new MenuItemv2 (frame, Command.Context, "_Cascade", "Sub menu..."); - - //cascadeShortcut.Accepting += (o, args) => - // { - // Point loc = cascadeShortcut.Frame.Location; - // subMenu.X = loc.X + menu.Frame.Width - 1; - // subMenu.Y = loc.Y; - // subMenu.Visible = !subMenu.Visible; - // }; - - //cascadeShortcut.Highlight += (o, args) => - // { + ConfigureSubMenu1 (frame, subMenu); - // { - // Point loc = cascadeShortcut.Frame.Location; - // subMenu.X = loc.X + menu.Frame.Width - 1; - // subMenu.Y = loc.Y; - // subMenu.Visible = args.NewValue.HasFlag(HighlightStyle.Hover); - // } - // }; + var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); + rootMenu.Add (cascadeShortcut); - //subMenu.HasFocusChanged += (o, args) => - // { - // if (!args.NewValue) - // { - // subMenu.Visible = false; - // } - // }; + var popoverMenu = new PopoverMenu (rootMenu) + { + Id = "popOverMenu", + Visible = true, + X =1, Y = 1 + }; - menu.Add (cascadeShortcut); + //Application.PopoverHost.Add (popoverMenu); + //Application.PopoverHost.Visible = true; - menu.SubViews.ElementAt (0).SetFocus (); + rootMenu.SubViews.ElementAt (0).SetFocus (); FrameView frameView = frame; + frameView.Add (popoverMenu); - frameView.Accepting += (o, args) => + frameView.UnboundCommand += (o, args) => { - eventSource.Add ($"Accepting: {frameView?.Id}"); + eventSource.Add ($"{args.Context!.Command}: {frameView?.Id}"); eventLog.MoveDown (); args.Cancel = true; }; - foreach (View view1 in frameView.SubViews.Where (b => b is Bar || b is MenuBarv2 || b is Menuv2)!) - { - var barView = (Bar)view1; - - barView.Accepting += (o, args) => - { - eventSource.Add ($"Accepting: {barView!.Id} {args.Context.Command}"); - eventLog.MoveDown (); - args.Cancel = true; - }; - - barView.Selecting += (o, args) => - { - eventSource.Add ($"Selecting: {barView!.Id} {args.Context.Command}"); - eventLog.MoveDown (); - args.Cancel = false; - }; - - if (barView is Menuv2 menuv2) - { - menuv2.MenuItemCommandInvoked += (o, args) => - { - if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) - { - eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); - } - - eventLog.MoveDown (); - }; - } - - foreach (View view2 in barView.SubViews.Where (s => s is Shortcut)!) - { - var sh = (Shortcut)view2; - - sh.Accepting += (o, args) => - { - eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - eventLog.MoveDown (); - args.Cancel = true; - }; - - sh.Selecting += (o, args) => - { - eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - eventLog.MoveDown (); - args.Cancel = false; - }; - } + frameView.Accepting += (o, args) => + { + eventSource.Add ($"{args.Context!.Command}: {frameView?.Id}"); + eventLog.MoveDown (); + // args.Cancel = true; + }; + + var menu = popoverMenu; + + menu.Accepting += (o, args) => + { + //Logging.Trace($"Accepting: {menu!.Id} {args.Context.Command}"); + //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); + //eventLog.MoveDown (); + //args.Cancel = true; + }; + + menu.Selecting += (o, args) => + { + //Logging.Trace ($"Selecting: {menu!.Id} {args.Context.Command}"); + //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); + //eventLog.MoveDown (); + //args.Cancel = false; + }; + + popoverMenu.Root.MenuItemCommandInvoked += (o, args) => + { + if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) + { + Logging.Trace($"Invoked: {sc.Title} {args.Context.Command}"); + eventSource.Add ($"Invoked: {sc.Title} {args.Context.Command}"); + //args.Cancel = true; + } + + eventLog.MoveDown (); + }; + + + foreach (View view2 in popoverMenu.Root.SubViews.Where (s => s is MenuItemv2)!) + { + var sh = (MenuItemv2)view2; + + sh.Accepting += (o, args) => + { + //Logging.Trace($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventLog.MoveDown (); + //args.Cancel = true; + }; + + sh.Selecting += (o, args) => + { + //Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventLog.MoveDown (); + //args.Cancel = false; + }; } app.Add (eventLog); Application.Run (app); app.Dispose (); + popoverMenu.Dispose (); Application.Shutdown (); } - private void ConfigureMenu (Menuv2 menu) + private void ConfigureRootMenu (View targetView, Menuv2 menu) { var shortcut1 = new MenuItemv2 { - Title = "Z_igzag", - Key = Key.I.WithCtrl, - Text = "Gonna zig zag" + Title = "_New", + Key = Key.N.WithCtrl, + Text = "New File", + Command = Command.New, + TargetView = targetView + }; + + var shortcut2 = new MenuItemv2 + { + Title = "_Open...", + Text = "Open File", + Key = Key.O.WithCtrl, + Command = Command.Open, + TargetView = targetView + }; + + var shortcut3 = new MenuItemv2 + { + Title = "_Save", + Text = "Save file", + Key = Key.S.WithCtrl, + Command = Command.Save, + TargetView = targetView + }; + + var shortcut4 = new MenuItemv2 + { + Title = "Sa_ve As...", + Text = "Save file as", + Key = Key.V.WithCtrl, + Command = Command.SaveAs, + TargetView = targetView + + }; + + + var shortcut5 = new MenuItemv2 + { + Title = "_Auto Save", + Text = "Automatically save", + Key = Key.A.WithCtrl, + TargetView = targetView + }; + + shortcut5.CommandView = new CheckBox + { + Title = shortcut5.Title, + HighlightStyle = HighlightStyle.None, + CanFocus = false }; + var line = new Line + { + X = -1, + Width = Dim.Fill ()! + 1 + }; + + + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + //shortcut4.Accepting += (sender, args) => args.Cancel = true; + + menu.Add (shortcut1, shortcut2, shortcut3, shortcut4, line, shortcut5); + } + + + private void ConfigureSubMenu1 (View targetView, Menuv2 menu) + { var shortcut2 = new MenuItemv2 { Title = "Za_G", @@ -213,8 +256,38 @@ private void ConfigureMenu (Menuv2 menu) }; // This ensures the checkbox state toggles when the hotkey of Title is pressed. - shortcut4.Accepting += (sender, args) => args.Cancel = true; + //shortcut4.Accepting += (sender, args) => args.Cancel = true; - menu.Add (shortcut1, shortcut2, shortcut3, line, shortcut4); + menu.Add (shortcut2, shortcut3, line, shortcut4); + } + private const string LOGFILE_LOCATION = "./logs"; + private static string _logFilePath = string.Empty; + private static readonly LoggingLevelSwitch _logLevelSwitch = new (); + + private static ILogger CreateLogger () + { + // Configure Serilog to write logs to a file + _logLevelSwitch.MinimumLevel = LogEventLevel.Verbose; + Log.Logger = new LoggerConfiguration () + .MinimumLevel.ControlledBy (_logLevelSwitch) + .Enrich.FromLogContext () // Enables dynamic enrichment + .WriteTo.Debug () + .WriteTo.File ( + _logFilePath, + rollingInterval: RollingInterval.Day, + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") + .CreateLogger (); + + // Create a logger factory compatible with Microsoft.Extensions.Logging + using ILoggerFactory loggerFactory = LoggerFactory.Create ( + builder => + { + builder + .AddSerilog (dispose: true) // Integrate Serilog with ILogger + .SetMinimumLevel (LogLevel.Trace); // Set minimum log level + }); + + // Get an ILogger instance + return loggerFactory.CreateLogger ("Global Logger"); } } From 856fe8a43cb4794f38428446013d4a3abcf68d69 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 03:33:29 +0100 Subject: [PATCH 60/75] CommandNotBound --- Terminal.Gui/Input/Command.cs | 2 +- Terminal.Gui/View/View.Command.cs | 14 ++-- Terminal.Gui/Views/Menu/MenuItemv2.cs | 2 +- Terminal.Gui/Views/Menu/PopoverMenu.cs | 66 ++++++++++++++----- .../View/ViewCommandTests.cs | 63 +++++++++++++++++- UICatalog/Scenarios/MenusV2.cs | 2 +- 6 files changed, 121 insertions(+), 28 deletions(-) diff --git a/Terminal.Gui/Input/Command.cs b/Terminal.Gui/Input/Command.cs index a939a4f927..6e2a05bcdf 100644 --- a/Terminal.Gui/Input/Command.cs +++ b/Terminal.Gui/Input/Command.cs @@ -15,7 +15,7 @@ namespace Terminal.Gui; public enum Command { /// - /// Indicates the command is not bound or invalid. Will call . + /// Indicates the command is not bound or invalid. Will call . /// NotBound = 0, diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 06cede38e1..998867113e 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -15,7 +15,7 @@ public partial class View // Command APIs private void SetupCommands () { // NotBound - Invoked if no handler is bound - AddCommand (Command.NotBound, RaiseUnboundCommand); + AddCommand (Command.NotBound, RaiseCommandNotBound); // Enter - Raise Accepted AddCommand (Command.Accept, RaiseAccepting); @@ -61,21 +61,21 @@ private void SetupCommands () /// if the event was raised and was not handled (or cancelled); input processing should continue. /// if the event was raised and handled (or cancelled); input processing should stop. /// - protected bool? RaiseUnboundCommand (ICommandContext? ctx) + protected bool? RaiseCommandNotBound (ICommandContext? ctx) { CommandEventArgs args = new () { Context = ctx }; // Best practice is to invoke the virtual method first. // This allows derived classes to handle the event and potentially cancel it. - if (OnUnboundCommand (args) || args.Cancel) + if (OnCommandNotBound (args) || args.Cancel) { return true; } // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. - UnboundCommand?.Invoke (this, args); + CommandNotBound?.Invoke (this, args); - return UnboundCommand is null ? null : args.Cancel; + return CommandNotBound is null ? null : args.Cancel; } /// @@ -85,12 +85,12 @@ private void SetupCommands () /// /// The event arguments. /// to stop processing. - protected virtual bool OnUnboundCommand (CommandEventArgs args) { return false; } + protected virtual bool OnCommandNotBound (CommandEventArgs args) { return false; } /// /// Cancelable event raised when a command that has not been bound is invoked. /// - public event EventHandler? UnboundCommand; + public event EventHandler? CommandNotBound; /// /// Called when the user is accepting the state of the View and the has been invoked. Calls which can be cancelled; if not cancelled raises . diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index 5e72f41ab1..57cba2db0e 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -13,7 +13,7 @@ public class MenuItemv2 : Shortcut /// public MenuItemv2 () : base (Key.Empty, null, null, null) { - HighlightStyle = HighlightStyle.Hover; + //HighlightStyle = HighlightStyle.Hover; } /// diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index f954197f2d..87ce3c63e3 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -9,7 +9,15 @@ public class PopoverMenu : View /// /// /// - public PopoverMenu (Menuv2 root) + public PopoverMenu () : this (null) + { + + } + + /// + /// + /// + public PopoverMenu (Menuv2? root) { CanFocus = true; Width = Dim.Fill (); @@ -19,27 +27,51 @@ public PopoverMenu (Menuv2 root) base.ColorScheme = Colors.ColorSchemes ["Menu"]; Root = root; - base.Add (root); - - root.Accepting += RootOnAccepting; - root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; + } - return; + private Menuv2? _root; - void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) + /// + /// + /// + public Menuv2? Root + { + get => _root; + set { - Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); - } + if (_root == value) + { + return; + } - void RootOnAccepting (object? sender, CommandEventArgs e) - { - Logging.Trace($"RootOnAccepting: {e.Context}"); + if (_root is { }) + { + base.Remove (_root); + _root.Accepting -= RootOnAccepting; + _root.MenuItemCommandInvoked -= RootOnMenuItemCommandInvoked; + } + + _root = value; + + if (_root is { }) + { + base.Add (_root); + _root.Accepting += RootOnAccepting; + _root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; + } + + return; + + void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) + { + Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); + } + + void RootOnAccepting (object? sender, CommandEventArgs e) + { + Logging.Trace ($"RootOnAccepting: {e.Context}"); + } } } - - /// - /// - /// - public Menuv2 Root { get; init; } } diff --git a/Tests/UnitTestsParallelizable/View/ViewCommandTests.cs b/Tests/UnitTestsParallelizable/View/ViewCommandTests.cs index 96434d9905..5671dbd9e3 100644 --- a/Tests/UnitTestsParallelizable/View/ViewCommandTests.cs +++ b/Tests/UnitTestsParallelizable/View/ViewCommandTests.cs @@ -226,10 +226,52 @@ public void HotKey_Command_SetsFocus () #endregion OnHotKey/HotKey tests + #region InvokeCommand Tests + + + [Fact] + public void InvokeCommand_NotBound_Invokes_CommandNotBound () + { + ViewEventTester view = new (); + + view.InvokeCommand (Command.NotBound); + + Assert.False (view.HasFocus); + Assert.Equal (1, view.OnCommandNotBoundCount); + Assert.Equal (1, view.CommandNotBoundCount); + } + + [Fact] + public void InvokeCommand_Command_Not_Bound_Invokes_CommandNotBound () + { + ViewEventTester view = new (); + + view.InvokeCommand (Command.New); + + Assert.False (view.HasFocus); + Assert.Equal (1, view.OnCommandNotBoundCount); + Assert.Equal (1, view.CommandNotBoundCount); + } + + [Fact] + public void InvokeCommand_Command_Bound_Does_Not_Invoke_CommandNotBound () + { + ViewEventTester view = new (); + + view.InvokeCommand (Command.Accept); + + Assert.False (view.HasFocus); + Assert.Equal (0, view.OnCommandNotBoundCount); + Assert.Equal (0, view.CommandNotBoundCount); + } + + #endregion + public class ViewEventTester : View { public ViewEventTester () { + Id = "viewEventTester"; CanFocus = true; Accepting += (s, a) => @@ -249,6 +291,12 @@ public ViewEventTester () a.Cancel = HandleSelecting; SelectingCount++; }; + + CommandNotBound += (s, a) => + { + a.Cancel = HandleCommandNotBound; + CommandNotBoundCount++; + }; } public int OnAcceptedCount { get; set; } @@ -282,6 +330,8 @@ protected override bool OnHandlingHotKey (CommandEventArgs args) public int OnSelectingCount { get; set; } public int SelectingCount { get; set; } public bool HandleOnSelecting { get; set; } + public bool HandleSelecting { get; set; } + /// protected override bool OnSelecting (CommandEventArgs args) @@ -291,6 +341,17 @@ protected override bool OnSelecting (CommandEventArgs args) return HandleOnSelecting; } - public bool HandleSelecting { get; set; } + public int OnCommandNotBoundCount { get; set; } + public int CommandNotBoundCount { get; set; } + + public bool HandleOnCommandNotBound { get; set; } + + public bool HandleCommandNotBound { get; set; } + + protected override bool OnCommandNotBound (CommandEventArgs args) + { + OnCommandNotBoundCount++; + return HandleOnCommandNotBound; + } } } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index d5ebe5a8b0..607bafd534 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -76,7 +76,7 @@ public override void Main () FrameView frameView = frame; frameView.Add (popoverMenu); - frameView.UnboundCommand += (o, args) => + frameView.CommandNotBound += (o, args) => { eventSource.Add ($"{args.Context!.Command}: {frameView?.Id}"); eventLog.MoveDown (); From a138289250dcd1281a5be1e1117acaa957b35a39 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 07:56:05 +0100 Subject: [PATCH 61/75] More POC --- Terminal.Gui/Application/Application.Run.cs | 2 +- Terminal.Gui/View/View.Navigation.cs | 22 ++ Terminal.Gui/View/View.cs | 2 +- Terminal.Gui/Views/Menu/MenuItemv2.cs | 72 +++-- Terminal.Gui/Views/Menu/Menuv2.cs | 108 +++++-- Terminal.Gui/Views/Menu/PopoverMenu.cs | 4 +- Terminal.Gui/Views/Shortcut.cs | 27 +- UICatalog/Scenarios/Generic.cs | 10 +- UICatalog/Scenarios/MenusV2.cs | 341 ++++++++++---------- UICatalog/UICatalog.cs | 1 - 10 files changed, 373 insertions(+), 216 deletions(-) diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index e0e4521c33..2b59eaab21 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -430,7 +430,7 @@ internal static void LayoutAndDrawImpl (bool forceDraw = false) if (PopoverHost is { Visible: true }) { - PopoverHost.SetNeedsDraw(); + //PopoverHost.SetNeedsDraw(); tops.Insert (0, PopoverHost); } diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index 7cce949d49..a37bd7ed06 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -315,6 +315,22 @@ public View? Focused } } + internal void RaiseFocusedChanged (View? previousFocused, View? focused) + { + //Logging.Trace($"RaiseFocusedChanged: {focused.Title}"); + OnFocusedChanged (previousFocused, focused); + FocusedChanged?.Invoke (this, new HasFocusEventArgs (true, true, previousFocused, focused)); + } + + /// + /// + /// + /// + /// + protected virtual void OnFocusedChanged (View? previousFocused, View? focused) { } + + public event EventHandler? FocusedChanged; + /// Returns a value indicating if this View is currently on Top (Active) public bool IsCurrentTop => Application.Top == this; @@ -853,6 +869,7 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false private void RaiseFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedView) { + // If we are the most focused view, we need to set the focused view in Application.Navigation if (newHasFocus && focusedView?.Focused is null) { Application.Navigation?.SetFocused (focusedView); @@ -864,6 +881,11 @@ private void RaiseFocusChanged (bool newHasFocus, View? previousFocusedView, Vie // Raise the event var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedView); HasFocusChanged?.Invoke (this, args); + + if (newHasFocus) + { + SuperView?.RaiseFocusedChanged (previousFocusedView, focusedView); + } } /// diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 48ed28ccd6..21885b6fd3 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -338,7 +338,7 @@ public virtual bool Visible if (!_visible) { // BUGBUG: Ideally we'd reset _previouslyFocused to the first focusable subview - _previouslyFocused = null; + _previouslyFocused = SubViews.FirstOrDefault(v => v.CanFocus); if (HasFocus) { HasFocus = false; diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index 57cba2db0e..0bc647ba35 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -1,5 +1,7 @@ #nullable enable +using System.ComponentModel; + namespace Terminal.Gui; /// @@ -13,7 +15,6 @@ public class MenuItemv2 : Shortcut /// public MenuItemv2 () : base (Key.Empty, null, null, null) { - //HighlightStyle = HighlightStyle.Hover; } /// @@ -78,7 +79,7 @@ public MenuItemv2 (View targetView, Command command, string commandText, string? if (SubMenu is { }) { - RaiseActivateSubMenu (); + // RaiseActivateSubMenu (); } return ret; @@ -90,25 +91,34 @@ public MenuItemv2 (View targetView, Command command, string commandText, string? public Menuv2? SubMenu { get; set; } /// - protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view) + protected override bool OnMouseEnter (CancelEventArgs eventArgs) { - base.OnHasFocusChanged(newHasFocus, previousFocusedView, view); - if (SubMenu is null || view == SubMenu) - { - return; - } + // Logging.Trace($"OnEnter {Title}"); + SetFocus (); + return base.OnMouseEnter (eventArgs); + } - if (newHasFocus) - { - if (!SubMenu.Visible) - { - RaiseActivateSubMenu (); - } - } - else - { - SubMenu.Visible = false; - } + /// + protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view) + { + //SetNeedsDraw(); + base.OnHasFocusChanged (newHasFocus, previousFocusedView, view); + //if (SubMenu is null || view == SubMenu) + //{ + // return; + //} + + //if (newHasFocus) + //{ + // if (!SubMenu.Visible) + // { + // RaiseActivateSubMenu (); + // } + //} + //else + //{ + // SubMenu.Visible = false; + //} } /// @@ -136,6 +146,30 @@ protected virtual void OnActivateSubMenu () { } /// /// public event EventHandler>? ActivateSubMenu; + + ///// + //public override Attribute GetNormalColor () + //{ + // if (HasFocus || SubMenu is { Visible: true }) + // { + // return base.GetFocusColor (); + // } + + // return base.GetNormalColor (); + + //} + + ///// + //public override Attribute GetHotNormalColor () + //{ + // if (HasFocus || SubMenu is { Visible: true }) + // { + // return base.GetHotFocusColor (); + // } + + // return base.GetHotNormalColor (); + + //} /// protected override void Dispose (bool disposing) diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index 3ee9351d15..8f3f70097f 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -27,6 +27,8 @@ private void OnVisibleChanged (object? sender, EventArgs e) { if (Visible) { + SelectedMenuItem = SubViews.Where (mi => mi is MenuItemv2).ElementAtOrDefault (0) as MenuItemv2; + //Application.GrabMouse(this); } else @@ -49,42 +51,39 @@ private void Menuv2_Initialized (object? sender, EventArgs e) ColorScheme = Colors.ColorSchemes ["Menu"]; } - /// - public override View? Add (View? view) + /// + protected override void OnSubViewAdded (View view) { - base.Add (view); + base.OnSubViewAdded (view); if (view is MenuItemv2 menuItem) { menuItem.CanFocus = true; menuItem.Orientation = Orientation.Vertical; - menuItem.HighlightStyle |= HighlightStyle.Hover; AddCommand (menuItem.Command, RaiseMenuItemCommandInvoked); menuItem.Accepting += MenuItemtOnAccepting; - menuItem.ActivateSubMenu += MenuItemOnActivateSubMenu; - void MenuItemOnActivateSubMenu (object? sender, EventArgs e) - { - Logging.Trace ($"MenuItemOnActivateSubMenu: {e}"); - - if (e.CurrentValue is { }) - { - SuperView.Add (e.CurrentValue); - e.CurrentValue.X = Frame.X + Frame.Width; - e.CurrentValue.Y = Frame.Y + menuItem.Frame.Y; - e.CurrentValue.Visible = true; - } - } + //menuItem.ActivateSubMenu += MenuItemOnActivateSubMenu; + //void MenuItemOnActivateSubMenu (object? sender, EventArgs e) + //{ + // Logging.Trace ($"MenuItemOnActivateSubMenu: {e}"); + + // if (e.CurrentValue is { }) + // { + // SuperView.Add (e.CurrentValue); + // e.CurrentValue.X = Frame.X + Frame.Width; + // e.CurrentValue.Y = Frame.Y + menuItem.Frame.Y; + // e.CurrentValue.Visible = true; + // } + //} void MenuItemtOnAccepting (object? sender, CommandEventArgs e) { //Logging.Trace($"MenuItemtOnAccepting: {e.Context}"); } } - - return view; } @@ -95,7 +94,7 @@ void MenuItemtOnAccepting (object? sender, CommandEventArgs e) /// protected bool? RaiseMenuItemCommandInvoked (ICommandContext? ctx) { - Logging.Trace($"RaiseMenuItemCommandInvoked: {ctx}"); + Logging.Trace ($"RaiseMenuItemCommandInvoked: {ctx}"); CommandEventArgs args = new () { Context = ctx }; // Best practice is to invoke the virtual method first. @@ -134,4 +133,73 @@ void MenuItemtOnAccepting (object? sender, CommandEventArgs e) /// /// public event EventHandler? MenuItemCommandInvoked; + + /// + protected override void OnFocusedChanged (View? previousFocused, View? focused) + { + base.OnFocusedChanged (previousFocused, focused); + SelectedMenuItem = focused as MenuItemv2; + RaiseSelectedMenuItemChanged (SelectedMenuItem); + + } + + /// + /// + /// + public MenuItemv2? SelectedMenuItem + { + get => Focused as MenuItemv2; + set + { + if (value == Focused) + { + return; + } + + //value?.SetFocus (); + } + } + + internal void RaiseSelectedMenuItemChanged (MenuItemv2? selected) + { + //Logging.Trace ($"RaiseSelectedMenuItemChanged: {selected?.Title}"); + + ShowSubMenu (selected); + OnSelectedMenuItemChanged (selected); + + SelectedMenuItemChanged?.Invoke (this, selected); + } + + /// + /// + /// + /// + protected virtual void OnSelectedMenuItemChanged (MenuItemv2? selected) + { + } + + /// + /// + /// + public event EventHandler? SelectedMenuItemChanged; + + public void ShowSubMenu (MenuItemv2? menuItem) + { + // Hide any other submenus that might be visible + foreach (MenuItemv2 mi in SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) + { + mi.ForceFocusColors = false; + mi.SubMenu!.Visible = false; + SuperView?.Remove (mi.SubMenu); + } + + if (menuItem is { SubMenu: {} }) + { + SuperView?.Add (menuItem.SubMenu); + menuItem.SubMenu.X = Frame.X + Frame.Width; + menuItem.SubMenu.Y = Frame.Y + menuItem.Frame.Y; + menuItem.SubMenu.Visible = true; + menuItem.ForceFocusColors = true; + } + } } \ No newline at end of file diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 87ce3c63e3..8cddced22c 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -22,8 +22,8 @@ public PopoverMenu (Menuv2? root) CanFocus = true; Width = Dim.Fill (); Height = Dim.Fill (); - ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; - base.Visible = false; + //ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; + //base.Visible = false; base.ColorScheme = Colors.ColorSchemes ["Menu"]; Root = root; diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index f8284a2069..774c4c3b00 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -694,12 +694,25 @@ public override ColorScheme? ColorScheme } } + private bool _forceFocusColors; + + public bool ForceFocusColors + { + get => _forceFocusColors; + set + { + _forceFocusColors = value; + SetColors (value); + //SetNeedsDraw(); + } + } + private ColorScheme? _nonFocusColorScheme; /// /// internal void SetColors (bool highlight = false) { - if (HasFocus || highlight) + if (HasFocus || highlight || ForceFocusColors) { if (_nonFocusColorScheme is null) { @@ -711,10 +724,10 @@ internal void SetColors (bool highlight = false) // When we have focus, we invert the colors base.ColorScheme = new (base.ColorScheme) { - Normal = base.ColorScheme.Focus, - HotNormal = base.ColorScheme.HotFocus, - HotFocus = base.ColorScheme.HotNormal, - Focus = base.ColorScheme.Normal + Normal = GetFocusColor(), + HotNormal = GetHotFocusColor(), + HotFocus = GetHotNormalColor(), + Focus = GetNormalColor(), }; } else @@ -735,8 +748,8 @@ internal void SetColors (bool highlight = false) { var cs = new ColorScheme (base.ColorScheme) { - Normal = base.ColorScheme.HotNormal, - HotNormal = base.ColorScheme.Normal + Normal = GetHotNormalColor(), + HotNormal = GetNormalColor() }; KeyView.ColorScheme = cs; } diff --git a/UICatalog/Scenarios/Generic.cs b/UICatalog/Scenarios/Generic.cs index 3fc926dedf..a1d332fb23 100644 --- a/UICatalog/Scenarios/Generic.cs +++ b/UICatalog/Scenarios/Generic.cs @@ -18,6 +18,14 @@ public override void Main () Title = GetQuitKeyAndName (), }; + FrameView frame = new () + { + Height = Dim.Fill (), + Width = Dim.Fill (), + Title = "Frame" + }; + appWindow.Add (frame); + var button = new Button { Id = "button", X = Pos.Center (), Y = 1, Text = "_Press me!" }; button.Accepting += (s, e) => @@ -27,7 +35,7 @@ public override void Main () MessageBox.ErrorQuery ("Error", "You pressed the button!", "_Ok"); }; - appWindow.Add (button); + frame.Add (button); // Run - Start the application. Application.Run (appWindow); diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index 607bafd534..294a2dd12b 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -23,130 +23,143 @@ public override void Main () Toplevel app = new (); app.Title = GetQuitKeyAndName (); - ObservableCollection eventSource = new (); - - var eventLog = new ListView - { - Title = "Event Log", - X = Pos.AnchorEnd (), - Width = Dim.Auto (), - Height = Dim.Fill (), // Make room for some wide things - ColorScheme = Colors.ColorSchemes ["Toplevel"], - Source = new ListWrapper (eventSource) - }; - eventLog.Border!.Thickness = new (0, 1, 0, 0); - - FrameView frame = new () + //ObservableCollection eventSource = new (); + + //var eventLog = new ListView + //{ + // Title = "Event Log", + // X = Pos.AnchorEnd (), + // Width = Dim.Auto (), + // Height = Dim.Fill (), // Make room for some wide things + // ColorScheme = Colors.ColorSchemes ["Toplevel"], + // Source = new ListWrapper (eventSource) + //}; + //eventLog.Border!.Thickness = new (0, 1, 0, 0); + + View frame = new () { Id = "frame", Title = "Cascading Menu...", - Width = Dim.Fill ()! - Dim.Width (eventLog), - Height = Dim.Fill () + + Width = Dim.Fill (),//! - Dim.Width (eventLog), + Height = Dim.Fill (), + BorderStyle = LineStyle.Dotted }; app.Add (frame); - var rootMenu = new Menuv2 () - { - Id = "rootMenu", - }; - ConfigureRootMenu (frame, rootMenu); + //var rootMenu = new Menuv2 () + //{ + // Id = "rootMenu", + //}; + //ConfigureRootMenu (frame, rootMenu); + + //var subMenu = new Menuv2 + //{ + // Id = "subMenu", + // Visible = false + //}; + //ConfigureSubMenu1 (frame, subMenu); + + //var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); + //rootMenu.Add (cascadeShortcut); + + //var popoverMenu = new PopoverMenu (rootMenu) + //{ + // Id = "popOverMenu", + // Visible = true, + // X =1, Y = 1 + //}; + + ////Application.PopoverHost.Add (popoverMenu); + //Application.PopoverHost.Visible = true; - var subMenu = new Menuv2 - { - Id = "subMenu", - Visible = false - }; - ConfigureSubMenu1 (frame, subMenu); + //rootMenu.SubViews.ElementAt (0).SetFocus (); - var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); - rootMenu.Add (cascadeShortcut); + //frame.Add (popoverMenu); - var popoverMenu = new PopoverMenu (rootMenu) + + var shortcut1 = new Button() { - Id = "popOverMenu", - Visible = true, - X =1, Y = 1 + //Title = "_New", + //Key = Key.N.WithCtrl, + Text = "New File", + //Command = Command.New, + //TargetView = targetView, + BorderStyle = LineStyle.Double }; - //Application.PopoverHost.Add (popoverMenu); - //Application.PopoverHost.Visible = true; - - rootMenu.SubViews.ElementAt (0).SetFocus (); - - FrameView frameView = frame; - frameView.Add (popoverMenu); - - frameView.CommandNotBound += (o, args) => - { - eventSource.Add ($"{args.Context!.Command}: {frameView?.Id}"); - eventLog.MoveDown (); - args.Cancel = true; - }; - - frameView.Accepting += (o, args) => - { - eventSource.Add ($"{args.Context!.Command}: {frameView?.Id}"); - eventLog.MoveDown (); - // args.Cancel = true; - }; - - var menu = popoverMenu; - - menu.Accepting += (o, args) => - { - //Logging.Trace($"Accepting: {menu!.Id} {args.Context.Command}"); - //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); - //eventLog.MoveDown (); - //args.Cancel = true; - }; - - menu.Selecting += (o, args) => - { - //Logging.Trace ($"Selecting: {menu!.Id} {args.Context.Command}"); - //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); - //eventLog.MoveDown (); - //args.Cancel = false; - }; - - popoverMenu.Root.MenuItemCommandInvoked += (o, args) => - { - if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) - { - Logging.Trace($"Invoked: {sc.Title} {args.Context.Command}"); - eventSource.Add ($"Invoked: {sc.Title} {args.Context.Command}"); - //args.Cancel = true; - } - - eventLog.MoveDown (); - }; - - - foreach (View view2 in popoverMenu.Root.SubViews.Where (s => s is MenuItemv2)!) - { - var sh = (MenuItemv2)view2; - - sh.Accepting += (o, args) => - { - //Logging.Trace($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - //eventLog.MoveDown (); - //args.Cancel = true; - }; - - sh.Selecting += (o, args) => - { - //Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - //eventLog.MoveDown (); - //args.Cancel = false; - }; - } - - app.Add (eventLog); + frame.Add (shortcut1); + + //frame.CommandNotBound += (o, args) => + // { + // eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + // eventLog.MoveDown (); + // args.Cancel = true; + // }; + + //frame.Accepting += (o, args) => + // { + // eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + // eventLog.MoveDown (); + // // args.Cancel = true; + // }; + + //popoverMenu.Accepting += (o, args) => + // { + // Logging.Trace($"Accepting: {popoverMenu!.Id} {args.Context.Command}"); + // //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); + // //eventLog.MoveDown (); + // //args.Cancel = true; + // }; + + //popoverMenu.Selecting += (o, args) => + // { + // Logging.Trace ($"Selecting: {popoverMenu!.Id} {args.Context.Command}"); + // //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); + // //eventLog.MoveDown (); + // //args.Cancel = false; + // }; + + ////popoverMenu.Root.MenuItemCommandInvoked += (o, args) => + //// { + //// Logging.Trace ($"MenuItemCommandInvoked"); + //// if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) + //// { + //// Logging.Trace($"Invoked: {sc.Title} {args.Context.Command}"); + //// eventSource.Add ($"Invoked: {sc.Title} {args.Context.Command}"); + //// //args.Cancel = true; + //// } + + //// eventLog.MoveDown (); + //// }; + + + //foreach (View view2 in popoverMenu.Root.SubViews.Where (s => s is MenuItemv2)!) + //{ + // var sh = (MenuItemv2)view2; + + // sh.Accepting += (o, args) => + // { + // Logging.Trace($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + // //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + // //eventLog.MoveDown (); + // //args.Cancel = true; + // }; + + // sh.Selecting += (o, args) => + // { + // Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + // //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + // //eventLog.MoveDown (); + // //args.Cancel = false; + // }; + //} + + //app.Add (eventLog); Application.Run (app); app.Dispose (); - popoverMenu.Dispose (); + //popoverMenu.Dispose (); Application.Shutdown (); } @@ -161,61 +174,61 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) TargetView = targetView }; - var shortcut2 = new MenuItemv2 - { - Title = "_Open...", - Text = "Open File", - Key = Key.O.WithCtrl, - Command = Command.Open, - TargetView = targetView - }; - - var shortcut3 = new MenuItemv2 - { - Title = "_Save", - Text = "Save file", - Key = Key.S.WithCtrl, - Command = Command.Save, - TargetView = targetView - }; - - var shortcut4 = new MenuItemv2 - { - Title = "Sa_ve As...", - Text = "Save file as", - Key = Key.V.WithCtrl, - Command = Command.SaveAs, - TargetView = targetView - - }; - - - var shortcut5 = new MenuItemv2 - { - Title = "_Auto Save", - Text = "Automatically save", - Key = Key.A.WithCtrl, - TargetView = targetView - }; - - shortcut5.CommandView = new CheckBox - { - Title = shortcut5.Title, - HighlightStyle = HighlightStyle.None, - CanFocus = false - }; - - var line = new Line - { - X = -1, - Width = Dim.Fill ()! + 1 - }; - - - // This ensures the checkbox state toggles when the hotkey of Title is pressed. - //shortcut4.Accepting += (sender, args) => args.Cancel = true; - - menu.Add (shortcut1, shortcut2, shortcut3, shortcut4, line, shortcut5); + //var shortcut2 = new MenuItemv2 + //{ + // Title = "_Open...", + // Text = "Open File", + // Key = Key.O.WithCtrl, + // Command = Command.Open, + // TargetView = targetView + //}; + + //var shortcut3 = new MenuItemv2 + //{ + // Title = "_Save", + // Text = "Save file", + // Key = Key.S.WithCtrl, + // Command = Command.Save, + // TargetView = targetView + //}; + + //var shortcut4 = new MenuItemv2 + //{ + // Title = "Sa_ve As...", + // Text = "Save file as", + // Key = Key.V.WithCtrl, + // Command = Command.SaveAs, + // TargetView = targetView + + //}; + + + //var shortcut5 = new MenuItemv2 + //{ + // Title = "_Auto Save", + // Text = "Automatically save", + // Key = Key.A.WithCtrl, + // TargetView = targetView + //}; + + //shortcut5.CommandView = new CheckBox + //{ + // Title = shortcut5.Title, + // HighlightStyle = HighlightStyle.None, + // CanFocus = false + //}; + + //var line = new Line + //{ + // X = -1, + // Width = Dim.Fill ()! + 1 + //}; + + + //// This ensures the checkbox state toggles when the hotkey of Title is pressed. + ////shortcut4.Accepting += (sender, args) => args.Cancel = true; + + menu.Add (shortcut1);//, shortcut2, shortcut3, shortcut4, line, shortcut5); } diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 425d2584e8..2bd070a270 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -695,7 +695,6 @@ private static void VerifyObjectsWereDisposed () // 'app' closed cleanly. foreach (View? inst in View.Instances) { - Debug.Assert (inst.WasDisposed); } From 7d54eb77b2598d3f8129ac5ad44929880d460840 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 09:02:10 +0100 Subject: [PATCH 62/75] Optimize Margin to not defer draw if there's no shadow --- Terminal.Gui/View/Adornment/Margin.cs | 2 +- Terminal.Gui/View/View.Drawing.cs | 38 ++- Terminal.Gui/Views/GraphView/Annotations.cs | 2 +- Terminal.Gui/Views/Menu/Menu.cs | 2 +- Terminal.Gui/Views/Menu/PopoverMenu.cs | 2 +- Terminal.Gui/Views/TileView.cs | 2 +- UICatalog/Scenarios/MenusV2.cs | 293 ++++++++++---------- 7 files changed, 169 insertions(+), 172 deletions(-) diff --git a/Terminal.Gui/View/Adornment/Margin.cs b/Terminal.Gui/View/Adornment/Margin.cs index ac19770282..9d5dc44e88 100644 --- a/Terminal.Gui/View/Adornment/Margin.cs +++ b/Terminal.Gui/View/Adornment/Margin.cs @@ -48,7 +48,7 @@ public Margin (View parent) : base (parent) internal void CacheClip () { - if (Thickness != Thickness.Empty) + if (Thickness != Thickness.Empty && ShadowStyle != ShadowStyle.None) { // PERFORMANCE: How expensive are these clones? _cachedClip = GetClip ()?.Clone (); diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index 9d5521c025..09954c701b 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -27,6 +27,7 @@ internal static void Draw (IEnumerable views, bool force) view.Draw (context); } + // Draw the margins (those whith Shadows) last to ensure they are drawn on top of the content. Margin.DrawMargins (viewsArray); } @@ -57,9 +58,9 @@ public void Draw (DrawContext? context = null) { // ------------------------------------ // Draw the Border and Padding. - // Note Margin is special-cased and drawn in a separate pass to support + // Note Margin with a Shadow is special-cased and drawn in a separate pass to support // transparent shadows. - DoDrawBorderAndPadding (originalClip); + DoDrawAdornments (originalClip); SetClip (originalClip); // ------------------------------------ @@ -106,7 +107,7 @@ public void Draw (DrawContext? context = null) // ------------------------------------ // Re-draw the border and padding subviews // HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas. - DoDrawBorderAndPaddingSubViews (); + DoDrawAdornmentsSubViews (); // ------------------------------------ // Advance the diagnostics draw indicator @@ -116,8 +117,8 @@ public void Draw (DrawContext? context = null) } // ------------------------------------ - // This causes the Margin to be drawn in a second pass - // PERFORMANCE: If there is a Margin, it will be redrawn each iteration of the main loop. + // This causes the Margin to be drawn in a second pass if it has a ShadowStyle + // PERFORMANCE: If there is a Margin w/ Shadow, it will be redrawn each iteration of the main loop. Margin?.CacheClip (); // ------------------------------------ @@ -131,8 +132,11 @@ public void Draw (DrawContext? context = null) #region DrawAdornments - private void DoDrawBorderAndPaddingSubViews () + private void DoDrawAdornmentsSubViews () { + + // NOTE: We do not support subviews of Margin? + if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty) { // PERFORMANCE: Get the check for DrawIndicator out of this somehow. @@ -164,7 +168,7 @@ private void DoDrawBorderAndPaddingSubViews () } } - private void DoDrawBorderAndPadding (Region? originalClip) + private void DoDrawAdornments (Region? originalClip) { if (this is Adornment) { @@ -194,27 +198,28 @@ private void DoDrawBorderAndPadding (Region? originalClip) // A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen. Border?.SetNeedsDraw (); Padding?.SetNeedsDraw (); + Margin?.SetNeedsDraw (); } - if (OnDrawingBorderAndPadding ()) + if (OnDrawingAdornments ()) { return; } // TODO: add event. - DrawBorderAndPadding (); + DrawAdornments (); } /// - /// Causes and to be drawn. + /// Causes , , and to be drawn. /// /// /// - /// is drawn in a separate pass. + /// is drawn in a separate pass if is set. /// /// - public void DrawBorderAndPadding () + public void DrawAdornments () { // We do not attempt to draw Margin. It is drawn in a separate pass. @@ -230,6 +235,11 @@ public void DrawBorderAndPadding () Padding?.Draw (); } + + if (Margin is { } && Margin.Thickness != Thickness.Empty && Margin.ShadowStyle == ShadowStyle.None) + { + Margin?.Draw (); + } } private void ClearFrame () @@ -255,7 +265,7 @@ private void ClearFrame () /// false (the default), this method will cause the be prepared to be rendered. /// /// to stop further drawing of the Adornments. - protected virtual bool OnDrawingBorderAndPadding () { return false; } + protected virtual bool OnDrawingAdornments () { return false; } #endregion DrawAdornments @@ -635,7 +645,7 @@ private void DoRenderLineCanvas () /// /// Gets or sets whether this View will use it's SuperView's for rendering any /// lines. If the rendering of any borders drawn by this Frame will be done by its parent's - /// SuperView. If (the default) this View's method will + /// SuperView. If (the default) this View's method will /// be /// called to render the borders. /// diff --git a/Terminal.Gui/Views/GraphView/Annotations.cs b/Terminal.Gui/Views/GraphView/Annotations.cs index 02b67c974b..7d41ae4f95 100644 --- a/Terminal.Gui/Views/GraphView/Annotations.cs +++ b/Terminal.Gui/Views/GraphView/Annotations.cs @@ -150,7 +150,7 @@ public void Render (GraphView graph) if (BorderStyle != LineStyle.None) { - DrawBorderAndPadding (); + DrawAdornments (); RenderLineCanvas (); } diff --git a/Terminal.Gui/Views/Menu/Menu.cs b/Terminal.Gui/Views/Menu/Menu.cs index 1ea62b3abd..3f1e406fb5 100644 --- a/Terminal.Gui/Views/Menu/Menu.cs +++ b/Terminal.Gui/Views/Menu/Menu.cs @@ -831,7 +831,7 @@ private void Top_DrawComplete (object? sender, DrawEventArgs e) return; } - DrawBorderAndPadding (); + DrawAdornments (); RenderLineCanvas (); // BUGBUG: Views should not change the clip. Doing so is an indcation of poor design or a bug in the framework. diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 8cddced22c..96257b5e88 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -22,7 +22,7 @@ public PopoverMenu (Menuv2? root) CanFocus = true; Width = Dim.Fill (); Height = Dim.Fill (); - //ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; + ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; //base.Visible = false; base.ColorScheme = Colors.ColorSchemes ["Menu"]; diff --git a/Terminal.Gui/Views/TileView.cs b/Terminal.Gui/Views/TileView.cs index f3aed9c2ab..398474f48a 100644 --- a/Terminal.Gui/Views/TileView.cs +++ b/Terminal.Gui/Views/TileView.cs @@ -173,7 +173,7 @@ public int IndexOf (View toFind, bool recursive = false) /// Overridden so no Frames get drawn /// - protected override bool OnDrawingBorderAndPadding () { return true; } + protected override bool OnDrawingAdornments () { return true; } /// protected override bool OnRenderingLineCanvas () { return false; } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index 294a2dd12b..d129fbc99a 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -23,102 +23,89 @@ public override void Main () Toplevel app = new (); app.Title = GetQuitKeyAndName (); - //ObservableCollection eventSource = new (); - - //var eventLog = new ListView - //{ - // Title = "Event Log", - // X = Pos.AnchorEnd (), - // Width = Dim.Auto (), - // Height = Dim.Fill (), // Make room for some wide things - // ColorScheme = Colors.ColorSchemes ["Toplevel"], - // Source = new ListWrapper (eventSource) - //}; - //eventLog.Border!.Thickness = new (0, 1, 0, 0); - - View frame = new () + ObservableCollection eventSource = new (); + + var eventLog = new ListView + { + Title = "Event Log", + X = Pos.AnchorEnd (), + Width = Dim.Auto (), + Height = Dim.Fill (), // Make room for some wide things + ColorScheme = Colors.ColorSchemes ["Toplevel"], + Source = new ListWrapper (eventSource) + }; + eventLog.Border!.Thickness = new (0, 1, 0, 0); + + FrameView frame = new () { Id = "frame", Title = "Cascading Menu...", - Width = Dim.Fill (),//! - Dim.Width (eventLog), + Width = Dim.Fill ()! - Dim.Width (eventLog), Height = Dim.Fill (), BorderStyle = LineStyle.Dotted }; app.Add (frame); - //var rootMenu = new Menuv2 () - //{ - // Id = "rootMenu", - //}; - //ConfigureRootMenu (frame, rootMenu); - - //var subMenu = new Menuv2 - //{ - // Id = "subMenu", - // Visible = false - //}; - //ConfigureSubMenu1 (frame, subMenu); - - //var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); - //rootMenu.Add (cascadeShortcut); - - //var popoverMenu = new PopoverMenu (rootMenu) - //{ - // Id = "popOverMenu", - // Visible = true, - // X =1, Y = 1 - //}; - - ////Application.PopoverHost.Add (popoverMenu); - //Application.PopoverHost.Visible = true; - - //rootMenu.SubViews.ElementAt (0).SetFocus (); + var rootMenu = new Menuv2 () + { + Id = "rootMenu", + }; + ConfigureRootMenu (frame, rootMenu); - //frame.Add (popoverMenu); + var subMenu = new Menuv2 + { + Id = "subMenu", + Visible = false + }; + ConfigureSubMenu1 (frame, subMenu); + var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); + rootMenu.Add (cascadeShortcut); - var shortcut1 = new Button() + var popoverMenu = new PopoverMenu (rootMenu) { - //Title = "_New", - //Key = Key.N.WithCtrl, - Text = "New File", - //Command = Command.New, - //TargetView = targetView, - BorderStyle = LineStyle.Double + Id = "popOverMenu", + Visible = true, + X = 1, Y = 1 }; - frame.Add (shortcut1); - - //frame.CommandNotBound += (o, args) => - // { - // eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); - // eventLog.MoveDown (); - // args.Cancel = true; - // }; - - //frame.Accepting += (o, args) => - // { - // eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); - // eventLog.MoveDown (); - // // args.Cancel = true; - // }; - - //popoverMenu.Accepting += (o, args) => - // { - // Logging.Trace($"Accepting: {popoverMenu!.Id} {args.Context.Command}"); - // //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); - // //eventLog.MoveDown (); - // //args.Cancel = true; - // }; - - //popoverMenu.Selecting += (o, args) => - // { - // Logging.Trace ($"Selecting: {popoverMenu!.Id} {args.Context.Command}"); - // //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); - // //eventLog.MoveDown (); - // //args.Cancel = false; - // }; + ////Application.PopoverHost.Add (popoverMenu); + //Application.PopoverHost.Visible = true; + + //rootMenu.SubViews.ElementAt (0).SetFocus (); + + frame.Add (popoverMenu); + + frame.CommandNotBound += (o, args) => + { + eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + eventLog.MoveDown (); + args.Cancel = true; + }; + + frame.Accepting += (o, args) => + { + eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + eventLog.MoveDown (); + // args.Cancel = true; + }; + + popoverMenu.Accepting += (o, args) => + { + Logging.Trace ($"Accepting: {popoverMenu!.Id} {args.Context.Command}"); + //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); + //eventLog.MoveDown (); + //args.Cancel = true; + }; + + popoverMenu.Selecting += (o, args) => + { + Logging.Trace ($"Selecting: {popoverMenu!.Id} {args.Context.Command}"); + //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); + //eventLog.MoveDown (); + //args.Cancel = false; + }; ////popoverMenu.Root.MenuItemCommandInvoked += (o, args) => //// { @@ -134,28 +121,28 @@ public override void Main () //// }; - //foreach (View view2 in popoverMenu.Root.SubViews.Where (s => s is MenuItemv2)!) - //{ - // var sh = (MenuItemv2)view2; - - // sh.Accepting += (o, args) => - // { - // Logging.Trace($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - // //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - // //eventLog.MoveDown (); - // //args.Cancel = true; - // }; - - // sh.Selecting += (o, args) => - // { - // Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - // //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); - // //eventLog.MoveDown (); - // //args.Cancel = false; - // }; - //} - - //app.Add (eventLog); + foreach (View view2 in popoverMenu.Root.SubViews.Where (s => s is MenuItemv2)!) + { + var sh = (MenuItemv2)view2; + + sh.Accepting += (o, args) => + { + Logging.Trace ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventLog.MoveDown (); + //args.Cancel = true; + }; + + sh.Selecting += (o, args) => + { + Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //eventLog.MoveDown (); + //args.Cancel = false; + }; + } + + app.Add (eventLog); Application.Run (app); app.Dispose (); @@ -174,61 +161,61 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) TargetView = targetView }; - //var shortcut2 = new MenuItemv2 - //{ - // Title = "_Open...", - // Text = "Open File", - // Key = Key.O.WithCtrl, - // Command = Command.Open, - // TargetView = targetView - //}; - - //var shortcut3 = new MenuItemv2 - //{ - // Title = "_Save", - // Text = "Save file", - // Key = Key.S.WithCtrl, - // Command = Command.Save, - // TargetView = targetView - //}; - - //var shortcut4 = new MenuItemv2 - //{ - // Title = "Sa_ve As...", - // Text = "Save file as", - // Key = Key.V.WithCtrl, - // Command = Command.SaveAs, - // TargetView = targetView - - //}; - - - //var shortcut5 = new MenuItemv2 - //{ - // Title = "_Auto Save", - // Text = "Automatically save", - // Key = Key.A.WithCtrl, - // TargetView = targetView - //}; - - //shortcut5.CommandView = new CheckBox - //{ - // Title = shortcut5.Title, - // HighlightStyle = HighlightStyle.None, - // CanFocus = false - //}; - - //var line = new Line - //{ - // X = -1, - // Width = Dim.Fill ()! + 1 - //}; + var shortcut2 = new MenuItemv2 + { + Title = "_Open...", + Text = "Open File", + Key = Key.O.WithCtrl, + Command = Command.Open, + TargetView = targetView + }; + + var shortcut3 = new MenuItemv2 + { + Title = "_Save", + Text = "Save file", + Key = Key.S.WithCtrl, + Command = Command.Save, + TargetView = targetView + }; + + var shortcut4 = new MenuItemv2 + { + Title = "Sa_ve As...", + Text = "Save file as", + Key = Key.V.WithCtrl, + Command = Command.SaveAs, + TargetView = targetView + + }; + + + var shortcut5 = new MenuItemv2 + { + Title = "_Auto Save", + Text = "Automatically save", + Key = Key.A.WithCtrl, + TargetView = targetView + }; + + shortcut5.CommandView = new CheckBox + { + Title = shortcut5.Title, + HighlightStyle = HighlightStyle.None, + CanFocus = false + }; + + var line = new Line + { + X = -1, + Width = Dim.Fill ()! + 1 + }; //// This ensures the checkbox state toggles when the hotkey of Title is pressed. ////shortcut4.Accepting += (sender, args) => args.Cancel = true; - menu.Add (shortcut1);//, shortcut2, shortcut3, shortcut4, line, shortcut5); + menu.Add (shortcut1, shortcut2, shortcut3, shortcut4, line, shortcut5); } From 06fb4da22235cfb330a80a4631f508d910644682 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 18:09:25 +0100 Subject: [PATCH 63/75] Logger cleanup --- .../AnsiResponseParser/AnsiMouseParser.cs | 2 +- .../ConsoleDrivers/V2/InputProcessor.cs | 2 +- UICatalog/UICatalog.cs | 17 ++++++++++------- docfx/docs/logging.md | 16 +++++++++++----- docfx/images/UICatalog_Logging.png | Bin 0 -> 53046 bytes 5 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 docfx/images/UICatalog_Logging.png diff --git a/Terminal.Gui/ConsoleDrivers/AnsiResponseParser/AnsiMouseParser.cs b/Terminal.Gui/ConsoleDrivers/AnsiResponseParser/AnsiMouseParser.cs index 83c6c41817..76d141c277 100644 --- a/Terminal.Gui/ConsoleDrivers/AnsiResponseParser/AnsiMouseParser.cs +++ b/Terminal.Gui/ConsoleDrivers/AnsiResponseParser/AnsiMouseParser.cs @@ -52,7 +52,7 @@ public bool IsMouse (string? cur) Flags = GetFlags (buttonCode, terminator) }; - Logging.Trace ($"{nameof (AnsiMouseParser)} handled as {input} mouse {m.Flags} at {m.Position}"); + //Logging.Trace ($"{nameof (AnsiMouseParser)} handled as {input} mouse {m.Flags} at {m.Position}"); return m; } diff --git a/Terminal.Gui/ConsoleDrivers/V2/InputProcessor.cs b/Terminal.Gui/ConsoleDrivers/V2/InputProcessor.cs index e7b7b8d2cc..e870fd4e9f 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/InputProcessor.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/InputProcessor.cs @@ -79,7 +79,7 @@ public void OnMouseEvent (MouseEventArgs a) foreach (MouseEventArgs e in _mouseInterpreter.Process (a)) { - Logging.Trace ($"Mouse Interpreter raising {e.Flags}"); + // Logging.Trace ($"Mouse Interpreter raising {e.Flags}"); // Pass on MouseEvent?.Invoke (this, e); diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 2bd070a270..3b3cd49d85 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -74,7 +74,7 @@ public class UICatalogApp private static Options _options; private static ObservableCollection? _scenarios; - private const string LOGFILE_LOCATION = "./logs"; + private const string LOGFILE_LOCATION = "logs"; private static string _logFilePath = string.Empty; private static readonly LoggingLevelSwitch _logLevelSwitch = new (); @@ -171,7 +171,7 @@ private static int Main (string [] args) resultsFile.AddAlias ("--f"); // what's the app name? - _logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}.log"; + _logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}"; Option debugLogLevel = new Option ("--debug-log-level", $"The level to use for logging (debug console and {_logFilePath})").FromAmong ( Enum.GetNames () ); @@ -1354,11 +1354,14 @@ private List CreateLoggingMenuItems () menuItems.Add (null!); menuItems.Add ( - new () - { - Title = $"Log file: {_logFilePath}" - //CanExecute = () => false - }); + new ( + $"_Open Log Folder", + "", + () => OpenUrl (LOGFILE_LOCATION), + null, + null, + null + )); return menuItems.ToArray ()!; } diff --git a/docfx/docs/logging.md b/docfx/docs/logging.md index 6df97a0feb..a175591ed2 100644 --- a/docfx/docs/logging.md +++ b/docfx/docs/logging.md @@ -1,14 +1,20 @@ # Logging -Logging has come to Terminal.Gui! You can now enable comprehensive logging of the internals of the libray. This can help diagnose issues with specific terminals, keyboard cultures and/or operating system specific issues. +Logging has come to Terminal.Gui! You can now enable comprehensive logging of the internals of the library. This can help diagnose issues with specific terminals, keyboard cultures and/or operating system specific issues. -To enable file logging you should set the static property `Logging.Logger` to an instance of `Microsoft.Extensions.Logging.ILogger`. If your program already uses logging you can provide a shared instance or instance from Dependency Injection (DI). +To enable file logging you should set the static property `Logging.Logger` to an instance of `Microsoft.Extensions.Logging.ILogger`. If your program already uses logging you can provide a shared instance or instance from Dependency Injection (DI). Alternatively you can create a new log to ensure only Terminal.Gui logs appear. -Any logging framework will work (Serilog, NLog, Log4Net etc) but you should ensure you only log to File or UDP etc (i.e. not to console!). +Any logging framework will work (Serilog, NLog, Log4Net etc) but you should ensure you don't log to the stdout console (File, Debug Output, or UDP etc... are all fine). -## Worked example with Serilog to file +## UICatalog + +UI Catalog has built-in UI for logging. It logs to both the debug console and a file. By default it only logs at the `Warning` level. + +![UICatalog Logging](../images/UICatalog_Logging.png) + +## Example with Serilog to file Here is an example of how to add logging of Terminal.Gui internals to your program using Serilog file log. @@ -81,7 +87,7 @@ Example logs: ## Metrics -If you are finding that the UI is slow or unresponsive - or are just interested in performance metrics. You can see these by instaling the `dotnet-counter` tool and running it for your process. +If you are finding that the UI is slow or unresponsive - or are just interested in performance metrics. You can see these by installing the `dotnet-counter` tool and running it for your process. ``` dotnet tool install dotnet-counters --global diff --git a/docfx/images/UICatalog_Logging.png b/docfx/images/UICatalog_Logging.png new file mode 100644 index 0000000000000000000000000000000000000000..55377ca866628505b6b828f2ccdd137c326e3968 GIT binary patch literal 53046 zcmZs@1z1#3*9JNW0wSS+(x4zM-L0a4bP3Yk-CZJG(%mfr5)#rN-5}lFARWUncaMJG z|KI<)_jyKPhBI^aS!eCF-gm8cO^Cd#I2Hym1_T1ZdN1)-5duLLfq$29!t=@g`{DG~6ngaxa-GTUr)NNB_2!RaszkmB$ z*;RWV=Hp1xoxJtX=UOZS9m9Y=tBeoZR4fiDf({2yO#$=99L4Q@gh4kI&Cnp0q0$V= zj_JOog#Thh4o4L^eRtfC25~C6K+N@ss-I&`c9V%2IgF-Poh@881z2Y zRmw*4ctI3w$l=Sb!n`O#j{mb^1kykvg6=!KmBx>yETpBipr!RePE|;5YGP`tu$2|F zXO2FU72bx5h{!VK_r%wUm`*u}f+A{2mw03t4wGR`m z1u9&2iyiVxOUvoo0=K(W)=y#KC4Njy$7iI?_Y(KMzwAnSfj^sE%=5|A@oCGg5owgj zog5)Wl9Tc^u#&03#JB1WoqBvd;mu@CSU9VOO zH`c#WMMEsPpCzLtyo^?2#5#q&VZFktTj02ACm?Sf8R3_i9hdW=z!Ad#EyE$nb*Td^ z^FuX+s(tQIeT1#!*P##< z8EGsC-QQY1FH3mGHSj@%fI&|1Se%e>yETESw(q^$%AT6m+uT_ugGlp3gpJIjbLfQ1 zo89qd=O}uK>{sl`#B`QIQugt^Phu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PapMTi)4{ncVEq;Zc*V2U^7)r0$Za(1B% zHIEgl!8NqpoSmdeg-O=G(a`w0Wvz@qRTyOyfsXp3kx>?poX16TRN)(4oxh)TaonKpGv0ks7Kg$GMw%n8)VJijsyVZK7A3Z&o|Q&VMnp^L|(V-bMF e4o_t%NrBnO!$UM)ed_FrQ&-VeE>(K*?*9S_4g+TZ literal 0 HcmV?d00001 From 0430fdcd8cbc47d0645e2dbc4ae600b405126e5f Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 13 Mar 2025 18:11:30 +0100 Subject: [PATCH 64/75] Reverted Generic --- UICatalog/Scenarios/Generic.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/UICatalog/Scenarios/Generic.cs b/UICatalog/Scenarios/Generic.cs index a1d332fb23..3fc926dedf 100644 --- a/UICatalog/Scenarios/Generic.cs +++ b/UICatalog/Scenarios/Generic.cs @@ -18,14 +18,6 @@ public override void Main () Title = GetQuitKeyAndName (), }; - FrameView frame = new () - { - Height = Dim.Fill (), - Width = Dim.Fill (), - Title = "Frame" - }; - appWindow.Add (frame); - var button = new Button { Id = "button", X = Pos.Center (), Y = 1, Text = "_Press me!" }; button.Accepting += (s, e) => @@ -35,7 +27,7 @@ public override void Main () MessageBox.ErrorQuery ("Error", "You pressed the button!", "_Ok"); }; - frame.Add (button); + appWindow.Add (button); // Run - Start the application. Application.Run (appWindow); From 3f3d2b5fb518a21390a61b4b43d5473cc592e616 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 16 Mar 2025 14:59:36 +0100 Subject: [PATCH 65/75] Cascading mostly working --- .../Application/Application.Popover.cs | 2 +- Terminal.Gui/View/View.Keyboard.cs | 2 +- Terminal.Gui/View/View.Layout.cs | 12 +- Terminal.Gui/Views/Bar.cs | 52 -------- Terminal.Gui/Views/Menu/MenuItemv2.cs | 1 + Terminal.Gui/Views/Menu/Menuv2.cs | 24 +--- Terminal.Gui/Views/Menu/PopoverMenu.cs | 112 ++++++++++++++++++ UICatalog/Scenarios/MenusV2.cs | 76 ++++++++++-- 8 files changed, 192 insertions(+), 89 deletions(-) diff --git a/Terminal.Gui/Application/Application.Popover.cs b/Terminal.Gui/Application/Application.Popover.cs index d4267bfd8d..a84f0702ab 100644 --- a/Terminal.Gui/Application/Application.Popover.cs +++ b/Terminal.Gui/Application/Application.Popover.cs @@ -17,5 +17,5 @@ public static partial class Application // Popover handling /// If the user clicks anywhere not occulded by a SubView of the PopoverHost, the PopoverHost will be hidden. /// /// - public static PopoverHost? PopoverHost { get; internal set; } + public static PopoverHost? PopoverHost { get; set; } } \ No newline at end of file diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index a47b333a1f..2c3ee5113f 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -604,7 +604,7 @@ internal bool InvokeCommandsBoundToHotKey (Key hotKey, ref bool? handled) } // Now, process any HotKey bindings in the subviews - foreach (View subview in InternalSubViews) + foreach (View subview in InternalSubViews.ToList()) { if (subview == Focused) { diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 5ae60b8ed6..1bc6074e9f 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -1048,7 +1048,7 @@ out int ny int maxDimension; View? superView; - if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) + if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { maxDimension = Application.Screen.Width; superView = Application.Top; @@ -1077,7 +1077,7 @@ out int ny } else { - nx = targetX; + nx = 0;//targetX; } //System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}"); @@ -1141,10 +1141,14 @@ out int ny ny = Math.Max (viewToMove.Frame.Bottom, 0); } } + else + { + ny = 0; + } - //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}"); + //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}"); - return superView!; + return superView!; } #endregion Utilities diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index dd9c4729ee..2f7ba31236 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -32,58 +32,6 @@ public Bar (IEnumerable? shortcuts) // Initialized += Bar_Initialized; MouseEvent += OnMouseEvent; - AddCommand (Command.Right, MoveRight); - - bool? MoveRight (ICommandContext? ctx) - { - if (Orientation == Orientation.Vertical) - { - return false; - } - - return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); - } - - AddCommand (Command.Left, MoveLeft); - - bool? MoveLeft (ICommandContext? ctx) - { - if (Orientation == Orientation.Vertical) - { - return false; - } - - return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); - } - - AddCommand (Command.Down, MoveDown); - - bool? MoveDown (ICommandContext? ctx) - { - if (Orientation == Orientation.Horizontal) - { - return false; - } - - return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); - } - - AddCommand (Command.Up, MoveUp); - - bool? MoveUp (ICommandContext? ctx) - { - if (Orientation == Orientation.Horizontal) - { - return false; - } - - return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); - } - - KeyBindings.Add (Key.CursorRight, Command.Right); - KeyBindings.Add (Key.CursorDown, Command.Down); - KeyBindings.Add (Key.CursorLeft, Command.Left); - KeyBindings.Add (Key.CursorUp, Command.Up); if (shortcuts is { }) { diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index 0bc647ba35..5dfa769e95 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -53,6 +53,7 @@ public MenuItemv2 (View targetView, Command command, string commandText, string? { // TODO: This is a temporary hack - add a flag or something instead KeyView.Text = $"{Glyphs.RightArrow}"; + subMenu.SuperMenuItem = this; } SubMenu = subMenu; diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index cb37e04dc4..175fa5837d 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -21,8 +21,11 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) Height = Dim.Auto (DimAutoStyle.Content, 1); Initialized += Menuv2_Initialized; VisibleChanged += OnVisibleChanged; + } + public MenuItemv2 SuperMenuItem { get; set; } + private void OnVisibleChanged (object? sender, EventArgs e) { if (Visible) @@ -150,7 +153,7 @@ internal void RaiseSelectedMenuItemChanged (MenuItemv2? selected) { //Logging.Trace ($"RaiseSelectedMenuItemChanged: {selected?.Title}"); - ShowSubMenu (selected); + //ShowSubMenu (selected); OnSelectedMenuItemChanged (selected); SelectedMenuItemChanged?.Invoke (this, selected); @@ -169,23 +172,4 @@ protected virtual void OnSelectedMenuItemChanged (MenuItemv2? selected) /// public event EventHandler? SelectedMenuItemChanged; - public void ShowSubMenu (MenuItemv2? menuItem) - { - // Hide any other submenus that might be visible - foreach (MenuItemv2 mi in SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) - { - mi.ForceFocusColors = false; - mi.SubMenu!.Visible = false; - SuperView?.Remove (mi.SubMenu); - } - - if (menuItem is { SubMenu: {} }) - { - SuperView?.Add (menuItem.SubMenu); - menuItem.SubMenu.X = Frame.X + Frame.Width; - menuItem.SubMenu.Y = Frame.Y + menuItem.Frame.Y; - menuItem.SubMenu.Visible = true; - menuItem.ForceFocusColors = true; - } - } } \ No newline at end of file diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 96257b5e88..19eb633b7a 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -1,4 +1,6 @@ #nullable enable +using Microsoft.CodeAnalysis; + namespace Terminal.Gui; /// @@ -28,6 +30,63 @@ public PopoverMenu (Menuv2? root) Root = root; + AddCommand (Command.Right, MoveRight); + bool? MoveRight (ICommandContext? ctx) + { + MenuItemv2? focused = MostFocused as MenuItemv2; + + if (focused is { SubMenu.Visible: true }) + { + focused.SubMenu.SetFocus (); + + return true; + } + + return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + } + KeyBindings.Add (Key.CursorRight, Command.Right); + + AddCommand (Command.Left, MoveLeft); + bool? MoveLeft (ICommandContext? ctx) + { + if (MostFocused is MenuItemv2 { SuperView: Menuv2 focusedMenu }) + { + focusedMenu.SuperMenuItem?.SetFocus (); + + return true; + } + return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); + } + KeyBindings.Add (Key.CursorLeft, Command.Left); + + //AddCommand (Command.Down, MoveDown); + + //bool? MoveDown (ICommandContext? ctx) + //{ + // if (Orientation == Orientation.Horizontal) + // { + // return false; + // } + + // return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + //} + + //AddCommand (Command.Up, MoveUp); + + //bool? MoveUp (ICommandContext? ctx) + //{ + // if (Orientation == Orientation.Horizontal) + // { + // return false; + // } + + // return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); + //} + + + //KeyBindings.Add (Key.CursorDown, Command.Down); + //KeyBindings.Add (Key.CursorUp, Command.Up); + } private Menuv2? _root; @@ -50,6 +109,7 @@ public Menuv2? Root base.Remove (_root); _root.Accepting -= RootOnAccepting; _root.MenuItemCommandInvoked -= RootOnMenuItemCommandInvoked; + _root.SelectedMenuItemChanged -= RootOnSelectedMenuItemChanged; } _root = value; @@ -59,6 +119,9 @@ public Menuv2? Root base.Add (_root); _root.Accepting += RootOnAccepting; _root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; + _root.SelectedMenuItemChanged += RootOnSelectedMenuItemChanged; + + } return; @@ -72,6 +135,55 @@ void RootOnAccepting (object? sender, CommandEventArgs e) { Logging.Trace ($"RootOnAccepting: {e.Context}"); } + + void RootOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) + { + Logging.Trace ($"RootOnSelectedMenuItemChanged: {e.Title}"); + ShowSubMenu (e); + } + } } + public void ShowSubMenu (MenuItemv2? menuItem) + { + // Hide any other submenus that might be visible + foreach (MenuItemv2 mi in menuItem.SuperView.SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) + { + mi.ForceFocusColors = false; + mi.SubMenu!.Visible = false; + Remove (mi.SubMenu); + } + + if (menuItem is { SubMenu: { Visible: false } }) + { + Add (menuItem.SubMenu); + Point pos = GetMostVisibleLocationForSubMenu (menuItem); + menuItem.SubMenu.X = pos.X; + menuItem.SubMenu.Y = pos.Y; + + menuItem.SubMenu.Visible = true; + menuItem.ForceFocusColors = true; + } + } + + /// + /// Given a , returns the most visible location for the submenu. + /// The location is relative to the Frame. + /// + /// + /// + internal Point GetMostVisibleLocationForSubMenu (MenuItemv2 menuItem) + { + Point pos = Point.Empty; + + // Calculate the initial position to the right of the menu item + pos.X = menuItem.SuperView!.Frame.X + menuItem.Frame.Width; + pos.Y = menuItem.SuperView.Frame.Y + menuItem.Frame.Y; + + GetLocationEnsuringFullVisibility (menuItem.SubMenu, pos.X, pos.Y, out int nx, out int ny); + + + return new (nx,ny); + } + } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index d129fbc99a..fccb1edcef 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -41,8 +41,10 @@ public override void Main () Id = "frame", Title = "Cascading Menu...", - Width = Dim.Fill ()! - Dim.Width (eventLog), - Height = Dim.Fill (), + X = 4, + Y = 4, + Width = Dim.Fill (8)! - Dim.Width (eventLog), + Height = Dim.Fill (8), BorderStyle = LineStyle.Dotted }; app.Add (frame); @@ -53,15 +55,25 @@ public override void Main () }; ConfigureRootMenu (frame, rootMenu); - var subMenu = new Menuv2 + var optionsSubMenu = new Menuv2 { - Id = "subMenu", + Id = "optionsSubMenu", Visible = false }; - ConfigureSubMenu1 (frame, subMenu); + ConfigureOptionsSubMenu (frame, optionsSubMenu); - var cascadeShortcut = new MenuItemv2 (frame, Command.Accept, "_Options", "File options", subMenu); - rootMenu.Add (cascadeShortcut); + var optionsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "O_ptions", "File options", optionsSubMenu); + rootMenu.Add (optionsSubMenuItem); + + var detailsSubMenu = new Menuv2 + { + Id = "detailsSubMenu", + Visible = false + }; + ConfigureDetialsSubMenu (frame, detailsSubMenu); + + var detailsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "_Details", "File details", detailsSubMenu); + rootMenu.Add (detailsSubMenuItem); var popoverMenu = new PopoverMenu (rootMenu) { @@ -219,12 +231,12 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) } - private void ConfigureSubMenu1 (View targetView, Menuv2 menu) + private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) { var shortcut2 = new MenuItemv2 { - Title = "Za_G", - Text = "Gonna zag", + Title = "_Option 1", + Text = "Some option #1", Key = Key.G.WithAlt }; @@ -232,7 +244,7 @@ private void ConfigureSubMenu1 (View targetView, Menuv2 menu) { Title = "_Three", Text = "The 3rd item", - Key = Key.D3.WithAlt + Key = Key.T.WithAlt }; var line = new Line @@ -245,9 +257,51 @@ private void ConfigureSubMenu1 (View targetView, Menuv2 menu) { Title = "_Four", Text = "Below the line", + Key = Key.D7.WithAlt + }; + + shortcut4.CommandView = new CheckBox + { + Title = shortcut4.Title, + HighlightStyle = HighlightStyle.None, + CanFocus = false + }; + + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + //shortcut4.Accepting += (sender, args) => args.Cancel = true; + + menu.Add (shortcut2, shortcut3, line, shortcut4); + } + + private void ConfigureDetialsSubMenu (View targetView, Menuv2 menu) + { + var shortcut2 = new MenuItemv2 + { + Title = "_Detail 1", + Text = "Some detail #1", + Key = Key.G.WithAlt + }; + + var shortcut3 = new MenuItemv2 + { + Title = "_Three", + Text = "The 3rd item", Key = Key.D3.WithAlt }; + var line = new Line + { + X = -1, + Width = Dim.Fill ()! + 1 + }; + + var shortcut4 = new MenuItemv2 + { + Title = "_Four", + Text = "Below the line", + Key = Key.D8.WithAlt + }; + shortcut4.CommandView = new CheckBox { Title = shortcut4.Title, From 52b01e4a66d9b13eb8163af20e6b2df1f2d923ca Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 17 Mar 2025 16:37:14 +0100 Subject: [PATCH 66/75] fixed layout bug --- Terminal.Gui/Views/Menu/PopoverMenu.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 19eb633b7a..7b56be09b7 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -157,6 +157,7 @@ public void ShowSubMenu (MenuItemv2? menuItem) if (menuItem is { SubMenu: { Visible: false } }) { Add (menuItem.SubMenu); + menuItem.SubMenu.Layout (); Point pos = GetMostVisibleLocationForSubMenu (menuItem); menuItem.SubMenu.X = pos.X; menuItem.SubMenu.Y = pos.Y; From eb58ef5f3a51e55b47d49320ffdd89b5f7970b28 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 17 Mar 2025 16:40:02 +0100 Subject: [PATCH 67/75] API docs --- Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs | 1 + Terminal.Gui/Views/Menu/Menuv2.cs | 12 +++--------- Terminal.Gui/Views/Menu/PopoverMenu.cs | 5 +++++ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs index 5c3bc6dba9..41597fe910 100644 --- a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs +++ b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs @@ -185,6 +185,7 @@ public bool ContainsKey (string key) } } + /// public void CopyTo (KeyValuePair [] array, int arrayIndex) { lock (_lock) diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index 175fa5837d..bdb43d281e 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -24,6 +24,9 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) } + /// + /// Gets or sets the menu item that opened this menu as a sub-menu. + /// public MenuItemv2 SuperMenuItem { get; set; } private void OnVisibleChanged (object? sender, EventArgs e) @@ -31,15 +34,6 @@ private void OnVisibleChanged (object? sender, EventArgs e) if (Visible) { SelectedMenuItem = SubViews.Where (mi => mi is MenuItemv2).ElementAtOrDefault (0) as MenuItemv2; - - //Application.GrabMouse(this); - } - else - { - if (Application.MouseGrabView == this) - { - //Application.UngrabMouse (); - } } } diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 7b56be09b7..27375ca06c 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -144,6 +144,11 @@ void RootOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) } } + + /// + /// + /// + /// public void ShowSubMenu (MenuItemv2? menuItem) { // Hide any other submenus that might be visible From 4c413dbbd81ae60165149454f57852e079b227bc Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 17 Mar 2025 16:47:29 +0100 Subject: [PATCH 68/75] API docs --- .../Drawing/Color/ColorScheme.Colors.cs | 3 + Terminal.Gui/Views/Menu/Menuv2.cs | 2 +- Terminal.Gui/Views/Menu/PopoverMenu.cs | 83 +++++-------------- 3 files changed, 27 insertions(+), 61 deletions(-) diff --git a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs index 41597fe910..0b86c5ea04 100644 --- a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs +++ b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs @@ -194,6 +194,7 @@ public void CopyTo (KeyValuePair [] array, int arrayIndex) } } + /// public IEnumerator> GetEnumerator () { lock (_lock) @@ -207,6 +208,7 @@ IEnumerator IEnumerable.GetEnumerator () return GetEnumerator (); } + /// public bool Remove (KeyValuePair item) { lock (_lock) @@ -220,6 +222,7 @@ public bool Remove (KeyValuePair item) } } + /// public bool Remove (string key) { lock (_lock) diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index bdb43d281e..9f666a66ac 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -27,7 +27,7 @@ public Menuv2 (IEnumerable shortcuts) : base (shortcuts) /// /// Gets or sets the menu item that opened this menu as a sub-menu. /// - public MenuItemv2 SuperMenuItem { get; set; } + public MenuItemv2? SuperMenuItem { get; set; } private void OnVisibleChanged (object? sender, EventArgs e) { diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 27375ca06c..deb9e8cc82 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -1,23 +1,15 @@ #nullable enable -using Microsoft.CodeAnalysis; - namespace Terminal.Gui; /// -/// /// public class PopoverMenu : View { /// - /// /// - public PopoverMenu () : this (null) - { - - } + public PopoverMenu () : this (null) { } /// - /// /// public PopoverMenu (Menuv2? root) { @@ -25,15 +17,17 @@ public PopoverMenu (Menuv2? root) Width = Dim.Fill (); Height = Dim.Fill (); ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse; + //base.Visible = false; base.ColorScheme = Colors.ColorSchemes ["Menu"]; Root = root; AddCommand (Command.Right, MoveRight); + bool? MoveRight (ICommandContext? ctx) { - MenuItemv2? focused = MostFocused as MenuItemv2; + var focused = MostFocused as MenuItemv2; if (focused is { SubMenu.Visible: true }) { @@ -44,9 +38,11 @@ public PopoverMenu (Menuv2? root) return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); } + KeyBindings.Add (Key.CursorRight, Command.Right); AddCommand (Command.Left, MoveLeft); + bool? MoveLeft (ICommandContext? ctx) { if (MostFocused is MenuItemv2 { SuperView: Menuv2 focusedMenu }) @@ -55,44 +51,17 @@ public PopoverMenu (Menuv2? root) return true; } + return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); } - KeyBindings.Add (Key.CursorLeft, Command.Left); - - //AddCommand (Command.Down, MoveDown); - - //bool? MoveDown (ICommandContext? ctx) - //{ - // if (Orientation == Orientation.Horizontal) - // { - // return false; - // } - - // return AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); - //} - - //AddCommand (Command.Up, MoveUp); - - //bool? MoveUp (ICommandContext? ctx) - //{ - // if (Orientation == Orientation.Horizontal) - // { - // return false; - // } - - // return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop); - //} - - - //KeyBindings.Add (Key.CursorDown, Command.Down); - //KeyBindings.Add (Key.CursorUp, Command.Up); + KeyBindings.Add (Key.CursorLeft, Command.Left); } private Menuv2? _root; /// - /// + /// Gets or sets the that is the root of the Popover Menu. /// public Menuv2? Root { @@ -120,39 +89,35 @@ public Menuv2? Root _root.Accepting += RootOnAccepting; _root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; _root.SelectedMenuItemChanged += RootOnSelectedMenuItemChanged; - - } return; - void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) - { - Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); - } + void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) { Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); } - void RootOnAccepting (object? sender, CommandEventArgs e) - { - Logging.Trace ($"RootOnAccepting: {e.Context}"); - } + void RootOnAccepting (object? sender, CommandEventArgs e) { Logging.Trace ($"RootOnAccepting: {e.Context}"); } void RootOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) { - Logging.Trace ($"RootOnSelectedMenuItemChanged: {e.Title}"); + Logging.Trace ($"RootOnSelectedMenuItemChanged: {e!.Title}"); ShowSubMenu (e); } - } } /// - /// /// /// public void ShowSubMenu (MenuItemv2? menuItem) { + if (menuItem is null) + { + return; + } + // Hide any other submenus that might be visible - foreach (MenuItemv2 mi in menuItem.SuperView.SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) + // BUBUG: I think this won't work for cascading + foreach (MenuItemv2 mi in menuItem!.SuperView!.SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) { mi.ForceFocusColors = false; mi.SubMenu!.Visible = false; @@ -163,7 +128,7 @@ public void ShowSubMenu (MenuItemv2? menuItem) { Add (menuItem.SubMenu); menuItem.SubMenu.Layout (); - Point pos = GetMostVisibleLocationForSubMenu (menuItem); + Point pos = GetMostVisibleLocationForSubMenu (menuItem); menuItem.SubMenu.X = pos.X; menuItem.SubMenu.Y = pos.Y; @@ -180,16 +145,14 @@ public void ShowSubMenu (MenuItemv2? menuItem) /// internal Point GetMostVisibleLocationForSubMenu (MenuItemv2 menuItem) { - Point pos = Point.Empty; + var pos = Point.Empty; // Calculate the initial position to the right of the menu item pos.X = menuItem.SuperView!.Frame.X + menuItem.Frame.Width; pos.Y = menuItem.SuperView.Frame.Y + menuItem.Frame.Y; - GetLocationEnsuringFullVisibility (menuItem.SubMenu, pos.X, pos.Y, out int nx, out int ny); + GetLocationEnsuringFullVisibility (menuItem.SubMenu!, pos.X, pos.Y, out int nx, out int ny); - - return new (nx,ny); + return new (nx, ny); } - } From 1ebc228086157e3c4e40b314988db1a1cbe68de6 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 18 Mar 2025 08:36:47 +0100 Subject: [PATCH 69/75] Fixed cascade --- Terminal.Gui/View/View.Layout.cs | 8 +- Terminal.Gui/View/View.Navigation.cs | 2 +- Terminal.Gui/Views/Menu/Menuv2.cs | 1 - Terminal.Gui/Views/Menu/PopoverMenu.cs | 108 +++++++++++++++---------- UICatalog/Scenarios/MenusV2.cs | 41 ++++++++++ 5 files changed, 112 insertions(+), 48 deletions(-) diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 1bc6074e9f..fb9ab4fe25 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -1136,10 +1136,10 @@ out int ny ? Math.Max (maxDimension - viewToMove.Frame.Height, menuVisible ? 1 : 0) : ny; - if (ny > viewToMove.Frame.Y + viewToMove.Frame.Height) - { - ny = Math.Max (viewToMove.Frame.Bottom, 0); - } + //if (ny > viewToMove.Frame.Y + viewToMove.Frame.Height) + //{ + // ny = Math.Max (viewToMove.Frame.Bottom, 0); + //} } else { diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs index a37bd7ed06..99804f2e0e 100644 --- a/Terminal.Gui/View/View.Navigation.cs +++ b/Terminal.Gui/View/View.Navigation.cs @@ -882,7 +882,7 @@ private void RaiseFocusChanged (bool newHasFocus, View? previousFocusedView, Vie var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedView); HasFocusChanged?.Invoke (this, args); - if (newHasFocus) + if (newHasFocus || focusedView is null) { SuperView?.RaiseFocusedChanged (previousFocusedView, focusedView); } diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index 9f666a66ac..55ebb5e683 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -123,7 +123,6 @@ protected override void OnFocusedChanged (View? previousFocused, View? focused) base.OnFocusedChanged (previousFocused, focused); SelectedMenuItem = focused as MenuItemv2; RaiseSelectedMenuItemChanged (SelectedMenuItem); - } /// diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index deb9e8cc82..555c512cbf 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -1,4 +1,6 @@ #nullable enable +using System.Diagnostics; + namespace Terminal.Gui; /// @@ -73,61 +75,35 @@ public Menuv2? Root return; } - if (_root is { }) - { - base.Remove (_root); - _root.Accepting -= RootOnAccepting; - _root.MenuItemCommandInvoked -= RootOnMenuItemCommandInvoked; - _root.SelectedMenuItemChanged -= RootOnSelectedMenuItemChanged; - } + HideAndRemoveSubMenu (_root); _root = value; - if (_root is { }) - { - base.Add (_root); - _root.Accepting += RootOnAccepting; - _root.MenuItemCommandInvoked += RootOnMenuItemCommandInvoked; - _root.SelectedMenuItemChanged += RootOnSelectedMenuItemChanged; - } - - return; - - void RootOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) { Logging.Trace ($"RootOnMenuItemCommandInvoked: {e.Context}"); } - - void RootOnAccepting (object? sender, CommandEventArgs e) { Logging.Trace ($"RootOnAccepting: {e.Context}"); } - - void RootOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) - { - Logging.Trace ($"RootOnSelectedMenuItemChanged: {e!.Title}"); - ShowSubMenu (e); - } + AddAndShowSubMenu (_root); } } /// + /// Pops up the submenu of the specified MenuItem, if there is one. /// /// public void ShowSubMenu (MenuItemv2? menuItem) { - if (menuItem is null) - { - return; - } + var menu = menuItem?.SuperView as Menuv2; - // Hide any other submenus that might be visible - // BUBUG: I think this won't work for cascading - foreach (MenuItemv2 mi in menuItem!.SuperView!.SubViews.Where (v => v is MenuItemv2 { SubMenu.Visible: true }).Cast ()) + // If there's a visible peer, remove / hide it + + Debug.Assert(menu is null || menu?.SubViews.Count(v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); + if (menu?.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 visiblePeer) { - mi.ForceFocusColors = false; - mi.SubMenu!.Visible = false; - Remove (mi.SubMenu); + HideAndRemoveSubMenu (visiblePeer.SubMenu); + visiblePeer.ForceFocusColors = false; } if (menuItem is { SubMenu: { Visible: false } }) { - Add (menuItem.SubMenu); - menuItem.SubMenu.Layout (); + AddAndShowSubMenu (menuItem.SubMenu); + Point pos = GetMostVisibleLocationForSubMenu (menuItem); menuItem.SubMenu.X = pos.X; menuItem.SubMenu.Y = pos.Y; @@ -148,11 +124,59 @@ internal Point GetMostVisibleLocationForSubMenu (MenuItemv2 menuItem) var pos = Point.Empty; // Calculate the initial position to the right of the menu item - pos.X = menuItem.SuperView!.Frame.X + menuItem.Frame.Width; - pos.Y = menuItem.SuperView.Frame.Y + menuItem.Frame.Y; - - GetLocationEnsuringFullVisibility (menuItem.SubMenu!, pos.X, pos.Y, out int nx, out int ny); + GetLocationEnsuringFullVisibility ( + menuItem.SubMenu!, + menuItem.SuperView!.Frame.X + menuItem.Frame.Width, + menuItem.SuperView.Frame.Y + menuItem.Frame.Y, + out int nx, + out int ny); return new (nx, ny); } + + private void AddAndShowSubMenu (Menuv2? menu) + { + if (menu is { }) + { + base.Add (menu); + + menu.Layout (); + + menu.Accepting += MenuOnAccepting; + menu.MenuItemCommandInvoked += MenuOnMenuItemCommandInvoked; + menu.SelectedMenuItemChanged += MenuOnSelectedMenuItemChanged; + } + } + private void HideAndRemoveSubMenu (Menuv2? menu) + { + Debug.Assert (menu is null || menu is { Visible: true }); + if (menu is { Visible: true }) + { + // If there's a visible submenu, remove / hide it + Debug.Assert (menu?.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); + if (menu?.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 mi) + { + HideAndRemoveSubMenu (mi.SubMenu); + mi.ForceFocusColors = false; + } + + menu.Visible = false; + menu.Accepting -= MenuOnAccepting; + menu.MenuItemCommandInvoked -= MenuOnMenuItemCommandInvoked; + menu.SelectedMenuItemChanged -= MenuOnSelectedMenuItemChanged; + base.Remove (menu); + } + } + + private void MenuOnAccepting (object? sender, CommandEventArgs e) { Logging.Trace ($"MenuOnSelectedMenuItemChanged: {e.Context}"); } + + private void MenuOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) { Logging.Trace ($"MenuOnMenuItemCommandInvoked: {e.Context}"); } + + private void MenuOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) + { + Logging.Trace ($"MenuOnSelectedMenuItemChanged: {e}"); + ShowSubMenu (e); + } + + } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index fccb1edcef..e608e8f5e5 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -75,6 +75,16 @@ public override void Main () var detailsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "_Details", "File details", detailsSubMenu); rootMenu.Add (detailsSubMenuItem); + var moreDetailsSubMenu = new Menuv2 + { + Id = "moreDetailsSubMenu", + Visible = false + }; + ConfigureMoreDetailsSubMenu (frame, moreDetailsSubMenu); + + var moreDetailsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "_More Details", "More details", moreDetailsSubMenu); + detailsSubMenu.Add (moreDetailsSubMenuItem); + var popoverMenu = new PopoverMenu (rootMenu) { Id = "popOverMenu", @@ -314,6 +324,37 @@ private void ConfigureDetialsSubMenu (View targetView, Menuv2 menu) menu.Add (shortcut2, shortcut3, line, shortcut4); } + + + private void ConfigureMoreDetailsSubMenu (View targetView, Menuv2 menu) + { + var shortcut2 = new MenuItemv2 + { + Title = "_Deeper Detail", + Text = "Deeper Detail", + Key = Key.V.WithAlt + }; + + var line = new Line + { + X = -1, + Width = Dim.Fill ()! + 1 + }; + + var shortcut4 = new MenuItemv2 + { + Title = "_Third", + Text = "Below the line", + Key = Key.D3.WithAlt + }; + + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + //shortcut4.Accepting += (sender, args) => args.Cancel = true; + + menu.Add (shortcut2, line, shortcut4); + } + + private const string LOGFILE_LOCATION = "./logs"; private static string _logFilePath = string.Empty; private static readonly LoggingLevelSwitch _logLevelSwitch = new (); From e0dde4248c954568f4e78040a733eec223db390b Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 20 Mar 2025 23:00:38 +0100 Subject: [PATCH 70/75] Events basically work --- .../Application/Application.Keyboard.cs | 2 +- Terminal.Gui/Input/CommandContext.cs | 6 +- Terminal.Gui/Input/ICommandContext.cs | 6 ++ Terminal.Gui/Input/IInputBinding.cs | 6 ++ Terminal.Gui/Input/Keyboard/KeyBinding.cs | 8 +- Terminal.Gui/Input/Mouse/MouseBinding.cs | 3 + Terminal.Gui/View/View.Command.cs | 27 +++++- Terminal.Gui/View/View.Layout.cs | 8 +- Terminal.Gui/Views/Menu/MenuItemv2.cs | 17 +++- Terminal.Gui/Views/Menu/Menuv2.cs | 4 +- Terminal.Gui/Views/Menu/PopoverMenu.cs | 26 ++++- Terminal.Gui/Views/ScrollBar/ScrollSlider.cs | 2 +- Terminal.Gui/Views/Shortcut.cs | 15 ++- UICatalog/Scenarios/MenusV2.cs | 94 +++++++++++-------- 14 files changed, 161 insertions(+), 63 deletions(-) diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 0d68319099..2e941e29ee 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -102,7 +102,7 @@ public static bool RaiseKeyDownEvent (Key key) if (_commandImplementations.TryGetValue (command, out View.CommandImplementation? implementation)) { - CommandContext context = new (command, binding); // Create the context here + CommandContext context = new (command, null, binding); // Create the context here return implementation (context); } diff --git a/Terminal.Gui/Input/CommandContext.cs b/Terminal.Gui/Input/CommandContext.cs index bf120996bf..ebe729f494 100644 --- a/Terminal.Gui/Input/CommandContext.cs +++ b/Terminal.Gui/Input/CommandContext.cs @@ -14,15 +14,19 @@ public record struct CommandContext : ICommandContext /// /// /// - public CommandContext (Command command, TBinding? binding) + public CommandContext (Command command, View? source, TBinding? binding) { Command = command; Binding = binding; + Source = source; } /// public Command Command { get; set; } + /// + public View? Source { get; set; } + /// /// The keyboard or mouse minding that was used to invoke the , if any. /// diff --git a/Terminal.Gui/Input/ICommandContext.cs b/Terminal.Gui/Input/ICommandContext.cs index 644029ca28..cafa6c8537 100644 --- a/Terminal.Gui/Input/ICommandContext.cs +++ b/Terminal.Gui/Input/ICommandContext.cs @@ -15,4 +15,10 @@ public interface ICommandContext /// The that is being invoked. /// public Command Command { get; set; } + + /// + /// The View that was the source of hte command invocation, if any. + /// (e.g. the view the user clicked on or the view that had focus when a key was pressed). + /// + public View? Source { get; set; } } diff --git a/Terminal.Gui/Input/IInputBinding.cs b/Terminal.Gui/Input/IInputBinding.cs index 2ce2bec8bc..eff8353479 100644 --- a/Terminal.Gui/Input/IInputBinding.cs +++ b/Terminal.Gui/Input/IInputBinding.cs @@ -10,4 +10,10 @@ public interface IInputBinding /// Gets or sets the commands this input binding will invoke. /// Command [] Commands { get; set; } + + /// + /// Arbitrary context that can be associated with this input binding. + /// + public object? Data { get; set; } + } diff --git a/Terminal.Gui/Input/Keyboard/KeyBinding.cs b/Terminal.Gui/Input/Keyboard/KeyBinding.cs index eb87b33813..59806ab169 100644 --- a/Terminal.Gui/Input/Keyboard/KeyBinding.cs +++ b/Terminal.Gui/Input/Keyboard/KeyBinding.cs @@ -36,6 +36,9 @@ public KeyBinding (Command [] commands, View? target, object? data = null) /// The commands this key binding will invoke. public Command [] Commands { get; set; } + /// + public object? Data { get; set; } + /// /// The Key that is bound to the . /// @@ -43,9 +46,4 @@ public KeyBinding (Command [] commands, View? target, object? data = null) /// The view the key binding is bound to. public View? Target { get; set; } - - /// - /// Arbitrary context that can be associated with this key binding. - /// - public object? Data { get; set; } } diff --git a/Terminal.Gui/Input/Mouse/MouseBinding.cs b/Terminal.Gui/Input/Mouse/MouseBinding.cs index 11689719a7..c4b7ad25d7 100644 --- a/Terminal.Gui/Input/Mouse/MouseBinding.cs +++ b/Terminal.Gui/Input/Mouse/MouseBinding.cs @@ -25,6 +25,9 @@ public MouseBinding (Command [] commands, MouseFlags mouseFlags) /// The commands this binding will invoke. public Command [] Commands { get; set; } + /// + public object? Data { get; set; } + /// /// The mouse event arguments. /// diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 998867113e..760315736a 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -137,7 +137,9 @@ private void SetupCommands () if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button) { - bool? handled = isDefaultView.InvokeCommand (Command.Accept, new ([Command.Accept], null, this)); + // TODO: It's a bit of a hack that this uses KeyBinding. There should be an InvokeCommmand that + // TODO: is generic? + bool? handled = isDefaultView.InvokeCommand (Command.Accept, ctx); if (handled == true) { return true; @@ -146,7 +148,7 @@ private void SetupCommands () if (SuperView is { }) { - return SuperView?.InvokeCommand (Command.Accept, new ([Command.Accept], null, this)) is true; + return SuperView?.InvokeCommand (Command.Accept, ctx) is true; } } @@ -374,10 +376,31 @@ private void SetupCommands () return implementation! (new CommandContext () { Command = command, + Source = this, Binding = binding, }); } + + /// + /// Invokes the specified command. + /// + /// The command to invoke. + /// The context to pass with the command. + /// + /// if no command was found; input processing should continue. + /// if the command was invoked and was not handled (or cancelled); input processing should continue. + /// if the command was invoked the command was handled (or cancelled); input processing should stop. + /// + public bool? InvokeCommand (Command command, ICommandContext? ctx) + { + if (!_commandImplementations.TryGetValue (command, out CommandImplementation? implementation)) + { + _commandImplementations.TryGetValue (Command.NotBound, out implementation); + } + return implementation! (ctx); + } + /// /// Invokes the specified command without context. /// diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index fb9ab4fe25..2c0403355c 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -1070,10 +1070,10 @@ out int ny nx = Math.Max (targetX, 0); nx = nx + viewToMove.Frame.Width > maxDimension ? Math.Max (maxDimension - viewToMove.Frame.Width, 0) : nx; - if (nx > viewToMove.Frame.X + viewToMove.Frame.Width) - { - nx = Math.Max (viewToMove.Frame.Right, 0); - } + //if (nx > viewToMove.Frame.X + viewToMove.Frame.Width) + //{ + // nx = Math.Max (viewToMove.Frame.Right, 0); + //} } else { diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index 5dfa769e95..e2b0de0ac9 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -69,20 +69,29 @@ public MenuItemv2 (View targetView, Command command, string commandText, string? /// public Command Command { get; set; } + + internal override bool? DispatchCommand (ICommandContext? commandContext) { - bool? ret = base.DispatchCommand (commandContext); + bool? ret = null; if (TargetView is { }) { + if (commandContext is null) + { + commandContext = new CommandContext (); + } + commandContext.Command = Command; ret = TargetView.InvokeCommand (Command, commandContext); } - if (SubMenu is { }) + if (ret is true) { - // RaiseActivateSubMenu (); + return ret; } + ret = base.DispatchCommand (commandContext); + return ret; } @@ -147,7 +156,7 @@ protected virtual void OnActivateSubMenu () { } /// /// public event EventHandler>? ActivateSubMenu; - + ///// //public override Attribute GetNormalColor () //{ diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index 55ebb5e683..fd861babe9 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -63,8 +63,8 @@ protected override void OnSubViewAdded (View view) menuItem.Accepting += MenuItemtOnAccepting; void MenuItemtOnAccepting (object? sender, CommandEventArgs e) - { - //Logging.Trace($"MenuItemtOnAccepting: {e.Context}"); + { + Logging.Trace ($"MenuItemOnAccepting: {e.Context?.Source?.Title}"); } } } diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 555c512cbf..bb8805157d 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -58,6 +58,14 @@ public PopoverMenu (Menuv2? root) } KeyBindings.Add (Key.CursorLeft, Command.Left); + + AddCommand (Command.NotBound, + ctx => + { + Logging.Trace ($"popoverMenu NotBound: {ctx}"); + + return false; + }); } private Menuv2? _root; @@ -75,10 +83,19 @@ public Menuv2? Root return; } + if (_root is { }) + { + _root.Accepting -= MenuOnAccepting; + } + HideAndRemoveSubMenu (_root); _root = value; + if (_root is { }) + { + _root.Accepting += MenuOnAccepting; + } AddAndShowSubMenu (_root); } } @@ -92,8 +109,8 @@ public void ShowSubMenu (MenuItemv2? menuItem) var menu = menuItem?.SuperView as Menuv2; // If there's a visible peer, remove / hide it - - Debug.Assert(menu is null || menu?.SubViews.Count(v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); + + Debug.Assert (menu is null || menu?.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); if (menu?.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 visiblePeer) { HideAndRemoveSubMenu (visiblePeer.SubMenu); @@ -168,7 +185,10 @@ private void HideAndRemoveSubMenu (Menuv2? menu) } } - private void MenuOnAccepting (object? sender, CommandEventArgs e) { Logging.Trace ($"MenuOnSelectedMenuItemChanged: {e.Context}"); } + private void MenuOnAccepting (object? sender, CommandEventArgs e) + { + Logging.Trace ($"MenuOnAccepting: {e.Context?.Source?.Title}"); + } private void MenuOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) { Logging.Trace ($"MenuOnMenuItemCommandInvoked: {e.Context}"); } diff --git a/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs b/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs index 78682dcfb7..28f02fdcfc 100644 --- a/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs +++ b/Terminal.Gui/Views/ScrollBar/ScrollSlider.cs @@ -241,7 +241,7 @@ private void RaisePositionChangeEvents (int newPosition) OnScrolled (distance); Scrolled?.Invoke (this, new (in distance)); - RaiseSelecting (new CommandContext (Command.Select, new KeyBinding ([Command.Select], null, distance))); + RaiseSelecting (new CommandContext (Command.Select, this, new KeyBinding ([Command.Select], null, distance))); } /// diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 774c4c3b00..eb61c92410 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -46,7 +46,6 @@ public class Shortcut : View, IOrientation, IDesignable /// public Shortcut () : this (Key.Empty, null, null, null) { } - /// /// /// Creates a new instance of . /// @@ -258,18 +257,24 @@ private void AddCommands () AddCommand (Command.Select, DispatchCommand); } + /// + /// Called when a Command has been invoked on this Shortcut. + /// + /// + /// internal virtual bool? DispatchCommand (ICommandContext? commandContext) { - CommandContext? keyCommandContext = commandContext is CommandContext ? (CommandContext)commandContext : default; + CommandContext? keyCommandContext = commandContext as CommandContext? ?? default (CommandContext); if (keyCommandContext?.Binding.Data != this) { - // Invoke Select on the command view to cause it to change state if it wants to + // Invoke Select on the CommandView to cause it to change state if it wants to // If this causes CommandView to raise Accept, we eat it keyCommandContext = keyCommandContext!.Value with { Binding = keyCommandContext.Value.Binding with { Data = this } }; CommandView.InvokeCommand (Command.Select, keyCommandContext); } + // BUGBUG: Why does this use keyCommandContext and not commandContext? if (RaiseSelecting (keyCommandContext) is true) { return true; @@ -280,6 +285,10 @@ private void AddCommands () var cancel = false; + if (commandContext is { }) + { + commandContext.Source = this; + } cancel = RaiseAccepting (commandContext) is true; if (cancel) diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index e608e8f5e5..cb4b873e04 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -62,7 +62,7 @@ public override void Main () }; ConfigureOptionsSubMenu (frame, optionsSubMenu); - var optionsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "O_ptions", "File options", optionsSubMenu); + var optionsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "O_ptions", "File options", optionsSubMenu); rootMenu.Add (optionsSubMenuItem); var detailsSubMenu = new Menuv2 @@ -72,7 +72,7 @@ public override void Main () }; ConfigureDetialsSubMenu (frame, detailsSubMenu); - var detailsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "_Details", "File details", detailsSubMenu); + var detailsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "_Details", "File details", detailsSubMenu); rootMenu.Add (detailsSubMenuItem); var moreDetailsSubMenu = new Menuv2 @@ -82,7 +82,7 @@ public override void Main () }; ConfigureMoreDetailsSubMenu (frame, moreDetailsSubMenu); - var moreDetailsSubMenuItem = new MenuItemv2 (frame, Command.Accept, "_More Details", "More details", moreDetailsSubMenu); + var moreDetailsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "_More Details", "More details", moreDetailsSubMenu); detailsSubMenu.Add (moreDetailsSubMenuItem); var popoverMenu = new PopoverMenu (rootMenu) @@ -101,29 +101,32 @@ public override void Main () frame.CommandNotBound += (o, args) => { - eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + Logging.Trace ($"frame CommandNotBound: {args.Context.Command}"); + eventSource.Add ($"frame CommandNotBound: {args.Context.Command}"); eventLog.MoveDown (); args.Cancel = true; }; frame.Accepting += (o, args) => { - eventSource.Add ($"{args.Context!.Command}: {frame?.Id}"); + Logging.Trace ($"frame CommandNotBound: {args?.Context?.Source?.Title}"); + eventSource.Add ($"frame Accepting: {args?.Context?.Source?.Title}: "); eventLog.MoveDown (); // args.Cancel = true; }; popoverMenu.Accepting += (o, args) => { - Logging.Trace ($"Accepting: {popoverMenu!.Id} {args.Context.Command}"); - //eventSource.Add ($"Accepting: {menu!.Id} {args.Context.Command}"); - //eventLog.MoveDown (); + + Logging.Trace ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); + eventSource.Add ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); + eventLog.MoveDown (); //args.Cancel = true; }; popoverMenu.Selecting += (o, args) => { - Logging.Trace ($"Selecting: {popoverMenu!.Id} {args.Context.Command}"); + Logging.Trace ($"{popoverMenu!.Id} Selecting: {args.Context}"); //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); //eventLog.MoveDown (); //args.Cancel = false; @@ -149,7 +152,7 @@ public override void Main () sh.Accepting += (o, args) => { - Logging.Trace ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + Logging.Trace ($"sh Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventLog.MoveDown (); //args.Cancel = true; @@ -157,7 +160,7 @@ public override void Main () sh.Selecting += (o, args) => { - Logging.Trace ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + Logging.Trace ($"sh Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventLog.MoveDown (); //args.Cancel = false; @@ -177,7 +180,8 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) var shortcut1 = new MenuItemv2 { Title = "_New", - Key = Key.N.WithCtrl, + Key = Key.N.WithAlt, + BindKeyToApplication = true, Text = "New File", Command = Command.New, TargetView = targetView @@ -187,7 +191,8 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) { Title = "_Open...", Text = "Open File", - Key = Key.O.WithCtrl, + Key = Key.O.WithAlt, + BindKeyToApplication = true, Command = Command.Open, TargetView = targetView }; @@ -196,7 +201,8 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) { Title = "_Save", Text = "Save file", - Key = Key.S.WithCtrl, + Key = Key.S.WithAlt, + BindKeyToApplication = true, Command = Command.Save, TargetView = targetView }; @@ -205,7 +211,8 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) { Title = "Sa_ve As...", Text = "Save file as", - Key = Key.V.WithCtrl, + Key = Key.V.WithAlt, + BindKeyToApplication = true, Command = Command.SaveAs, TargetView = targetView @@ -216,26 +223,27 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) { Title = "_Auto Save", Text = "Automatically save", - Key = Key.A.WithCtrl, - TargetView = targetView - }; + Key = Key.A.WithAlt, + BindKeyToApplication = true, - shortcut5.CommandView = new CheckBox - { - Title = shortcut5.Title, - HighlightStyle = HighlightStyle.None, - CanFocus = false }; + //shortcut5.CommandView = new CheckBox + //{ + // Title = shortcut5.Title, + // HighlightStyle = HighlightStyle.None, + // CanFocus = false + //}; + var line = new Line { - X = -1, + X = -1, Width = Dim.Fill ()! + 1 }; - //// This ensures the checkbox state toggles when the hotkey of Title is pressed. - ////shortcut4.Accepting += (sender, args) => args.Cancel = true; + // This ensures the checkbox state toggles when the hotkey of Title is pressed. + //shortcut4.Accepting += (sender, args) => args.Cancel = true; menu.Add (shortcut1, shortcut2, shortcut3, shortcut4, line, shortcut5); } @@ -245,16 +253,20 @@ private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) { var shortcut2 = new MenuItemv2 { - Title = "_Option 1", - Text = "Some option #1", - Key = Key.G.WithAlt + Title = "_Enable Overwrite", + Text = "Overwrite", + Key = Key.O.WithAlt, + BindKeyToApplication = true, + Command = Command.EnableOverwrite, + TargetView = targetView }; var shortcut3 = new MenuItemv2 { Title = "_Three", Text = "The 3rd item", - Key = Key.T.WithAlt + Key = Key.T.WithAlt, + BindKeyToApplication = true, }; var line = new Line @@ -267,7 +279,8 @@ private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) { Title = "_Four", Text = "Below the line", - Key = Key.D7.WithAlt + Key = Key.D7.WithAlt, + BindKeyToApplication = true, }; shortcut4.CommandView = new CheckBox @@ -278,7 +291,7 @@ private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) }; // This ensures the checkbox state toggles when the hotkey of Title is pressed. - //shortcut4.Accepting += (sender, args) => args.Cancel = true; + // shortcut4.Accepting += (sender, args) => args.Cancel = true; menu.Add (shortcut2, shortcut3, line, shortcut4); } @@ -289,14 +302,16 @@ private void ConfigureDetialsSubMenu (View targetView, Menuv2 menu) { Title = "_Detail 1", Text = "Some detail #1", - Key = Key.G.WithAlt + Key = Key.G.WithAlt, + BindKeyToApplication = true, }; var shortcut3 = new MenuItemv2 { Title = "_Three", Text = "The 3rd item", - Key = Key.D3.WithAlt + Key = Key.D9.WithAlt, + BindKeyToApplication = true, }; var line = new Line @@ -309,7 +324,9 @@ private void ConfigureDetialsSubMenu (View targetView, Menuv2 menu) { Title = "_Four", Text = "Below the line", - Key = Key.D8.WithAlt + Key = Key.D8.WithAlt, + BindKeyToApplication = true, + }; shortcut4.CommandView = new CheckBox @@ -332,7 +349,8 @@ private void ConfigureMoreDetailsSubMenu (View targetView, Menuv2 menu) { Title = "_Deeper Detail", Text = "Deeper Detail", - Key = Key.V.WithAlt + Key = Key.D.WithAlt, + BindKeyToApplication = true, }; var line = new Line @@ -345,7 +363,9 @@ private void ConfigureMoreDetailsSubMenu (View targetView, Menuv2 menu) { Title = "_Third", Text = "Below the line", - Key = Key.D3.WithAlt + Key = Key.D5.WithAlt, + BindKeyToApplication = true, + }; // This ensures the checkbox state toggles when the hotkey of Title is pressed. From c7336d710bf1b0841fcfe2f7f7cd992557ce3d35 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 21 Mar 2025 09:33:51 +0100 Subject: [PATCH 71/75] code cleanup --- Terminal.Gui/Views/Menu/PopoverMenu.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index bb8805157d..9cb4e4a48e 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -29,9 +29,7 @@ public PopoverMenu (Menuv2? root) bool? MoveRight (ICommandContext? ctx) { - var focused = MostFocused as MenuItemv2; - - if (focused is { SubMenu.Visible: true }) + if (MostFocused is MenuItemv2 { SubMenu.Visible: true } focused) { focused.SubMenu.SetFocus (); @@ -170,8 +168,8 @@ private void HideAndRemoveSubMenu (Menuv2? menu) if (menu is { Visible: true }) { // If there's a visible submenu, remove / hide it - Debug.Assert (menu?.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); - if (menu?.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 mi) + Debug.Assert (menu.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); + if (menu.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 mi) { HideAndRemoveSubMenu (mi.SubMenu); mi.ForceFocusColors = false; From 658c5f4c4899c96f65c9f85db8684e026bc9e52c Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 21 Mar 2025 16:21:04 +0100 Subject: [PATCH 72/75] Fixed IsDefault bug; --- Terminal.Gui/Views/ComboBox.cs | 10 +++------- Terminal.Gui/Views/MessageBox.cs | 18 ++++++------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index 6def5670da..8b28f12882 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -80,11 +80,7 @@ public ComboBox () // Things this view knows how to do AddCommand (Command.Accept, (ctx) => { - if (ctx is not CommandContext keyCommandContext) - { - return false; - } - if (keyCommandContext.Binding.Data == _search) + if (ctx?.Source == _search) { return null; } @@ -93,8 +89,8 @@ public ComboBox () AddCommand (Command.Toggle, () => ExpandCollapse ()); AddCommand (Command.Expand, () => Expand ()); AddCommand (Command.Collapse, () => Collapse ()); - AddCommand (Command.Down, () => MoveDown ()); - AddCommand (Command.Up, () => MoveUp ()); + AddCommand (Command.Down, MoveDown); + AddCommand (Command.Up, MoveUp); AddCommand (Command.PageDown, () => PageDown ()); AddCommand (Command.PageUp, () => PageUp ()); AddCommand (Command.Start, () => MoveHome ()); diff --git a/Terminal.Gui/Views/MessageBox.cs b/Terminal.Gui/Views/MessageBox.cs index 31b4a41ec2..f7b2979f51 100644 --- a/Terminal.Gui/Views/MessageBox.cs +++ b/Terminal.Gui/Views/MessageBox.cs @@ -360,26 +360,20 @@ params string [] buttons b.IsDefault = true; b.Accepting += (_, e) => { - if (e.Context is not CommandContext keyCommandContext) - { - return; - } - - // TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we can simplify this - if (keyCommandContext.Binding.Data is Button button) + if (e?.Context?.Source is Button button) { Clicked = (int)button.Data!; } - else if (keyCommandContext.Binding.Target is Button btn) - { - Clicked = (int)btn.Data!; - } else { Clicked = defaultButton; } - e.Cancel = true; + if (e is { }) + { + e.Cancel = true; + } + Application.RequestStop (); }; } From a9acc857da4b41b683f981b0cc2d508f6e411b09 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 21 Mar 2025 16:43:35 +0100 Subject: [PATCH 73/75] Enabled hotkey support --- Terminal.Gui/Views/Menu/PopoverMenu.cs | 43 +++++++++++++++++ UICatalog/Scenarios/MenusV2.cs | 66 +++++++++++++------------- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 9cb4e4a48e..6ab4b1c9df 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -95,9 +95,52 @@ public Menuv2? Root _root.Accepting += MenuOnAccepting; } AddAndShowSubMenu (_root); + + // TODO: This needs to be done whenever any MenuItem in the menu tree changes to support dynamic menus + // TODO: And it needs to clear them first + IEnumerable all = GetMenuItemsOfAllSubMenus (); + foreach (MenuItemv2 menu in all) + { + if (menu.Key.IsValid) + { + Logging.Trace ($"{menu.Key}->{menu.Command}"); + KeyBindings.Add (menu.Key, menu.Command); + } + } + } + } + + internal IEnumerable GetMenuItemsOfAllSubMenus () + { + List result = []; + if (Root == null) + { + return result; } + + Stack stack = new (); + stack.Push (Root); + + while (stack.Count > 0) + { + Menuv2 currentMenu = stack.Pop (); + foreach (View subView in currentMenu.SubViews) + { + if (subView is MenuItemv2 menuItem) + { + result.Add (menuItem); + if (menuItem.SubMenu != null) + { + stack.Push (menuItem.SubMenu); + } + } + } + } + + return result; } + /// /// Pops up the submenu of the specified MenuItem, if there is one. /// diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index cb4b873e04..505e967462 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -36,10 +36,10 @@ public override void Main () }; eventLog.Border!.Thickness = new (0, 1, 0, 0); - FrameView frame = new () + FrameView targetView = new () { - Id = "frame", - Title = "Cascading Menu...", + Id = "targetView", + Title = "Target View", X = 4, Y = 4, @@ -47,22 +47,22 @@ public override void Main () Height = Dim.Fill (8), BorderStyle = LineStyle.Dotted }; - app.Add (frame); + app.Add (targetView); var rootMenu = new Menuv2 () { Id = "rootMenu", }; - ConfigureRootMenu (frame, rootMenu); + ConfigureRootMenu (targetView, rootMenu); var optionsSubMenu = new Menuv2 { Id = "optionsSubMenu", Visible = false }; - ConfigureOptionsSubMenu (frame, optionsSubMenu); + ConfigureOptionsSubMenu (targetView, optionsSubMenu); - var optionsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "O_ptions", "File options", optionsSubMenu); + var optionsSubMenuItem = new MenuItemv2 (targetView, Command.NotBound, "O_ptions", "File options", optionsSubMenu); rootMenu.Add (optionsSubMenuItem); var detailsSubMenu = new Menuv2 @@ -70,9 +70,9 @@ public override void Main () Id = "detailsSubMenu", Visible = false }; - ConfigureDetialsSubMenu (frame, detailsSubMenu); + ConfigureDetialsSubMenu (targetView, detailsSubMenu); - var detailsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "_Details", "File details", detailsSubMenu); + var detailsSubMenuItem = new MenuItemv2 (targetView, Command.NotBound, "_Details", "File details", detailsSubMenu); rootMenu.Add (detailsSubMenuItem); var moreDetailsSubMenu = new Menuv2 @@ -80,9 +80,9 @@ public override void Main () Id = "moreDetailsSubMenu", Visible = false }; - ConfigureMoreDetailsSubMenu (frame, moreDetailsSubMenu); + ConfigureMoreDetailsSubMenu (targetView, moreDetailsSubMenu); - var moreDetailsSubMenuItem = new MenuItemv2 (frame, Command.NotBound, "_More Details", "More details", moreDetailsSubMenu); + var moreDetailsSubMenuItem = new MenuItemv2 (targetView, Command.NotBound, "_More Details", "More details", moreDetailsSubMenu); detailsSubMenu.Add (moreDetailsSubMenuItem); var popoverMenu = new PopoverMenu (rootMenu) @@ -97,36 +97,36 @@ public override void Main () //rootMenu.SubViews.ElementAt (0).SetFocus (); - frame.Add (popoverMenu); + targetView.Add (popoverMenu); - frame.CommandNotBound += (o, args) => + targetView.CommandNotBound += (o, args) => { - Logging.Trace ($"frame CommandNotBound: {args.Context.Command}"); - eventSource.Add ($"frame CommandNotBound: {args.Context.Command}"); + Logging.Trace ($"targetView CommandNotBound: {args.Context.Command}"); + eventSource.Add ($"targetView CommandNotBound: {args.Context.Command}"); eventLog.MoveDown (); args.Cancel = true; }; - frame.Accepting += (o, args) => + targetView.Accepting += (o, args) => { - Logging.Trace ($"frame CommandNotBound: {args?.Context?.Source?.Title}"); - eventSource.Add ($"frame Accepting: {args?.Context?.Source?.Title}: "); + Logging.Trace ($"targetView CommandNotBound: {args?.Context?.Source?.Title}"); + eventSource.Add ($"targetView Accepting: {args?.Context?.Source?.Title}: "); eventLog.MoveDown (); - // args.Cancel = true; + args.Cancel = true; }; popoverMenu.Accepting += (o, args) => { - Logging.Trace ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); - eventSource.Add ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); - eventLog.MoveDown (); + //Logging.Trace ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); + //eventSource.Add ($"{popoverMenu!.Id} Accepting: {args?.Context?.Source?.Title}"); + //eventLog.MoveDown (); //args.Cancel = true; }; popoverMenu.Selecting += (o, args) => { - Logging.Trace ($"{popoverMenu!.Id} Selecting: {args.Context}"); + //Logging.Trace ($"{popoverMenu!.Id} Selecting: {args.Context}"); //eventSource.Add ($"Selecting: {menu!.Id} {args.Context.Command}"); //eventLog.MoveDown (); //args.Cancel = false; @@ -152,7 +152,7 @@ public override void Main () sh.Accepting += (o, args) => { - Logging.Trace ($"sh Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //Logging.Trace ($"sh Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventSource.Add ($"Accepting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventLog.MoveDown (); //args.Cancel = true; @@ -160,7 +160,7 @@ public override void Main () sh.Selecting += (o, args) => { - Logging.Trace ($"sh Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); + //Logging.Trace ($"sh Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventSource.Add ($"Selecting: {sh!.SuperView?.Id} {sh!.CommandView.Text}"); //eventLog.MoveDown (); //args.Cancel = false; @@ -228,12 +228,12 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) }; - //shortcut5.CommandView = new CheckBox - //{ - // Title = shortcut5.Title, - // HighlightStyle = HighlightStyle.None, - // CanFocus = false - //}; + shortcut5.CommandView = new CheckBox + { + Title = shortcut5.Title, + HighlightStyle = HighlightStyle.None, + CanFocus = false + }; var line = new Line { @@ -253,9 +253,9 @@ private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) { var shortcut2 = new MenuItemv2 { - Title = "_Enable Overwrite", + Title = "Enable Over_write", Text = "Overwrite", - Key = Key.O.WithAlt, + Key = Key.W.WithAlt, BindKeyToApplication = true, Command = Command.EnableOverwrite, TargetView = targetView From a73f89f6b8967bc76384127ba362ba6d52ac0e6e Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 21 Mar 2025 18:43:43 +0100 Subject: [PATCH 74/75] Made context-menu-like --- Terminal.Gui/Views/Menu/MenuItemv2.cs | 67 ++++++------- Terminal.Gui/Views/Menu/Menuv2.cs | 59 ++++++------ Terminal.Gui/Views/Menu/PopoverMenu.cs | 128 ++++++++++++++++++++++--- UICatalog/Scenarios/Bars.cs | 4 +- UICatalog/Scenarios/MenusV2.cs | 34 ++++++- 5 files changed, 208 insertions(+), 84 deletions(-) diff --git a/Terminal.Gui/Views/Menu/MenuItemv2.cs b/Terminal.Gui/Views/Menu/MenuItemv2.cs index e2b0de0ac9..fd2c22b2bf 100644 --- a/Terminal.Gui/Views/Menu/MenuItemv2.cs +++ b/Terminal.Gui/Views/Menu/MenuItemv2.cs @@ -85,12 +85,14 @@ public MenuItemv2 (View targetView, Command command, string commandText, string? ret = TargetView.InvokeCommand (Command, commandContext); } - if (ret is true) + if (ret is not true) { - return ret; + ret = base.DispatchCommand (commandContext); } - ret = base.DispatchCommand (commandContext); + Logging.Trace ($"{commandContext?.Source?.Title}"); + + RaiseAccepted (commandContext); return ret; } @@ -108,54 +110,43 @@ protected override bool OnMouseEnter (CancelEventArgs eventArgs) return base.OnMouseEnter (eventArgs); } - /// - protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view) - { - //SetNeedsDraw(); - base.OnHasFocusChanged (newHasFocus, previousFocusedView, view); - //if (SubMenu is null || view == SubMenu) - //{ - // return; - //} - - //if (newHasFocus) - //{ - // if (!SubMenu.Visible) - // { - // RaiseActivateSubMenu (); - // } - //} - //else - //{ - // SubMenu.Visible = false; - //} - } /// + /// Riases the / event indicating this item (or submenu) + /// was accepted. This is used to determine when to hide the menu. /// - /// - /// - protected void RaiseActivateSubMenu () + /// + /// + protected bool? RaiseAccepted (ICommandContext? ctx) { - if (SubMenu is null) - { - return; - } + Logging.Trace ($"RaiseAccepted: {ctx}"); + CommandEventArgs args = new () { Context = ctx }; - OnActivateSubMenu (); + OnAccepted (args); + Accepted?.Invoke (this, args); - // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. - var args = new EventArgs (SubMenu); - ActivateSubMenu?.Invoke (this, args); + return true; } /// + /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. /// - protected virtual void OnActivateSubMenu () { } + /// + /// + /// + protected virtual void OnAccepted (CommandEventArgs args) { } /// + /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. /// - public event EventHandler>? ActivateSubMenu; + /// + /// + /// See for more information. + /// + /// + public event EventHandler? Accepted; + + ///// //public override Attribute GetNormalColor () diff --git a/Terminal.Gui/Views/Menu/Menuv2.cs b/Terminal.Gui/Views/Menu/Menuv2.cs index fd861babe9..8f852759ad 100644 --- a/Terminal.Gui/Views/Menu/Menuv2.cs +++ b/Terminal.Gui/Views/Menu/Menuv2.cs @@ -58,64 +58,65 @@ protected override void OnSubViewAdded (View view) menuItem.CanFocus = true; menuItem.Orientation = Orientation.Vertical; - AddCommand (menuItem.Command, RaiseMenuItemCommandInvoked); + AddCommand (menuItem.Command, RaiseAccepted); - menuItem.Accepting += MenuItemtOnAccepting; + menuItem.Selecting += MenuItemOnSelecting; + menuItem.Accepting += MenuItemOnAccepting; + menuItem.Accepted += MenuItemOnAccepted; - void MenuItemtOnAccepting (object? sender, CommandEventArgs e) - { - Logging.Trace ($"MenuItemOnAccepting: {e.Context?.Source?.Title}"); + void MenuItemOnSelecting (object? sender, CommandEventArgs e) + { + //Logging.Trace ($"MenuItemOnSelecting: {e.Context?.Source?.Title}"); + } + + void MenuItemOnAccepting (object? sender, CommandEventArgs e) + { + // Logging.Trace ($"MenuItemOnAccepting: {e.Context?.Source?.Title}"); + } + + void MenuItemOnAccepted (object? sender, CommandEventArgs e) + { + Logging.Trace ($"MenuItemOnAccepted: {e.Context?.Source?.Title}"); + RaiseAccepted (e.Context); } } } /// - /// + /// Riases the / event indicating an item in this menu (or submenu) + /// was accepted. This is used to determine when to hide the menu. /// /// /// - protected bool? RaiseMenuItemCommandInvoked (ICommandContext? ctx) + protected bool? RaiseAccepted (ICommandContext? ctx) { - Logging.Trace ($"RaiseMenuItemCommandInvoked: {ctx}"); + Logging.Trace ($"RaiseAccepted: {ctx}"); CommandEventArgs args = new () { Context = ctx }; - // Best practice is to invoke the virtual method first. - // This allows derived classes to handle the event and potentially cancel it. - args.Cancel = OnMenuItemCommandInvoked (args) || args.Cancel; - - if (!args.Cancel) - { - // If the event is not canceled by the virtual method, raise the event to notify any external subscribers. - MenuItemCommandInvoked?.Invoke (this, args); - } + OnAccepted (args); + Accepted?.Invoke (this, args); - return MenuItemCommandInvoked is null ? null : args.Cancel; + return true; } /// - /// Called when the user is accepting the state of the View and the has been invoked. Set CommandEventArgs.Cancel to - /// and return to stop processing. + /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. /// /// - /// - /// See for more information. - /// /// /// - /// to stop processing. - protected virtual bool OnMenuItemCommandInvoked (CommandEventArgs args) { return false; } + protected virtual void OnAccepted (CommandEventArgs args) { } /// - /// Cancelable event raised when the user is accepting the state of the View and the has been invoked. Set - /// CommandEventArgs.Cancel to cancel the event. + /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. /// /// /// - /// See for more information. + /// See for more information. /// /// - public event EventHandler? MenuItemCommandInvoked; + public event EventHandler? Accepted; /// protected override void OnFocusedChanged (View? previousFocused, View? focused) diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index 6ab4b1c9df..f2241c06b0 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -64,6 +64,55 @@ public PopoverMenu (Menuv2? root) return false; }); + + KeyBindings.Add (DefaultKey, Command.Quit); + KeyBindings.Add (Application.QuitKey, Command.Quit); + + AddCommand (Command.Quit, ctx => + { + Visible = false; + return RaiseAccepted (ctx); + }); + } + + /// + /// The mouse flags that will cause the popover menu to be visible. The default is which is typically the right mouse button. + /// + public MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked; + + /// The default key for activating the popover menu. + [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] + public static Key DefaultKey { get; set; } = Key.F10.WithShift; + + /// + /// Sets the position of the Popover Menu. The actual position of the menu will be adjusted to + /// ensure the menu fully fits on the screen, and the mouse cursor is over the first cell of the + /// first MenuItem. + /// + /// + public void SetPosition (Point? screenPosition) + { + screenPosition ??= Application.GetLastMousePosition (); + + if (screenPosition is { }) + { + X = screenPosition.Value.X - GetViewportOffsetFromFrame ().X; + Y = screenPosition.Value.Y - GetViewportOffsetFromFrame ().Y; + } + } + + /// + protected override void OnVisibleChanged () + { + base.OnVisibleChanged (); + if (Visible) + { + AddAndShowSubMenu (_root); + } + else + { + HideAndRemoveSubMenu (_root); + } } private Menuv2? _root; @@ -194,33 +243,32 @@ internal Point GetMostVisibleLocationForSubMenu (MenuItemv2 menuItem) private void AddAndShowSubMenu (Menuv2? menu) { - if (menu is { }) + if (menu is { SuperView: null }) { base.Add (menu); - + menu.Visible = true; menu.Layout (); menu.Accepting += MenuOnAccepting; - menu.MenuItemCommandInvoked += MenuOnMenuItemCommandInvoked; + menu.Accepted += MenuAccepted; menu.SelectedMenuItemChanged += MenuOnSelectedMenuItemChanged; } } private void HideAndRemoveSubMenu (Menuv2? menu) { - Debug.Assert (menu is null || menu is { Visible: true }); if (menu is { Visible: true }) { // If there's a visible submenu, remove / hide it - Debug.Assert (menu.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); - if (menu.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 mi) + Debug.Assert (menu.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) <= 1); + if (menu.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 visiblePeer) { - HideAndRemoveSubMenu (mi.SubMenu); - mi.ForceFocusColors = false; + HideAndRemoveSubMenu (visiblePeer.SubMenu); + visiblePeer.ForceFocusColors = false; } menu.Visible = false; menu.Accepting -= MenuOnAccepting; - menu.MenuItemCommandInvoked -= MenuOnMenuItemCommandInvoked; + menu.Accepted -= MenuAccepted; menu.SelectedMenuItemChanged -= MenuOnSelectedMenuItemChanged; base.Remove (menu); } @@ -228,16 +276,72 @@ private void HideAndRemoveSubMenu (Menuv2? menu) private void MenuOnAccepting (object? sender, CommandEventArgs e) { - Logging.Trace ($"MenuOnAccepting: {e.Context?.Source?.Title}"); + //Logging.Trace ($"{e.Context?.Source?.Title}"); + } + + private void MenuAccepted (object? sender, CommandEventArgs e) + { + Logging.Trace ($"{e.Context?.Source?.Title}"); + + if (e.Context?.Source is MenuItemv2 { SubMenu: null }) + { + HideAndRemoveSubMenu (_root); + } + + RaiseAccepted (e.Context); } - private void MenuOnMenuItemCommandInvoked (object? sender, CommandEventArgs e) { Logging.Trace ($"MenuOnMenuItemCommandInvoked: {e.Context}"); } + /// + /// Riases the / event indicating a menu (or submenu) + /// was accepted and the Menus in the PopoverMenu were hidden. Use this to determine when to hide the PopoverMenu. + /// + /// + /// + protected bool? RaiseAccepted (ICommandContext? ctx) + { + Logging.Trace ($"RaiseAccepted: {ctx}"); + CommandEventArgs args = new () { Context = ctx }; + + OnAccepted (args); + Accepted?.Invoke (this, args); + + return true; + } + + /// + /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. + /// + /// + /// + /// + protected virtual void OnAccepted (CommandEventArgs args) { } + + /// + /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. + /// + /// + /// + /// See for more information. + /// + /// + public event EventHandler? Accepted; + private void MenuOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) { - Logging.Trace ($"MenuOnSelectedMenuItemChanged: {e}"); + //Logging.Trace ($"{e}"); ShowSubMenu (e); } + /// + protected override void Dispose (bool disposing) + { + if (disposing) + { + _root?.Dispose (); + _root = null; + } + base.Dispose (disposing); + } } diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 22bf3111ba..071cbc8587 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/UICatalog/Scenarios/Bars.cs @@ -321,11 +321,11 @@ void MenuLikeExamplesMouseClick (object? sender, MouseEventArgs e) if (barView is Menuv2 menuv2) { - menuv2.MenuItemCommandInvoked += (o, args) => + menuv2.Accepted += (o, args) => { if (args.Context is CommandContext { Binding.Data: MenuItemv2 { } sc }) { - eventSource.Add ($"Invoked: {sc.Id} {args.Context.Command}"); + eventSource.Add ($"Accepted: {sc.Id} {args.Context.Command}"); } eventLog.MoveDown (); diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index 505e967462..d2d9df568f 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -36,7 +36,7 @@ public override void Main () }; eventLog.Border!.Thickness = new (0, 1, 0, 0); - FrameView targetView = new () + TargetView targetView = new () { Id = "targetView", Title = "Target View", @@ -124,6 +124,15 @@ public override void Main () //args.Cancel = true; }; + popoverMenu.Accepted += (o, args) => + { + Logging.Trace ($"{popoverMenu!.Id} Accepted: {args?.Context?.Source?.Title}"); + eventSource.Add ($"{popoverMenu!.Id} Accepted: {args?.Context?.Source?.Title}"); + eventLog.MoveDown (); + + popoverMenu.Visible = false; + }; + popoverMenu.Selecting += (o, args) => { //Logging.Trace ($"{popoverMenu!.Id} Selecting: {args.Context}"); @@ -237,7 +246,7 @@ private void ConfigureRootMenu (View targetView, Menuv2 menu) var line = new Line { - X = -1, + X = -1, Width = Dim.Fill ()! + 1 }; @@ -291,7 +300,7 @@ private void ConfigureOptionsSubMenu (View targetView, Menuv2 menu) }; // This ensures the checkbox state toggles when the hotkey of Title is pressed. - // shortcut4.Accepting += (sender, args) => args.Cancel = true; + // shortcut4.Accepting += (sender, args) => args.Cancel = true; menu.Add (shortcut2, shortcut3, line, shortcut4); } @@ -374,6 +383,25 @@ private void ConfigureMoreDetailsSubMenu (View targetView, Menuv2 menu) menu.Add (shortcut2, line, shortcut4); } + public class TargetView : FrameView + { + public TargetView () + { + AddCommand (Command.Context, + ctx => + { + if (SubViews.FirstOrDefault (v => v is PopoverMenu) is PopoverMenu { } popoverMenu) + { + popoverMenu.Visible = true; + } + + return true; + }); + + KeyBindings.Add (PopoverMenu.DefaultKey, Command.Context); + } + } + private const string LOGFILE_LOCATION = "./logs"; private static string _logFilePath = string.Empty; From 619d5c08b7f95bfb099bafb21abfcbf3ebd05ddf Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 22 Mar 2025 08:03:21 +0100 Subject: [PATCH 75/75] Improved usability --- Terminal.Gui/Views/Menu/PopoverMenu.cs | 120 ++++++++++++++++--------- UICatalog/Scenarios/MenusV2.cs | 27 ++++-- 2 files changed, 100 insertions(+), 47 deletions(-) diff --git a/Terminal.Gui/Views/Menu/PopoverMenu.cs b/Terminal.Gui/Views/Menu/PopoverMenu.cs index f2241c06b0..47c3b00cc8 100644 --- a/Terminal.Gui/Views/Menu/PopoverMenu.cs +++ b/Terminal.Gui/Views/Menu/PopoverMenu.cs @@ -57,54 +57,78 @@ public PopoverMenu (Menuv2? root) KeyBindings.Add (Key.CursorLeft, Command.Left); - AddCommand (Command.NotBound, - ctx => - { - Logging.Trace ($"popoverMenu NotBound: {ctx}"); + AddCommand ( + Command.NotBound, + ctx => + { + Logging.Trace ($"popoverMenu NotBound: {ctx}"); - return false; - }); + return false; + }); KeyBindings.Add (DefaultKey, Command.Quit); KeyBindings.Add (Application.QuitKey, Command.Quit); - AddCommand (Command.Quit, ctx => - { - Visible = false; - return RaiseAccepted (ctx); - }); + AddCommand ( + Command.Quit, + ctx => + { + Visible = false; + + return RaiseAccepted (ctx); + }); } /// - /// The mouse flags that will cause the popover menu to be visible. The default is which is typically the right mouse button. + /// The mouse flags that will cause the popover menu to be visible. The default is + /// which is typically the right mouse button. /// - public MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked; + public static MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked; /// The default key for activating the popover menu. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))] public static Key DefaultKey { get; set; } = Key.F10.WithShift; /// - /// Sets the position of the Popover Menu. The actual position of the menu will be adjusted to + /// Makes the menu visible and locates it at . The actual position of the menu + /// will be adjusted to /// ensure the menu fully fits on the screen, and the mouse cursor is over the first cell of the /// first MenuItem. /// - /// - public void SetPosition (Point? screenPosition) + /// If , the current mouse position will be used. + public void MakeVisible (Point? idealScreenPosition = null) + { + Visible = true; + SetPosition (idealScreenPosition); + } + + /// + /// Locates the menu at . The actual position of the menu will be adjusted to + /// ensure the menu fully fits on the screen, and the mouse cursor is over the first cell of the + /// first MenuItem (if possible). + /// + /// If , the current mouse position will be used. + public void SetPosition (Point? idealScreenPosition = null) { - screenPosition ??= Application.GetLastMousePosition (); + idealScreenPosition ??= Application.GetLastMousePosition (); - if (screenPosition is { }) + if (idealScreenPosition is { } && Root is { }) { - X = screenPosition.Value.X - GetViewportOffsetFromFrame ().X; - Y = screenPosition.Value.Y - GetViewportOffsetFromFrame ().Y; + Point pos = idealScreenPosition.Value; + pos.Offset (-Root.GetAdornmentsThickness ().Left, -Root.GetAdornmentsThickness ().Top); + + pos = GetMostVisibleLocationForSubMenu (Root, ScreenToViewport (pos)); + + Root.X = pos.X; + Root.Y = pos.Y; } } - /// + /// protected override void OnVisibleChanged () { base.OnVisibleChanged (); + if (Visible) { AddAndShowSubMenu (_root); @@ -143,11 +167,13 @@ public Menuv2? Root { _root.Accepting += MenuOnAccepting; } + AddAndShowSubMenu (_root); // TODO: This needs to be done whenever any MenuItem in the menu tree changes to support dynamic menus // TODO: And it needs to clear them first IEnumerable all = GetMenuItemsOfAllSubMenus (); + foreach (MenuItemv2 menu in all) { if (menu.Key.IsValid) @@ -162,6 +188,7 @@ public Menuv2? Root internal IEnumerable GetMenuItemsOfAllSubMenus () { List result = []; + if (Root == null) { return result; @@ -173,11 +200,13 @@ internal IEnumerable GetMenuItemsOfAllSubMenus () while (stack.Count > 0) { Menuv2 currentMenu = stack.Pop (); + foreach (View subView in currentMenu.SubViews) { if (subView is MenuItemv2 menuItem) { result.Add (menuItem); + if (menuItem.SubMenu != null) { stack.Push (menuItem.SubMenu); @@ -189,18 +218,18 @@ internal IEnumerable GetMenuItemsOfAllSubMenus () return result; } - /// /// Pops up the submenu of the specified MenuItem, if there is one. /// /// - public void ShowSubMenu (MenuItemv2? menuItem) + internal void ShowSubMenu (MenuItemv2? menuItem) { var menu = menuItem?.SuperView as Menuv2; // If there's a visible peer, remove / hide it Debug.Assert (menu is null || menu?.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) < 2); + if (menu?.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 visiblePeer) { HideAndRemoveSubMenu (visiblePeer.SubMenu); @@ -211,7 +240,12 @@ public void ShowSubMenu (MenuItemv2? menuItem) { AddAndShowSubMenu (menuItem.SubMenu); - Point pos = GetMostVisibleLocationForSubMenu (menuItem); + Point idealLocation = ScreenToViewport ( + new ( + menuItem.FrameToScreen ().Right - menuItem.SubMenu.GetAdornmentsThickness ().Left, + menuItem.FrameToScreen ().Top - menuItem.SubMenu.GetAdornmentsThickness ().Top)); + + Point pos = GetMostVisibleLocationForSubMenu (menuItem.SubMenu, idealLocation); menuItem.SubMenu.X = pos.X; menuItem.SubMenu.Y = pos.Y; @@ -221,20 +255,20 @@ public void ShowSubMenu (MenuItemv2? menuItem) } /// - /// Given a , returns the most visible location for the submenu. - /// The location is relative to the Frame. + /// Gets the most visible screen-relative location for . /// - /// + /// The menu to locate. + /// Ideal screen-relative location. /// - internal Point GetMostVisibleLocationForSubMenu (MenuItemv2 menuItem) + internal Point GetMostVisibleLocationForSubMenu (Menuv2 menu, Point idealLocation) { var pos = Point.Empty; // Calculate the initial position to the right of the menu item GetLocationEnsuringFullVisibility ( - menuItem.SubMenu!, - menuItem.SuperView!.Frame.X + menuItem.Frame.Width, - menuItem.SuperView.Frame.Y + menuItem.Frame.Y, + menu, + idealLocation.X, + idealLocation.Y, out int nx, out int ny); @@ -252,14 +286,18 @@ private void AddAndShowSubMenu (Menuv2? menu) menu.Accepting += MenuOnAccepting; menu.Accepted += MenuAccepted; menu.SelectedMenuItemChanged += MenuOnSelectedMenuItemChanged; + + // TODO: Find the menu item below the mouse, if any, and select it } } + private void HideAndRemoveSubMenu (Menuv2? menu) { if (menu is { Visible: true }) { // If there's a visible submenu, remove / hide it Debug.Assert (menu.SubViews.Count (v => v is MenuItemv2 { SubMenu.Visible: true }) <= 1); + if (menu.SubViews.FirstOrDefault (v => v is MenuItemv2 { SubMenu.Visible: true }) is MenuItemv2 visiblePeer) { HideAndRemoveSubMenu (visiblePeer.SubMenu); @@ -286,9 +324,8 @@ private void MenuAccepted (object? sender, CommandEventArgs e) if (e.Context?.Source is MenuItemv2 { SubMenu: null }) { HideAndRemoveSubMenu (_root); + RaiseAccepted (e.Context); } - - RaiseAccepted (e.Context); } /// @@ -309,7 +346,8 @@ private void MenuAccepted (object? sender, CommandEventArgs e) } /// - /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. + /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the + /// menu. /// /// /// @@ -317,23 +355,23 @@ private void MenuAccepted (object? sender, CommandEventArgs e) protected virtual void OnAccepted (CommandEventArgs args) { } /// - /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the menu. + /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the + /// menu. /// /// - /// - /// See for more information. - /// + /// + /// See for more information. + /// /// public event EventHandler? Accepted; - private void MenuOnSelectedMenuItemChanged (object? sender, MenuItemv2? e) { //Logging.Trace ($"{e}"); ShowSubMenu (e); } - /// + /// protected override void Dispose (bool disposing) { if (disposing) @@ -341,7 +379,7 @@ protected override void Dispose (bool disposing) _root?.Dispose (); _root = null; } + base.Dispose (disposing); } - } diff --git a/UICatalog/Scenarios/MenusV2.cs b/UICatalog/Scenarios/MenusV2.cs index d2d9df568f..f109ced135 100644 --- a/UICatalog/Scenarios/MenusV2.cs +++ b/UICatalog/Scenarios/MenusV2.cs @@ -41,10 +41,10 @@ public override void Main () Id = "targetView", Title = "Target View", - X = 4, - Y = 4, - Width = Dim.Fill (8)! - Dim.Width (eventLog), - Height = Dim.Fill (8), + X = 1, + Y = 1, + Width = Dim.Fill (2)! - Dim.Width (eventLog), + Height = Dim.Fill (2), BorderStyle = LineStyle.Dotted }; app.Add (targetView); @@ -89,7 +89,6 @@ public override void Main () { Id = "popOverMenu", Visible = true, - X = 1, Y = 1 }; ////Application.PopoverHost.Add (popoverMenu); @@ -392,13 +391,29 @@ public TargetView () { if (SubViews.FirstOrDefault (v => v is PopoverMenu) is PopoverMenu { } popoverMenu) { - popoverMenu.Visible = true; + popoverMenu.MakeVisible (); } return true; }); KeyBindings.Add (PopoverMenu.DefaultKey, Command.Context); + + MouseBindings.ReplaceCommands (PopoverMenu.MouseFlags, Command.Context); + + AddCommand (Command.Cancel, + ctx => + { + if (SubViews.FirstOrDefault (v => v is PopoverMenu) is PopoverMenu { } popoverMenu) + { + popoverMenu.Visible = false; + } + + return true; + }); + + MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked, Command.Cancel); + } }