Scrolling.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #nullable enable
  2. using System.Diagnostics;
  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. private object? _progressTimer = null;
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var win = new Window
  15. {
  16. Title = GetQuitKeyAndName ()
  17. };
  18. var label = new Label { X = 0, Y = 0 };
  19. win.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. win.Add (demoView);
  36. var hCheckBox = new CheckBox
  37. {
  38. X = Pos.X (demoView),
  39. Y = Pos.Bottom (demoView),
  40. Text = "_HorizontalScrollBar.Visible",
  41. CheckedState = demoView.HorizontalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  42. };
  43. win.Add (hCheckBox);
  44. hCheckBox.CheckedStateChanged += (sender, args) => { demoView.HorizontalScrollBar.Visible = args.Value == CheckState.Checked; };
  45. var vCheckBox = new CheckBox
  46. {
  47. X = Pos.Right (hCheckBox) + 3,
  48. Y = Pos.Bottom (demoView),
  49. Text = "_VerticalScrollBar.Visible",
  50. CheckedState = demoView.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  51. };
  52. win.Add (vCheckBox);
  53. vCheckBox.CheckedStateChanged += (sender, args) => { demoView.VerticalScrollBar.Visible = args.Value == CheckState.Checked; };
  54. var ahCheckBox = new CheckBox
  55. {
  56. X = Pos.Left (demoView),
  57. Y = Pos.Bottom (hCheckBox),
  58. Text = "_AutoShow (both)",
  59. CheckedState = demoView.HorizontalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked
  60. };
  61. ahCheckBox.CheckedStateChanging += (s, e) =>
  62. {
  63. demoView.HorizontalScrollBar.AutoShow = e.Result == CheckState.Checked;
  64. demoView.VerticalScrollBar.AutoShow = e.Result == CheckState.Checked;
  65. };
  66. win.Add (ahCheckBox);
  67. demoView.VerticalScrollBar.VisibleChanging += (sender, args) => { vCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked; };
  68. demoView.HorizontalScrollBar.VisibleChanging += (sender, args) =>
  69. {
  70. hCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked;
  71. };
  72. // Add a progress bar to cause constant redraws
  73. var progress = new ProgressBar
  74. {
  75. X = Pos.Center (), Y = Pos.AnchorEnd (), Width = Dim.Fill ()
  76. };
  77. win.Add (progress);
  78. win.Initialized += WinOnInitialized;
  79. win.IsRunningChanged += WinIsRunningChanged;
  80. Application.Run (win);
  81. win.IsRunningChanged -= WinIsRunningChanged;
  82. win.Dispose ();
  83. Application.Shutdown ();
  84. return;
  85. void WinOnInitialized (object? sender, EventArgs e)
  86. {
  87. bool TimerFn ()
  88. {
  89. progress.Pulse ();
  90. return _progressTimer is { };
  91. }
  92. _progressTimer = Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn);
  93. }
  94. void WinIsRunningChanged (object? sender, EventArgs<bool> args)
  95. {
  96. if (!args.Value && _progressTimer is { })
  97. {
  98. Application.RemoveTimeout (_progressTimer);
  99. _progressTimer = null;
  100. }
  101. }
  102. }
  103. }