ScrollDemo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Scroll Demo", "Demonstrates using Scroll view.")]
  5. [ScenarioCategory ("Drawing")]
  6. [ScenarioCategory ("Scrolling")]
  7. public class ScrollDemo : Scenario
  8. {
  9. private ViewDiagnosticFlags _diagnosticFlags;
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. _diagnosticFlags = View.Diagnostics;
  14. Window app = new ()
  15. {
  16. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  17. };
  18. var editor = new AdornmentsEditor ();
  19. app.Add (editor);
  20. var view = new FrameView
  21. {
  22. Title = "Demo View",
  23. X = Pos.Right (editor),
  24. Width = Dim.Fill (),
  25. Height = Dim.Fill (),
  26. ColorScheme = Colors.ColorSchemes ["Base"]
  27. };
  28. app.Add (view);
  29. var scroll = new Scroll
  30. {
  31. X = Pos.AnchorEnd (),
  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. 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. || (e.NewValue
  51. > (scroll.Orientation == Orientation.Vertical
  52. ? scroll.SuperView?.GetContentSize ().Width
  53. : scroll.SuperView?.GetContentSize ().Height)))
  54. {
  55. // TODO: This must be handled in the ScrollSlider if Width and Height being virtual
  56. e.Cancel = true;
  57. return;
  58. }
  59. if (scroll.Orientation == Orientation.Vertical)
  60. {
  61. scroll.Width = e.NewValue;
  62. }
  63. else
  64. {
  65. scroll.Height = e.NewValue;
  66. }
  67. };
  68. var rgOrientation = new RadioGroup
  69. {
  70. Y = Pos.Bottom (lblWidthHeight),
  71. RadioLabels = ["Vertical", "Horizontal"],
  72. Orientation = Orientation.Horizontal
  73. };
  74. view.Add (rgOrientation);
  75. rgOrientation.SelectedItemChanged += (s, e) =>
  76. {
  77. if (e.SelectedItem == e.PreviousSelectedItem)
  78. {
  79. return;
  80. }
  81. if (rgOrientation.SelectedItem == 0)
  82. {
  83. scroll.Orientation = Orientation.Vertical;
  84. scroll.X = Pos.AnchorEnd ();
  85. scroll.Y = 0;
  86. scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scroll.SuperView.GetContentSize ().Width);
  87. scroll.Width = scrollWidthHeight.Value;
  88. scroll.Height = Dim.Fill ();
  89. scroll.Size /= 3;
  90. }
  91. else
  92. {
  93. scroll.Orientation = Orientation.Horizontal;
  94. scroll.X = 0;
  95. scroll.Y = Pos.AnchorEnd ();
  96. scroll.Width = Dim.Fill ();
  97. scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scroll.SuperView.GetContentSize ().Height);
  98. scroll.Height = scrollWidthHeight.Value;
  99. scroll.Size *= 3;
  100. }
  101. };
  102. var lblSize = new Label
  103. {
  104. Y = Pos.Bottom (rgOrientation),
  105. Text = "Size:"
  106. };
  107. view.Add (lblSize);
  108. NumericUpDown<int> scrollSize = new ()
  109. {
  110. Value = scroll.Size,
  111. X = Pos.Right (lblSize) + 1,
  112. Y = Pos.Top (lblSize)
  113. };
  114. view.Add (scrollSize);
  115. scrollSize.ValueChanging += (s, e) =>
  116. {
  117. if (e.NewValue < 0)
  118. {
  119. e.Cancel = true;
  120. return;
  121. }
  122. if (scroll.Size != e.NewValue)
  123. {
  124. scroll.Size = e.NewValue;
  125. }
  126. };
  127. var lblPosition = new Label
  128. {
  129. Y = Pos.Bottom (lblSize),
  130. Text = "Position:"
  131. };
  132. view.Add (lblPosition);
  133. NumericUpDown<int> scrollPosition = new ()
  134. {
  135. Value = scroll.Position,
  136. X = Pos.Right (lblPosition) + 1,
  137. Y = Pos.Top (lblPosition)
  138. };
  139. view.Add (scrollPosition);
  140. scrollPosition.ValueChanging += (s, e) =>
  141. {
  142. if (e.NewValue < 0)
  143. {
  144. e.Cancel = true;
  145. return;
  146. }
  147. if (scroll.Position != e.NewValue)
  148. {
  149. scroll.Position = e.NewValue;
  150. }
  151. if (scroll.Position != e.NewValue)
  152. {
  153. e.Cancel = true;
  154. }
  155. };
  156. var ckbKeepContentInAllViewport = new CheckBox
  157. {
  158. Y = Pos.Bottom (scrollPosition), Text = "KeepContentInAllViewport",
  159. CheckedState = scroll.KeepContentInAllViewport ? CheckState.Checked : CheckState.UnChecked
  160. };
  161. ckbKeepContentInAllViewport.CheckedStateChanging += (s, e) => scroll.KeepContentInAllViewport = e.NewValue == CheckState.Checked;
  162. view.Add (ckbKeepContentInAllViewport);
  163. var lblSizeChanged = new Label
  164. {
  165. Y = Pos.Bottom (ckbKeepContentInAllViewport) + 1
  166. };
  167. view.Add (lblSizeChanged);
  168. scroll.SizeChanged += (s, e) =>
  169. {
  170. lblSizeChanged.Text = $"SizeChanged event - CurrentValue: {e.CurrentValue}";
  171. if (scrollSize.Value != e.CurrentValue)
  172. {
  173. scrollSize.Value = e.CurrentValue;
  174. }
  175. };
  176. var lblPosChanging = new Label
  177. {
  178. Y = Pos.Bottom (lblSizeChanged)
  179. };
  180. view.Add (lblPosChanging);
  181. scroll.PositionChanging += (s, e) => { lblPosChanging.Text = $"PositionChanging event - CurrentValue: {e.CurrentValue}; NewValue: {e.NewValue}"; };
  182. var lblPositionChanged = new Label
  183. {
  184. Y = Pos.Bottom (lblPosChanging)
  185. };
  186. view.Add (lblPositionChanged);
  187. scroll.PositionChanged += (s, e) =>
  188. {
  189. lblPositionChanged.Text = $"PositionChanged event - CurrentValue: {e.CurrentValue}";
  190. scrollPosition.Value = e.CurrentValue;
  191. };
  192. var lblScrollFrame = new Label
  193. {
  194. Y = Pos.Bottom (lblPositionChanged) + 1
  195. };
  196. view.Add (lblScrollFrame);
  197. var lblScrollViewport = new Label
  198. {
  199. Y = Pos.Bottom (lblScrollFrame)
  200. };
  201. view.Add (lblScrollViewport);
  202. var lblScrollContentSize = new Label
  203. {
  204. Y = Pos.Bottom (lblScrollViewport)
  205. };
  206. view.Add (lblScrollContentSize);
  207. scroll.LayoutComplete += (s, e) =>
  208. {
  209. lblScrollFrame.Text = $"Scroll Frame: {scroll.Frame.ToString ()}";
  210. lblScrollViewport.Text = $"Scroll Viewport: {scroll.Viewport.ToString ()}";
  211. lblScrollContentSize.Text = $"Scroll ContentSize: {scroll.GetContentSize ().ToString ()}";
  212. };
  213. editor.Initialized += (s, e) =>
  214. {
  215. scroll.Size = int.Max (app.GetContentSize ().Height * 2, app.GetContentSize ().Width * 2);
  216. editor.ViewToEdit = scroll;
  217. };
  218. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  219. Application.Run (app);
  220. app.Dispose ();
  221. Application.Shutdown ();
  222. }
  223. }