DimAutoDemo.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("DimAuto", "Demonstrates Dim.Auto")]
  4. [ScenarioCategory ("Layout")]
  5. public class DimAutoDemo : Scenario {
  6. public override void Init ()
  7. {
  8. Application.Init ();
  9. ConfigurationManager.Themes.Theme = Theme;
  10. ConfigurationManager.Apply ();
  11. Application.Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
  12. }
  13. public override void Setup ()
  14. {
  15. var textField = new TextField { Text = "Type here", X = 1, Y = 0, Width = 20, Height = 1 };
  16. var label = new Label {
  17. X = Pos.Left (textField),
  18. Y = Pos.Bottom (textField),
  19. AutoSize = true,
  20. ColorScheme = Colors.ColorSchemes["Error"]
  21. };
  22. textField.TextChanged += (s, e) => {
  23. label.Text = textField.Text;
  24. };
  25. var resetButton = new Button () {
  26. Text = "P_ut Button Back",
  27. Y = Pos.Bottom (label)
  28. };
  29. resetButton.X = Pos.AnchorEnd () - 19;
  30. var movingButton = new Button () {
  31. Text = "Press to make button move down.",
  32. X = 0,
  33. Y = Pos.Bottom (resetButton),
  34. Width = 10
  35. };
  36. movingButton.Clicked += (s, e) => {
  37. movingButton.Y = movingButton.Frame.Y + 1;
  38. };
  39. var view = new FrameView () {
  40. Title = "Type in the TextField to make View grow.",
  41. X = 3,
  42. Y = 3,
  43. Width = Dim.Auto (min: Dim.Percent (50)),
  44. Height = Dim.Auto (min: 10)
  45. };
  46. view.ValidatePosDim = true;
  47. view.Add (textField, label, resetButton, movingButton);
  48. resetButton.Clicked += (s, e) => {
  49. movingButton.Y = Pos.Bottom (resetButton);
  50. };
  51. var dlgButton = new Button () {
  52. Text = "Open Test _Dialog",
  53. X = Pos.Right (view),
  54. Y = Pos.Top (view)
  55. };
  56. dlgButton.Clicked += DlgButton_Clicked;
  57. Application.Top.Add (view, dlgButton);
  58. }
  59. private void DlgButton_Clicked (object sender, System.EventArgs e)
  60. {
  61. var dlg = new Dialog () {
  62. Title = "Test Dialog"
  63. };
  64. //var ok = new Button ("Bye") { IsDefault = true };
  65. //ok.Clicked += (s, _) => Application.RequestStop (dlg);
  66. //dlg.AddButton (ok);
  67. //var cancel = new Button ("Abort") { };
  68. //cancel.Clicked += (s, _) => Application.RequestStop (dlg);
  69. //dlg.AddButton (cancel);
  70. var label = new Label ("This is a label (AutoSize = false; Dim.Auto(3/20). Press Esc to close. Even more text.") {
  71. AutoSize = false,
  72. X = Pos.Center (),
  73. Y = 0,
  74. Height = Dim.Auto (min: 3),
  75. Width = Dim.Auto (min: 20),
  76. ColorScheme = Colors.ColorSchemes ["Menu"]
  77. };
  78. var text = new TextField () {
  79. Text = "TextField... X = 1; Y = Pos.Bottom (label), Width = Dim.Fill (1); Height = Dim.Fill(1)",
  80. TextFormatter = new TextFormatter () { WordWrap = true },
  81. X = 20,
  82. Y = Pos.Bottom (label),
  83. Width = Dim.Fill (20),
  84. Height = Dim.Fill (10)
  85. };
  86. var btn = new Button ("AnchorEnd") {
  87. Y = Pos.AnchorEnd (1)
  88. };
  89. // TODO: We should really fix AnchorEnd to do this automatically.
  90. btn.X = Pos.AnchorEnd () - (Pos.Right (btn) - Pos.Left (btn));
  91. dlg.Add (label);
  92. dlg.Add (text);
  93. dlg.Add (btn);
  94. Application.Run (dlg);
  95. }
  96. }