Scrolling.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using Microsoft.CodeAnalysis.CSharp.Syntax;
  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 AllViewsView
  20. {
  21. Id = "demoView",
  22. X = 2,
  23. Y = Pos.Bottom (label) + 1,
  24. Width = Dim.Fill (4),
  25. Height = Dim.Fill (4)
  26. };
  27. label.Text =
  28. $"{demoView}\nContentSize: {demoView.GetContentSize ()}\nViewport.Location: {demoView.Viewport.Location}";
  29. demoView.ViewportChanged += (_, _) =>
  30. {
  31. label.Text =
  32. $"{demoView}\nContentSize: {demoView.GetContentSize ()}\nViewport.Location: {demoView.Viewport.Location}";
  33. };
  34. app.Add (demoView);
  35. //// NOTE: This call to EnableScrollBar is technically not needed because the reference
  36. //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
  37. //// NOTE: The call included in this sample to for illustration purposes.
  38. //demoView.EnableScrollBar (Orientation.Horizontal);
  39. var hCheckBox = new CheckBox
  40. {
  41. X = Pos.X (demoView),
  42. Y = Pos.Bottom (demoView),
  43. Text = "_HorizontalScrollBar.Visible",
  44. CheckedState = demoView.HorizontalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  45. };
  46. app.Add (hCheckBox);
  47. hCheckBox.CheckedStateChanged += (sender, args) => { demoView.HorizontalScrollBar.Visible = args.Value == CheckState.Checked; };
  48. //// NOTE: This call to EnableScrollBar is technically not needed because the reference
  49. //// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
  50. //// NOTE: The call included in this sample to for illustration purposes.
  51. //demoView.EnableScrollBar (Orientation.Vertical);
  52. var vCheckBox = new CheckBox
  53. {
  54. X = Pos.Right (hCheckBox) + 3,
  55. Y = Pos.Bottom (demoView),
  56. Text = "_VerticalScrollBar.Visible",
  57. CheckedState = demoView.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked
  58. };
  59. app.Add (vCheckBox);
  60. vCheckBox.CheckedStateChanged += (sender, args) => { demoView.VerticalScrollBar.Visible = args.Value == CheckState.Checked; };
  61. var ahCheckBox = new CheckBox
  62. {
  63. X = Pos.Left (demoView),
  64. Y = Pos.Bottom (hCheckBox),
  65. Text = "_AutoShow (both)",
  66. CheckedState = demoView.HorizontalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked
  67. };
  68. ahCheckBox.CheckedStateChanging += (s, e) =>
  69. {
  70. demoView.HorizontalScrollBar.AutoShow = e.Result == CheckState.Checked;
  71. demoView.VerticalScrollBar.AutoShow = e.Result == CheckState.Checked;
  72. };
  73. app.Add (ahCheckBox);
  74. demoView.VerticalScrollBar.VisibleChanging += (sender, args) => { vCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked; };
  75. demoView.HorizontalScrollBar.VisibleChanging += (sender, args) =>
  76. {
  77. hCheckBox.CheckedState = args.NewValue ? CheckState.Checked : CheckState.UnChecked;
  78. };
  79. // Add a progress bar to cause constant redraws
  80. var progress = new ProgressBar
  81. {
  82. X = Pos.Center (), Y = Pos.AnchorEnd (), Width = Dim.Fill ()
  83. };
  84. app.Add (progress);
  85. var pulsing = true;
  86. app.Initialized += AppOnInitialized;
  87. app.Unloaded += AppUnloaded;
  88. Application.Run (app);
  89. app.Unloaded -= AppUnloaded;
  90. app.Dispose ();
  91. Application.Shutdown ();
  92. return;
  93. void AppOnInitialized (object sender, EventArgs e)
  94. {
  95. bool TimerFn ()
  96. {
  97. progress.Pulse ();
  98. return pulsing;
  99. }
  100. Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn);
  101. }
  102. void AppUnloaded (object sender, EventArgs args) { pulsing = false; }
  103. }
  104. }