123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Text Alignment and Direction", "Demos horizontal and vertical text alignment and direction.")]
- [ScenarioCategory ("Text and Formatting")]
- public class TextAlignmentAndDirection : Scenario
- {
- public override void Main ()
- {
- Application.Init ();
- Window app = new ()
- {
- Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
- };
- var txt = $"Hello World{Environment.NewLine}HELLO WORLD{Environment.NewLine}世界 您好";
- var color1 = new ColorScheme { Normal = new (Color.Black, Color.Gray) };
- var color2 = new ColorScheme { Normal = new (Color.Black, Color.DarkGray) };
- List<Label> singleLineLabels = new (); // single line
- List<Label> multiLineLabels = new (); // multi line
- // Horizontal Single-Line
- var labelHL = new Label
- {
- X = 0,
- Y = 0,
- Width = 6,
- Height = 1,
- TextAlignment = Alignment.End,
- ColorScheme = Colors.ColorSchemes ["Dialog"],
- Text = "Start"
- };
- var labelHC = new Label
- {
- X = 0,
- Y = 1,
- Width = 6,
- Height = 1,
- TextAlignment = Alignment.End,
- ColorScheme = Colors.ColorSchemes ["Dialog"],
- Text = "Center"
- };
- var labelHR = new Label
- {
- X = 0,
- Y = 2,
- Width = 6,
- Height = 1,
- TextAlignment = Alignment.End,
- ColorScheme = Colors.ColorSchemes ["Dialog"],
- Text = "End"
- };
- var labelHJ = new Label
- {
- X = 0,
- Y = 3,
- Width = 6,
- Height = 1,
- TextAlignment = Alignment.End,
- ColorScheme = Colors.ColorSchemes ["Dialog"],
- Text = "Fill"
- };
- var txtLabelHL = new Label
- {
- X = Pos.Right (labelHL) + 1,
- Y = Pos.Y (labelHL),
- Width = Dim.Fill (9),
- Height = 1,
- ColorScheme = color1,
- TextAlignment = Alignment.Start,
- Text = txt
- };
- var txtLabelHC = new Label
- {
- X = Pos.Right (labelHC) + 1,
- Y = Pos.Y (labelHC),
- Width = Dim.Fill (9),
- Height = 1,
- ColorScheme = color2,
- TextAlignment = Alignment.Center,
- Text = txt
- };
- var txtLabelHR = new Label
- {
- X = Pos.Right (labelHR) + 1,
- Y = Pos.Y (labelHR),
- Width = Dim.Fill (9),
- Height = 1,
- ColorScheme = color1,
- TextAlignment = Alignment.End,
- Text = txt
- };
- var txtLabelHJ = new Label
- {
- X = Pos.Right (labelHJ) + 1,
- Y = Pos.Y (labelHJ),
- Width = Dim.Fill (9),
- Height = 1,
- ColorScheme = color2,
- TextAlignment = Alignment.Fill,
- Text = txt
- };
- singleLineLabels.Add (txtLabelHL);
- singleLineLabels.Add (txtLabelHC);
- singleLineLabels.Add (txtLabelHR);
- singleLineLabels.Add (txtLabelHJ);
- app.Add (labelHL);
- app.Add (txtLabelHL);
- app.Add (labelHC);
- app.Add (txtLabelHC);
- app.Add (labelHR);
- app.Add (txtLabelHR);
- app.Add (labelHJ);
- app.Add (txtLabelHJ);
- // Vertical Single-Line
- var labelVT = new Label
- {
- X = Pos.AnchorEnd () - 6,
- Y = 0,
- Width = 2,
- Height = 6,
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End,
- Text = "Start"
- };
- labelVT.TextFormatter.WordWrap = false;
- var labelVM = new Label
- {
- X = Pos.AnchorEnd () - 4,
- Y = 0,
- Width = 2,
- Height = 6,
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End,
- Text = "Center"
- };
- labelVM.TextFormatter.WordWrap = false;
- var labelVB = new Label
- {
- X = Pos.AnchorEnd () - 2,
- Y = 0,
- Width = 2,
- Height = 6,
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End,
- Text = "End"
- };
- labelVB.TextFormatter.WordWrap = false;
- var labelVJ = new Label
- {
- X = Pos.AnchorEnd (),
- Y = 0,
- Width = 2,
- Height = 6,
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End,
- Text = "Fill"
- };
- labelVJ.TextFormatter.WordWrap = false;
- var txtLabelVT = new Label
- {
- X = Pos.X (labelVT),
- Y = Pos.Bottom (labelVT) + 1,
- Width = 2,
- Height = Dim.Fill (),
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.Start,
- Text = txt
- };
- txtLabelVT.TextFormatter.WordWrap = false;
- var txtLabelVM = new Label
- {
- X = Pos.X (labelVM),
- Y = Pos.Bottom (labelVM) + 1,
- Width = 2,
- Height = Dim.Fill (),
- ColorScheme = color2,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.Center,
- Text = txt
- };
- txtLabelVM.TextFormatter.WordWrap = false;
- var txtLabelVB = new Label
- {
- X = Pos.X (labelVB),
- Y = Pos.Bottom (labelVB) + 1,
- Width = 2,
- Height = Dim.Fill (),
- ColorScheme = color1,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End,
- Text = txt
- };
- txtLabelVB.TextFormatter.WordWrap = false;
- var txtLabelVJ = new Label
- {
- X = Pos.X (labelVJ),
- Y = Pos.Bottom (labelVJ) + 1,
- Width = 2,
- Height = Dim.Fill (),
- ColorScheme = color2,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.Fill,
- Text = txt
- };
- txtLabelVJ.TextFormatter.WordWrap = false;
- singleLineLabels.Add (txtLabelVT);
- singleLineLabels.Add (txtLabelVM);
- singleLineLabels.Add (txtLabelVB);
- singleLineLabels.Add (txtLabelVJ);
- app.Add (labelVT);
- app.Add (txtLabelVT);
- app.Add (labelVM);
- app.Add (txtLabelVM);
- app.Add (labelVB);
- app.Add (txtLabelVB);
- app.Add (labelVJ);
- app.Add (txtLabelVJ);
- // Multi-Line
- var container = new View
- {
- X = 0,
- Y = Pos.Bottom (txtLabelHJ),
- Width = Dim.Fill (31),
- Height = Dim.Fill (4)
- //ColorScheme = color2
- };
- var txtLabelTL = new Label
- {
- X = 0 /* */,
- Y = 1,
- Width = Dim.Percent (100 / 3),
- Height = Dim.Percent (100 / 3),
- TextAlignment = Alignment.Start,
- VerticalTextAlignment = Alignment.Start,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelTL.TextFormatter.MultiLine = true;
- var txtLabelTC = new Label
- {
- X = Pos.Right (txtLabelTL) + 2,
- Y = 1,
- Width = Dim.Percent (33),
- Height = Dim.Percent (33),
- TextAlignment = Alignment.Center,
- VerticalTextAlignment = Alignment.Start,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelTC.TextFormatter.MultiLine = true;
- var txtLabelTR = new Label
- {
- X = Pos.Right (txtLabelTC) + 2,
- Y = 1,
- Width = Dim.Percent (100, DimPercentMode.Position),
- Height = Dim.Percent (33),
- TextAlignment = Alignment.End,
- VerticalTextAlignment = Alignment.Start,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelTR.TextFormatter.MultiLine = true;
- var txtLabelML = new Label
- {
- X = Pos.X (txtLabelTL),
- Y = Pos.Bottom (txtLabelTL) + 1,
- Width = Dim.Width (txtLabelTL),
- Height = Dim.Percent (33),
- TextAlignment = Alignment.Start,
- VerticalTextAlignment = Alignment.Center,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelML.TextFormatter.MultiLine = true;
- var txtLabelMC = new Label
- {
- X = Pos.X (txtLabelTC),
- Y = Pos.Bottom (txtLabelTC) + 1,
- Width = Dim.Width (txtLabelTC),
- Height = Dim.Percent (33),
- TextAlignment = Alignment.Center,
- VerticalTextAlignment = Alignment.Center,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelMC.TextFormatter.MultiLine = true;
- var txtLabelMR = new Label
- {
- X = Pos.X (txtLabelTR),
- Y = Pos.Bottom (txtLabelTR) + 1,
- Width = Dim.Percent (100, DimPercentMode.Position),
- Height = Dim.Percent (33),
- TextAlignment = Alignment.End,
- VerticalTextAlignment = Alignment.Center,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelMR.TextFormatter.MultiLine = true;
- var txtLabelBL = new Label
- {
- X = Pos.X (txtLabelML),
- Y = Pos.Bottom (txtLabelML) + 1,
- Width = Dim.Width (txtLabelML),
- Height = Dim.Percent (100, DimPercentMode.Position),
- TextAlignment = Alignment.Start,
- VerticalTextAlignment = Alignment.End,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelBL.TextFormatter.MultiLine = true;
- var txtLabelBC = new Label
- {
- X = Pos.X (txtLabelMC),
- Y = Pos.Bottom (txtLabelMC) + 1,
- Width = Dim.Width (txtLabelMC),
- Height = Dim.Percent (100, DimPercentMode.Position),
- TextAlignment = Alignment.Center,
- VerticalTextAlignment = Alignment.End,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelBC.TextFormatter.MultiLine = true;
- var txtLabelBR = new Label
- {
- X = Pos.X (txtLabelMR),
- Y = Pos.Bottom (txtLabelMR) + 1,
- Width = Dim.Percent (100, DimPercentMode.Position),
- Height = Dim.Percent (100, DimPercentMode.Position),
- TextAlignment = Alignment.End,
- VerticalTextAlignment = Alignment.End,
- ColorScheme = color1,
- Text = txt
- };
- txtLabelBR.TextFormatter.MultiLine = true;
- multiLineLabels.Add (txtLabelTL);
- multiLineLabels.Add (txtLabelTC);
- multiLineLabels.Add (txtLabelTR);
- multiLineLabels.Add (txtLabelML);
- multiLineLabels.Add (txtLabelMC);
- multiLineLabels.Add (txtLabelMR);
- multiLineLabels.Add (txtLabelBL);
- multiLineLabels.Add (txtLabelBC);
- multiLineLabels.Add (txtLabelBR);
- // Save Alignment in Data
- foreach (Label t in multiLineLabels)
- {
- t.Data = new { h = t.TextAlignment, v = t.VerticalTextAlignment };
- }
- container.Add (txtLabelTL);
- container.Add (txtLabelTC);
- container.Add (txtLabelTR);
- container.Add (txtLabelML);
- container.Add (txtLabelMC);
- container.Add (txtLabelMR);
- container.Add (txtLabelBL);
- container.Add (txtLabelBC);
- container.Add (txtLabelBR);
- app.Add (container);
- // Edit Text
- var label = new Label
- {
- X = 1,
- Y = Pos.Bottom (container) + 1,
- Width = 10,
- Height = 1,
- Text = "Edit Text:"
- };
- var editText = new TextView
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (31),
- Height = 3,
- Text = txt
- };
- editText.MouseClick += (s, m) =>
- {
- foreach (Label v in singleLineLabels)
- {
- v.Text = editText.Text;
- }
- foreach (Label v in multiLineLabels)
- {
- v.Text = editText.Text;
- }
- };
- app.KeyUp += (s, m) =>
- {
- foreach (Label v in singleLineLabels)
- {
- v.Text = editText.Text;
- }
- foreach (Label v in multiLineLabels)
- {
- v.Text = editText.Text;
- }
- };
- editText.SetFocus ();
- app.Add (label, editText);
- // JUSTIFY CHECKBOX
- var justifyCheckbox = new CheckBox
- {
- X = Pos.Right (container) + 1,
- Y = Pos.Y (container) + 1,
- Width = Dim.Fill (10),
- Height = 1,
- Text = "Fill"
- };
- app.Add (justifyCheckbox);
- // JUSTIFY OPTIONS
- var justifyOptions = new RadioGroup
- {
- X = Pos.Left (justifyCheckbox) + 1,
- Y = Pos.Y (justifyCheckbox) + 1,
- Width = Dim.Fill (9),
- RadioLabels = ["Current direction", "Opposite direction", "FIll Both"],
- Enabled = false
- };
- justifyCheckbox.Toggled += (s, e) => ToggleJustify (e.OldValue is { } && (bool)e.OldValue);
- justifyOptions.SelectedItemChanged += (s, e) => { ToggleJustify (false, true); };
- app.Add (justifyOptions);
- // WRAP CHECKBOX
- var wrapCheckbox = new CheckBox
- {
- X = Pos.Right (container) + 1,
- Y = Pos.Bottom (justifyOptions),
- Width = Dim.Fill (10),
- Height = 1,
- Text = "Word Wrap"
- };
- wrapCheckbox.Checked = wrapCheckbox.TextFormatter.WordWrap;
- wrapCheckbox.Toggled += (s, e) =>
- {
- if (e.OldValue == true)
- {
- foreach (Label t in multiLineLabels)
- {
- t.TextFormatter.WordWrap = false;
- }
- }
- else
- {
- foreach (Label t in multiLineLabels)
- {
- t.TextFormatter.WordWrap = true;
- }
- }
- };
- app.Add (wrapCheckbox);
- // AUTOSIZE CHECKBOX
- var autoSizeCheckbox = new CheckBox
- {
- X = Pos.Right (container) + 1,
- Y = Pos.Y (wrapCheckbox) + 1,
- Width = Dim.Fill (10),
- Height = 1,
- Text = "AutoSize"
- };
- autoSizeCheckbox.Checked = autoSizeCheckbox.TextFormatter.AutoSize;
- autoSizeCheckbox.Toggled += (s, e) =>
- {
- if (e.OldValue == true)
- {
- foreach (Label t in multiLineLabels)
- {
- t.TextFormatter.AutoSize = false;
- }
- }
- else
- {
- foreach (Label t in multiLineLabels)
- {
- t.TextFormatter.AutoSize = true;
- }
- }
- };
- app.Add (autoSizeCheckbox);
- // Direction Options
- List<TextDirection> directionsEnum = Enum.GetValues (typeof (TextDirection)).Cast<TextDirection> ().ToList ();
- var directionOptions = new RadioGroup
- {
- X = Pos.Right (container) + 1,
- Y = Pos.Bottom (autoSizeCheckbox) + 1,
- Width = Dim.Fill (10),
- Height = Dim.Fill (1),
- HotKeySpecifier = (Rune)'\xffff',
- RadioLabels = directionsEnum.Select (e => e.ToString ()).ToArray ()
- };
- directionOptions.SelectedItemChanged += (s, ev) =>
- {
- bool justChecked = justifyCheckbox.Checked is { } && (bool)justifyCheckbox.Checked;
- if (justChecked)
- {
- ToggleJustify (true);
- }
- foreach (Label v in multiLineLabels)
- {
- v.TextDirection = (TextDirection)ev.SelectedItem;
- }
- if (justChecked)
- {
- ToggleJustify (false);
- }
- };
- app.Add (directionOptions);
- Application.Run (app);
- app.Dispose ();
- void ToggleJustify (bool oldValue, bool wasJustOptions = false)
- {
- if (oldValue)
- {
- if (!wasJustOptions)
- {
- justifyOptions.Enabled = false;
- }
- foreach (Label t in multiLineLabels)
- {
- t.TextAlignment = (Alignment)((dynamic)t.Data).h;
- t.VerticalTextAlignment = (Alignment)((dynamic)t.Data).v;
- }
- }
- else
- {
- foreach (Label t in multiLineLabels)
- {
- if (!wasJustOptions)
- {
- justifyOptions.Enabled = true;
- }
- if (TextFormatter.IsVerticalDirection (t.TextDirection))
- {
- switch (justifyOptions.SelectedItem)
- {
- case 0:
- t.VerticalTextAlignment = Alignment.Fill;
- t.TextAlignment = ((dynamic)t.Data).h;
- break;
- case 1:
- t.VerticalTextAlignment = (Alignment)((dynamic)t.Data).v;
- t.TextAlignment = Alignment.Fill;
- break;
- case 2:
- t.VerticalTextAlignment = Alignment.Fill;
- t.TextAlignment = Alignment.Fill;
- break;
- }
- }
- else
- {
- switch (justifyOptions.SelectedItem)
- {
- case 0:
- t.TextAlignment = Alignment.Fill;
- t.VerticalTextAlignment = ((dynamic)t.Data).v;
- break;
- case 1:
- t.TextAlignment = (Alignment)((dynamic)t.Data).h;
- t.VerticalTextAlignment = Alignment.Fill;
- break;
- case 2:
- t.TextAlignment = Alignment.Fill;
- t.VerticalTextAlignment = Alignment.Fill;
- break;
- }
- }
- }
- }
- }
- }
- }
|