DimAutoDemo.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using Terminal.Gui;
  3. using static Terminal.Gui.Dim;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("DimAuto", "Demonstrates Dim.Auto")]
  6. [ScenarioCategory ("Layout")]
  7. public class DimAutoDemo : Scenario
  8. {
  9. public override void Main ()
  10. {
  11. Application.Init ();
  12. // Setup - Create a top-level application window and configure it.
  13. Window appWindow = new ()
  14. {
  15. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  16. };
  17. var view = new FrameView
  18. {
  19. Title = "Type to make View grow",
  20. X = 1,
  21. Y = 1,
  22. Width = Auto (DimAutoStyle.Subviews, 40),
  23. Height = Auto (DimAutoStyle.Subviews, 10)
  24. };
  25. view.ValidatePosDim = true;
  26. var textEdit = new TextView { Text = "", X = 1, Y = 0, Width = 20, Height = 4 };
  27. view.Add (textEdit);
  28. var vlabel = new Label
  29. {
  30. Text = textEdit.Text,
  31. X = Pos.Left (textEdit),
  32. Y = Pos.Bottom (textEdit) + 1,
  33. Width = Auto (DimAutoStyle.Text, 1),
  34. Height = Auto (DimAutoStyle.Text, 8),
  35. ColorScheme = Colors.ColorSchemes ["Error"],
  36. TextDirection = TextDirection.TopBottom_LeftRight
  37. };
  38. vlabel.Id = "vlabel";
  39. view.Add (vlabel);
  40. var hlabel = new Label
  41. {
  42. Text = textEdit.Text,
  43. X = Pos.Right (vlabel) + 1,
  44. Y = Pos.Bottom (textEdit),
  45. Width = Auto (DimAutoStyle.Text, 20),
  46. Height = Auto (DimAutoStyle.Text, 1),
  47. ColorScheme = Colors.ColorSchemes ["Error"]
  48. };
  49. hlabel.Id = "hlabel";
  50. view.Add (hlabel);
  51. var heightAuto = new View
  52. {
  53. X = Pos.Right (vlabel) + 1,
  54. Y = Pos.Bottom (hlabel) + 1,
  55. Width = 20,
  56. Height = Auto (),
  57. ColorScheme = Colors.ColorSchemes ["Error"],
  58. Title = "W: 20, H: Auto",
  59. BorderStyle = LineStyle.Rounded
  60. };
  61. heightAuto.Id = "heightAuto";
  62. view.Add (heightAuto);
  63. var widthAuto = new View
  64. {
  65. X = Pos.Right (heightAuto) + 1,
  66. Y = Pos.Bottom (hlabel) + 1,
  67. Width = Auto (),
  68. Height = 5,
  69. ColorScheme = Colors.ColorSchemes ["Error"],
  70. Title = "W: Auto, H: 5",
  71. BorderStyle = LineStyle.Rounded
  72. };
  73. widthAuto.Id = "widthAuto";
  74. view.Add (widthAuto);
  75. var bothAuto = new View
  76. {
  77. X = Pos.Right (widthAuto) + 1,
  78. Y = Pos.Bottom (hlabel) + 1,
  79. Width = Auto (),
  80. Height = Auto (),
  81. ColorScheme = Colors.ColorSchemes ["Error"],
  82. Title = "W: Auto, H: Auto",
  83. BorderStyle = LineStyle.Rounded
  84. };
  85. bothAuto.Id = "bothAuto";
  86. view.Add (bothAuto);
  87. textEdit.ContentsChanged += (s, e) =>
  88. {
  89. hlabel.Text = textEdit.Text;
  90. vlabel.Text = textEdit.Text;
  91. heightAuto.Text = textEdit.Text;
  92. widthAuto.Text = textEdit.Text;
  93. bothAuto.Text = textEdit.Text;
  94. };
  95. var movingButton = new Button
  96. {
  97. Text = "_Move down",
  98. X = Pos.Right (vlabel),
  99. Y = Pos.Bottom (vlabel),
  100. };
  101. movingButton.Accept += (s, e) => { movingButton.Y = movingButton.Frame.Y + 1; };
  102. view.Add (movingButton);
  103. var resetButton = new Button
  104. {
  105. Text = "_Reset Button",
  106. X = Pos.Right (movingButton),
  107. Y = Pos.Top (movingButton)
  108. };
  109. resetButton.Accept += (s, e) => { movingButton.Y = Pos.Bottom (hlabel); };
  110. view.Add (resetButton);
  111. var dlgButton = new Button
  112. {
  113. Text = "Open Test _Dialog",
  114. X = Pos.Right (view),
  115. Y = Pos.Top (view)
  116. };
  117. dlgButton.Accept += DlgButton_Clicked;
  118. appWindow.Add (view, dlgButton);
  119. // Run - Start the application.
  120. Application.Run (appWindow);
  121. appWindow.Dispose ();
  122. // Shutdown - Calling Application.Shutdown is required.
  123. Application.Shutdown ();
  124. }
  125. private void DlgButton_Clicked (object sender, EventArgs e)
  126. {
  127. var dlg = new Dialog
  128. {
  129. Title = "Test Dialog",
  130. Width = Dim.Auto (min: Dim.Percent (10)),
  131. //Height = Dim.Auto (min: Dim.Percent (50))
  132. };
  133. //var ok = new Button ("Bye") { IsDefault = true };
  134. //ok.Clicked += (s, _) => Application.RequestStop (dlg);
  135. //dlg.AddButton (ok);
  136. //var cancel = new Button ("Abort") { };
  137. //cancel.Clicked += (s, _) => Application.RequestStop (dlg);
  138. //dlg.AddButton (cancel);
  139. //var label = new Label
  140. //{
  141. // ValidatePosDim = true,
  142. // Text = "This is a label (AutoSize = false; Dim.Auto(3/20). Press Esc to close. Even more text.",
  143. // AutoSize = false,
  144. // X = Pos.Center (),
  145. // Y = 0,
  146. // Height = Auto (min: 3),
  147. // Width = Auto (min: 20),
  148. // ColorScheme = Colors.ColorSchemes ["Menu"]
  149. //};
  150. var text = new TextField
  151. {
  152. ValidatePosDim = true,
  153. Text = "TextField: X=1; Y=Pos.Bottom (label)+1, Width=Dim.Fill (0); Height=1",
  154. TextFormatter = new TextFormatter { WordWrap = true },
  155. X = 0,
  156. Y = 0, //Pos.Bottom (label) + 1,
  157. Width = Fill (10),
  158. Height = 1
  159. };
  160. //var btn = new Button
  161. //{
  162. // Text = "AnchorEnd", Y = Pos.AnchorEnd (1)
  163. //};
  164. //// TODO: We should really fix AnchorEnd to do this automatically.
  165. //btn.X = Pos.AnchorEnd () - (Pos.Right (btn) - Pos.Left (btn));
  166. //dlg.Add (label);
  167. dlg.Add (text);
  168. //dlg.Add (btn);
  169. Application.Run (dlg);
  170. dlg.Dispose ();
  171. }
  172. }