2
0

ScrollDemo.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Scroll Demo", "Demonstrates using Scroll view.")]
  4. [ScenarioCategory ("Drawing")]
  5. [ScenarioCategory ("Scrolling")]
  6. public class ScrollDemo : Scenario
  7. {
  8. private ViewDiagnosticFlags _diagnosticFlags;
  9. public override void Main ()
  10. {
  11. Application.Init ();
  12. _diagnosticFlags = View.Diagnostics;
  13. Window app = new ()
  14. {
  15. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  16. };
  17. var editor = new Adornments.AdornmentsEditor ();
  18. app.Add (editor);
  19. var view = new FrameView
  20. {
  21. Title = "Demo View",
  22. X = Pos.Right (editor),
  23. Width = Dim.Fill (),
  24. Height = Dim.Fill (),
  25. ColorScheme = Colors.ColorSchemes ["Base"]
  26. };
  27. app.Add (view);
  28. var scroll = new Scroll
  29. {
  30. X = Pos.AnchorEnd (),
  31. Width = Dim.Fill (),
  32. Height = Dim.Fill ()
  33. };
  34. view.Add (scroll);
  35. var rgOrientation = new RadioGroup
  36. {
  37. RadioLabels = ["Vertical", "Horizontal"],
  38. Orientation = Orientation.Horizontal
  39. };
  40. view.Add (rgOrientation);
  41. rgOrientation.SelectedItemChanged += (s, e) =>
  42. {
  43. if (e.SelectedItem == e.PreviousSelectedItem)
  44. {
  45. return;
  46. }
  47. if (rgOrientation.SelectedItem == 0)
  48. {
  49. scroll.Orientation = Orientation.Vertical;
  50. scroll.X = Pos.AnchorEnd ();
  51. scroll.Height = Dim.Fill ();
  52. scroll.Size /= 3;
  53. }
  54. else
  55. {
  56. scroll.Orientation = Orientation.Horizontal;
  57. scroll.Y = Pos.AnchorEnd ();
  58. scroll.Width = Dim.Fill ();
  59. scroll.Size *= 3;
  60. }
  61. };
  62. var lblSize = new Label
  63. {
  64. Y = Pos.Bottom (rgOrientation),
  65. Text = "Size:"
  66. };
  67. view.Add (lblSize);
  68. Buttons.NumericUpDown<int> scrollSize = new Buttons.NumericUpDown<int>
  69. {
  70. Value = scroll.Size,
  71. X = Pos.Right (lblSize) + 1,
  72. Y = Pos.Top (lblSize)
  73. };
  74. view.Add (scrollSize);
  75. scrollSize.ValueChanging += (s, e) =>
  76. {
  77. if (e.NewValue < 0)
  78. {
  79. e.Cancel = true;
  80. return;
  81. }
  82. if (scroll.Size != e.NewValue)
  83. {
  84. scroll.Size = e.NewValue;
  85. }
  86. };
  87. var lblPosition = new Label
  88. {
  89. Y = Pos.Bottom (lblSize),
  90. Text = "Position:"
  91. };
  92. view.Add (lblPosition);
  93. Buttons.NumericUpDown<int> scrollPosition = new Buttons.NumericUpDown<int>
  94. {
  95. Value = scroll.Position,
  96. X = Pos.Right (lblPosition) + 1,
  97. Y = Pos.Top (lblPosition)
  98. };
  99. view.Add (scrollPosition);
  100. scrollPosition.ValueChanging += (s, e) =>
  101. {
  102. if (e.NewValue < 0)
  103. {
  104. e.Cancel = true;
  105. return;
  106. }
  107. if (scroll.Position != e.NewValue)
  108. {
  109. scroll.Position = e.NewValue;
  110. }
  111. if (scroll.Position != e.NewValue)
  112. {
  113. e.Cancel = true;
  114. }
  115. };
  116. var lblSizeChanged = new Label
  117. {
  118. Y = Pos.Bottom (lblPosition) + 1
  119. };
  120. view.Add (lblSizeChanged);
  121. scroll.SizeChanged += (s, e) =>
  122. {
  123. lblSizeChanged.Text = $"SizeChanged event - OldValue: {e.OldValue}; NewValue: {e.NewValue}";
  124. if (scrollSize.Value != e.NewValue)
  125. {
  126. scrollSize.Value = e.NewValue;
  127. }
  128. };
  129. var lblPosChanging = new Label
  130. {
  131. Y = Pos.Bottom (lblSizeChanged)
  132. };
  133. view.Add (lblPosChanging);
  134. scroll.PositionChanging += (s, e) => { lblPosChanging.Text = $"PositionChanging event - OldValue: {e.OldValue}; NewValue: {e.NewValue}"; };
  135. var lblPositionChanged = new Label
  136. {
  137. Y = Pos.Bottom (lblPosChanging)
  138. };
  139. view.Add (lblPositionChanged);
  140. scroll.PositionChanged += (s, e) =>
  141. {
  142. lblPositionChanged.Text = $"PositionChanged event - OldValue: {e.OldValue}; NewValue: {e.NewValue}";
  143. scrollPosition.Value = e.NewValue;
  144. };
  145. var lblScrollFrame = new Label
  146. {
  147. Y = Pos.Bottom (lblPositionChanged) + 1
  148. };
  149. view.Add (lblScrollFrame);
  150. scroll.LayoutComplete += (s, e) => lblScrollFrame.Text = $"Scroll Frame: {scroll.Frame.ToString ()}";
  151. editor.Initialized += (s, e) =>
  152. {
  153. scroll.Size = 40;
  154. editor.ViewToEdit = view;
  155. };
  156. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  157. Application.Run (app);
  158. app.Dispose ();
  159. Application.Shutdown ();
  160. }
  161. }