ContentScrolling.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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.GetContentSize () 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. SetContentSize (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. private void VirtualDemoView_MouseEvent (object sender, MouseEventEventArgs e)
  45. {
  46. if (e.MouseEvent.Flags == MouseFlags.WheeledDown)
  47. {
  48. ScrollVertical (1);
  49. return;
  50. }
  51. if (e.MouseEvent.Flags == MouseFlags.WheeledUp)
  52. {
  53. ScrollVertical (-1);
  54. return;
  55. }
  56. if (e.MouseEvent.Flags == MouseFlags.WheeledRight)
  57. {
  58. ScrollHorizontal (1);
  59. return;
  60. }
  61. if (e.MouseEvent.Flags == MouseFlags.WheeledLeft)
  62. {
  63. ScrollHorizontal (-1);
  64. }
  65. }
  66. private void VirtualDemoView_LayoutComplete (object sender, LayoutEventArgs e)
  67. {
  68. Label status = Border.Subviews.OfType<Label> ().FirstOrDefault ();
  69. if (status is { })
  70. {
  71. status.Title = $"Frame: {Frame}\n\nViewport: {Viewport}, ContentSize = {GetContentSize ()}";
  72. status.Width = Border.Frame.Width - status.Frame.X - Border.Thickness.Right;
  73. status.Height = Border.Thickness.Top;
  74. }
  75. SetNeedsDisplay ();
  76. }
  77. }
  78. public override void Main ()
  79. {
  80. Application.Init ();
  81. _diagnosticFlags = View.Diagnostics;
  82. Window app = new ()
  83. {
  84. Title = GetQuitKeyAndName (),
  85. // Use a different colorscheme so ViewSettings.ClearContentOnly is obvious
  86. ColorScheme = Colors.ColorSchemes ["Toplevel"]
  87. };
  88. var editor = new AdornmentsEditor
  89. {
  90. AutoSelectViewToEdit = true
  91. };
  92. app.Add (editor);
  93. var view = new ScrollingDemoView
  94. {
  95. Title = "Demo View",
  96. X = Pos.Right (editor),
  97. Width = Dim.Fill (),
  98. Height = Dim.Fill ()
  99. };
  100. app.Add (view);
  101. // Add Scroll Setting UI to Padding
  102. view.Padding.Thickness = view.Padding.Thickness with { Top = view.Padding.Thickness.Top + 4 };
  103. //view.Padding.ColorScheme = Colors.ColorSchemes ["Error"];
  104. var cbAllowNegativeX = new CheckBox
  105. {
  106. Title = "Allow _X < 0",
  107. Y = 0,
  108. CanFocus = false
  109. };
  110. cbAllowNegativeX.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX) ? CheckState.Checked : CheckState.UnChecked;
  111. view.Padding.Add (cbAllowNegativeX);
  112. var cbAllowNegativeY = new CheckBox
  113. {
  114. Title = "Allow _Y < 0",
  115. X = Pos.Right (cbAllowNegativeX) + 1,
  116. Y = 0,
  117. CanFocus = false
  118. };
  119. cbAllowNegativeY.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY) ? CheckState.Checked : CheckState.UnChecked;
  120. view.Padding.Add (cbAllowNegativeY);
  121. var cbAllowXGreaterThanContentWidth = new CheckBox
  122. {
  123. Title = "All_ow X > Content",
  124. Y = Pos.Bottom (cbAllowNegativeX),
  125. CanFocus = false
  126. };
  127. cbAllowXGreaterThanContentWidth.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth) ? CheckState.Checked : CheckState.UnChecked;
  128. view.Padding.Add (cbAllowXGreaterThanContentWidth);
  129. void AllowNegativeX_Toggle (object sender, CancelEventArgs<CheckState> e)
  130. {
  131. if (e.NewValue == CheckState.Checked)
  132. {
  133. view.ViewportSettings |= ViewportSettings.AllowNegativeX;
  134. }
  135. else
  136. {
  137. view.ViewportSettings &= ~ViewportSettings.AllowNegativeX;
  138. }
  139. SetHorizontalScrollBar (e.NewValue, cbAllowXGreaterThanContentWidth.CheckedState);
  140. }
  141. void AllowXGreaterThanContentWidth_Toggle (object sender, CancelEventArgs<CheckState> e)
  142. {
  143. if (e.NewValue == CheckState.Checked)
  144. {
  145. view.ViewportSettings |= ViewportSettings.AllowXGreaterThanContentWidth;
  146. }
  147. else
  148. {
  149. view.ViewportSettings &= ~ViewportSettings.AllowXGreaterThanContentWidth;
  150. }
  151. SetHorizontalScrollBar (e.NewValue, cbAllowNegativeX.CheckedState);
  152. }
  153. var cbAllowYGreaterThanContentHeight = new CheckBox
  154. {
  155. Title = "Allo_w Y > Content",
  156. X = Pos.Right (cbAllowXGreaterThanContentWidth) + 1,
  157. Y = Pos.Bottom (cbAllowNegativeX),
  158. CanFocus = false
  159. };
  160. cbAllowYGreaterThanContentHeight.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight) ? CheckState.Checked : CheckState.UnChecked;
  161. view.Padding.Add (cbAllowYGreaterThanContentHeight);
  162. void AllowNegativeY_Toggle (object sender, CancelEventArgs<CheckState> e)
  163. {
  164. if (e.NewValue == CheckState.Checked)
  165. {
  166. view.ViewportSettings |= ViewportSettings.AllowNegativeY;
  167. }
  168. else
  169. {
  170. view.ViewportSettings &= ~ViewportSettings.AllowNegativeY;
  171. }
  172. SetVerticalScrollBar (e.NewValue, cbAllowYGreaterThanContentHeight.CheckedState);
  173. }
  174. void AllowYGreaterThanContentHeight_Toggle (object sender, CancelEventArgs<CheckState> e)
  175. {
  176. if (e.NewValue == CheckState.Checked)
  177. {
  178. view.ViewportSettings |= ViewportSettings.AllowYGreaterThanContentHeight;
  179. }
  180. else
  181. {
  182. view.ViewportSettings &= ~ViewportSettings.AllowYGreaterThanContentHeight;
  183. }
  184. SetVerticalScrollBar (e.NewValue, cbAllowNegativeY.CheckedState);
  185. }
  186. var labelContentSize = new Label
  187. {
  188. Title = "_ContentSize:",
  189. Y = Pos.Bottom (cbAllowYGreaterThanContentHeight)
  190. };
  191. NumericUpDown<int> contentSizeWidth = new NumericUpDown<int>
  192. {
  193. Value = view.GetContentSize ().Width,
  194. X = Pos.Right (labelContentSize) + 1,
  195. Y = Pos.Top (labelContentSize)
  196. };
  197. contentSizeWidth.ValueChanging += ContentSizeWidth_ValueChanged;
  198. void ContentSizeWidth_ValueChanged (object sender, CancelEventArgs<int> e)
  199. {
  200. if (e.NewValue < 0)
  201. {
  202. e.Cancel = true;
  203. return;
  204. }
  205. // BUGBUG: set_ContentSize is supposed to be `protected`.
  206. view.SetContentSize (view.GetContentSize () with { Width = e.NewValue });
  207. }
  208. var labelComma = new Label
  209. {
  210. Title = ",",
  211. X = Pos.Right (contentSizeWidth),
  212. Y = Pos.Top (labelContentSize)
  213. };
  214. NumericUpDown<int> contentSizeHeight = new NumericUpDown<int>
  215. {
  216. Value = view.GetContentSize ().Height,
  217. X = Pos.Right (labelComma) + 1,
  218. Y = Pos.Top (labelContentSize),
  219. CanFocus = false
  220. };
  221. contentSizeHeight.ValueChanging += ContentSizeHeight_ValueChanged;
  222. void ContentSizeHeight_ValueChanged (object sender, CancelEventArgs<int> e)
  223. {
  224. if (e.NewValue < 0)
  225. {
  226. e.Cancel = true;
  227. return;
  228. }
  229. // BUGBUG: set_ContentSize is supposed to be `protected`.
  230. view.SetContentSize (view.GetContentSize () with { Height = e.NewValue });
  231. }
  232. var cbClearOnlyVisible = new CheckBox
  233. {
  234. Title = "ClearContentOnly",
  235. X = Pos.Right (contentSizeHeight) + 1,
  236. Y = Pos.Top (labelContentSize),
  237. CanFocus = false
  238. };
  239. cbClearOnlyVisible.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly) ? CheckState.Checked : CheckState.UnChecked;
  240. cbClearOnlyVisible.CheckedStateChanging += ClearVisibleContentOnly_Toggle;
  241. void ClearVisibleContentOnly_Toggle (object sender, CancelEventArgs<CheckState> e)
  242. {
  243. if (e.NewValue == CheckState.Checked)
  244. {
  245. view.ViewportSettings |= ViewportSettings.ClearContentOnly;
  246. }
  247. else
  248. {
  249. view.ViewportSettings &= ~ViewportSettings.ClearContentOnly;
  250. }
  251. }
  252. var cbDoNotClipContent = new CheckBox
  253. {
  254. Title = "ClipContentOnly",
  255. X = Pos.Right (cbClearOnlyVisible) + 1,
  256. Y = Pos.Top (labelContentSize),
  257. CanFocus = false
  258. };
  259. cbDoNotClipContent.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly) ? CheckState.Checked : CheckState.UnChecked;
  260. cbDoNotClipContent.CheckedStateChanging += ClipVisibleContentOnly_Toggle;
  261. void ClipVisibleContentOnly_Toggle (object sender, CancelEventArgs<CheckState> e)
  262. {
  263. if (e.NewValue == CheckState.Checked)
  264. {
  265. view.ViewportSettings |= ViewportSettings.ClipContentOnly;
  266. }
  267. else
  268. {
  269. view.ViewportSettings &= ~ViewportSettings.ClipContentOnly;
  270. }
  271. }
  272. var cbVerticalScrollBar = new CheckBox
  273. {
  274. Title = "Vertical ScrollBar",
  275. X = 0,
  276. Y = Pos.Bottom (labelContentSize),
  277. CanFocus = false
  278. };
  279. view.VerticalScrollBar.ShowScrollIndicator = false;
  280. cbVerticalScrollBar.CheckedState = view.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked;
  281. cbVerticalScrollBar.CheckedStateChanging += VerticalScrollBar_Toggle;
  282. void VerticalScrollBar_Toggle (object sender, CancelEventArgs<CheckState> e)
  283. {
  284. view.VerticalScrollBar.ShowScrollIndicator = e.NewValue == CheckState.Checked;
  285. }
  286. var cbHorizontalScrollBar = new CheckBox
  287. {
  288. Title = "Horizontal ScrollBar",
  289. X = Pos.Right (cbVerticalScrollBar) + 1,
  290. Y = Pos.Bottom (labelContentSize),
  291. CanFocus = false,
  292. CheckedState = view.HorizontalScrollBar.ShowScrollIndicator ? CheckState.Checked : CheckState.UnChecked
  293. };
  294. view.HorizontalScrollBar.ShowScrollIndicator = false;
  295. cbHorizontalScrollBar.CheckedStateChanging += HorizontalScrollBar_Toggle;
  296. void HorizontalScrollBar_Toggle (object sender, CancelEventArgs<CheckState> e)
  297. {
  298. view.HorizontalScrollBar.ShowScrollIndicator = e.NewValue == CheckState.Checked;
  299. }
  300. var cbAutoHideVerticalScrollBar = new CheckBox
  301. {
  302. Title = "Auto-hide Vertical ScrollBar",
  303. X = Pos.Right (cbHorizontalScrollBar) + 1,
  304. Y = Pos.Bottom (labelContentSize),
  305. CanFocus = false,
  306. CheckedState = view.HorizontalScrollBar.AutoHide ? CheckState.Checked : CheckState.UnChecked
  307. };
  308. view.VerticalScrollBar.AutoHide = true;
  309. cbAutoHideVerticalScrollBar.CheckedStateChanging += AutoHideVerticalScrollBar_Toggle;
  310. void AutoHideVerticalScrollBar_Toggle (object sender, CancelEventArgs<CheckState> e)
  311. {
  312. view.VerticalScrollBar.AutoHide = e.NewValue == CheckState.Checked;
  313. }
  314. var cbAutoHideHorizontalScrollBar = new CheckBox
  315. {
  316. Title = "Auto-hide Horizontal ScrollBar",
  317. X = Pos.Right (cbAutoHideVerticalScrollBar) + 1,
  318. Y = Pos.Bottom (labelContentSize),
  319. CanFocus = false,
  320. CheckedState = view.HorizontalScrollBar.AutoHide ? CheckState.Checked : CheckState.UnChecked
  321. };
  322. view.HorizontalScrollBar.AutoHide = true;
  323. cbAutoHideHorizontalScrollBar.CheckedStateChanging += AutoHideHorizontalScrollBar_Toggle;
  324. void AutoHideHorizontalScrollBar_Toggle (object sender, CancelEventArgs<CheckState> e)
  325. {
  326. view.HorizontalScrollBar.AutoHide = e.NewValue == CheckState.Checked;
  327. }
  328. cbAllowNegativeX.CheckedStateChanging += AllowNegativeX_Toggle;
  329. cbAllowNegativeY.CheckedStateChanging += AllowNegativeY_Toggle;
  330. cbAllowXGreaterThanContentWidth.CheckedStateChanging += AllowXGreaterThanContentWidth_Toggle;
  331. cbAllowYGreaterThanContentHeight.CheckedStateChanging += AllowYGreaterThanContentHeight_Toggle;
  332. void SetHorizontalScrollBar (CheckState newValue, CheckState value)
  333. {
  334. if (newValue == CheckState.Checked)
  335. {
  336. cbAutoHideHorizontalScrollBar.CheckedState = CheckState.UnChecked;
  337. view.HorizontalScrollBar.AutoHide = view.HorizontalScrollBar.ShowScrollIndicator = false;
  338. cbHorizontalScrollBar.CheckedState = CheckState.UnChecked;
  339. }
  340. else
  341. {
  342. cbAutoHideHorizontalScrollBar.CheckedState = CheckState.Checked;
  343. view.HorizontalScrollBar.AutoHide = view.HorizontalScrollBar.ShowScrollIndicator = newValue == CheckState.UnChecked
  344. && value == CheckState.UnChecked;
  345. cbHorizontalScrollBar.CheckedState = CheckState.Checked;
  346. }
  347. }
  348. void SetVerticalScrollBar (CheckState newValue, CheckState value)
  349. {
  350. if (newValue == CheckState.Checked)
  351. {
  352. cbAutoHideVerticalScrollBar.CheckedState = CheckState.UnChecked;
  353. view.VerticalScrollBar.AutoHide = view.VerticalScrollBar.ShowScrollIndicator = false;
  354. cbVerticalScrollBar.CheckedState = CheckState.UnChecked;
  355. }
  356. else
  357. {
  358. cbAutoHideVerticalScrollBar.CheckedState = CheckState.Checked;
  359. view.VerticalScrollBar.AutoHide = view.VerticalScrollBar.ShowScrollIndicator = newValue == CheckState.UnChecked
  360. && value == CheckState.UnChecked;
  361. cbVerticalScrollBar.CheckedState = CheckState.Checked;
  362. }
  363. }
  364. view.Padding.Add (labelContentSize, contentSizeWidth, labelComma, contentSizeHeight, cbClearOnlyVisible, cbDoNotClipContent, cbVerticalScrollBar, cbHorizontalScrollBar, cbAutoHideVerticalScrollBar, cbAutoHideHorizontalScrollBar);
  365. // Add demo views to show that things work correctly
  366. var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test TextField" };
  367. var colorPicker = new ColorPicker16 { Title = "BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
  368. colorPicker.BorderStyle = LineStyle.RoundedDotted;
  369. colorPicker.ColorChanged += (s, e) =>
  370. {
  371. colorPicker.SuperView.ColorScheme = new (colorPicker.SuperView.ColorScheme)
  372. {
  373. Normal = new (
  374. colorPicker.SuperView.ColorScheme.Normal.Foreground,
  375. e.CurrentValue
  376. )
  377. };
  378. };
  379. var textView = new TextView
  380. {
  381. X = Pos.Center (),
  382. Y = 10,
  383. Title = "TextView",
  384. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
  385. AllowsTab = false,
  386. Width = 30,
  387. Height = 6 // TODO: Use Dim.Auto
  388. };
  389. textView.Border.Thickness = new (1, 3, 1, 1);
  390. var charMap = new CharMap
  391. {
  392. X = Pos.Center (),
  393. Y = Pos.Bottom (textView) + 1,
  394. Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.GetContentSize ().Width)),
  395. Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
  396. };
  397. charMap.Accept += (s, e) =>
  398. MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
  399. var buttonAnchored = new Button
  400. {
  401. X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Bottom Right"
  402. };
  403. view.Margin.Data = "Margin";
  404. view.Margin.Thickness = new (0);
  405. view.Border.Data = "Border";
  406. view.Border.Thickness = new (3);
  407. view.Padding.Data = "Padding";
  408. view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
  409. var longLabel = new Label
  410. {
  411. Id = "label2",
  412. X = 0,
  413. Y = 30,
  414. Text =
  415. "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."
  416. };
  417. longLabel.TextFormatter.WordWrap = true;
  418. view.Add (longLabel);
  419. List<object> options = new () { "Option 1", "Option 2", "Option 3" };
  420. Slider slider = new (options)
  421. {
  422. X = 0,
  423. Y = Pos.Bottom (textField) + 1,
  424. Orientation = Orientation.Vertical,
  425. Type = SliderType.Multiple,
  426. AllowEmpty = false,
  427. BorderStyle = LineStyle.Double,
  428. Title = "_Slider"
  429. };
  430. view.Add (slider);
  431. editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
  432. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  433. editor.AutoSelectViewToEdit = true;
  434. editor.AutoSelectSuperView = view;
  435. editor.AutoSelectAdornments = false;
  436. Application.Run (app);
  437. app.Dispose ();
  438. Application.Shutdown ();
  439. }
  440. }