Browse Source

View.Add returns added view

Tig 1 year ago
parent
commit
713c5f112d

+ 6 - 2
Terminal.Gui/View/ViewSubViews.cs

@@ -40,11 +40,13 @@ public partial class View
     ///         the lifecycle of the subviews to be transferred to this View.
     ///     </para>
     /// </remarks>
-    public virtual void Add (View view)
+    /// <param name="view">The view to add.</param>
+    /// <returns>The view that was added.</returns>
+    public virtual View Add (View view)
     {
         if (view is null)
         {
-            return;
+            return view;
         }
 
         if (_subviews is null)
@@ -94,6 +96,8 @@ public partial class View
         CheckDimAuto ();
         SetNeedsLayout ();
         SetNeedsDisplay ();
+
+        return view;
     }
 
     /// <summary>Adds the specified views (children) to the view.</summary>

+ 3 - 1
Terminal.Gui/Views/Bar.cs

@@ -58,10 +58,12 @@ public class Bar : View
 
     public bool StatusBarStyle { get; set; } = true;
 
-    public override void Add (View view)
+    public override View Add (View view)
     {
         base.Add (view);
         AdjustSubviewBorders ();
+
+        return view;
     }
 
     /// <inheritdoc />

+ 2 - 1
Terminal.Gui/Views/ScrollView.cs

@@ -346,7 +346,7 @@ public class ScrollView : View
 
     /// <summary>Adds the view to the scrollview.</summary>
     /// <param name="view">The view to add to the scrollview.</param>
-    public override void Add (View view)
+    public override View Add (View view)
     {
         if (view is ScrollBarView.ContentBottomRightCorner)
         {
@@ -365,6 +365,7 @@ public class ScrollView : View
         }
 
         SetNeedsLayout ();
+        return view;
     }
 
     /// <inheritdoc/>

+ 3 - 17
Terminal.Gui/Views/Shortcut.cs

@@ -431,12 +431,9 @@ public class Shortcut : View
             {
                 // When the CommandView fires its Accept event, we want to act as though the
                 // Shortcut was clicked.
-                var args = new HandledEventArgs ();
-                Accept?.Invoke (this, args);
-
-                if (args.Handled)
+                if (base.OnAccept() == true)
                 {
-                    e.Cancel = args.Handled;
+                    e.Cancel = true;
                 }
 
                 //e.Cancel = true;
@@ -620,12 +617,6 @@ public class Shortcut : View
 
     #region Accept Handling
 
-    /// <summary>
-    ///     The event fired when the <see cref="Command.Accept"/> command is received. This
-    ///     occurs if the user clicks on the Shortcut or presses <see cref="Key"/>.
-    /// </summary>
-    public new event EventHandler<HandledEventArgs> Accept;
-
     /// <summary>
     ///     Called when the <see cref="Command.Accept"/> command is received. This
     ///     occurs if the user clicks on the Bar with the mouse or presses the key bound to
@@ -656,15 +647,10 @@ public class Shortcut : View
 
         if (handled == false)
         {
-            var args = new HandledEventArgs ();
-            Accept?.Invoke (this, args);
-
-            if (args.Handled is false)
+            if (base.OnAccept () is false)
             {
                 Action?.Invoke ();
             }
-
-            args.Handled = true;
         }
 
         return true;

+ 2 - 2
Terminal.Gui/Views/StatusBar.cs

@@ -22,7 +22,7 @@ public class StatusBar : Bar
     }
 
     /// <inheritdoc />
-    public override void Add (View view)
+    public override View Add (View view)
     {
         view.CanFocus = false;
         if (view is Shortcut shortcut)
@@ -30,7 +30,7 @@ public class StatusBar : Bar
             shortcut.KeyBindingScope = KeyBindingScope.Application;
             shortcut.AlignmentModes = AlignmentModes.EndToStart | AlignmentModes.IgnoreFirstOrLast;
         }
-        base.Add (view);
+        return base.Add (view);
     }
 
 }

+ 2 - 2
Terminal.Gui/Views/Toplevel.cs

@@ -186,11 +186,11 @@ public partial class Toplevel : View
     public event EventHandler<ToplevelEventArgs> Activate;
 
     /// <inheritdoc/>
-    public override void Add (View view)
+    public override View Add (View view)
     {
         CanFocus = true;
         AddMenuStatusBar (view);
-        base.Add (view);
+        return base.Add (view);
     }
 
     /// <summary>

+ 3 - 1
Terminal.Gui/Views/Wizard/WizardStep.cs

@@ -126,7 +126,7 @@ public class WizardStep : FrameView
 
     /// <summary>Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.</summary>
     /// <param name="view"><see cref="View"/> to add to this container</param>
-    public override void Add (View view)
+    public override View Add (View view)
     {
         _contentView.Add (view);
 
@@ -136,6 +136,8 @@ public class WizardStep : FrameView
         }
 
         ShowHide ();
+
+        return view;
     }
 
     /// <summary>Removes a <see cref="View"/> from <see cref="WizardStep"/>.</summary>

+ 1 - 1
UICatalog/Scenarios/Bars.cs

@@ -446,7 +446,7 @@ public class Bars : Scenario
         shortcut.Accept += (s, e) =>
                            {
                                labelHelp.Text = labelHelp.Text + "!";
-                               e.Handled = true;
+                               e.Cancel = true;
                            };
 
         statusBar.Add (shortcut);

+ 13 - 3
UICatalog/Scenarios/GraphViewExample.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using Terminal.Gui;
@@ -20,6 +21,8 @@ public class GraphViewExample : Scenario
     private GraphView _graphView;
     private MenuItem _miDiags;
     private MenuItem _miShowBorder;
+    private ViewDiagnosticFlags _viewDiagnostics;
+
     public override void Main ()
     {
         Application.Init ();
@@ -183,18 +186,25 @@ public class GraphViewExample : Scenario
                 CanFocus = false
             }
         };
-        diagShortcut.Accept += DiagShortcut_Accept;
-        statusBar.Add (diagShortcut);
+        statusBar.Add (diagShortcut).Accept += DiagShortcut_Accept;
 
         _graphs [_currentGraph++ % _graphs.Length] ();
+
+        _viewDiagnostics = View.Diagnostics;
         Application.Run (app);
+        View.Diagnostics = _viewDiagnostics;
         app.Dispose ();
         Application.Shutdown ();
+
     }
 
-    private void DiagShortcut_Accept (object sender, System.ComponentModel.HandledEventArgs e)
+    private void DiagShortcut_Accept (object sender, CancelEventArgs e)
     {
         ToggleDiagnostics();
+        if (sender is Shortcut shortcut && shortcut.CommandView is CheckBox checkBox)
+        {
+            checkBox.Checked = _miDiags.Checked;
+        }
     }
 
     private void ToggleDiagnostics ()

+ 8 - 8
UICatalog/Scenarios/Shortcuts.cs

@@ -82,14 +82,14 @@ public class Shortcuts : Scenario
         vShortcut2.Accept += (o, args) =>
                             {
                                 // Cycle to next item. If at end, set 0
-                                //if (((RadioGroup)vShortcut2.CommandView).SelectedItem < ((RadioGroup)vShortcut2.CommandView).RadioLabels.Length - 1)
-                                //{
-                                //    ((RadioGroup)vShortcut2.CommandView).SelectedItem++;
-                                //}
-                                //else
-                                //{
-                                //    ((RadioGroup)vShortcut2.CommandView).SelectedItem = 0;
-                                //}
+                                if (((RadioGroup)vShortcut2.CommandView).SelectedItem < ((RadioGroup)vShortcut2.CommandView).RadioLabels.Length - 1)
+                                {
+                                    ((RadioGroup)vShortcut2.CommandView).SelectedItem++;
+                                }
+                                else
+                                {
+                                    ((RadioGroup)vShortcut2.CommandView).SelectedItem = 0;
+                                }
                             };
         vShortcut2.Border.Thickness = new (1, 1, 1, 1);
         Application.Top.Add (vShortcut2);