DimAutoDemo.cs 7.0 KB

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