Scrolling.cs 8.6 KB

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