ScrollBarDemo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("ScrollBar Demo", "Demonstrates using ScrollBar view.")]
  5. [ScenarioCategory ("Drawing")]
  6. [ScenarioCategory ("Scrolling")]
  7. public class ScrollBarDemo : 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 scrollBar = new ScrollBar
  30. {
  31. X = Pos.AnchorEnd (),
  32. Height = Dim.Fill (),
  33. };
  34. view.Add (scrollBar);
  35. var lblWidthHeight = new Label
  36. {
  37. Text = "Width/Height:"
  38. };
  39. view.Add (lblWidthHeight);
  40. NumericUpDown<int> scrollWidthHeight = new ()
  41. {
  42. Value = scrollBar.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. > (scrollBar.Orientation == Orientation.Vertical
  52. ? scrollBar.SuperView?.GetContentSize ().Width
  53. : scrollBar.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 (scrollBar.Orientation == Orientation.Vertical)
  60. {
  61. scrollBar.Width = e.NewValue;
  62. }
  63. else
  64. {
  65. scrollBar.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. scrollBar.Orientation = Orientation.Vertical;
  84. scrollBar.X = Pos.AnchorEnd ();
  85. scrollBar.Y = 0;
  86. scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView.GetContentSize ().Width);
  87. scrollBar.Width = scrollWidthHeight.Value;
  88. scrollBar.Height = Dim.Fill ();
  89. scrollBar.Size /= 3;
  90. }
  91. else
  92. {
  93. scrollBar.Orientation = Orientation.Horizontal;
  94. scrollBar.X = 0;
  95. scrollBar.Y = Pos.AnchorEnd ();
  96. scrollBar.Width = Dim.Fill ();
  97. scrollWidthHeight.Value = Math.Min (scrollWidthHeight.Value, scrollBar.SuperView.GetContentSize ().Height);
  98. scrollBar.Height = scrollWidthHeight.Value;
  99. scrollBar.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 = scrollBar.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 (scrollBar.Size != e.NewValue)
  123. {
  124. scrollBar.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 = scrollBar.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 (scrollBar.Position != e.NewValue)
  148. {
  149. scrollBar.Position = e.NewValue;
  150. }
  151. if (scrollBar.Position != e.NewValue)
  152. {
  153. e.Cancel = true;
  154. }
  155. };
  156. var lblSizeChanged = new Label
  157. {
  158. Y = Pos.Bottom (lblPosition) + 1
  159. };
  160. view.Add (lblSizeChanged);
  161. scrollBar.SizeChanged += (s, e) =>
  162. {
  163. lblSizeChanged.Text = $"SizeChanged event - CurrentValue: {e.CurrentValue}";
  164. if (scrollSize.Value != e.CurrentValue)
  165. {
  166. scrollSize.Value = e.CurrentValue;
  167. }
  168. };
  169. var lblPosChanging = new Label
  170. {
  171. Y = Pos.Bottom (lblSizeChanged)
  172. };
  173. view.Add (lblPosChanging);
  174. scrollBar.PositionChanging += (s, e) => { lblPosChanging.Text = $"PositionChanging event - CurrentValue: {e.CurrentValue}; NewValue: {e.NewValue}"; };
  175. var lblPositionChanged = new Label
  176. {
  177. Y = Pos.Bottom (lblPosChanging)
  178. };
  179. view.Add (lblPositionChanged);
  180. scrollBar.PositionChanged += (s, e) =>
  181. {
  182. lblPositionChanged.Text = $"PositionChanged event - CurrentValue: {e.CurrentValue}";
  183. scrollPosition.Value = e.CurrentValue;
  184. };
  185. var lblScrollFrame = new Label
  186. {
  187. Y = Pos.Bottom (lblPositionChanged) + 1
  188. };
  189. view.Add (lblScrollFrame);
  190. var lblScrollViewport = new Label
  191. {
  192. Y = Pos.Bottom (lblScrollFrame)
  193. };
  194. view.Add (lblScrollViewport);
  195. var lblScrollContentSize = new Label
  196. {
  197. Y = Pos.Bottom (lblScrollViewport)
  198. };
  199. view.Add (lblScrollContentSize);
  200. scrollBar.LayoutComplete += (s, e) =>
  201. {
  202. lblScrollFrame.Text = $"ScrollBar Frame: {scrollBar.Frame.ToString ()}";
  203. lblScrollViewport.Text = $"ScrollBar Viewport: {scrollBar.Viewport.ToString ()}";
  204. lblScrollContentSize.Text = $"ScrollBar ContentSize: {scrollBar.GetContentSize ().ToString ()}";
  205. };
  206. editor.Initialized += (s, e) =>
  207. {
  208. scrollBar.Size = int.Max (app.GetContentSize ().Height * 2, app.GetContentSize ().Width * 2);
  209. editor.ViewToEdit = scrollBar;
  210. };
  211. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  212. Application.Run (app);
  213. app.Dispose ();
  214. Application.Shutdown ();
  215. }
  216. }