2
0

Scrolling.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Scrolling", "Content scrolling, IScrollBars, etc...")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("Scrolling")]
  7. [ScenarioCategory ("Tests")]
  8. public class Scrolling : Scenario
  9. {
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. var app = new Window
  14. {
  15. Title = GetQuitKeyAndName ()
  16. };
  17. var label = new Label { X = 0, Y = 0 };
  18. app.Add (label);
  19. var demoView = new DemoView
  20. {
  21. Id = "demoView",
  22. X = 2,
  23. Y = Pos.Bottom (label) + 1,
  24. Width = 60,
  25. Height = 20
  26. };
  27. demoView.SetContentSize (new (80, 25));
  28. label.Text =
  29. $"{demoView}\nContentSize: {demoView.GetContentSize ()}\nViewport.Location: {demoView.Viewport.Location}";
  30. demoView.ViewportChanged += (_, _) =>
  31. {
  32. label.Text =
  33. $"{demoView}\nContentSize: {demoView.GetContentSize ()}\nViewport.Location: {demoView.Viewport.Location}";
  34. };
  35. app.Add (demoView);
  36. //// NOTE: This call to EnableScrollBar is technically not needed because the reference
  37. //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
  38. //// NOTE: The call included in this sample to for illustration purposes.
  39. //demoView.EnableScrollBar (Orientation.Horizontal);
  40. var hCheckBox = new CheckBox
  41. {
  42. X = Pos.X (demoView),
  43. Y = Pos.Bottom (demoView),
  44. Text = "_HorizontalScrollBar.Visible",
  45. CheckedState = demoView.HorizontalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  46. };
  47. app.Add (hCheckBox);
  48. hCheckBox.CheckedStateChanged += (sender, args) => { demoView.HorizontalScrollBar.Visible = args.CurrentValue == CheckState.Checked; };
  49. //// NOTE: This call to EnableScrollBar is technically not needed because the reference
  50. //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
  51. //// NOTE: The call included in this sample to for illustration purposes.
  52. //demoView.EnableScrollBar (Orientation.Vertical);
  53. var vCheckBox = new CheckBox
  54. {
  55. X = Pos.Right (hCheckBox) + 3,
  56. Y = Pos.Bottom (demoView),
  57. Text = "_VerticalScrollBar.Visible",
  58. CheckedState = demoView.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  59. };
  60. app.Add (vCheckBox);
  61. vCheckBox.CheckedStateChanged += (sender, args) => { demoView.VerticalScrollBar.Visible = args.CurrentValue == CheckState.Checked; };
  62. var ahCheckBox = new CheckBox
  63. {
  64. X = Pos.Left (demoView),
  65. Y = Pos.Bottom (hCheckBox),
  66. Text = "_AutoShow (both)",
  67. CheckedState = demoView.HorizontalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked
  68. };
  69. ahCheckBox.CheckedStateChanging += (s, e) =>
  70. {
  71. demoView.HorizontalScrollBar.AutoShow = e.NewValue == CheckState.Checked;
  72. demoView.VerticalScrollBar.AutoShow = e.NewValue == CheckState.Checked;
  73. };
  74. app.Add (ahCheckBox);
  75. demoView.VerticalScrollBar.VisibleChanging += (sender, args) => { vCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked; };
  76. demoView.HorizontalScrollBar.VisibleChanging += (sender, args) =>
  77. {
  78. hCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked;
  79. };
  80. var count = 0;
  81. var mousePos = new Label
  82. {
  83. X = Pos.Right (demoView) + 1,
  84. Y = Pos.AnchorEnd (1),
  85. Width = 50,
  86. Text = "Mouse: "
  87. };
  88. app.Add (mousePos);
  89. Application.MouseEvent += (sender, a) => { mousePos.Text = $"Mouse: ({a.Position}) - {a.Flags} {count++}"; };
  90. // Add a progress bar to cause constant redraws
  91. var progress = new ProgressBar { X = Pos.Right (demoView) + 1, Y = Pos.AnchorEnd (2), Width = 50 };
  92. app.Add (progress);
  93. var pulsing = true;
  94. bool timer ()
  95. {
  96. progress.Pulse ();
  97. return pulsing;
  98. }
  99. Application.AddTimeout (TimeSpan.FromMilliseconds (300), timer);
  100. app.Unloaded += AppUnloaded;
  101. Application.Run (app);
  102. app.Unloaded -= AppUnloaded;
  103. app.Dispose ();
  104. Application.Shutdown ();
  105. return;
  106. void AppUnloaded (object sender, EventArgs args) { pulsing = false; }
  107. }
  108. }
  109. public class DemoView : View
  110. {
  111. public DemoView ()
  112. {
  113. base.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  114. CanFocus = true;
  115. BorderStyle = LineStyle.Heavy;
  116. Arrangement = ViewArrangement.Resizable;
  117. Initialized += OnInitialized;
  118. HorizontalScrollBar.AutoShow = true;
  119. VerticalScrollBar.AutoShow = true;
  120. }
  121. private void OnInitialized (object sender, EventArgs e)
  122. {
  123. var rulerView = new View
  124. {
  125. Height = Dim.Fill (),
  126. Width = Dim.Fill ()
  127. };
  128. rulerView.Border!.Thickness = new (1);
  129. rulerView.Border.LineStyle = LineStyle.None;
  130. rulerView.Border.Diagnostics = ViewDiagnosticFlags.Ruler;
  131. rulerView.Border.ColorScheme = Colors.ColorSchemes ["Error"];
  132. Add (rulerView);
  133. var pressMeButton = new Button
  134. {
  135. X = 1,
  136. Y = 1,
  137. Text = "Press me!"
  138. };
  139. pressMeButton.Accepting += (s, e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  140. Add (pressMeButton);
  141. var aLongButton = new Button
  142. {
  143. X = Pos.Right (pressMeButton),
  144. Y = Pos.Bottom (pressMeButton),
  145. Text = "A very long button. Should be wide enough to demo clipping!"
  146. };
  147. aLongButton.Accepting += (s, e) => MessageBox.Query (20, 7, "MessageBox", "Neat?", "Yes", "No");
  148. Add (aLongButton);
  149. Add (
  150. new TextField
  151. {
  152. X = Pos.Left (pressMeButton),
  153. Y = Pos.Bottom (aLongButton) + 1,
  154. Width = 50,
  155. ColorScheme = Colors.ColorSchemes ["Dialog"],
  156. Text = "This is a test of..."
  157. }
  158. );
  159. Add (
  160. new TextField
  161. {
  162. X = Pos.Left (pressMeButton),
  163. Y = Pos.Bottom (aLongButton) + 3,
  164. Width = 50,
  165. ColorScheme = Colors.ColorSchemes ["Dialog"],
  166. Text = "... the emergency broadcast system."
  167. }
  168. );
  169. Add (
  170. new TextField
  171. {
  172. X = Pos.Left (pressMeButton),
  173. Y = 40,
  174. Width = 50,
  175. ColorScheme = Colors.ColorSchemes ["Error"],
  176. Text = "Last line"
  177. }
  178. );
  179. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  180. var anchorButton = new Button
  181. {
  182. X = Pos.AnchorEnd (),
  183. Y = Pos.AnchorEnd (),
  184. Text = "Bottom Right"
  185. };
  186. anchorButton.Accepting += (s, e) =>
  187. {
  188. // This demonstrates how to have a dynamically sized button
  189. // Each time the button is clicked the button's text gets longer
  190. anchorButton.Text += "!";
  191. };
  192. Add (anchorButton);
  193. }
  194. }