Scrolling.cs 9.9 KB

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