Scrolling.cs 4.7 KB

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