Scrolling.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Scrolling", "Demonstrates scrolling etc...")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("Scrolling")]
  7. [ScenarioCategory ("Tests")]
  8. public class Scrolling : Scenario
  9. {
  10. private ViewDiagnosticFlags _diagnosticFlags;
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. _diagnosticFlags = View.Diagnostics;
  15. View.Diagnostics = ViewDiagnosticFlags.Ruler;
  16. var app = new Window ()
  17. {
  18. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  19. // Offset to stress clipping
  20. X = 3,
  21. Y = 3,
  22. Width = Dim.Fill (3),
  23. Height = Dim.Fill (3),
  24. };
  25. var label = new Label { X = 0, Y = 0 };
  26. app.Add (label);
  27. var scrollView = new ScrollView
  28. {
  29. Id = "scrollView",
  30. X = 2,
  31. Y = Pos.Bottom (label) + 1,
  32. Width = 60,
  33. Height = 20,
  34. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  35. ContentSize = new (120, 40),
  36. //ContentOffset = Point.Empty,
  37. ShowVerticalScrollIndicator = true,
  38. ShowHorizontalScrollIndicator = true
  39. };
  40. scrollView.Padding.Thickness = new (1);
  41. label.Text = $"{scrollView}\nContentSize: {scrollView.ContentSize}\nContentOffset: {scrollView.ContentOffset}";
  42. const string rule = "0123456789";
  43. var horizontalRuler = new Label
  44. {
  45. X = 0,
  46. Y = 0,
  47. AutoSize = false,
  48. Width = Dim.Fill (),
  49. Height = 2,
  50. ColorScheme = Colors.ColorSchemes ["Error"]
  51. };
  52. scrollView.Add (horizontalRuler);
  53. const string vrule = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
  54. var verticalRuler = new Label
  55. {
  56. X = 0,
  57. Y = 0,
  58. AutoSize = false,
  59. Width = 1,
  60. Height = Dim.Fill (),
  61. ColorScheme = Colors.ColorSchemes ["Error"]
  62. };
  63. scrollView.Add (verticalRuler);
  64. var pressMeButton = new Button { X = 3, Y = 3, Text = "Press me!" };
  65. pressMeButton.Accept += (s, e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  66. scrollView.Add (pressMeButton);
  67. var aLongButton = new Button
  68. {
  69. X = 3,
  70. Y = 4,
  71. AutoSize = false,
  72. Width = Dim.Fill (3),
  73. Text = "A very long button. Should be wide enough to demo clipping!"
  74. };
  75. aLongButton.Accept += (s, e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  76. scrollView.Add (aLongButton);
  77. scrollView.Add (
  78. new TextField
  79. {
  80. X = 3,
  81. Y = 5,
  82. Width = 50,
  83. ColorScheme = Colors.ColorSchemes ["Dialog"],
  84. Text = "This is a test of...",
  85. }
  86. );
  87. scrollView.Add (
  88. new TextField
  89. {
  90. X = 3,
  91. Y = 10,
  92. Width = 50,
  93. ColorScheme = Colors.ColorSchemes ["Dialog"],
  94. Text = "... the emergency broadcast system.",
  95. }
  96. );
  97. scrollView.Add (
  98. new TextField
  99. {
  100. X = 3,
  101. Y = 99,
  102. Width = 50,
  103. ColorScheme = Colors.ColorSchemes ["Dialog"],
  104. Text = "Last line",
  105. }
  106. );
  107. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  108. var anchorButton = new Button { Y = Pos.AnchorEnd (0) - 1, Text = "Bottom Right" };
  109. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  110. anchorButton.X = Pos.AnchorEnd (0) - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  111. anchorButton.Accept += (s, e) =>
  112. {
  113. // This demonstrates how to have a dynamically sized button
  114. // Each time the button is clicked the button's text gets longer
  115. // The call to Win.LayoutSubviews causes the Computed layout to
  116. // get updated.
  117. anchorButton.Text += "!";
  118. app.LayoutSubviews ();
  119. };
  120. scrollView.Add (anchorButton);
  121. app.Add (scrollView);
  122. var hCheckBox = new CheckBox
  123. {
  124. X = Pos.X (scrollView),
  125. Y = Pos.Bottom (scrollView),
  126. Text = "Horizontal Scrollbar",
  127. Checked = scrollView.ShowHorizontalScrollIndicator
  128. };
  129. app.Add (hCheckBox);
  130. var vCheckBox = new CheckBox
  131. {
  132. X = Pos.Right (hCheckBox) + 3,
  133. Y = Pos.Bottom (scrollView),
  134. Text = "Vertical Scrollbar",
  135. Checked = scrollView.ShowVerticalScrollIndicator
  136. };
  137. app.Add (vCheckBox);
  138. var t = "Auto Hide Scrollbars";
  139. var ahCheckBox = new CheckBox
  140. {
  141. X = Pos.Left (scrollView), Y = Pos.Bottom (hCheckBox), Text = t, Checked = scrollView.AutoHideScrollBars
  142. };
  143. var k = "Keep Content Always In Viewport";
  144. var keepCheckBox = new CheckBox
  145. {
  146. X = Pos.Left (scrollView), Y = Pos.Bottom (ahCheckBox), Text = k, Checked = scrollView.AutoHideScrollBars
  147. };
  148. hCheckBox.Toggled += (s, e) =>
  149. {
  150. if (ahCheckBox.Checked == false)
  151. {
  152. scrollView.ShowHorizontalScrollIndicator = (bool)hCheckBox.Checked;
  153. }
  154. else
  155. {
  156. hCheckBox.Checked = true;
  157. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  158. }
  159. };
  160. vCheckBox.Toggled += (s, e) =>
  161. {
  162. if (ahCheckBox.Checked == false)
  163. {
  164. scrollView.ShowVerticalScrollIndicator = (bool)vCheckBox.Checked;
  165. }
  166. else
  167. {
  168. vCheckBox.Checked = true;
  169. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  170. }
  171. };
  172. ahCheckBox.Toggled += (s, e) =>
  173. {
  174. scrollView.AutoHideScrollBars = (bool)ahCheckBox.Checked;
  175. hCheckBox.Checked = true;
  176. vCheckBox.Checked = true;
  177. };
  178. app.Add (ahCheckBox);
  179. keepCheckBox.Toggled += (s, e) => scrollView.KeepContentAlwaysInViewport = (bool)keepCheckBox.Checked;
  180. app.Add (keepCheckBox);
  181. var count = 0;
  182. var mousePos = new Label
  183. {
  184. X = Pos.Right (scrollView) + 1,
  185. Y = Pos.AnchorEnd (1),
  186. AutoSize = false,
  187. Width = 50,
  188. Text = "Mouse: "
  189. };
  190. app.Add (mousePos);
  191. Application.MouseEvent += (sender, a) => { mousePos.Text = $"Mouse: ({a.X},{a.Y}) - {a.Flags} {count++}"; };
  192. // Add a progress bar to cause constant redraws
  193. var progress = new ProgressBar { X = Pos.Right (scrollView) + 1, Y = Pos.AnchorEnd (2), Width = 50 };
  194. app.Add (progress);
  195. var pulsing = true;
  196. bool timer ()
  197. {
  198. progress.Pulse ();
  199. return pulsing;
  200. }
  201. Application.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  202. app.Loaded += App_Loaded;
  203. app.Unloaded += app_Unloaded;
  204. Application.Run (app);
  205. app.Loaded -= App_Loaded;
  206. app.Unloaded -= app_Unloaded;
  207. app.Dispose ();
  208. return;
  209. // Local functions
  210. void App_Loaded (object sender, EventArgs args)
  211. {
  212. horizontalRuler.Text =
  213. rule.Repeat ((int)Math.Ceiling (horizontalRuler.Viewport.Width / (double)rule.Length)) [
  214. ..horizontalRuler.Viewport.Width]
  215. + "\n"
  216. + "| ".Repeat (
  217. (int)Math.Ceiling (horizontalRuler.Viewport.Width / (double)rule.Length)
  218. ) [
  219. ..horizontalRuler.Viewport.Width];
  220. verticalRuler.Text =
  221. vrule.Repeat ((int)Math.Ceiling (verticalRuler.Viewport.Height * 2 / (double)rule.Length))
  222. [..(verticalRuler.Viewport.Height * 2)];
  223. }
  224. void app_Unloaded (object sender, EventArgs args)
  225. {
  226. View.Diagnostics = _diagnosticFlags;
  227. pulsing = false;
  228. }
  229. }
  230. }