Scrolling.cs 8.5 KB

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