123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("PosJustification", "Shows off Pos.Justify")]
- [ScenarioCategory ("Layout")]
- public sealed class PosJustification : Scenario
- {
- private readonly Aligner _horizJustifier = new ();
- private int _leftMargin;
- private readonly Aligner _vertJustifier = new ();
- private int _topMargin;
- public override void Main ()
- {
- // Init
- Application.Init ();
- // Setup - Create a top-level application window and configure it.
- Window appWindow = new ()
- {
- Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()} - {GetDescription ()}"
- };
- SetupHorizontalControls (appWindow);
- SetupVerticalControls (appWindow);
- Setup3by3Grid (appWindow);
- // Run - Start the application.
- Application.Run (appWindow);
- appWindow.Dispose ();
- // Shutdown - Calling Application.Shutdown is required.
- Application.Shutdown ();
- }
- private void SetupHorizontalControls (Window appWindow)
- {
- ColorScheme colorScheme = Colors.ColorSchemes ["Toplevel"];
- RadioGroup justification = new ()
- {
- X = Pos.Justify (_horizJustifier.Alignment),
- Y = Pos.Center (),
- RadioLabels = GetUniqueEnumNames<Alignment> (false).ToArray (),
- ColorScheme = colorScheme
- };
- justification.SelectedItemChanged += (s, e) =>
- {
- _horizJustifier.Alignment =
- (Alignment)Enum.Parse (typeof (Alignment), justification.SelectedItem.ToString ());
- foreach (View view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
- {
- if (view.X is Pos.PosJustify j)
- {
- var newJust = new Pos.PosJustify (_horizJustifier.Alignment)
- {
- Justifier =
- {
- PutSpaceBetweenItems = _horizJustifier.PutSpaceBetweenItems
- }
- };
- view.X = newJust;
- }
- }
- };
- appWindow.Add (justification);
- CheckBox putSpaces = new ()
- {
- X = Pos.Justify (_horizJustifier.Alignment),
- Y = Pos.Top (justification),
- ColorScheme = colorScheme,
- Text = "Spaces"
- };
- putSpaces.Toggled += (s, e) =>
- {
- _horizJustifier.PutSpaceBetweenItems = e.NewValue is { } && e.NewValue.Value;
- foreach (View view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
- {
- if (view.X is Pos.PosJustify j)
- {
- j.Justifier.PutSpaceBetweenItems = _horizJustifier.PutSpaceBetweenItems;
- }
- }
- };
- appWindow.Add (putSpaces);
- CheckBox margin = new ()
- {
- X = Pos.Left (putSpaces),
- Y = Pos.Bottom (putSpaces),
- ColorScheme = colorScheme,
- Text = "Margin"
- };
- margin.Toggled += (s, e) =>
- {
- _leftMargin = e.NewValue is { } && e.NewValue.Value ? 1 : 0;
- foreach (View view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
- {
- // Skip the justification radio group
- if (view != justification)
- {
- view.Margin.Thickness = new (_leftMargin, 0, 0, 0);
- }
- }
- appWindow.LayoutSubviews ();
- };
- appWindow.Add (margin);
- List<Button> addedViews = new List<Button> ();
- addedViews.Add (
- new()
- {
- X = Pos.Justify (_horizJustifier.Alignment),
- Y = Pos.Center (),
- Text = NumberToWords.Convert (0)
- });
- Buttons.NumericUpDown<int> addedViewsUpDown = new Buttons.NumericUpDown<int>
- {
- X = Pos.Justify (_horizJustifier.Alignment),
- Y = Pos.Top (justification),
- Width = 9,
- Title = "Added",
- ColorScheme = colorScheme,
- BorderStyle = LineStyle.None,
- Value = addedViews.Count
- };
- addedViewsUpDown.Border.Thickness = new (0, 1, 0, 0);
- addedViewsUpDown.ValueChanging += (s, e) =>
- {
- if (e.NewValue < 0)
- {
- e.Cancel = true;
- return;
- }
- // Add or remove buttons
- if (e.NewValue < e.OldValue)
- {
- // Remove buttons
- for (int i = e.OldValue - 1; i >= e.NewValue; i--)
- {
- Button button = addedViews [i];
- appWindow.Remove (button);
- addedViews.RemoveAt (i);
- button.Dispose ();
- }
- }
- if (e.NewValue > e.OldValue)
- {
- // Add buttons
- for (int i = e.OldValue; i < e.NewValue; i++)
- {
- var button = new Button
- {
- X = Pos.Justify (_horizJustifier.Alignment),
- Y = Pos.Center (),
- Text = NumberToWords.Convert (i + 1)
- };
- appWindow.Add (button);
- addedViews.Add (button);
- }
- }
- };
- appWindow.Add (addedViewsUpDown);
- appWindow.Add (addedViews [0]);
- }
- private void SetupVerticalControls (Window appWindow)
- {
- ColorScheme colorScheme = Colors.ColorSchemes ["Error"];
- RadioGroup justification = new ()
- {
- X = 0,
- Y = Pos.Justify (_vertJustifier.Alignment),
- RadioLabels = GetUniqueEnumNames<Alignment> (true).Reverse ().ToArray (),
- ColorScheme = colorScheme
- };
- justification.SelectedItemChanged += (s, e) =>
- {
- _vertJustifier.Alignment =
- (Alignment)Enum.Parse (typeof (Alignment), justification.SelectedItem.ToString ());
- foreach (View view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
- {
- if (view.Y is Pos.PosJustify j)
- {
- var newJust = new Pos.PosJustify (_vertJustifier.Alignment)
- {
- Justifier =
- {
- PutSpaceBetweenItems = _vertJustifier.PutSpaceBetweenItems
- }
- };
- view.Y = newJust;
- }
- }
- };
- appWindow.Add (justification);
- CheckBox putSpaces = new ()
- {
- X = 0,
- Y = Pos.Justify (_vertJustifier.Alignment),
- ColorScheme = colorScheme,
- Text = "Spaces"
- };
- putSpaces.Toggled += (s, e) =>
- {
- _vertJustifier.PutSpaceBetweenItems = e.NewValue is { } && e.NewValue.Value;
- foreach (View view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
- {
- if (view.Y is Pos.PosJustify j)
- {
- j.Justifier.PutSpaceBetweenItems = _vertJustifier.PutSpaceBetweenItems;
- }
- }
- };
- appWindow.Add (putSpaces);
- CheckBox margin = new ()
- {
- X = Pos.Right (putSpaces) + 1,
- Y = Pos.Top (putSpaces),
- ColorScheme = colorScheme,
- Text = "Margin"
- };
- margin.Toggled += (s, e) =>
- {
- _topMargin = e.NewValue is { } && e.NewValue.Value ? 1 : 0;
- foreach (View view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
- {
- // Skip the justification radio group
- if (view != justification)
- {
- view.Margin.Thickness = new (0, _topMargin, 0, 0);
- }
- }
- appWindow.LayoutSubviews ();
- };
- appWindow.Add (margin);
- List<CheckBox> addedViews = new List<CheckBox> ();
- addedViews.Add (
- new()
- {
- X = 0,
- Y = Pos.Justify (_vertJustifier.Alignment),
- Text = NumberToWords.Convert (0)
- });
- Buttons.NumericUpDown<int> addedViewsUpDown = new Buttons.NumericUpDown<int>
- {
- X = 0,
- Y = Pos.Justify (_vertJustifier.Alignment),
- Width = 9,
- Title = "Added",
- ColorScheme = colorScheme,
- BorderStyle = LineStyle.None,
- Value = addedViews.Count
- };
- addedViewsUpDown.Border.Thickness = new (0, 1, 0, 0);
- addedViewsUpDown.ValueChanging += (s, e) =>
- {
- if (e.NewValue < 0)
- {
- e.Cancel = true;
- return;
- }
- // Add or remove buttons
- if (e.NewValue < e.OldValue)
- {
- // Remove buttons
- for (int i = e.OldValue - 1; i >= e.NewValue; i--)
- {
- CheckBox button = addedViews [i];
- appWindow.Remove (button);
- addedViews.RemoveAt (i);
- button.Dispose ();
- }
- }
- if (e.NewValue > e.OldValue)
- {
- // Add buttons
- for (int i = e.OldValue; i < e.NewValue; i++)
- {
- var button = new CheckBox
- {
- X = 0,
- Y = Pos.Justify (_vertJustifier.Alignment),
- Text = NumberToWords.Convert (i + 1)
- };
- appWindow.Add (button);
- addedViews.Add (button);
- }
- }
- };
- appWindow.Add (addedViewsUpDown);
- 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
- {
- Title = "3 by 3",
- BorderStyle = LineStyle.Single,
- X = Pos.AnchorEnd (),
- Y = Pos.AnchorEnd (),
- Width = Dim.Percent (30),
- Height = Dim.Percent (30)
- };
- for (var i = 0; i < 9; i++)
- {
- var v = new View
- {
- Title = $"{i}",
- BorderStyle = LineStyle.Dashed,
- Height = 3,
- Width = 5
- };
- v.X = Pos.Justify (Alignment.Right, i / 3);
- v.Y = Pos.Justify (Alignment.Justified, i % 3 + 10);
- container.Add (v);
- }
- appWindow.Add (container);
- }
- }
|