DimAutoDemo.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using Terminal.Gui;
  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. // For diagnostics
  18. appWindow.Padding.Thickness = new Thickness (1);
  19. FrameView dimAutoFrameView = CreateDimAutoContentFrameView ();
  20. FrameView sliderFrameView = CreateSliderFrameView ();
  21. sliderFrameView.X = Pos.Right(dimAutoFrameView) + 1;
  22. sliderFrameView.Width = Dim.Fill ();
  23. sliderFrameView.Height = Dim.Fill ();
  24. //var dlgButton = new Button
  25. //{
  26. // Text = "Open Test _Dialog",
  27. // X = Pos.Right (dimAutoFrameView),
  28. // Y = Pos.Top (dimAutoFrameView)
  29. //};
  30. //dlgButton.Accept += DlgButton_Clicked;
  31. appWindow.Add (dimAutoFrameView, sliderFrameView /*dlgButton*/);
  32. // Run - Start the application.
  33. Application.Run (appWindow);
  34. appWindow.Dispose ();
  35. // Shutdown - Calling Application.Shutdown is required.
  36. Application.Shutdown ();
  37. }
  38. private static FrameView CreateDimAutoContentFrameView ()
  39. {
  40. var dimAutoFrameView = new FrameView
  41. {
  42. Title = "Type to make View grow",
  43. X = 0,
  44. Y = 0,
  45. Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: Dim.Percent (25)),
  46. Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 10)
  47. };
  48. dimAutoFrameView.Margin.Thickness = new Thickness (1);
  49. dimAutoFrameView.ValidatePosDim = true;
  50. var textEdit = new TextView
  51. {
  52. Text = "",
  53. X = 0, Y = 0, Width = 20, Height = 4
  54. };
  55. dimAutoFrameView.Add (textEdit);
  56. var vlabel = new Label
  57. {
  58. Text = textEdit.Text,
  59. X = Pos.Left (textEdit),
  60. Y = Pos.Bottom (textEdit) + 1,
  61. Width = Dim.Auto (DimAutoStyle.Text, 1),
  62. Height = Dim.Auto (DimAutoStyle.Text, 8),
  63. ColorScheme = Colors.ColorSchemes ["Error"],
  64. TextDirection = TextDirection.TopBottom_LeftRight
  65. };
  66. vlabel.Id = "vlabel";
  67. dimAutoFrameView.Add (vlabel);
  68. var hlabel = new Label
  69. {
  70. Text = textEdit.Text,
  71. X = Pos.Right (vlabel) + 1,
  72. Y = Pos.Bottom (textEdit),
  73. Width = Dim.Auto (DimAutoStyle.Text, 20),
  74. Height = Dim.Auto (DimAutoStyle.Text, 1),
  75. ColorScheme = Colors.ColorSchemes ["Error"]
  76. };
  77. hlabel.Id = "hlabel";
  78. dimAutoFrameView.Add (hlabel);
  79. var heightAuto = new View
  80. {
  81. X = Pos.Right (vlabel) + 1,
  82. Y = Pos.Bottom (hlabel) + 1,
  83. Width = 20,
  84. Height = Dim.Auto (),
  85. ColorScheme = Colors.ColorSchemes ["Error"],
  86. Title = "W: 20, H: Auto",
  87. BorderStyle = LineStyle.Rounded
  88. };
  89. heightAuto.Id = "heightAuto";
  90. dimAutoFrameView.Add (heightAuto);
  91. var widthAuto = new View
  92. {
  93. X = Pos.Right (heightAuto) + 1,
  94. Y = Pos.Bottom (hlabel) + 1,
  95. Width = Dim.Auto (),
  96. Height = 5,
  97. ColorScheme = Colors.ColorSchemes ["Error"],
  98. Title = "W: Auto, H: 5",
  99. BorderStyle = LineStyle.Rounded
  100. };
  101. widthAuto.Id = "widthAuto";
  102. dimAutoFrameView.Add (widthAuto);
  103. var bothAuto = new View
  104. {
  105. X = Pos.Right (widthAuto) + 1,
  106. Y = Pos.Bottom (hlabel) + 1,
  107. Width = Dim.Auto (),
  108. Height = Dim.Auto (),
  109. ColorScheme = Colors.ColorSchemes ["Error"],
  110. Title = "W: Auto, H: Auto",
  111. BorderStyle = LineStyle.Rounded
  112. };
  113. bothAuto.Id = "bothAuto";
  114. dimAutoFrameView.Add (bothAuto);
  115. textEdit.ContentsChanged += (s, e) =>
  116. {
  117. hlabel.Text = textEdit.Text;
  118. vlabel.Text = textEdit.Text;
  119. heightAuto.Text = textEdit.Text;
  120. widthAuto.Text = textEdit.Text;
  121. bothAuto.Text = textEdit.Text;
  122. };
  123. var movingButton = new Button
  124. {
  125. Text = "_Click\nTo Move\nDown",
  126. X = Pos.Right (vlabel),
  127. Y = Pos.Bottom (vlabel)
  128. };
  129. movingButton.Accept += (s, e) => { movingButton.Y = movingButton.Frame.Y + 1; };
  130. dimAutoFrameView.Add (movingButton);
  131. var resetButton = new Button
  132. {
  133. Text = "_Reset Button (AnchorEnd)",
  134. X = Pos.AnchorEnd (),
  135. Y = Pos.AnchorEnd (1)
  136. };
  137. resetButton.Accept += (s, e) => { movingButton.Y = Pos.Bottom (hlabel); };
  138. dimAutoFrameView.Add (resetButton);
  139. return dimAutoFrameView;
  140. }
  141. private static FrameView CreateSliderFrameView ()
  142. {
  143. var sliderFrameView = new FrameView
  144. {
  145. Title = "Slider - Example of a DimAuto View",
  146. };
  147. List<object> options = new () { "One", "Two", "Three", "Four" };
  148. Slider slider = new (options)
  149. {
  150. X = 0,
  151. Y = 0,
  152. Type = SliderType.Multiple,
  153. AllowEmpty = false,
  154. BorderStyle = LineStyle.Double,
  155. Title = "_Slider"
  156. };
  157. sliderFrameView.Add (slider);
  158. return sliderFrameView;
  159. }
  160. private void DlgButton_Clicked (object sender, EventArgs e)
  161. {
  162. var dlg = new Dialog
  163. {
  164. Title = "Test Dialog",
  165. Width = Dim.Auto (minimumContentDim: Dim.Percent (10))
  166. //Height = Dim.Auto (min: Dim.Percent (50))
  167. };
  168. var text = new TextField
  169. {
  170. ValidatePosDim = true,
  171. Text = "TextField: X=1; Y=Pos.Bottom (label)+1, Width=Dim.Fill (0); Height=1",
  172. TextFormatter = new () { WordWrap = true },
  173. X = 0,
  174. Y = 0, //Pos.Bottom (label) + 1,
  175. Width = Dim.Fill (10),
  176. Height = 1
  177. };
  178. //var btn = new Button
  179. //{
  180. // Text = "AnchorEnd", Y = Pos.AnchorEnd (1)
  181. //};
  182. //// TODO: We should really fix AnchorEnd to do this automatically.
  183. //btn.X = Pos.AnchorEnd () - (Pos.Right (btn) - Pos.Left (btn));
  184. //dlg.Add (label);
  185. dlg.Add (text);
  186. //dlg.Add (btn);
  187. Application.Run (dlg);
  188. dlg.Dispose ();
  189. }
  190. }