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

This commit is contained in:
Tig
2024-05-11 07:29:26 -06:00
parent b75bfb248a
commit 175c2fc0dd
6 changed files with 67 additions and 81 deletions

View File

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

View File

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

View File

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

View File

@@ -47,14 +47,14 @@ public sealed class PosAlign : Scenario
{ {
X = Pos.Align (_horizAligner.Alignment), X = Pos.Align (_horizAligner.Alignment),
Y = Pos.Center (), Y = Pos.Center (),
RadioLabels = GetUniqueEnumNames<Alignment> (false).ToArray (), RadioLabels = new [] { "Left", "Right", "Centered", "Justified", "FirstLeftRestRight", "LastRightRestLeft" },
ColorScheme = colorScheme ColorScheme = colorScheme
}; };
alignRadioGroup.SelectedItemChanged += (s, e) => alignRadioGroup.SelectedItemChanged += (s, e) =>
{ {
_horizAligner.Alignment = _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)) foreach (View view in appWindow.Subviews.Where (v => v.X is Pos.PosAlign))
{ {
@@ -193,14 +193,14 @@ public sealed class PosAlign : Scenario
{ {
X = 0, X = 0,
Y = Pos.Align (_vertAligner.Alignment), Y = Pos.Align (_vertAligner.Alignment),
RadioLabels = GetUniqueEnumNames<Alignment> (true).Reverse ().ToArray (), RadioLabels = new [] { "Top", "Bottom", "Centered", "Justified", "FirstTopRestBottom", "LastBottomRestTop" },
ColorScheme = colorScheme ColorScheme = colorScheme
}; };
alignRadioGroup.SelectedItemChanged += (s, e) => alignRadioGroup.SelectedItemChanged += (s, e) =>
{ {
_vertAligner.Alignment = _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)) 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]); 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) private void Setup3by3Grid (Window appWindow)
{ {
var container = new View var container = new View

View File

@@ -66,7 +66,7 @@ public class TextFormatterDemo : Scenario
static IEnumerable<T> GetUniqueEnumValues<T> () where T : Enum static IEnumerable<T> GetUniqueEnumValues<T> () where T : Enum
{ {
var values = new HashSet<T> (); 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)) 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 [] singleLines = new Label [alignments.Count];
Label [] multipleLines = new Label [alignments.Count]; Label [] multipleLines = new Label [alignments.Count];
var multiLineHeight = 5; 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, X = 0,
Width = Dim.Fill (), Width = Dim.Fill (),
@@ -94,9 +94,9 @@ public class TextFormatterDemo : Scenario
Text = text Text = text
}; };
multipleLines [(int)alignment] = new() multipleLines [i] = new ()
{ {
TextAlignment = alignment, TextAlignment = alignments [i],
X = 0, X = 0,
Width = Dim.Fill (), Width = Dim.Fill (),
@@ -112,33 +112,33 @@ public class TextFormatterDemo : Scenario
}; };
app.Add (label); 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); app.Add (label);
singleLines [(int)alignment].Y = Pos.Bottom (label); singleLines [i].Y = Pos.Bottom (label);
app.Add (singleLines [(int)alignment]); app.Add (singleLines [i]);
label = singleLines [(int)alignment]; 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); 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); app.Add (label);
multipleLines [(int)alignment].Y = Pos.Bottom (label); multipleLines [i].Y = Pos.Bottom (label);
app.Add (multipleLines [(int)alignment]); app.Add (multipleLines [i]);
label = multipleLines [(int)alignment]; label = multipleLines [i];
} }
unicodeCheckBox.Toggled += (s, e) => 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; singleLines [i].Text = e.OldValue == true ? text : unicode;
multipleLines [(int)alignment].Text = e.OldValue == true ? text : unicode; multipleLines [i].Text = e.OldValue == true ? text : unicode;
} }
}; };