Scrolling.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using Microsoft.CodeAnalysis.CSharp.Syntax;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Scrolling", "Content scrolling, IScrollBars, etc...")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Scrolling")]
  8. [ScenarioCategory ("Tests")]
  9. public class Scrolling : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var app = new Window
  15. {
  16. Title = GetQuitKeyAndName ()
  17. };
  18. var label = new Label { X = 0, Y = 0 };
  19. app.Add (label);
  20. var demoView = new AllViewsView
  21. {
  22. Id = "demoView",
  23. X = 2,
  24. Y = Pos.Bottom (label) + 1,
  25. Width = Dim.Fill (4),
  26. Height = Dim.Fill (4)
  27. };
  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. // Add a progress bar to cause constant redraws
  81. var progress = new ProgressBar
  82. {
  83. X = Pos.Center (), Y = Pos.AnchorEnd (), Width = Dim.Fill ()
  84. };
  85. app.Add (progress);
  86. var pulsing = true;
  87. app.Initialized += AppOnInitialized;
  88. app.Unloaded += AppUnloaded;
  89. Application.Run (app);
  90. app.Unloaded -= AppUnloaded;
  91. app.Dispose ();
  92. Application.Shutdown ();
  93. return;
  94. void AppOnInitialized (object sender, EventArgs e)
  95. {
  96. bool TimerFn ()
  97. {
  98. progress.Pulse ();
  99. return pulsing;
  100. }
  101. Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn);
  102. }
  103. void AppUnloaded (object sender, EventArgs args) { pulsing = false; }
  104. }
  105. }