Jelajahi Sumber

Changed Alignment enum to have distinct values. Updated related code.

Tig 1 tahun lalu
induk
melakukan
175c2fc0dd

+ 27 - 21
Terminal.Gui/Drawing/Aligner.cs

@@ -32,7 +32,7 @@ public enum Alignment
     ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
     ///     each item.
     /// </summary>
-    Top = Left,
+    Top,
 
     /// <summary>
     ///     The items will be aligned to the right.
@@ -57,7 +57,7 @@ public enum Alignment
     ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
     ///     each item.
     /// </summary>
-    Bottom = Right,
+    Bottom,
 
     /// <summary>
     ///     The group will be centered in the container.
@@ -77,23 +77,23 @@ public enum Alignment
     /// </example>
     Centered,
 
-    /// <summary>
-    ///     The items will be justified. Space will be added between the items such that the first item
-    ///     is at the start and the right side of the last item against the end.
-    ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
-    ///     each item.
-    /// </summary>
-    /// <remarks>
-    ///     <para>
-    ///         Extra space will be distributed between the items, biased towards the left.
-    ///     </para>
-    /// </remarks>
-    /// <example>
-    ///     <c>
-    ///         111    2222     33333
-    ///     </c>
-    /// </example>
-    Justified,
+        /// <summary>
+        ///     The items will be justified. Space will be added between the items such that the first item
+        ///     is at the start and the right side of the last item against the end.
+        ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
+        ///     each item.
+        /// </summary>
+        /// <remarks>
+        ///     <para>
+        ///         Extra space will be distributed between the items, biased towards the left.
+        ///     </para>
+        /// </remarks>
+        /// <example>
+        ///     <c>
+        ///         111    2222     33333
+        ///     </c>
+        /// </example>
+        Justified,
 
     /// <summary>
     ///     The first item will be aligned to the left and the remaining will aligned to the right.
@@ -118,7 +118,7 @@ public enum Alignment
     ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
     ///     each item.
     /// </summary>
-    FirstTopRestBottom = FirstLeftRestRight,
+    FirstTopRestBottom,
 
     /// <summary>
     ///     The last item will be aligned to the right and the remaining will aligned to the left.
@@ -143,7 +143,7 @@ public enum Alignment
     ///     Set <see cref="Aligner.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one line between
     ///     each item.
     /// </summary>
-    LastBottomRestTop = LastRightRestLeft
+    LastBottomRestTop
 }
 
 /// <summary>
@@ -255,6 +255,7 @@ public class Aligner : INotifyPropertyChanged
         switch (alignment)
         {
             case Alignment.Left:
+            case Alignment.Top:
                 var currentPosition = 0;
 
                 for (var i = 0; i < sizes.Length; i++)
@@ -275,7 +276,10 @@ public class Aligner : INotifyPropertyChanged
                 }
 
                 break;
+
             case Alignment.Right:
+            case Alignment.Bottom:
+
                 currentPosition = containerSize - totalItemsSize - spaces;
 
                 for (var i = 0; i < sizes.Length; i++)
@@ -337,6 +341,7 @@ public class Aligner : INotifyPropertyChanged
 
             // 111 2222        33333
             case Alignment.LastRightRestLeft:
+            case Alignment.LastBottomRestTop:
                 if (sizes.Length > 1)
                 {
                     if (totalItemsSize > containerSize)
@@ -374,6 +379,7 @@ public class Aligner : INotifyPropertyChanged
 
             // 111        2222 33333
             case Alignment.FirstLeftRestRight:
+            case Alignment.FirstTopRestBottom:
                 if (sizes.Length > 1)
                 {
                     currentPosition = 0;

+ 2 - 2
Terminal.Gui/Text/TextFormatter.cs

@@ -17,9 +17,9 @@ public class TextFormatter
     private Size _size;
     private int _tabWidth = 4;
     private string _text;
-    private Alignment _textAlignment;
+    private Alignment _textAlignment = Alignment.Left;
     private TextDirection _textDirection;
-    private Alignment _textVerticalAlignment;
+    private Alignment _textVerticalAlignment = Alignment.Top;
     private bool _wordWrap = true;
 
     /// <summary>Get or sets the horizontal text alignment.</summary>

+ 13 - 12
UICatalog/Scenarios/Dialogs.cs

@@ -43,7 +43,7 @@ public class Dialogs : Scenario
         };
         frame.Add (widthEdit);
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (label),
@@ -77,7 +77,7 @@ public class Dialogs : Scenario
                    }
                   );
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (label),
@@ -121,7 +121,7 @@ public class Dialogs : Scenario
         };
         frame.Add (glyphsNotWords);
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (glyphsNotWords),
@@ -145,7 +145,7 @@ public class Dialogs : Scenario
             }
         }
 
-        var labels = GetUniqueEnumNames<Alignment> ();
+        var labels = new [] { "Left", "Centered", "Right", "Justified", "FirstLeftRestRight", "LastRightRestLeft" };
         var alignmentGroup = new RadioGroup
         {
             X = Pos.Right (label) + 1,
@@ -153,7 +153,7 @@ public class Dialogs : Scenario
             RadioLabels = labels.ToArray (),
         };
         frame.Add (alignmentGroup);
-        alignmentGroup.SelectedItem = labels.ToList().IndexOf (Dialog.DefaultButtonAlignment.ToString());
+        alignmentGroup.SelectedItem = labels.ToList ().IndexOf (Dialog.DefaultButtonAlignment.ToString ());
 
         frame.ValidatePosDim = true;
 
@@ -173,7 +173,7 @@ public class Dialogs : Scenario
 
         Win.Add (frame);
 
-        label = new()
+        label = new ()
         {
             X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = Alignment.Right, Text = "Button Pressed:"
         };
@@ -220,7 +220,7 @@ public class Dialogs : Scenario
         TextField titleEdit,
         TextField numButtonsEdit,
         CheckBox glyphsNotWords,
-        RadioGroup styleRadioGroup,
+        RadioGroup alignmentRadioGroup,
         Label buttonPressedLabel
     )
     {
@@ -247,7 +247,7 @@ public class Dialogs : Scenario
                 {
                     buttonId = i;
 
-                    button = new()
+                    button = new ()
                     {
                         Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
                         IsDefault = buttonId == 0
@@ -279,10 +279,11 @@ public class Dialogs : Scenario
 
             // This tests dynamically adding buttons; ensuring the dialog resizes if needed and 
             // the buttons are laid out correctly
-            dialog = new()
+            dialog = new ()
             {
                 Title = titleEdit.Text,
-                ButtonAlignment = (Alignment)styleRadioGroup.SelectedItem,
+                ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentRadioGroup.RadioLabels [alignmentRadioGroup.SelectedItem]),
+
                 Buttons = buttons.ToArray ()
             };
 
@@ -301,7 +302,7 @@ public class Dialogs : Scenario
 
                               if (glyphsNotWords.Checked == true)
                               {
-                                  button = new()
+                                  button = new ()
                                   {
                                       Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
                                       IsDefault = buttonId == 0
@@ -309,7 +310,7 @@ public class Dialogs : Scenario
                               }
                               else
                               {
-                                  button = new() { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
+                                  button = new () { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
                               }
 
                               button.Accept += (s, e) =>

+ 4 - 25
UICatalog/Scenarios/PosAlign.cs

@@ -47,14 +47,14 @@ public sealed class PosAlign : Scenario
         {
             X = Pos.Align (_horizAligner.Alignment),
             Y = Pos.Center (),
-            RadioLabels = GetUniqueEnumNames<Alignment> (false).ToArray (),
+            RadioLabels = new [] { "Left", "Right", "Centered", "Justified", "FirstLeftRestRight", "LastRightRestLeft" },
             ColorScheme = colorScheme
         };
 
         alignRadioGroup.SelectedItemChanged += (s, e) =>
                                              {
                                                  _horizAligner.Alignment =
-                                                     (Alignment)Enum.Parse (typeof (Alignment), alignRadioGroup.SelectedItem.ToString ());
+                                                     (Alignment)Enum.Parse (typeof (Alignment), alignRadioGroup.RadioLabels [alignRadioGroup.SelectedItem]);
 
                                                  foreach (View view in appWindow.Subviews.Where (v => v.X is Pos.PosAlign))
                                                  {
@@ -193,14 +193,14 @@ public sealed class PosAlign : Scenario
         {
             X = 0,
             Y = Pos.Align (_vertAligner.Alignment),
-            RadioLabels = GetUniqueEnumNames<Alignment> (true).Reverse ().ToArray (),
+            RadioLabels = new [] { "Top", "Bottom", "Centered", "Justified", "FirstTopRestBottom", "LastBottomRestTop" },
             ColorScheme = colorScheme
         };
 
         alignRadioGroup.SelectedItemChanged += (s, e) =>
                                              {
                                                  _vertAligner.Alignment =
-                                                     (Alignment)Enum.Parse (typeof (Alignment), alignRadioGroup.SelectedItem.ToString ());
+                                                     (Alignment)Enum.Parse (typeof (Alignment), alignRadioGroup.RadioLabels [alignRadioGroup.SelectedItem]);
 
                                                  foreach (View view in appWindow.Subviews.Where (v => v.Y is Pos.PosAlign))
                                                  {
@@ -331,27 +331,6 @@ public sealed class PosAlign : Scenario
         appWindow.Add (addedViews [0]);
     }
 
-    private static IEnumerable<string> GetUniqueEnumNames<T> (bool reverse) where T : Enum
-    {
-        HashSet<int> values = new HashSet<int> ();
-        string [] names = Enum.GetNames (typeof (T));
-
-        if (reverse)
-        {
-            names = Enum.GetNames (typeof (T)).Reverse ().ToArray ();
-        }
-
-        foreach (string name in names)
-        {
-            var value = (int)Enum.Parse (typeof (T), name);
-
-            if (values.Add (value))
-            {
-                yield return name;
-            }
-        }
-    }
-
     private void Setup3by3Grid (Window appWindow)
     {
         var container = new View

+ 21 - 21
UICatalog/Scenarios/TextFormatterDemo.cs

@@ -66,7 +66,7 @@ public class TextFormatterDemo : Scenario
         static IEnumerable<T> GetUniqueEnumValues<T> () where T : Enum
         {
             var values = new HashSet<T> ();
-            foreach (T v in Enum.GetValues (typeof(T)))
+            foreach (T v in Enum.GetValues (typeof (T)))
             {
                 if (values.Add (v))
                 {
@@ -75,17 +75,17 @@ public class TextFormatterDemo : Scenario
             }
         }
 
-        List<Alignment> alignments = GetUniqueEnumValues<Alignment>().ToList ();
+        List<Alignment> alignments = new () { Alignment.Left, Alignment.Right, Alignment.Centered, Alignment.Justified };
         Label [] singleLines = new Label [alignments.Count];
         Label [] multipleLines = new Label [alignments.Count];
 
         var multiLineHeight = 5;
 
-        foreach (Alignment alignment in alignments)
+        for (int i = 0; i < alignments.Count; i++)
         {
-            singleLines [(int)alignment] = new()
+            singleLines [i] = new ()
             {
-                TextAlignment = alignment,
+                TextAlignment = alignments [i],
                 X = 0,
 
                 Width = Dim.Fill (),
@@ -94,9 +94,9 @@ public class TextFormatterDemo : Scenario
                 Text = text
             };
 
-            multipleLines [(int)alignment] = new()
+            multipleLines [i] = new ()
             {
-                TextAlignment = alignment,
+                TextAlignment = alignments [i],
                 X = 0,
 
                 Width = Dim.Fill (),
@@ -112,33 +112,33 @@ public class TextFormatterDemo : Scenario
         };
         app.Add (label);
 
-        foreach (Alignment alignment in alignments)
+        for (int i = 0; i < alignments.Count; i++)
         {
-            label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
+            label = new () { Y = Pos.Bottom (label), Text = $"{alignments [i]}:" };
             app.Add (label);
-            singleLines [(int)alignment].Y = Pos.Bottom (label);
-            app.Add (singleLines [(int)alignment]);
-            label = singleLines [(int)alignment];
+            singleLines [i].Y = Pos.Bottom (label);
+            app.Add (singleLines [i]);
+            label = singleLines [i];
         }
 
-        label = new() { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
+        label = new () { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
         app.Add (label);
 
-        foreach (Alignment alignment in alignments)
+        for (int i = 0; i < alignments.Count; i++)
         {
-            label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
+            label = new () { Y = Pos.Bottom (label), Text = $"{alignments [i]}:" };
             app.Add (label);
-            multipleLines [(int)alignment].Y = Pos.Bottom (label);
-            app.Add (multipleLines [(int)alignment]);
-            label = multipleLines [(int)alignment];
+            multipleLines [i].Y = Pos.Bottom (label);
+            app.Add (multipleLines [i]);
+            label = multipleLines [i];
         }
 
         unicodeCheckBox.Toggled += (s, e) =>
                                    {
-                                       foreach (Alignment alignment in alignments)
+                                       for (int i = 0; i < alignments.Count; i++)
                                        {
-                                           singleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
-                                           multipleLines [(int)alignment].Text = e.OldValue == true ? text : unicode;
+                                           singleLines [i].Text = e.OldValue == true ? text : unicode;
+                                           multipleLines [i].Text = e.OldValue == true ? text : unicode;
                                        }
                                    };
 

+ 0 - 0
UnitTests/View/Layout/Pos.AlighTests.cs → UnitTests/View/Layout/Pos.AlignTests.cs