Explorar o código

Merge pull request #35 from BDisp/tig_v2_2432-DimAuto

Adding all possible text directions variants and improving scenario.
Tig hai 1 ano
pai
achega
8da8a355d3

+ 43 - 6
Terminal.Gui/Text/TextFormatter.cs

@@ -321,7 +321,7 @@ public class TextFormatter
             int x, y;
 
             // Horizontal Alignment
-            if (Alignment == TextAlignment.Right || (Alignment == TextAlignment.Justified && !IsLeftToRight (Direction)))
+            if (Alignment is TextAlignment.Right)
             {
                 if (isVertical)
                 {
@@ -336,7 +336,7 @@ public class TextFormatter
                     CursorPosition = screen.Width - runesWidth + (_hotKeyPos > -1 ? _hotKeyPos : 0);
                 }
             }
-            else if (Alignment is TextAlignment.Left or TextAlignment.Justified)
+            else if (Alignment is TextAlignment.Left)
             {
                 if (isVertical)
                 {
@@ -352,7 +352,30 @@ public class TextFormatter
 
                 CursorPosition = _hotKeyPos > -1 ? _hotKeyPos : 0;
             }
-            else if (Alignment == TextAlignment.Centered)
+            else if (Alignment is TextAlignment.Justified)
+            {
+                if (isVertical)
+                {
+                    int runesWidth = GetColumnsRequiredForVerticalText (linesFormatted, 0, linesFormatted.Count, TabWidth);
+                    int prevLineWidth = line > 0 ? GetColumnsRequiredForVerticalText (linesFormatted, line - 1, 1, TabWidth) : 0;
+                    int firstLineWidth = GetColumnsRequiredForVerticalText (linesFormatted, 0, 1, TabWidth);
+                    int lastLineWidth = GetColumnsRequiredForVerticalText (linesFormatted, linesFormatted.Count - 1, 1, TabWidth);
+                    var interval = (int)Math.Round ((double)(screen.Width + firstLineWidth + lastLineWidth) / linesFormatted.Count);
+
+                    x = line == 0
+                            ? screen.Left
+                            : line < linesFormatted.Count - 1
+                                ? screen.Width - runesWidth <= lastLineWidth ? screen.Left + prevLineWidth : screen.Left + line * interval
+                                : screen.Right - lastLineWidth;
+                }
+                else
+                {
+                    x = screen.Left;
+                }
+
+                CursorPosition = _hotKeyPos > -1 ? _hotKeyPos : 0;
+            }
+            else if (Alignment is TextAlignment.Centered)
             {
                 if (isVertical)
                 {
@@ -376,7 +399,7 @@ public class TextFormatter
             }
 
             // Vertical Alignment
-            if (VerticalAlignment == VerticalTextAlignment.Bottom || (VerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (Direction)))
+            if (VerticalAlignment is VerticalTextAlignment.Bottom)
             {
                 if (isVertical)
                 {
@@ -387,7 +410,7 @@ public class TextFormatter
                     y = screen.Bottom - linesFormatted.Count + line;
                 }
             }
-            else if (VerticalAlignment is VerticalTextAlignment.Top or VerticalTextAlignment.Justified)
+            else if (VerticalAlignment is VerticalTextAlignment.Top)
             {
                 if (isVertical)
                 {
@@ -398,7 +421,21 @@ public class TextFormatter
                     y = screen.Top + line;
                 }
             }
-            else if (VerticalAlignment == VerticalTextAlignment.Middle)
+            else if (VerticalAlignment is VerticalTextAlignment.Justified)
+            {
+                if (isVertical)
+                {
+                    y = screen.Top;
+                }
+                else
+                {
+                    var interval = (int)Math.Round ((double)(screen.Height + 2) / linesFormatted.Count);
+
+                    y = line == 0 ? screen.Top :
+                        line < linesFormatted.Count - 1 ? screen.Height - interval <= 1 ? screen.Top + 1 : screen.Top + line * interval : screen.Bottom - 1;
+                }
+            }
+            else if (VerticalAlignment is VerticalTextAlignment.Middle)
             {
                 if (isVertical)
                 {

+ 61 - 7
UICatalog/Scenarios/TextAlignmentsAndDirection.cs

@@ -462,16 +462,34 @@ public class TextAlignmentsAndDirections : Scenario
             Text = "Justify"
         };
 
+        app.Add (justifyCheckbox);
+
+        // JUSTIFY OPTIONS
+
+        var justifyOptions = new RadioGroup
+        {
+            X = Pos.Left (justifyCheckbox) + 1,
+            Y = Pos.Y (justifyCheckbox) + 1,
+            Width = Dim.Fill (11),
+            RadioLabels = ["Current direction", "Opposite direction", "Justify Both"],
+            Enabled = false
+        };
+
         justifyCheckbox.Toggled += (s, e) => ToggleJustify (e.OldValue is { } && (bool)e.OldValue);
 
-        app.Add (justifyCheckbox);
+        justifyOptions.SelectedItemChanged += (s, e) =>
+                                              {
+                                                  ToggleJustify (false, true);
+                                              };
+
+        app.Add (justifyOptions);
 
         // WRAP CHECKBOX
 
         var wrapCheckbox = new CheckBox
         {
             X = Pos.Right (container) + 1,
-            Y = Pos.Y (justifyCheckbox) + 1,
+            Y = Pos.Bottom (justifyOptions),
             Width = Dim.Fill (10),
             Height = 1,
             Text = "Word Wrap",
@@ -564,10 +582,15 @@ public class TextAlignmentsAndDirections : Scenario
         Application.Run (app);
         app.Dispose ();
 
-        void ToggleJustify (bool oldValue)
+        void ToggleJustify (bool oldValue, bool wasJustOptions = false)
         {
             if (oldValue == true)
             {
+                if (!wasJustOptions)
+                {
+                    justifyOptions.Enabled = false;
+                }
+
                 foreach (Label t in mtxts)
                 {
                     t.TextAlignment = (TextAlignment)((dynamic)t.Data).h;
@@ -578,15 +601,46 @@ public class TextAlignmentsAndDirections : Scenario
             {
                 foreach (Label t in mtxts)
                 {
+                    if (!wasJustOptions)
+                    {
+                        justifyOptions.Enabled = true;
+                    }
+
                     if (TextFormatter.IsVerticalDirection (t.TextDirection))
                     {
-                        t.VerticalTextAlignment = VerticalTextAlignment.Justified;
-                        t.TextAlignment = ((dynamic)t.Data).h;
+                        switch (justifyOptions.SelectedItem)
+                        {
+                            case 0:
+                                t.VerticalTextAlignment = VerticalTextAlignment.Justified;
+                                t.TextAlignment = ((dynamic)t.Data).h;
+                                break;
+                            case 1:
+                                t.VerticalTextAlignment = (VerticalTextAlignment)((dynamic)t.Data).v;
+                                t.TextAlignment = TextAlignment.Justified;
+                                break;
+                            case 2:
+                                t.VerticalTextAlignment = VerticalTextAlignment.Justified;
+                                t.TextAlignment = TextAlignment.Justified;
+                                break;
+                        }
                     }
                     else
                     {
-                        t.TextAlignment = TextAlignment.Justified;
-                        t.VerticalTextAlignment = ((dynamic)t.Data).v;
+                        switch (justifyOptions.SelectedItem)
+                        {
+                            case 0:
+                                t.TextAlignment = TextAlignment.Justified;
+                                t.VerticalTextAlignment = ((dynamic)t.Data).v;
+                                break;
+                            case 1:
+                                t.TextAlignment = (TextAlignment)((dynamic)t.Data).h;
+                                t.VerticalTextAlignment = VerticalTextAlignment.Justified;
+                                break;
+                            case 2:
+                                t.TextAlignment = TextAlignment.Justified;
+                                t.VerticalTextAlignment = VerticalTextAlignment.Justified;
+                                break;
+                        }
                     }
                 }
             }

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1617 - 1
UnitTests/Text/TextFormatterTests.cs


Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio