From 175c2fc0ddfd2daea2d517bc66c6ec517ea2db3b Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 11 May 2024 07:29:26 -0600 Subject: [PATCH] Changed Alignment enum to have distinct values. Updated related code. --- Terminal.Gui/Drawing/Aligner.cs | 48 +++++++++++-------- Terminal.Gui/Text/TextFormatter.cs | 4 +- UICatalog/Scenarios/Dialogs.cs | 25 +++++----- UICatalog/Scenarios/PosAlign.cs | 29 ++--------- UICatalog/Scenarios/TextFormatterDemo.cs | 42 ++++++++-------- .../{Pos.AlighTests.cs => Pos.AlignTests.cs} | 0 6 files changed, 67 insertions(+), 81 deletions(-) rename UnitTests/View/Layout/{Pos.AlighTests.cs => Pos.AlignTests.cs} (100%) diff --git a/Terminal.Gui/Drawing/Aligner.cs b/Terminal.Gui/Drawing/Aligner.cs index 03f0b7ced..c1057eb26 100644 --- a/Terminal.Gui/Drawing/Aligner.cs +++ b/Terminal.Gui/Drawing/Aligner.cs @@ -32,7 +32,7 @@ public enum Alignment /// Set to to ensure at least one line between /// each item. /// - Top = Left, + Top, /// /// The items will be aligned to the right. @@ -57,7 +57,7 @@ public enum Alignment /// Set to to ensure at least one line between /// each item. /// - Bottom = Right, + Bottom, /// /// The group will be centered in the container. @@ -77,23 +77,23 @@ public enum Alignment /// Centered, - /// - /// 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 to to ensure at least one space between - /// each item. - /// - /// - /// - /// Extra space will be distributed between the items, biased towards the left. - /// - /// - /// - /// - /// 111 2222 33333 - /// - /// - Justified, + /// + /// 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 to to ensure at least one space between + /// each item. + /// + /// + /// + /// Extra space will be distributed between the items, biased towards the left. + /// + /// + /// + /// + /// 111 2222 33333 + /// + /// + Justified, /// /// 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 to to ensure at least one line between /// each item. /// - FirstTopRestBottom = FirstLeftRestRight, + FirstTopRestBottom, /// /// 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 to to ensure at least one line between /// each item. /// - LastBottomRestTop = LastRightRestLeft + LastBottomRestTop } /// @@ -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; diff --git a/Terminal.Gui/Text/TextFormatter.cs b/Terminal.Gui/Text/TextFormatter.cs index 3d2e0d61d..2567f08dd 100644 --- a/Terminal.Gui/Text/TextFormatter.cs +++ b/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; /// Get or sets the horizontal text alignment. diff --git a/UICatalog/Scenarios/Dialogs.cs b/UICatalog/Scenarios/Dialogs.cs index aa3253ef9..231d1ce70 100644 --- a/UICatalog/Scenarios/Dialogs.cs +++ b/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 (); + 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) => diff --git a/UICatalog/Scenarios/PosAlign.cs b/UICatalog/Scenarios/PosAlign.cs index a6280a0e0..8abacae35 100644 --- a/UICatalog/Scenarios/PosAlign.cs +++ b/UICatalog/Scenarios/PosAlign.cs @@ -47,14 +47,14 @@ public sealed class PosAlign : Scenario { X = Pos.Align (_horizAligner.Alignment), Y = Pos.Center (), - RadioLabels = GetUniqueEnumNames (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 (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 GetUniqueEnumNames (bool reverse) where T : Enum - { - HashSet values = new HashSet (); - 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 diff --git a/UICatalog/Scenarios/TextFormatterDemo.cs b/UICatalog/Scenarios/TextFormatterDemo.cs index c18d25042..ac5a8cb33 100644 --- a/UICatalog/Scenarios/TextFormatterDemo.cs +++ b/UICatalog/Scenarios/TextFormatterDemo.cs @@ -66,7 +66,7 @@ public class TextFormatterDemo : Scenario static IEnumerable GetUniqueEnumValues () where T : Enum { var values = new HashSet (); - 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 alignments = GetUniqueEnumValues().ToList (); + List 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; } }; diff --git a/UnitTests/View/Layout/Pos.AlighTests.cs b/UnitTests/View/Layout/Pos.AlignTests.cs similarity index 100% rename from UnitTests/View/Layout/Pos.AlighTests.cs rename to UnitTests/View/Layout/Pos.AlignTests.cs