Browse Source

Got Dialog working. AnchorEnd has a bug

Tig 1 year ago
parent
commit
c9830de3c0

+ 15 - 10
Terminal.Gui/Drawing/Justification.cs

@@ -86,34 +86,34 @@ public enum Justification
 public class Justifier
 {
     /// <summary>
-    /// Gets or sets how the <see cref="Justifier"/> justifies items within a container.
+    ///     Gets or sets how the <see cref="Justifier"/> justifies items within a container.
     /// </summary>
     public Justification Justification { get; set; }
 
     /// <summary>
-    /// The size of the container.
+    ///     The size of the container.
     /// </summary>
     public int ContainerSize { get; set; }
 
     /// <summary>
-    ///     Gets or sets whether <see cref="Justifier"/> puts a space is placed between items. Default is <see langword="false"/>. If <see langword="true"/>, a space will be
+    ///     Gets or sets whether <see cref="Justifier"/> puts a space is placed between items. Default is
+    ///     <see langword="false"/>. If <see langword="true"/>, a space will be
     ///     placed between each item, which is useful for justifying text.
     /// </summary>
     public bool PutSpaceBetweenItems { get; set; }
 
     /// <summary>
-    ///     Takes a list of items and returns their positions when justified within a container <see name="ContainerSize"/> wide based on the specified
+    ///     Takes a list of items and returns their positions when justified within a container <see name="ContainerSize"/>
+    ///     wide based on the specified
     ///     <see cref="Justification"/>.
     /// </summary>
     /// <param name="sizes">The sizes of the items to justify.</param>
     /// <returns>The locations of the items, from left to right.</returns>
-    public int [] Justify (int [] sizes)
-    {
-        return Justify (Justification, PutSpaceBetweenItems, ContainerSize, sizes);
-    }
+    public int [] Justify (int [] sizes) { return Justify (Justification, PutSpaceBetweenItems, ContainerSize, sizes); }
 
     /// <summary>
-    ///     Takes a list of items and returns their positions when justified within a container <paramref name="containerSize"/> wide based on the specified
+    ///     Takes a list of items and returns their positions when justified within a container
+    ///     <paramref name="containerSize"/> wide based on the specified
     ///     <see cref="Justification"/>.
     /// </summary>
     /// <param name="sizes">The sizes of the items to justify.</param>
@@ -136,6 +136,7 @@ public class Justifier
         int totalItemsAndSpaces = totalItemsSize + totalGaps * maxSpaceBetweenItems; // total size of items and spaces if we had enough room
 
         int spaces = totalGaps * maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
+
         if (totalItemsSize >= containerSize)
         {
             spaces = 0;
@@ -153,6 +154,7 @@ public class Justifier
                 for (var i = 0; i < sizes.Length; i++)
                 {
                     CheckSizeCannotBeNegative (i, sizes);
+
                     if (i == 0)
                     {
                         positions [0] = 0; // first item position
@@ -190,6 +192,7 @@ public class Justifier
                     for (var i = 0; i < sizes.Length; i++)
                     {
                         CheckSizeCannotBeNegative (i, sizes);
+
                         if (i == 0)
                         {
                             positions [i] = remainingSpace / 2; // first item position
@@ -234,7 +237,8 @@ public class Justifier
 
                     for (var i = 0; i < sizes.Length; i++)
                     {
-                        CheckSizeCannotBeNegative (i,sizes);
+                        CheckSizeCannotBeNegative (i, sizes);
+
                         if (i < sizes.Length - 1)
                         {
                             int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
@@ -265,6 +269,7 @@ public class Justifier
                     for (int i = sizes.Length - 1; i >= 0; i--)
                     {
                         CheckSizeCannotBeNegative (i, sizes);
+
                         if (i == sizes.Length - 1)
                         {
                             // start at right

+ 5 - 5
Terminal.Gui/View/ViewText.cs

@@ -248,12 +248,12 @@ public partial class View
     /// <returns></returns>
     internal void SetTextFormatterSize ()
     {
-        //if (!IsInitialized)
-        //{
-        //    TextFormatter.Size = Size.Empty;
+        if (!IsInitialized)
+        {
+            TextFormatter.Size = Size.Empty;
 
-        //    return;
-        //}
+            return;
+        }
 
         if (string.IsNullOrEmpty (TextFormatter.Text))
         {

+ 105 - 102
Terminal.Gui/Views/Dialog.cs

@@ -92,6 +92,7 @@ public class Dialog : Window
         }
     }
 
+    // TODO: Update button.X = Pos.Justify when alignment changes
     /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
     public Justification ButtonAlignment { get; set; }
 
@@ -131,7 +132,9 @@ public class Dialog : Window
             return;
         }
 
-        //button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
+        button.X = Pos.Justify (ButtonAlignment);
+        button.Y = Pos.AnchorEnd () - 1;
+
         _buttons.Add (button);
         Add (button);
 
@@ -171,108 +174,108 @@ public class Dialog : Window
         return widths.Sum ();
     }
 
-    private void LayoutButtons ()
-    {
-        if (_buttons.Count == 0 || !IsInitialized)
-        {
-            return;
-        }
+    //private void LayoutButtons ()
+    //{
+    //    if (_buttons.Count == 0 || !IsInitialized)
+    //    {
+    //        return;
+    //    }
 
-        var shiftLeft = 0;
+    //    var shiftLeft = 0;
 
-        int buttonsWidth = GetButtonsWidth ();
+    //    int buttonsWidth = GetButtonsWidth ();
 
-        switch (ButtonAlignment)
-        {
-            case Justification.Centered:
-                // Center Buttons
-                shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
-
-                for (int i = _buttons.Count - 1; i >= 0; i--)
-                {
-                    Button button = _buttons [i];
-                    shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
-
-                    if (shiftLeft > -1)
-                    {
-                        button.X = Pos.AnchorEnd (shiftLeft);
-                    }
-                    else
-                    {
-                        button.X = Viewport.Width - shiftLeft;
-                    }
-
-                    button.Y = Pos.AnchorEnd (1);
-                }
-
-                break;
-
-            case Justification.Justified:
-                // Justify Buttons
-                // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
-
-                var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
-
-                for (int i = _buttons.Count - 1; i >= 0; i--)
-                {
-                    Button button = _buttons [i];
-
-                    if (i == _buttons.Count - 1)
-                    {
-                        shiftLeft += button.Frame.Width;
-                        button.X = Pos.AnchorEnd (shiftLeft);
-                    }
-                    else
-                    {
-                        if (i == 0)
-                        {
-                            // first (leftmost) button 
-                            int left = Viewport.Width;
-                            button.X = Pos.AnchorEnd (left);
-                        }
-                        else
-                        {
-                            shiftLeft += button.Frame.Width + spacing;
-                            button.X = Pos.AnchorEnd (shiftLeft);
-                        }
-                    }
-
-                    button.Y = Pos.AnchorEnd (1);
-                }
-
-                break;
-
-            case Justification.Left:
-                // Left Align Buttons
-                Button prevButton = _buttons [0];
-                prevButton.X = 0;
-                prevButton.Y = Pos.AnchorEnd (1);
-
-                for (var i = 1; i < _buttons.Count; i++)
-                {
-                    Button button = _buttons [i];
-                    button.X = Pos.Right (prevButton) + 1;
-                    button.Y = Pos.AnchorEnd (1);
-                    prevButton = button;
-                }
-
-                break;
-
-            case Justification.Right:
-                // Right align buttons
-                shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
-                _buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
-                _buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
-
-                for (int i = _buttons.Count - 2; i >= 0; i--)
-                {
-                    Button button = _buttons [i];
-                    shiftLeft += button.Frame.Width + 1;
-                    button.X = Pos.AnchorEnd (shiftLeft);
-                    button.Y = Pos.AnchorEnd (1);
-                }
-
-                break;
-        }
-    }
+    //    switch (ButtonAlignment)
+    //    {
+    //        case Justification.Centered:
+    //            // Center Buttons
+    //            shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
+
+    //            for (int i = _buttons.Count - 1; i >= 0; i--)
+    //            {
+    //                Button button = _buttons [i];
+    //                shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
+
+    //                if (shiftLeft > -1)
+    //                {
+    //                    button.X = Pos.AnchorEnd (shiftLeft);
+    //                }
+    //                else
+    //                {
+    //                    button.X = Viewport.Width - shiftLeft;
+    //                }
+
+    //                button.Y = Pos.AnchorEnd (1);
+    //            }
+
+    //            break;
+
+    //        case Justification.Justified:
+    //            // Justify Buttons
+    //            // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
+
+    //            var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
+
+    //            for (int i = _buttons.Count - 1; i >= 0; i--)
+    //            {
+    //                Button button = _buttons [i];
+
+    //                if (i == _buttons.Count - 1)
+    //                {
+    //                    shiftLeft += button.Frame.Width;
+    //                    button.X = Pos.AnchorEnd (shiftLeft);
+    //                }
+    //                else
+    //                {
+    //                    if (i == 0)
+    //                    {
+    //                        // first (leftmost) button 
+    //                        int left = Viewport.Width;
+    //                        button.X = Pos.AnchorEnd (left);
+    //                    }
+    //                    else
+    //                    {
+    //                        shiftLeft += button.Frame.Width + spacing;
+    //                        button.X = Pos.AnchorEnd (shiftLeft);
+    //                    }
+    //                }
+
+    //                button.Y = Pos.AnchorEnd (1);
+    //            }
+
+    //            break;
+
+    //        case Justification.Left:
+    //            // Left Align Buttons
+    //            Button prevButton = _buttons [0];
+    //            prevButton.X = 0;
+    //            prevButton.Y = Pos.AnchorEnd (1);
+
+    //            for (var i = 1; i < _buttons.Count; i++)
+    //            {
+    //                Button button = _buttons [i];
+    //                button.X = Pos.Right (prevButton) + 1;
+    //                button.Y = Pos.AnchorEnd (1);
+    //                prevButton = button;
+    //            }
+
+    //            break;
+
+    //        case Justification.Right:
+    //            // Right align buttons
+    //            shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
+    //            _buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
+    //            _buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
+
+    //            for (int i = _buttons.Count - 2; i >= 0; i--)
+    //            {
+    //                Button button = _buttons [i];
+    //                shiftLeft += button.Frame.Width + 1;
+    //                button.X = Pos.AnchorEnd (shiftLeft);
+    //                button.Y = Pos.AnchorEnd (1);
+    //            }
+
+    //            break;
+    //    }
+    //}
 }

+ 6 - 1
Terminal.Gui/Views/MessageBox.cs

@@ -325,7 +325,12 @@ public static class MessageBox
 
             foreach (string s in buttons)
             {
-                var b = new Button { Text = s, Y = Pos.AnchorEnd (), X = Pos.Justify (Justification.Centered) };
+                var b = new Button
+                {
+                    Text = s, 
+                    Y = Pos.AnchorEnd (), 
+                    X = Pos.Justify (Justification.Centered)
+                };
 
                 if (count == defaultButton)
                 {

+ 5 - 2
UICatalog/Scenarios/Dialogs.cs

@@ -137,7 +137,7 @@ public class Dialogs : Scenario
         {
             X = Pos.Right (label) + 1,
             Y = Pos.Top (label),
-            RadioLabels = new [] { "_Centered", "_Justified", "_Left", "_Right" }
+            RadioLabels = Enum.GetNames (typeof (Justification)),
         };
         frame.Add (styleRadioGroup);
 
@@ -241,7 +241,10 @@ public class Dialogs : Scenario
                 }
                 else
                 {
-                    button = new Button { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
+                    button = new Button
+                    {
+                        Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0
+                    };
                 }
 
                 button.Accept += (s, e) =>