PosJustification.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Collections.Generic;
  2. using System;
  3. using Terminal.Gui;
  4. using System.Linq;
  5. using System.Reflection.Emit;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("PosJustification", "Shows off Pos.Justify")]
  8. [ScenarioCategory ("Layout")]
  9. public sealed class PosJustification : Scenario
  10. {
  11. private Justifier _horizJustifier = new Justifier ();
  12. private int _leftMargin = 0;
  13. private Justifier _vertJustifier = new Justifier ();
  14. private int _topMargin = 0;
  15. public override void Main ()
  16. {
  17. // Init
  18. Application.Init ();
  19. // Setup - Create a top-level application window and configure it.
  20. Window appWindow = new ()
  21. {
  22. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()} - {GetDescription ()}"
  23. };
  24. SetupHorizontalControls (appWindow);
  25. SetupVerticalControls (appWindow);
  26. // Run - Start the application.
  27. Application.Run (appWindow);
  28. appWindow.Dispose ();
  29. // Shutdown - Calling Application.Shutdown is required.
  30. Application.Shutdown ();
  31. }
  32. private void SetupHorizontalControls (Window appWindow)
  33. {
  34. ColorScheme colorScheme = Colors.ColorSchemes ["Toplevel"];
  35. RadioGroup justification = new ()
  36. {
  37. X = Pos.Justify (_horizJustifier.Justification),
  38. Y = Pos.Center (),
  39. RadioLabels = GetUniqueEnumNames<Justification> (false).ToArray (),
  40. ColorScheme = colorScheme
  41. };
  42. justification.SelectedItemChanged += (s, e) =>
  43. {
  44. _horizJustifier.Justification = (Justification)Enum.Parse (typeof (Justification), justification.SelectedItem.ToString ());
  45. foreach (var view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
  46. {
  47. if (view.X is Pos.PosJustify j)
  48. {
  49. var newJust = new Pos.PosJustify (_horizJustifier.Justification)
  50. {
  51. Justifier =
  52. {
  53. PutSpaceBetweenItems = _horizJustifier.PutSpaceBetweenItems
  54. }
  55. };
  56. view.X = newJust;
  57. }
  58. }
  59. };
  60. appWindow.Add (justification);
  61. CheckBox putSpaces = new ()
  62. {
  63. X = Pos.Justify (_horizJustifier.Justification),
  64. Y = Pos.Top (justification),
  65. ColorScheme = colorScheme,
  66. Text = "Spaces",
  67. };
  68. putSpaces.Toggled += (s, e) =>
  69. {
  70. _horizJustifier.PutSpaceBetweenItems = e.NewValue is { } && e.NewValue.Value;
  71. foreach (var view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
  72. {
  73. if (view.X is Pos.PosJustify j)
  74. {
  75. j.Justifier.PutSpaceBetweenItems = _horizJustifier.PutSpaceBetweenItems;
  76. }
  77. }
  78. };
  79. appWindow.Add (putSpaces);
  80. CheckBox margin = new ()
  81. {
  82. X = Pos.Left (putSpaces),
  83. Y = Pos.Bottom (putSpaces),
  84. ColorScheme = colorScheme,
  85. Text = "Margin",
  86. };
  87. margin.Toggled += (s, e) =>
  88. {
  89. _leftMargin = e.NewValue is { } && e.NewValue.Value ? 1 : 0;
  90. foreach (var view in appWindow.Subviews.Where (v => v.X is Pos.PosJustify))
  91. {
  92. // Skip the justification radio group
  93. if (view != justification)
  94. {
  95. view.Margin.Thickness = new Thickness (_leftMargin, 0, 0, 0);
  96. }
  97. }
  98. appWindow.LayoutSubviews ();
  99. };
  100. appWindow.Add (margin);
  101. var addedViews = new List<Button> ();
  102. addedViews.Add (new Button
  103. {
  104. X = Pos.Justify (_horizJustifier.Justification),
  105. Y = Pos.Center (),
  106. Text = NumberToWords.Convert (0)
  107. });
  108. var addedViewsUpDown = new Buttons.NumericUpDown<int> ()
  109. {
  110. X = Pos.Justify (_horizJustifier.Justification),
  111. Y = Pos.Top (justification),
  112. Width = 9,
  113. Title = "Added",
  114. ColorScheme = colorScheme,
  115. BorderStyle = LineStyle.None,
  116. Value = addedViews.Count
  117. };
  118. addedViewsUpDown.Border.Thickness = new Thickness (0, 1, 0, 0);
  119. addedViewsUpDown.ValueChanging += (s, e) =>
  120. {
  121. if (e.NewValue < 0)
  122. {
  123. e.Cancel = true;
  124. return;
  125. }
  126. // Add or remove buttons
  127. if (e.NewValue < e.OldValue)
  128. {
  129. // Remove buttons
  130. for (int i = e.OldValue - 1; i >= e.NewValue; i--)
  131. {
  132. var button = addedViews [i];
  133. appWindow.Remove (button);
  134. addedViews.RemoveAt (i);
  135. button.Dispose ();
  136. }
  137. }
  138. if (e.NewValue > e.OldValue)
  139. {
  140. // Add buttons
  141. for (int i = e.OldValue; i < e.NewValue; i++)
  142. {
  143. var button = new Button
  144. {
  145. X = Pos.Justify (_horizJustifier.Justification),
  146. Y = Pos.Center (),
  147. Text = NumberToWords.Convert (i + 1)
  148. };
  149. appWindow.Add (button);
  150. addedViews.Add (button);
  151. }
  152. }
  153. };
  154. appWindow.Add (addedViewsUpDown);
  155. appWindow.Add (addedViews [0]);
  156. }
  157. private void SetupVerticalControls (Window appWindow)
  158. {
  159. ColorScheme colorScheme = Colors.ColorSchemes ["Error"];
  160. RadioGroup justification = new ()
  161. {
  162. X = 0,
  163. Y = Pos.Justify (_vertJustifier.Justification),
  164. RadioLabels = GetUniqueEnumNames<Justification> (true).Reverse ().ToArray (),
  165. ColorScheme = colorScheme
  166. };
  167. justification.SelectedItemChanged += (s, e) =>
  168. {
  169. _vertJustifier.Justification = (Justification)Enum.Parse (typeof (Justification), justification.SelectedItem.ToString ());
  170. foreach (var view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
  171. {
  172. if (view.Y is Pos.PosJustify j)
  173. {
  174. var newJust = new Pos.PosJustify (_vertJustifier.Justification)
  175. {
  176. Justifier =
  177. {
  178. PutSpaceBetweenItems = _vertJustifier.PutSpaceBetweenItems
  179. }
  180. };
  181. view.Y = newJust;
  182. }
  183. }
  184. };
  185. appWindow.Add (justification);
  186. CheckBox putSpaces = new ()
  187. {
  188. X = 0,
  189. Y = Pos.Justify (_vertJustifier.Justification),
  190. ColorScheme = colorScheme,
  191. Text = "Spaces",
  192. };
  193. putSpaces.Toggled += (s, e) =>
  194. {
  195. _vertJustifier.PutSpaceBetweenItems = e.NewValue is { } && e.NewValue.Value;
  196. foreach (var view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
  197. {
  198. if (view.Y is Pos.PosJustify j)
  199. {
  200. j.Justifier.PutSpaceBetweenItems = _vertJustifier.PutSpaceBetweenItems;
  201. }
  202. }
  203. };
  204. appWindow.Add (putSpaces);
  205. CheckBox margin = new ()
  206. {
  207. X = Pos.Right (putSpaces) + 1,
  208. Y = Pos.Top (putSpaces),
  209. ColorScheme = colorScheme,
  210. Text = "Margin",
  211. };
  212. margin.Toggled += (s, e) =>
  213. {
  214. _topMargin = e.NewValue is { } && e.NewValue.Value ? 1 : 0;
  215. foreach (var view in appWindow.Subviews.Where (v => v.Y is Pos.PosJustify))
  216. {
  217. // Skip the justification radio group
  218. if (view != justification)
  219. {
  220. view.Margin.Thickness = new Thickness (0, _topMargin, 0, 0);
  221. }
  222. }
  223. appWindow.LayoutSubviews ();
  224. };
  225. appWindow.Add (margin);
  226. var addedViews = new List<CheckBox> ();
  227. addedViews.Add (new CheckBox
  228. {
  229. X = 0,
  230. Y = Pos.Justify (_vertJustifier.Justification),
  231. Text = NumberToWords.Convert (0)
  232. });
  233. var addedViewsUpDown = new Buttons.NumericUpDown<int> ()
  234. {
  235. X = 0,
  236. Y = Pos.Justify (_vertJustifier.Justification),
  237. Width = 9,
  238. Title = "Added",
  239. ColorScheme = colorScheme,
  240. BorderStyle = LineStyle.None,
  241. Value = addedViews.Count
  242. };
  243. addedViewsUpDown.Border.Thickness = new Thickness (0, 1, 0, 0);
  244. addedViewsUpDown.ValueChanging += (s, e) =>
  245. {
  246. if (e.NewValue < 0)
  247. {
  248. e.Cancel = true;
  249. return;
  250. }
  251. // Add or remove buttons
  252. if (e.NewValue < e.OldValue)
  253. {
  254. // Remove buttons
  255. for (int i = e.OldValue - 1; i >= e.NewValue; i--)
  256. {
  257. var button = addedViews [i];
  258. appWindow.Remove (button);
  259. addedViews.RemoveAt (i);
  260. button.Dispose ();
  261. }
  262. }
  263. if (e.NewValue > e.OldValue)
  264. {
  265. // Add buttons
  266. for (int i = e.OldValue; i < e.NewValue; i++)
  267. {
  268. var button = new CheckBox ()
  269. {
  270. X = 0,
  271. Y = Pos.Justify (_vertJustifier.Justification),
  272. Text = NumberToWords.Convert (i + 1)
  273. };
  274. appWindow.Add (button);
  275. addedViews.Add (button);
  276. }
  277. }
  278. };
  279. appWindow.Add (addedViewsUpDown);
  280. appWindow.Add (addedViews [0]);
  281. }
  282. static IEnumerable<string> GetUniqueEnumNames<T> (bool reverse) where T : Enum
  283. {
  284. var values = new HashSet<int> ();
  285. var names = Enum.GetNames (typeof (T));
  286. if (reverse)
  287. {
  288. names = Enum.GetNames (typeof (T)).Reverse ().ToArray ();
  289. }
  290. foreach (var name in names)
  291. {
  292. var value = (int)Enum.Parse (typeof (T), name);
  293. if (values.Add (value))
  294. {
  295. yield return name;
  296. }
  297. }
  298. }
  299. }