Files
Terminal.Gui/Examples/UICatalog/Scenarios/TextAlignmentAndDirection.cs
Tig 7422385457 Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/Clipping.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 13:55:54 -06:00

658 lines
21 KiB
C#

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
{
internal class AlignmentAndDirectionView : View
{
public AlignmentAndDirectionView ()
{
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent;
BorderStyle = LineStyle.Dotted;
}
}
public override void Main ()
{
Application.Init ();
Window app = new ()
{
Title = GetQuitKeyAndName ()
};
var txt = $"Hello World{Environment.NewLine}HELLO WORLD{Environment.NewLine}世界 您好";
SchemeManager.AddScheme ("TextAlignmentAndDirection1", new Scheme { Normal = new (Color.Black, Color.Gray) });
SchemeManager.AddScheme ("TextAlignmentAndDirection2", new Scheme { Normal = new (Color.Black, Color.DarkGray) });
List<View> singleLineLabels = new (); // single line
List<View> multiLineLabels = new (); // multi line
// Horizontal Single-Line
var labelHL = new Label
{
X = 0,
Y = 0,
Width = 6,
Height = 1,
TextAlignment = Alignment.End,
SchemeName = "Dialog",
Text = "Start",
};
var labelHC = new Label
{
X = 0,
Y = 1,
Width = 6,
Height = 1,
TextAlignment = Alignment.End,
SchemeName = "Dialog",
Text = "Center"
};
var labelHR = new Label
{
X = 0,
Y = 2,
Width = 6,
Height = 1,
TextAlignment = Alignment.End,
SchemeName = "Dialog",
Text = "End"
};
var labelHJ = new Label
{
X = 0,
Y = 3,
Width = 6,
Height = 1,
TextAlignment = Alignment.End,
SchemeName = "Dialog",
Text = "Fill"
};
var txtLabelHL = new View
{
X = Pos.Right (labelHL) + 1,
Y = Pos.Y (labelHL),
Width = Dim.Fill (9),
Height = 1,
SchemeName = "TextAlignmentAndDirection1",
TextAlignment = Alignment.Start,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
var txtLabelHC = new View
{
X = Pos.Right (labelHC) + 1,
Y = Pos.Y (labelHC),
Width = Dim.Fill (9),
Height = 1,
SchemeName = "TextAlignmentAndDirection2",
TextAlignment = Alignment.Center,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
var txtLabelHR = new View
{
X = Pos.Right (labelHR) + 1,
Y = Pos.Y (labelHR),
Width = Dim.Fill (9),
Height = 1,
SchemeName = "TextAlignmentAndDirection1",
TextAlignment = Alignment.End,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
var txtLabelHJ = new View
{
X = Pos.Right (labelHJ) + 1,
Y = Pos.Y (labelHJ),
Width = Dim.Fill (9),
Height = 1,
SchemeName = "TextAlignmentAndDirection2",
TextAlignment = Alignment.Fill,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
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,
SchemeName = "TextAlignmentAndDirection1",
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,
SchemeName = "TextAlignmentAndDirection1",
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,
SchemeName = "TextAlignmentAndDirection1",
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,
SchemeName = "TextAlignmentAndDirection1",
TextDirection = TextDirection.TopBottom_LeftRight,
VerticalTextAlignment = Alignment.End,
Text = "Fill"
};
labelVJ.TextFormatter.WordWrap = false;
var txtLabelVT = new View
{
X = Pos.X (labelVT),
Y = Pos.Bottom (labelVT) + 1,
Width = 2,
Height = Dim.Fill (),
SchemeName = "TextAlignmentAndDirection1",
TextDirection = TextDirection.TopBottom_LeftRight,
VerticalTextAlignment = Alignment.Start,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
txtLabelVT.TextFormatter.WordWrap = false;
var txtLabelVM = new View
{
X = Pos.X (labelVM),
Y = Pos.Bottom (labelVM) + 1,
Width = 2,
Height = Dim.Fill (),
SchemeName = "TextAlignmentAndDirection2",
TextDirection = TextDirection.TopBottom_LeftRight,
VerticalTextAlignment = Alignment.Center,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
txtLabelVM.TextFormatter.WordWrap = false;
var txtLabelVB = new View
{
X = Pos.X (labelVB),
Y = Pos.Bottom (labelVB) + 1,
Width = 2,
Height = Dim.Fill (),
SchemeName = "TextAlignmentAndDirection1",
TextDirection = TextDirection.TopBottom_LeftRight,
VerticalTextAlignment = Alignment.End,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
txtLabelVB.TextFormatter.WordWrap = false;
var txtLabelVJ = new View
{
X = Pos.X (labelVJ),
Y = Pos.Bottom (labelVJ) + 1,
Width = 2,
Height = Dim.Fill (),
SchemeName = "TextAlignmentAndDirection2",
TextDirection = TextDirection.TopBottom_LeftRight,
VerticalTextAlignment = Alignment.Fill,
Text = txt,
ViewportSettings = Terminal.Gui.ViewportSettings.Transparent
};
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)
//SchemeName = "TextAlignmentAndDirection2"
};
var txtLabelTL = new AlignmentAndDirectionView
{
X = 0,
Y = 1,
Width = Dim.Percent (100 / 3),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.Start,
VerticalTextAlignment = Alignment.Start,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelTL.TextFormatter.MultiLine = true;
var txtLabelTC = new AlignmentAndDirectionView
{
X = Pos.Right (txtLabelTL),
Y = 1,
Width = Dim.Percent (100 / 3),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.Center,
VerticalTextAlignment = Alignment.Start,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelTC.TextFormatter.MultiLine = true;
var txtLabelTR = new AlignmentAndDirectionView
{
X = Pos.Right (txtLabelTC),
Y = 1,
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.End,
VerticalTextAlignment = Alignment.Start,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelTR.TextFormatter.MultiLine = true;
var txtLabelML = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelTL),
Y = Pos.Bottom (txtLabelTL),
Width = Dim.Width (txtLabelTL),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.Start,
VerticalTextAlignment = Alignment.Center,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelML.TextFormatter.MultiLine = true;
var txtLabelMC = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelTC),
Y = Pos.Bottom (txtLabelTC),
Width = Dim.Width (txtLabelTC),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.Center,
VerticalTextAlignment = Alignment.Center,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelMC.TextFormatter.MultiLine = true;
var txtLabelMR = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelTR),
Y = Pos.Bottom (txtLabelTR),
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (100 / 3),
TextAlignment = Alignment.End,
VerticalTextAlignment = Alignment.Center,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelMR.TextFormatter.MultiLine = true;
var txtLabelBL = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelML),
Y = Pos.Bottom (txtLabelML),
Width = Dim.Width (txtLabelML),
Height = Dim.Percent (100, DimPercentMode.Position),
TextAlignment = Alignment.Start,
VerticalTextAlignment = Alignment.End,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelBL.TextFormatter.MultiLine = true;
var txtLabelBC = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelMC),
Y = Pos.Bottom (txtLabelMC),
Width = Dim.Width (txtLabelMC),
Height = Dim.Percent (100, DimPercentMode.Position),
TextAlignment = Alignment.Center,
VerticalTextAlignment = Alignment.End,
SchemeName = "TextAlignmentAndDirection1",
Text = txt,
};
txtLabelBC.TextFormatter.MultiLine = true;
var txtLabelBR = new AlignmentAndDirectionView
{
X = Pos.X (txtLabelMR),
Y = Pos.Bottom (txtLabelMR),
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (100, DimPercentMode.Position),
TextAlignment = Alignment.End,
VerticalTextAlignment = Alignment.End,
SchemeName = "TextAlignmentAndDirection1",
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 (View 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 (View v in singleLineLabels)
{
v.Text = editText.Text;
}
foreach (View v in multiLineLabels)
{
v.Text = editText.Text;
}
};
app.KeyUp += (s, m) =>
{
foreach (View v in singleLineLabels)
{
v.Text = editText.Text;
}
foreach (View 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.CheckedStateChanging += (s, e) => ToggleJustify (e.NewValue != CheckState.Checked);
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.CheckedState = wrapCheckbox.TextFormatter.WordWrap ? CheckState.Checked : CheckState.UnChecked;
wrapCheckbox.CheckedStateChanging += (s, e) =>
{
if (e.CurrentValue == CheckState.Checked)
{
foreach (View t in multiLineLabels)
{
t.TextFormatter.WordWrap = false;
}
}
else
{
foreach (View t in multiLineLabels)
{
t.TextFormatter.WordWrap = true;
}
}
};
app.Add (wrapCheckbox);
List<TextDirection> directionsEnum = Enum.GetValues (typeof (TextDirection)).Cast<TextDirection> ().ToList ();
var directionOptions = new RadioGroup
{
X = Pos.Right (container) + 1,
Y = Pos.Bottom (wrapCheckbox) + 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.CheckedState == CheckState.Checked;
if (justChecked)
{
ToggleJustify (true);
}
foreach (View v in multiLineLabels)
{
v.TextDirection = (TextDirection)ev.SelectedItem;
}
if (justChecked)
{
ToggleJustify (false);
}
};
app.Add (directionOptions);
Application.Run (app);
app.Dispose ();
Application.Shutdown ();
// Be a good citizen and remove the schemes we added
SchemeManager.RemoveScheme ("TextAlignmentAndDirection1");
SchemeManager.RemoveScheme ("TextAlignmentAndDirection2");
return;
void ToggleJustify (bool oldValue, bool wasJustOptions = false)
{
if (oldValue)
{
if (!wasJustOptions)
{
justifyOptions.Enabled = false;
}
foreach (View t in multiLineLabels)
{
t.TextAlignment = (Alignment)((dynamic)t.Data).h;
t.VerticalTextAlignment = (Alignment)((dynamic)t.Data).v;
}
}
else
{
foreach (View 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;
}
}
}
}
}
}
}