2
0

Scrolling.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog {
  4. [ScenarioMetadata (Name: "Scrolling", Description: "Demonstrates ScrollView etc...")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("Bug Repro")]
  7. class Scrolling : Scenario {
  8. class Box10x : View {
  9. int w = 40;
  10. int h = 50;
  11. public bool WantCursorPosition { get; set; } = false;
  12. public Box10x (int x, int y) : base (new Rect (x, y, 20, 10))
  13. {
  14. }
  15. public Size GetContentSize ()
  16. {
  17. return new Size (w, h);
  18. }
  19. public void SetCursorPosition (Point pos)
  20. {
  21. throw new NotImplementedException ();
  22. }
  23. public override void Redraw (Rect bounds)
  24. {
  25. //Point pos = new Point (region.X, region.Y);
  26. Driver.SetAttribute (ColorScheme.Focus);
  27. for (int y = 0; y < h; y++) {
  28. Move (0, y);
  29. Driver.AddStr (y.ToString ());
  30. for (int x = 0; x < w - y.ToString ().Length; x++) {
  31. //Driver.AddRune ((Rune)('0' + (x + y) % 10));
  32. if (y.ToString ().Length < w)
  33. Driver.AddStr (" ");
  34. }
  35. }
  36. //Move (pos.X, pos.Y);
  37. }
  38. }
  39. class Filler : View {
  40. int w = 40;
  41. int h = 50;
  42. public Filler (Rect rect) : base (rect)
  43. {
  44. w = rect.Width;
  45. h = rect.Height;
  46. }
  47. public Size GetContentSize ()
  48. {
  49. return new Size (w, h);
  50. }
  51. public override void Redraw (Rect bounds)
  52. {
  53. Driver.SetAttribute (ColorScheme.Focus);
  54. var f = Frame;
  55. w = 0;
  56. h = 0;
  57. for (int y = 0; y < f.Width; y++) {
  58. Move (0, y);
  59. var nw = 0;
  60. for (int x = 0; x < f.Height; x++) {
  61. Rune r;
  62. switch (x % 3) {
  63. case 0:
  64. var er = y.ToString ().ToCharArray (0, 1) [0];
  65. nw += er.ToString ().Length;
  66. Driver.AddRune (er);
  67. if (y > 9) {
  68. er = y.ToString ().ToCharArray (1, 1) [0];
  69. nw += er.ToString ().Length;
  70. Driver.AddRune (er);
  71. }
  72. r = '.';
  73. break;
  74. case 1:
  75. r = 'o';
  76. break;
  77. default:
  78. r = 'O';
  79. break;
  80. }
  81. Driver.AddRune (r);
  82. nw += Rune.RuneLen (r);
  83. }
  84. if (nw > w)
  85. w = nw;
  86. h = y + 1;
  87. }
  88. }
  89. }
  90. public override void Setup ()
  91. {
  92. Win.X = 3;
  93. Win.Y = 3;
  94. Win.Width = Dim.Fill () - 3;
  95. Win.Height = Dim.Fill () - 3;
  96. var label = new Label ("ScrollView (new Rect (2, 2, 50, 20)) with a 200, 100 ContentSize...") {
  97. X = 0,
  98. Y = 0,
  99. ColorScheme = Colors.Dialog
  100. };
  101. Win.Add (label);
  102. // BUGBUG: ScrollView only supports Absolute Positioning (#72)
  103. var scrollView = new ScrollView (new Rect (2, 2, 50, 20)) {
  104. ColorScheme = Colors.TopLevel,
  105. ContentSize = new Size (200, 100),
  106. //ContentOffset = new Point (0, 0),
  107. ShowVerticalScrollIndicator = true,
  108. ShowHorizontalScrollIndicator = true,
  109. };
  110. const string rule = "|123456789";
  111. var horizontalRuler = new Label ("") {
  112. X = 0,
  113. Y = 0,
  114. Width = Dim.Fill (1), // BUGBUG: I don't think this should be needed; DimFill() should respect container's frame. X does.
  115. ColorScheme = Colors.Error
  116. };
  117. scrollView.Add (horizontalRuler);
  118. const string vrule = "|\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
  119. var verticalRuler = new Label ("") {
  120. X = 0,
  121. Y = 0,
  122. Width = 1,
  123. Height = Dim.Fill (),
  124. ColorScheme = Colors.Error
  125. };
  126. scrollView.Add (verticalRuler);
  127. Win.LayoutComplete += (a) => {
  128. horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)] +
  129. "\n" + "| ".Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
  130. verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height * 2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height * 2)];
  131. };
  132. var pressMeButton = new Button ("Press me!") {
  133. X = 3,
  134. Y = 3,
  135. };
  136. pressMeButton.Clicked += () => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  137. scrollView.Add (pressMeButton);
  138. var aLongButton = new Button ("A very long button. Should be wide enough to demo clipping!") {
  139. X = 3,
  140. Y = 4,
  141. Width = Dim.Fill (6),
  142. };
  143. aLongButton.Clicked += () => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  144. scrollView.Add (aLongButton);
  145. scrollView.Add (new TextField ("This is a test of...") {
  146. X = 3,
  147. Y = 5,
  148. Width = 50,
  149. ColorScheme = Colors.Dialog
  150. });
  151. scrollView.Add (new TextField ("... the emergency broadcast system.") {
  152. X = 3,
  153. Y = 10,
  154. Width = 50,
  155. ColorScheme = Colors.Dialog
  156. });
  157. scrollView.Add (new TextField ("Last line") {
  158. X = 3,
  159. Y = 99,
  160. Width = 50,
  161. ColorScheme = Colors.Dialog
  162. });
  163. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  164. var anchorButton = new Button ("Bottom Right") {
  165. Y = Pos.AnchorEnd () - 1,
  166. };
  167. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  168. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  169. anchorButton.Clicked += () => {
  170. // Ths demonstrates how to have a dynamically sized button
  171. // Each time the button is clicked the button's text gets longer
  172. // The call to Win.LayoutSubviews causes the Computed layout to
  173. // get updated.
  174. anchorButton.Text += "!";
  175. Win.LayoutSubviews ();
  176. };
  177. scrollView.Add (anchorButton);
  178. var hCheckBox = new CheckBox ("Horizontal Scrollbar", scrollView.ShowHorizontalScrollIndicator) {
  179. X = Pos.X(scrollView),
  180. Y = Pos.Bottom(scrollView) + 1,
  181. };
  182. Win.Add (hCheckBox);
  183. var vCheckBox = new CheckBox ("Vertical Scrollbar", scrollView.ShowVerticalScrollIndicator) {
  184. X = Pos.Right (hCheckBox) + 3,
  185. Y = Pos.Bottom (scrollView) + 1,
  186. };
  187. Win.Add (vCheckBox);
  188. var t = "Auto Hide Scrollbars";
  189. var ahCheckBox = new CheckBox (t, scrollView.AutoHideScrollBars) {
  190. X = Pos.Left (scrollView) + (scrollView.Bounds.Width / 2) - (t.Length / 2),
  191. Y = Pos.Bottom (scrollView) + 3,
  192. };
  193. var k = "Keep Content Always In Viewport";
  194. var keepCheckBox = new CheckBox (k, scrollView.AutoHideScrollBars) {
  195. X = Pos.Left (scrollView) + (scrollView.Bounds.Width / 2) - (k.Length / 2),
  196. Y = Pos.Bottom (scrollView) + 4,
  197. };
  198. hCheckBox.Toggled += (_) => {
  199. if (!ahCheckBox.Checked) {
  200. scrollView.ShowHorizontalScrollIndicator = hCheckBox.Checked;
  201. } else {
  202. hCheckBox.Checked = true;
  203. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  204. }
  205. };
  206. vCheckBox.Toggled += (_) => {
  207. if (!ahCheckBox.Checked) {
  208. scrollView.ShowVerticalScrollIndicator = vCheckBox.Checked;
  209. } else {
  210. vCheckBox.Checked = true;
  211. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  212. }
  213. };
  214. ahCheckBox.Toggled += (_) => {
  215. scrollView.AutoHideScrollBars = ahCheckBox.Checked;
  216. hCheckBox.Checked = true;
  217. vCheckBox.Checked = true;
  218. };
  219. Win.Add (ahCheckBox);
  220. keepCheckBox.Toggled += (_) => scrollView.KeepContentAlwaysInViewport = keepCheckBox.Checked;
  221. Win.Add (keepCheckBox);
  222. var scrollView2 = new ScrollView (new Rect (55, 2, 20, 8)) {
  223. ContentSize = new Size (20, 50),
  224. //ContentOffset = new Point (0, 0),
  225. ShowVerticalScrollIndicator = true,
  226. ShowHorizontalScrollIndicator = true
  227. };
  228. var filler = new Filler (new Rect (0, 0, 60, 40));
  229. scrollView2.Add (filler);
  230. scrollView2.DrawContent += (r) => {
  231. scrollView2.ContentSize = filler.GetContentSize ();
  232. };
  233. // This is just to debug the visuals of the scrollview when small
  234. var scrollView3 = new ScrollView (new Rect (55, 15, 3, 3)) {
  235. ContentSize = new Size (100, 100),
  236. ShowVerticalScrollIndicator = true,
  237. ShowHorizontalScrollIndicator = true
  238. };
  239. scrollView3.Add (new Box10x (0, 0));
  240. int count = 0;
  241. var mousePos = new Label ("Mouse: ");
  242. mousePos.X = Pos.Right(scrollView) + 1;
  243. mousePos.Y = Pos.AnchorEnd (1);
  244. mousePos.Width = 50;
  245. Application.RootMouseEvent += delegate (MouseEvent me) {
  246. mousePos.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  247. };
  248. var progress = new ProgressBar ();
  249. progress.X = Pos.Right (scrollView) + 1;
  250. progress.Y = Pos.AnchorEnd (2);
  251. progress.Width = 50;
  252. bool timer (MainLoop caller)
  253. {
  254. progress.Pulse ();
  255. return true;
  256. }
  257. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  258. Win.Add (scrollView, scrollView2, scrollView3, mousePos, progress);
  259. }
  260. }
  261. }