ScrollDemo.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = 1,
  32. Height = Dim.Fill ()
  33. };
  34. view.Add (scroll);
  35. var lblWidthHeight = new Label
  36. {
  37. Text = "Width/Height:"
  38. };
  39. view.Add (lblWidthHeight);
  40. Buttons.NumericUpDown<int> scrollWidthHeight = new()
  41. {
  42. Value = scroll.Frame.Width,
  43. X = Pos.Right (lblWidthHeight) + 1,
  44. Y = Pos.Top (lblWidthHeight)
  45. };
  46. view.Add (scrollWidthHeight);
  47. scrollWidthHeight.ValueChanging += (s, e) =>
  48. {
  49. if (e.NewValue < 1)
  50. {
  51. e.Cancel = true;
  52. return;
  53. }
  54. if (scroll.Orientation == Orientation.Vertical)
  55. {
  56. scroll.Width = e.NewValue;
  57. }
  58. else
  59. {
  60. scroll.Height = e.NewValue;
  61. }
  62. };
  63. var rgOrientation = new RadioGroup
  64. {
  65. Y = Pos.Bottom (lblWidthHeight),
  66. RadioLabels = ["Vertical", "Horizontal"],
  67. Orientation = Orientation.Horizontal
  68. };
  69. view.Add (rgOrientation);
  70. rgOrientation.SelectedItemChanged += (s, e) =>
  71. {
  72. if (e.SelectedItem == e.PreviousSelectedItem)
  73. {
  74. return;
  75. }
  76. if (rgOrientation.SelectedItem == 0)
  77. {
  78. scroll.Orientation = Orientation.Vertical;
  79. scroll.X = Pos.AnchorEnd ();
  80. scroll.Width = scrollWidthHeight.Value;
  81. scroll.Height = Dim.Fill ();
  82. scroll.Size /= 3;
  83. }
  84. else
  85. {
  86. scroll.Orientation = Orientation.Horizontal;
  87. scroll.Y = Pos.AnchorEnd ();
  88. scroll.Width = Dim.Fill ();
  89. scroll.Height = scrollWidthHeight.Value;
  90. scroll.Size *= 3;
  91. }
  92. };
  93. var lblSize = new Label
  94. {
  95. Y = Pos.Bottom (rgOrientation),
  96. Text = "Size:"
  97. };
  98. view.Add (lblSize);
  99. Buttons.NumericUpDown<int> scrollSize = new()
  100. {
  101. Value = scroll.Size,
  102. X = Pos.Right (lblSize) + 1,
  103. Y = Pos.Top (lblSize)
  104. };
  105. view.Add (scrollSize);
  106. scrollSize.ValueChanging += (s, e) =>
  107. {
  108. if (e.NewValue < 0)
  109. {
  110. e.Cancel = true;
  111. return;
  112. }
  113. if (scroll.Size != e.NewValue)
  114. {
  115. scroll.Size = e.NewValue;
  116. }
  117. };
  118. var lblPosition = new Label
  119. {
  120. Y = Pos.Bottom (lblSize),
  121. Text = "Position:"
  122. };
  123. view.Add (lblPosition);
  124. Buttons.NumericUpDown<int> scrollPosition = new()
  125. {
  126. Value = scroll.Position,
  127. X = Pos.Right (lblPosition) + 1,
  128. Y = Pos.Top (lblPosition)
  129. };
  130. view.Add (scrollPosition);
  131. scrollPosition.ValueChanging += (s, e) =>
  132. {
  133. if (e.NewValue < 0)
  134. {
  135. e.Cancel = true;
  136. return;
  137. }
  138. if (scroll.Position != e.NewValue)
  139. {
  140. scroll.Position = e.NewValue;
  141. }
  142. if (scroll.Position != e.NewValue)
  143. {
  144. e.Cancel = true;
  145. }
  146. };
  147. var lblSizeChanged = new Label
  148. {
  149. Y = Pos.Bottom (lblPosition) + 1
  150. };
  151. view.Add (lblSizeChanged);
  152. scroll.SizeChanged += (s, e) =>
  153. {
  154. lblSizeChanged.Text = $"SizeChanged event - OldValue: {e.OldValue}; NewValue: {e.NewValue}";
  155. if (scrollSize.Value != e.NewValue)
  156. {
  157. scrollSize.Value = e.NewValue;
  158. }
  159. };
  160. var lblPosChanging = new Label
  161. {
  162. Y = Pos.Bottom (lblSizeChanged)
  163. };
  164. view.Add (lblPosChanging);
  165. scroll.PositionChanging += (s, e) => { lblPosChanging.Text = $"PositionChanging event - OldValue: {e.OldValue}; NewValue: {e.NewValue}"; };
  166. var lblPositionChanged = new Label
  167. {
  168. Y = Pos.Bottom (lblPosChanging)
  169. };
  170. view.Add (lblPositionChanged);
  171. scroll.PositionChanged += (s, e) =>
  172. {
  173. lblPositionChanged.Text = $"PositionChanged event - OldValue: {e.OldValue}; NewValue: {e.NewValue}";
  174. scrollPosition.Value = e.NewValue;
  175. };
  176. var lblScrollFrame = new Label
  177. {
  178. Y = Pos.Bottom (lblPositionChanged) + 1
  179. };
  180. view.Add (lblScrollFrame);
  181. scroll.LayoutComplete += (s, e) => lblScrollFrame.Text = $"Scroll Frame: {scroll.Frame.ToString ()}";
  182. editor.Initialized += (s, e) =>
  183. {
  184. scroll.Size = 40;
  185. editor.ViewToEdit = view;
  186. };
  187. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  188. Application.Run (app);
  189. app.Dispose ();
  190. Application.Shutdown ();
  191. }
  192. }