From 713c5f112d1a58f05a50e0a7a8d5d2f82ff7d17a Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 16 Jun 2024 13:34:02 -0700 Subject: [PATCH] View.Add returns added view --- Terminal.Gui/View/ViewSubViews.cs | 8 ++++++-- Terminal.Gui/Views/Bar.cs | 4 +++- Terminal.Gui/Views/ScrollView.cs | 3 ++- Terminal.Gui/Views/Shortcut.cs | 20 +++----------------- Terminal.Gui/Views/StatusBar.cs | 4 ++-- Terminal.Gui/Views/Toplevel.cs | 4 ++-- Terminal.Gui/Views/Wizard/WizardStep.cs | 4 +++- UICatalog/Scenarios/Bars.cs | 2 +- UICatalog/Scenarios/GraphViewExample.cs | 16 +++++++++++++--- UICatalog/Scenarios/Shortcuts.cs | 16 ++++++++-------- 10 files changed, 43 insertions(+), 38 deletions(-) diff --git a/Terminal.Gui/View/ViewSubViews.cs b/Terminal.Gui/View/ViewSubViews.cs index 05d332e30..8369f12ea 100644 --- a/Terminal.Gui/View/ViewSubViews.cs +++ b/Terminal.Gui/View/ViewSubViews.cs @@ -40,11 +40,13 @@ public partial class View /// the lifecycle of the subviews to be transferred to this View. /// /// - public virtual void Add (View view) + /// The view to add. + /// The view that was added. + 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; } /// Adds the specified views (children) to the view. diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index f69579417..b25f0127a 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/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; } /// diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index 42a226544..bebe2936a 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/Terminal.Gui/Views/ScrollView.cs @@ -346,7 +346,7 @@ public class ScrollView : View /// Adds the view to the scrollview. /// The view to add to the scrollview. - 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; } /// diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 348a21029..ff35a7159 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/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 - /// - /// The event fired when the command is received. This - /// occurs if the user clicks on the Shortcut or presses . - /// - public new event EventHandler Accept; - /// /// Called when the 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; diff --git a/Terminal.Gui/Views/StatusBar.cs b/Terminal.Gui/Views/StatusBar.cs index e5c26baff..966f2d59a 100644 --- a/Terminal.Gui/Views/StatusBar.cs +++ b/Terminal.Gui/Views/StatusBar.cs @@ -22,7 +22,7 @@ public class StatusBar : Bar } /// - 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); } } diff --git a/Terminal.Gui/Views/Toplevel.cs b/Terminal.Gui/Views/Toplevel.cs index 117599efb..5ca39c24d 100644 --- a/Terminal.Gui/Views/Toplevel.cs +++ b/Terminal.Gui/Views/Toplevel.cs @@ -186,11 +186,11 @@ public partial class Toplevel : View public event EventHandler Activate; /// - public override void Add (View view) + public override View Add (View view) { CanFocus = true; AddMenuStatusBar (view); - base.Add (view); + return base.Add (view); } /// diff --git a/Terminal.Gui/Views/Wizard/WizardStep.cs b/Terminal.Gui/Views/Wizard/WizardStep.cs index c28c3653c..e583f2334 100644 --- a/Terminal.Gui/Views/Wizard/WizardStep.cs +++ b/Terminal.Gui/Views/Wizard/WizardStep.cs @@ -126,7 +126,7 @@ public class WizardStep : FrameView /// Add the specified to the . /// to add to this container - 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; } /// Removes a from . diff --git a/UICatalog/Scenarios/Bars.cs b/UICatalog/Scenarios/Bars.cs index 9c310dfa2..81d2cad6e 100644 --- a/UICatalog/Scenarios/Bars.cs +++ b/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); diff --git a/UICatalog/Scenarios/GraphViewExample.cs b/UICatalog/Scenarios/GraphViewExample.cs index 73f718615..9c9beb097 100644 --- a/UICatalog/Scenarios/GraphViewExample.cs +++ b/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 () diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index 26292b76d..7e1906e2b 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/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);