ContentScrolling.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("Content Scrolling", "Demonstrates using View.Viewport and View.ContentSize to scroll content.")]
  7. [ScenarioCategory ("Layout")]
  8. [ScenarioCategory ("Drawing")]
  9. [ScenarioCategory ("Scrolling")]
  10. public class ContentScrolling : Scenario
  11. {
  12. private ViewDiagnosticFlags _diagnosticFlags;
  13. public class ScrollingDemoView : FrameView
  14. {
  15. public ScrollingDemoView ()
  16. {
  17. Width = Dim.Fill ();
  18. Height = Dim.Fill ();
  19. ColorScheme = Colors.ColorSchemes ["Base"];
  20. Text =
  21. "Text (ScrollingDemoView.Text). This is long text.\nThe second line.\n3\n4\n5th line\nLine 6. This is a longer line that should wrap automatically.";
  22. CanFocus = true;
  23. BorderStyle = LineStyle.Rounded;
  24. Arrangement = ViewArrangement.Fixed;
  25. ContentSize = new (60, 40);
  26. ViewportSettings |= ViewportSettings.ClearContentOnly;
  27. ViewportSettings |= ViewportSettings.ClipContentOnly;
  28. // Things this view knows how to do
  29. AddCommand (Command.ScrollDown, () => ScrollVertical (1));
  30. AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
  31. AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
  32. AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
  33. // Default keybindings for all ListViews
  34. KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
  35. KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
  36. KeyBindings.Add (Key.CursorLeft, Command.ScrollLeft);
  37. KeyBindings.Add (Key.CursorRight, Command.ScrollRight);
  38. // Add a status label to the border that shows Viewport and ContentSize values. Bit of a hack.
  39. // TODO: Move to Padding with controls
  40. Border.Add (new Label { X = 20 });
  41. LayoutComplete += VirtualDemoView_LayoutComplete;
  42. MouseEvent += VirtualDemoView_MouseEvent;
  43. }
  44. [ObsoleteAttribute ("This method is obsolete and will be removed in v2.", false)]
  45. public void SetContentSize (Size contentSize)
  46. {
  47. ContentSize = contentSize;
  48. }
  49. private void VirtualDemoView_MouseEvent (object sender, MouseEventEventArgs e)
  50. {
  51. if (e.MouseEvent.Flags == MouseFlags.WheeledDown)
  52. {
  53. ScrollVertical (1);
  54. return;
  55. }
  56. if (e.MouseEvent.Flags == MouseFlags.WheeledUp)
  57. {
  58. ScrollVertical (-1);
  59. return;
  60. }
  61. if (e.MouseEvent.Flags == MouseFlags.WheeledRight)
  62. {
  63. ScrollHorizontal (1);
  64. return;
  65. }
  66. if (e.MouseEvent.Flags == MouseFlags.WheeledLeft)
  67. {
  68. ScrollHorizontal (-1);
  69. }
  70. }
  71. private void VirtualDemoView_LayoutComplete (object sender, LayoutEventArgs e)
  72. {
  73. Label status = Border.Subviews.OfType<Label> ().FirstOrDefault ();
  74. if (status is { })
  75. {
  76. status.Title = $"Frame: {Frame}\n\nViewport: {Viewport}, ContentSize = {ContentSize}";
  77. status.Width = Border.Frame.Width - status.Frame.X - Border.Thickness.Right;
  78. status.Height = Border.Thickness.Top;
  79. }
  80. SetNeedsDisplay ();
  81. }
  82. }
  83. public override void Main ()
  84. {
  85. Application.Init ();
  86. _diagnosticFlags = View.Diagnostics;
  87. Window app = new ()
  88. {
  89. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  90. // Use a different colorscheme so ViewSettings.ClearContentOnly is obvious
  91. ColorScheme = Colors.ColorSchemes ["Toplevel"]
  92. };
  93. var editor = new AdornmentsEditor
  94. {
  95. AutoSelectViewToEdit = true
  96. };
  97. app.Add (editor);
  98. var view = new ScrollingDemoView
  99. {
  100. Title = "Demo View",
  101. X = Pos.Right (editor),
  102. Width = Dim.Fill (),
  103. Height = Dim.Fill ()
  104. };
  105. app.Add (view);
  106. // Add Scroll Setting UI to Padding
  107. view.Padding.Thickness = new (0, 3, 0, 0);
  108. view.Padding.ColorScheme = Colors.ColorSchemes ["Error"];
  109. var cbAllowNegativeX = new CheckBox
  110. {
  111. Title = "Allow _X < 0",
  112. Y = 0,
  113. CanFocus = false
  114. };
  115. cbAllowNegativeX.Checked = view.ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX);
  116. cbAllowNegativeX.Toggled += AllowNegativeX_Toggled;
  117. void AllowNegativeX_Toggled (object sender, StateEventArgs<bool?> e)
  118. {
  119. if (e.NewValue == true)
  120. {
  121. view.ViewportSettings |= ViewportSettings.AllowNegativeX;
  122. }
  123. else
  124. {
  125. view.ViewportSettings &= ~ViewportSettings.AllowNegativeX;
  126. }
  127. }
  128. view.Padding.Add (cbAllowNegativeX);
  129. var cbAllowNegativeY = new CheckBox
  130. {
  131. Title = "Allow _Y < 0",
  132. X = Pos.Right (cbAllowNegativeX) + 1,
  133. Y = 0,
  134. CanFocus = false
  135. };
  136. cbAllowNegativeY.Checked = view.ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY);
  137. cbAllowNegativeY.Toggled += AllowNegativeY_Toggled;
  138. void AllowNegativeY_Toggled (object sender, StateEventArgs<bool?> e)
  139. {
  140. if (e.NewValue == true)
  141. {
  142. view.ViewportSettings |= ViewportSettings.AllowNegativeY;
  143. }
  144. else
  145. {
  146. view.ViewportSettings &= ~ViewportSettings.AllowNegativeY;
  147. }
  148. }
  149. view.Padding.Add (cbAllowNegativeY);
  150. var cbAllowXGreaterThanContentWidth = new CheckBox
  151. {
  152. Title = "All_ow X > Content",
  153. Y = Pos.Bottom (cbAllowNegativeX),
  154. CanFocus = false
  155. };
  156. cbAllowXGreaterThanContentWidth.Checked = view.ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth);
  157. cbAllowXGreaterThanContentWidth.Toggled += AllowXGreaterThanContentWidth_Toggled;
  158. void AllowXGreaterThanContentWidth_Toggled (object sender, StateEventArgs<bool?> e)
  159. {
  160. if (e.NewValue == true)
  161. {
  162. view.ViewportSettings |= ViewportSettings.AllowXGreaterThanContentWidth;
  163. }
  164. else
  165. {
  166. view.ViewportSettings &= ~ViewportSettings.AllowXGreaterThanContentWidth;
  167. }
  168. }
  169. view.Padding.Add (cbAllowXGreaterThanContentWidth);
  170. var cbAllowYGreaterThanContentHeight = new CheckBox
  171. {
  172. Title = "Allo_w Y > Content",
  173. X = Pos.Right (cbAllowXGreaterThanContentWidth) + 1,
  174. Y = Pos.Bottom (cbAllowNegativeX),
  175. CanFocus = false
  176. };
  177. cbAllowYGreaterThanContentHeight.Checked = view.ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight);
  178. cbAllowYGreaterThanContentHeight.Toggled += AllowYGreaterThanContentHeight_Toggled;
  179. void AllowYGreaterThanContentHeight_Toggled (object sender, StateEventArgs<bool?> e)
  180. {
  181. if (e.NewValue == true)
  182. {
  183. view.ViewportSettings |= ViewportSettings.AllowYGreaterThanContentHeight;
  184. }
  185. else
  186. {
  187. view.ViewportSettings &= ~ViewportSettings.AllowYGreaterThanContentHeight;
  188. }
  189. }
  190. view.Padding.Add (cbAllowYGreaterThanContentHeight);
  191. var labelContentSize = new Label
  192. {
  193. Title = "_ContentSize:",
  194. Y = Pos.Bottom (cbAllowYGreaterThanContentHeight)
  195. };
  196. Buttons.NumericUpDown<int> contentSizeWidth = new Buttons.NumericUpDown<int>
  197. {
  198. Value = view.ContentSize.Width,
  199. X = Pos.Right (labelContentSize) + 1,
  200. Y = Pos.Top (labelContentSize)
  201. };
  202. contentSizeWidth.ValueChanging += ContentSizeWidth_ValueChanged;
  203. void ContentSizeWidth_ValueChanged (object sender, StateEventArgs<int> e)
  204. {
  205. if (e.NewValue < 0)
  206. {
  207. e.Cancel = true;
  208. return;
  209. }
  210. // BUGBUG: set_ContentSize is supposed to be `protected`.
  211. view.SetContentSize (view.ContentSize with { Width = e.NewValue });
  212. }
  213. var labelComma = new Label
  214. {
  215. Title = ",",
  216. X = Pos.Right (contentSizeWidth),
  217. Y = Pos.Top (labelContentSize)
  218. };
  219. Buttons.NumericUpDown<int> contentSizeHeight = new Buttons.NumericUpDown<int>
  220. {
  221. Value = view.ContentSize.Height,
  222. X = Pos.Right (labelComma) + 1,
  223. Y = Pos.Top (labelContentSize),
  224. CanFocus = false
  225. };
  226. contentSizeHeight.ValueChanging += ContentSizeHeight_ValueChanged;
  227. void ContentSizeHeight_ValueChanged (object sender, StateEventArgs<int> e)
  228. {
  229. if (e.NewValue < 0)
  230. {
  231. e.Cancel = true;
  232. return;
  233. }
  234. // BUGBUG: set_ContentSize is supposed to be `protected`.
  235. view.SetContentSize (view.ContentSize with { Height = e.NewValue });
  236. }
  237. var cbClearOnlyVisible = new CheckBox
  238. {
  239. Title = "ClearContentOnly",
  240. X = Pos.Right (contentSizeHeight) + 1,
  241. Y = Pos.Top (labelContentSize),
  242. CanFocus = false
  243. };
  244. cbClearOnlyVisible.Checked = view.ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly);
  245. cbClearOnlyVisible.Toggled += ClearVisibleContentOnly_Toggled;
  246. void ClearVisibleContentOnly_Toggled (object sender, StateEventArgs<bool?> e)
  247. {
  248. if (e.NewValue == true)
  249. {
  250. view.ViewportSettings |= ViewportSettings.ClearContentOnly;
  251. }
  252. else
  253. {
  254. view.ViewportSettings &= ~ViewportSettings.ClearContentOnly;
  255. }
  256. }
  257. var cbDoNotClipContent = new CheckBox
  258. {
  259. Title = "ClipContentOnly",
  260. X = Pos.Right (cbClearOnlyVisible) + 1,
  261. Y = Pos.Top (labelContentSize),
  262. CanFocus = false
  263. };
  264. cbDoNotClipContent.Checked = view.ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly);
  265. cbDoNotClipContent.Toggled += ClipVisibleContentOnly_Toggled;
  266. void ClipVisibleContentOnly_Toggled (object sender, StateEventArgs<bool?> e)
  267. {
  268. if (e.NewValue == true)
  269. {
  270. view.ViewportSettings |= ViewportSettings.ClipContentOnly;
  271. }
  272. else
  273. {
  274. view.ViewportSettings &= ~ViewportSettings.ClipContentOnly;
  275. }
  276. }
  277. view.Padding.Add (labelContentSize, contentSizeWidth, labelComma, contentSizeHeight, cbClearOnlyVisible, cbDoNotClipContent);
  278. // Add demo views to show that things work correctly
  279. var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test TextField" };
  280. var colorPicker = new ColorPicker { Title = "BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
  281. colorPicker.BorderStyle = LineStyle.RoundedDotted;
  282. colorPicker.ColorChanged += (s, e) =>
  283. {
  284. colorPicker.SuperView.ColorScheme = new (colorPicker.SuperView.ColorScheme)
  285. {
  286. Normal = new (
  287. colorPicker.SuperView.ColorScheme.Normal.Foreground,
  288. e.Color
  289. )
  290. };
  291. };
  292. var textView = new TextView
  293. {
  294. X = Pos.Center (),
  295. Y = 10,
  296. Title = "TextView",
  297. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
  298. AllowsTab = false,
  299. Width = 30,
  300. Height = 6 // TODO: Use Dim.Auto
  301. };
  302. textView.Border.Thickness = new (1, 3, 1, 1);
  303. var charMap = new CharMap
  304. {
  305. X = Pos.Center (),
  306. Y = Pos.Bottom (textView) + 1,
  307. Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.ContentSize.Width)),
  308. Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
  309. };
  310. charMap.Accept += (s, e) =>
  311. MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
  312. var buttonAnchored = new Button
  313. {
  314. X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Bottom Right"
  315. };
  316. view.Margin.Data = "Margin";
  317. view.Margin.Thickness = new (0);
  318. view.Border.Data = "Border";
  319. view.Border.Thickness = new (3);
  320. view.Padding.Data = "Padding";
  321. view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
  322. var longLabel = new Label
  323. {
  324. Id = "label2",
  325. X = 0,
  326. Y = 30,
  327. Text =
  328. "This label is long. It should clip to the ContentArea if ClipContentOnly is set. This is a virtual scrolling demo. Use the arrow keys and/or mouse wheel to scroll the content."
  329. };
  330. longLabel.TextFormatter.WordWrap = true;
  331. view.Add (longLabel);
  332. List<object> options = new () { "Option 1", "Option 2", "Option 3" };
  333. Slider slider = new (options)
  334. {
  335. X = 0,
  336. Y = Pos.Bottom (textField) + 1,
  337. Orientation = Orientation.Vertical,
  338. Type = SliderType.Multiple,
  339. AllowEmpty = false,
  340. BorderStyle = LineStyle.Double,
  341. Title = "_Slider"
  342. };
  343. view.Add (slider);
  344. editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
  345. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  346. Application.Run (app);
  347. app.Dispose ();
  348. Application.Shutdown ();
  349. }
  350. }