Scrolling.cs 8.5 KB

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