Scrolling.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios {
  4. [ScenarioMetadata (Name: "Scrolling", Description: "Demonstrates ScrollView etc...")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("ScrollView")]
  7. public 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. // Offset Win to stress clipping
  93. Win.X = 1;
  94. Win.Y = 1;
  95. Win.Width = Dim.Fill (1);
  96. Win.Height = Dim.Fill (1);
  97. var label = new Label () {
  98. X = 0,
  99. Y = 0
  100. };
  101. Win.Add (label);
  102. var scrollView = new ScrollView {
  103. Id = "scrollView",
  104. X = 2,
  105. Y = Pos.Bottom(label) + 1,
  106. Width = 50,
  107. Height = 20,
  108. ColorScheme = Colors.TopLevel,
  109. ContentSize = new Size (200, 100),
  110. //ContentOffset = new Point (0, 0),
  111. ShowVerticalScrollIndicator = true,
  112. ShowHorizontalScrollIndicator = true,
  113. };
  114. label.Text = $"{scrollView}\nContentSize: {scrollView.ContentSize}\nContentOffset: {scrollView.ContentOffset}";
  115. const string rule = "0123456789";
  116. var horizontalRuler = new Label () {
  117. X = 0,
  118. Y = 0,
  119. Width = Dim.Fill (), // FIXED: I don't think this should be needed; DimFill() should respect container's frame. X does.
  120. Height = 2,
  121. ColorScheme = Colors.Error,
  122. AutoSize = false
  123. };
  124. scrollView.Add (horizontalRuler);
  125. const string vrule = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
  126. var verticalRuler = new Label () {
  127. X = 0,
  128. Y = 0,
  129. Width = 1,
  130. Height = Dim.Fill (),
  131. ColorScheme = Colors.Error,
  132. AutoSize = false
  133. };
  134. scrollView.Add (verticalRuler);
  135. void Top_Loaded (object sender, EventArgs args)
  136. {
  137. // BUGBUG: v2 - this broke somehow - fix later
  138. //horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)] +
  139. // "\n" + "| ".Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
  140. //verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height * 2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height * 2)];
  141. Application.Top.Loaded -= Top_Loaded;
  142. }
  143. Application.Top.Loaded += Top_Loaded;
  144. var pressMeButton = new Button ("Press me!") {
  145. X = 3,
  146. Y = 3,
  147. };
  148. pressMeButton.Clicked += (s,e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  149. scrollView.Add (pressMeButton);
  150. var aLongButton = new Button ("A very long button. Should be wide enough to demo clipping!") {
  151. X = 3,
  152. Y = 4,
  153. Width = Dim.Fill (3),
  154. };
  155. aLongButton.Clicked += (s,e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  156. scrollView.Add (aLongButton);
  157. scrollView.Add (new TextField ("This is a test of...") {
  158. X = 3,
  159. Y = 5,
  160. Width = 50,
  161. ColorScheme = Colors.Dialog
  162. });
  163. scrollView.Add (new TextField ("... the emergency broadcast system.") {
  164. X = 3,
  165. Y = 10,
  166. Width = 50,
  167. ColorScheme = Colors.Dialog
  168. });
  169. scrollView.Add (new TextField ("Last line") {
  170. X = 3,
  171. Y = 99,
  172. Width = 50,
  173. ColorScheme = Colors.Dialog
  174. });
  175. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  176. var anchorButton = new Button ("Bottom Right") {
  177. Y = Pos.AnchorEnd () - 1,
  178. };
  179. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  180. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  181. anchorButton.Clicked += (s,e) => {
  182. // Ths demonstrates how to have a dynamically sized button
  183. // Each time the button is clicked the button's text gets longer
  184. // The call to Win.LayoutSubviews causes the Computed layout to
  185. // get updated.
  186. anchorButton.Text += "!";
  187. Win.LayoutSubviews ();
  188. };
  189. scrollView.Add (anchorButton);
  190. Win.Add (scrollView);
  191. var hCheckBox = new CheckBox ("Horizontal Scrollbar", scrollView.ShowHorizontalScrollIndicator) {
  192. X = Pos.X (scrollView),
  193. Y = Pos.Bottom (scrollView),
  194. };
  195. Win.Add (hCheckBox);
  196. var vCheckBox = new CheckBox ("Vertical Scrollbar", scrollView.ShowVerticalScrollIndicator) {
  197. X = Pos.Right (hCheckBox) + 3,
  198. Y = Pos.Bottom (scrollView),
  199. };
  200. Win.Add (vCheckBox);
  201. var t = "Auto Hide Scrollbars";
  202. var ahCheckBox = new CheckBox (t, scrollView.AutoHideScrollBars) {
  203. X = Pos.Left (scrollView),
  204. Y = Pos.Bottom (hCheckBox),
  205. };
  206. var k = "Keep Content Always In Viewport";
  207. var keepCheckBox = new CheckBox (k, scrollView.AutoHideScrollBars) {
  208. X = Pos.Left (scrollView),
  209. Y = Pos.Bottom (ahCheckBox),
  210. };
  211. hCheckBox.Toggled += (s, e) => {
  212. if (ahCheckBox.Checked == false) {
  213. scrollView.ShowHorizontalScrollIndicator = (bool)hCheckBox.Checked;
  214. } else {
  215. hCheckBox.Checked = true;
  216. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  217. }
  218. };
  219. vCheckBox.Toggled += (s, e) => {
  220. if (ahCheckBox.Checked == false) {
  221. scrollView.ShowVerticalScrollIndicator = (bool)vCheckBox.Checked;
  222. } else {
  223. vCheckBox.Checked = true;
  224. MessageBox.Query ("Message", "Disable Auto Hide Scrollbars first.", "Ok");
  225. }
  226. };
  227. ahCheckBox.Toggled += (s, e) => {
  228. scrollView.AutoHideScrollBars = (bool)ahCheckBox.Checked;
  229. hCheckBox.Checked = true;
  230. vCheckBox.Checked = true;
  231. };
  232. Win.Add (ahCheckBox);
  233. keepCheckBox.Toggled += (s,e) => scrollView.KeepContentAlwaysInViewport = (bool)keepCheckBox.Checked;
  234. Win.Add (keepCheckBox);
  235. //var scrollView2 = new ScrollView (new Rect (55, 2, 20, 8)) {
  236. // ContentSize = new Size (20, 50),
  237. // //ContentOffset = new Point (0, 0),
  238. // ShowVerticalScrollIndicator = true,
  239. // ShowHorizontalScrollIndicator = true
  240. //};
  241. //var filler = new Filler (new Rect (0, 0, 60, 40));
  242. //scrollView2.Add (filler);
  243. //scrollView2.DrawContent += (s,e) => {
  244. // scrollView2.ContentSize = filler.GetContentSize ();
  245. //};
  246. //Win.Add (scrollView2);
  247. //// This is just to debug the visuals of the scrollview when small
  248. //var scrollView3 = new ScrollView (new Rect (55, 15, 3, 3)) {
  249. // ContentSize = new Size (100, 100),
  250. // ShowVerticalScrollIndicator = true,
  251. // ShowHorizontalScrollIndicator = true
  252. //};
  253. //scrollView3.Add (new Box10x (0, 0));
  254. //Win.Add (scrollView3);
  255. int count = 0;
  256. var mousePos = new Label ("Mouse: ") {
  257. X = Pos.Right (scrollView) + 1,
  258. Y = Pos.AnchorEnd (1),
  259. Width = 50,
  260. };
  261. Win.Add (mousePos);
  262. Application.RootMouseEvent += delegate (MouseEvent me) {
  263. mousePos.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
  264. };
  265. var progress = new ProgressBar {
  266. X = Pos.Right (scrollView) + 1,
  267. Y = Pos.AnchorEnd (2),
  268. Width = 50
  269. };
  270. Win.Add (progress);
  271. bool pulsing = true;
  272. bool timer (MainLoop caller)
  273. {
  274. progress.Pulse ();
  275. return pulsing;
  276. }
  277. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  278. void Top_Unloaded (object sender, EventArgs args)
  279. {
  280. pulsing = false;
  281. Application.Top.Unloaded -= Top_Unloaded;
  282. }
  283. Application.Top.Unloaded += Top_Unloaded;
  284. }
  285. }
  286. }