ScrollDemo.cs 9.1 KB

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