Browse Source

Tweaked close button demo

Tig Kindel 1 year ago
parent
commit
fe2c69356a

+ 28 - 0
Terminal.Gui/View/Adornment/Border.cs

@@ -49,6 +49,9 @@ public class Border : Adornment
     { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
     }
 
+    /// <summary>
+    ///    The close button for the border. Set to <see cref="Button.Visible"/>, to <see langword="true"/> to enable.
+    /// </summary>
     public Button CloseButton { get; internal set; }
 
     /// <inheritdoc/>
@@ -58,6 +61,31 @@ public class Border : Adornment
         Parent = parent;
     }
 
+    /// <inheritdoc/>
+    public override void BeginInit ()
+    {
+        base.BeginInit ();
+
+        if (Parent is { })
+        {
+            CloseButton = new Button ()
+            {
+                Text = "X", // So it's not visible to not break unit tests
+                Y = 0,
+                CanFocus = true,
+                NoDecorations = false,
+                Visible = false,
+            };
+            //CloseButton.BorderStyle = LineStyle.Single;
+            //CloseButton.Border.Thickness = new (1, 0, 1, 0);
+            CloseButton.X = Pos.AnchorEnd () - (Pos.Right (CloseButton) - Pos.Left (CloseButton)) + Thickness.Left + 1; // +1 for the border
+            Add (CloseButton);
+            CloseButton.Accept += (s, e) => {
+                                      e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
+                                  };
+        }
+    }
+
     /// <summary>
     ///     The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
     ///     scheme. color scheme.

+ 5 - 1
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -120,9 +120,13 @@ public partial class View
     /// </remarks>
     public LineStyle BorderStyle
     {
-        get => Border.LineStyle;
+        get => Border?.LineStyle ?? LineStyle.None;
         set
         {
+            if (Border is null)
+            {
+                return;
+            }
             if (value != LineStyle.None)
             {
                 Border.Thickness = new (1);

+ 1 - 15
Terminal.Gui/View/View.cs

@@ -467,21 +467,7 @@ public partial class View : Responder, ISupportInitializeNotification
         Margin?.BeginInit ();
 
         Border?.BeginInit ();
-        //if (Border is { })
-        //{
-        //    Border.CloseButton = new Button ()
-        //    {
-        //        Text = " ", // So it's not visible to not break unit tests
-        //        X = Pos.AnchorEnd (1),
-        //        Y = 0,
-        //        CanFocus = true,
-        //        NoDecorations = true,
-        //        NoPadding = true,
-        //        Visible = true,
-        //    };
-        //    Border.Add (Border.CloseButton);
-        //    Border.CloseButton.Accept += (s, e) => { Border.Parent.InvokeCommand (Command.Accept); };
-        //}
+
 
         Padding?.BeginInit ();
 

+ 20 - 9
UICatalog/Scenarios/Adornments.cs

@@ -86,18 +86,29 @@ public class Adornments : Scenario
 
         editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
 
-        var labelInPadding = new Label () { X = 1, Y = 0, Title = "_Text:" };
-        view.Padding.Add (labelInPadding);
+        view.Initialized += (s, e) =>
+                            {
+                                var labelInPadding = new Label () { X = 1, Y = 0, Title = "_Text:" };
+                                view.Padding.Add (labelInPadding);
 
-        var textFieldInPadding = new TextField () { X = Pos.Right (labelInPadding) + 1, Y = Pos.Top (labelInPadding), Width = 15, Text = "some text" };
-        textFieldInPadding.Accept += (s, e) => MessageBox.Query (20, 7, "TextField", textFieldInPadding.Text, "Ok");
-        view.Padding.Add (textFieldInPadding);
+                                var textFieldInPadding = new TextField () { X = Pos.Right (labelInPadding) + 1, Y = Pos.Top (labelInPadding), Width = 15, Text = "some text" };
+                                textFieldInPadding.Accept += (s, e) => MessageBox.Query (20, 7, "TextField", textFieldInPadding.Text, "Ok");
+                                view.Padding.Add (textFieldInPadding);
 
-        var btnButtonInPadding = new Button { X = Pos.Center (), Y = 1, Text = "_Button in Padding" };
-        btnButtonInPadding.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Button in Padding Pressed!", "Ok");
-        view.Padding.Add (btnButtonInPadding);
+                                var btnButtonInPadding = new Button { X = Pos.Center (), Y = 1, Text = "_Button in Padding" };
+                                btnButtonInPadding.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Button in Padding Pressed!", "Ok");
+                                view.Padding.Add (btnButtonInPadding);
+
+                                view.Border.CloseButton.Visible = true;
+                                view.Border.CloseButton.Accept += (s, e) =>
+                                                                  {
+                                                                      MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
+                                                                      e.Cancel = true;
+                                                                  };
+
+                                view.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
+                            };
 
-        view.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
 
         Application.Run (editor);
         Application.Shutdown ();