Scrolling.cs 9.3 KB

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