Scrolling.cs 9.7 KB

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