Scrolling.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. scrollView.Add (new Button ("Press me!") {
  133. X = 3,
  134. Y = 3,
  135. Clicked = () => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No")
  136. });
  137. scrollView.Add (new Button ("A very long button. Should be wide enough to demo clipping!") {
  138. X = 3,
  139. Y = 4,
  140. Width = Dim.Fill (6),
  141. Clicked = () => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No")
  142. });
  143. scrollView.Add (new TextField ("This is a test of...") {
  144. X = 3,
  145. Y = 5,
  146. Width = 50,
  147. ColorScheme = Colors.Dialog
  148. });
  149. scrollView.Add (new TextField ("... the emergency broadcast system.") {
  150. X = 3,
  151. Y = 10,
  152. Width = 50,
  153. ColorScheme = Colors.Dialog
  154. });
  155. scrollView.Add (new TextField ("Last line") {
  156. X = 3,
  157. Y = 99,
  158. Width = 50,
  159. ColorScheme = Colors.Dialog
  160. });
  161. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  162. var anchorButton = new Button ("Bottom Right") {
  163. Y = Pos.AnchorEnd () - 1,
  164. };
  165. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  166. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  167. anchorButton.Clicked = () => {
  168. // Ths demonstrates how to have a dynamically sized button
  169. // Each time the button is clicked the button's text gets longer
  170. // The call to Win.LayoutSubviews causes the Computed layout to
  171. // get updated.
  172. anchorButton.Text += "!";
  173. Win.LayoutSubviews ();
  174. };
  175. scrollView.Add (anchorButton);
  176. var hCheckBox = new CheckBox ("Horizontal Scrollbar", scrollView.ShowHorizontalScrollIndicator) {
  177. X = Pos.X(scrollView),
  178. Y = Pos.Bottom(scrollView) + 1,
  179. };
  180. Win.Add (hCheckBox);
  181. var vCheckBox = new CheckBox ("Vertical Scrollbar", scrollView.ShowVerticalScrollIndicator) {
  182. X = Pos.Right (hCheckBox) + 3,
  183. Y = Pos.Bottom (scrollView) + 1,
  184. };
  185. Win.Add (vCheckBox);
  186. var t = "Auto Hide Scrollbars";
  187. var ahCheckBox = new CheckBox (t, scrollView.AutoHideScrollBars) {
  188. X = Pos.Left (scrollView) + scrollView.Bounds.Width / 2 - t.Length / 2,
  189. Y = Pos.Bottom (scrollView) + 3,
  190. };
  191. hCheckBox.Toggled += (previousChecked) => {
  192. if (!ahCheckBox.Checked) {
  193. scrollView.ShowHorizontalScrollIndicator = hCheckBox.Checked;
  194. } else {
  195. hCheckBox.Checked = true;
  196. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  197. }
  198. };
  199. vCheckBox.Toggled += (previousChecked) => {
  200. if (!ahCheckBox.Checked) {
  201. scrollView.ShowVerticalScrollIndicator = vCheckBox.Checked;
  202. } else {
  203. vCheckBox.Checked = true;
  204. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  205. }
  206. };
  207. ahCheckBox.Toggled += (previousChecked) => {
  208. scrollView.AutoHideScrollBars = ahCheckBox.Checked;
  209. hCheckBox.Checked = true;
  210. vCheckBox.Checked = true;
  211. };
  212. Win.Add (ahCheckBox);
  213. var scrollView2 = new ScrollView (new Rect (55, 2, 20, 8)) {
  214. ContentSize = new Size (20, 50),
  215. //ContentOffset = new Point (0, 0),
  216. ShowVerticalScrollIndicator = true,
  217. ShowHorizontalScrollIndicator = true
  218. };
  219. var filler = new Filler (new Rect (0, 0, 60, 40));
  220. scrollView2.Add (filler);
  221. scrollView2.DrawContent = (r) => {
  222. scrollView2.ContentSize = filler.GetContentSize ();
  223. };
  224. // This is just to debug the visuals of the scrollview when small
  225. var scrollView3 = new ScrollView (new Rect (55, 15, 3, 3)) {
  226. ContentSize = new Size (100, 100),
  227. ShowVerticalScrollIndicator = true,
  228. ShowHorizontalScrollIndicator = true
  229. };
  230. scrollView3.Add (new Box10x (0, 0));
  231. int count = 0;
  232. var mousePos = new Label ("Mouse: ");
  233. mousePos.X = Pos.Right(scrollView) + 1;
  234. mousePos.Y = Pos.AnchorEnd (1);
  235. mousePos.Width = 50;
  236. Application.RootMouseEvent += delegate (MouseEvent me) {
  237. mousePos.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  238. };
  239. var progress = new ProgressBar ();
  240. progress.X = Pos.Right (scrollView) + 1;
  241. progress.Y = Pos.AnchorEnd (2);
  242. progress.Width = 50;
  243. bool timer (MainLoop caller)
  244. {
  245. progress.Pulse ();
  246. return true;
  247. }
  248. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  249. Win.Add (scrollView, scrollView2, scrollView3, mousePos, progress);
  250. }
  251. }
  252. }