ContentScrolling.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 = new (0, 3, 0, 0);
  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. cbAllowNegativeX.CheckedStateChanging += AllowNegativeX_Toggle;
  112. void AllowNegativeX_Toggle (object sender, CancelEventArgs<CheckState> e)
  113. {
  114. if (e.NewValue == CheckState.Checked)
  115. {
  116. view.ViewportSettings |= ViewportSettings.AllowNegativeX;
  117. }
  118. else
  119. {
  120. view.ViewportSettings &= ~ViewportSettings.AllowNegativeX;
  121. }
  122. }
  123. view.Padding.Add (cbAllowNegativeX);
  124. var cbAllowNegativeY = new CheckBox
  125. {
  126. Title = "Allow _Y < 0",
  127. X = Pos.Right (cbAllowNegativeX) + 1,
  128. Y = 0,
  129. CanFocus = false
  130. };
  131. cbAllowNegativeY.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowNegativeY) ? CheckState.Checked : CheckState.UnChecked;
  132. cbAllowNegativeY.CheckedStateChanging += AllowNegativeY_Toggle;
  133. void AllowNegativeY_Toggle (object sender, CancelEventArgs<CheckState> e)
  134. {
  135. if (e.NewValue == CheckState.Checked)
  136. {
  137. view.ViewportSettings |= ViewportSettings.AllowNegativeY;
  138. }
  139. else
  140. {
  141. view.ViewportSettings &= ~ViewportSettings.AllowNegativeY;
  142. }
  143. }
  144. view.Padding.Add (cbAllowNegativeY);
  145. var cbAllowXGreaterThanContentWidth = new CheckBox
  146. {
  147. Title = "All_ow X > Content",
  148. Y = Pos.Bottom (cbAllowNegativeX),
  149. CanFocus = false
  150. };
  151. cbAllowXGreaterThanContentWidth.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowXGreaterThanContentWidth) ? CheckState.Checked : CheckState.UnChecked;
  152. cbAllowXGreaterThanContentWidth.CheckedStateChanging += AllowXGreaterThanContentWidth_Toggle;
  153. void AllowXGreaterThanContentWidth_Toggle (object sender, CancelEventArgs<CheckState> e)
  154. {
  155. if (e.NewValue == CheckState.Checked)
  156. {
  157. view.ViewportSettings |= ViewportSettings.AllowXGreaterThanContentWidth;
  158. }
  159. else
  160. {
  161. view.ViewportSettings &= ~ViewportSettings.AllowXGreaterThanContentWidth;
  162. }
  163. }
  164. view.Padding.Add (cbAllowXGreaterThanContentWidth);
  165. var cbAllowYGreaterThanContentHeight = new CheckBox
  166. {
  167. Title = "Allo_w Y > Content",
  168. X = Pos.Right (cbAllowXGreaterThanContentWidth) + 1,
  169. Y = Pos.Bottom (cbAllowNegativeX),
  170. CanFocus = false
  171. };
  172. cbAllowYGreaterThanContentHeight.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowYGreaterThanContentHeight) ? CheckState.Checked : CheckState.UnChecked;
  173. cbAllowYGreaterThanContentHeight.CheckedStateChanging += AllowYGreaterThanContentHeight_Toggle;
  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. }
  185. view.Padding.Add (cbAllowYGreaterThanContentHeight);
  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. view.Padding.Add (labelContentSize, contentSizeWidth, labelComma, contentSizeHeight, cbClearOnlyVisible, cbDoNotClipContent);
  273. // Add demo views to show that things work correctly
  274. var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test TextField" };
  275. var colorPicker = new ColorPicker16 { Title = "BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
  276. colorPicker.BorderStyle = LineStyle.RoundedDotted;
  277. colorPicker.ColorChanged += (s, e) =>
  278. {
  279. colorPicker.SuperView.ColorScheme = new (colorPicker.SuperView.ColorScheme)
  280. {
  281. Normal = new (
  282. colorPicker.SuperView.ColorScheme.Normal.Foreground,
  283. e.CurrentValue
  284. )
  285. };
  286. };
  287. var textView = new TextView
  288. {
  289. X = Pos.Center (),
  290. Y = 10,
  291. Title = "TextView",
  292. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
  293. AllowsTab = false,
  294. Width = 30,
  295. Height = 6 // TODO: Use Dim.Auto
  296. };
  297. textView.Border.Thickness = new (1, 3, 1, 1);
  298. var charMap = new CharMap
  299. {
  300. X = Pos.Center (),
  301. Y = Pos.Bottom (textView) + 1,
  302. Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.GetContentSize ().Width)),
  303. Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
  304. };
  305. charMap.Accept += (s, e) =>
  306. MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
  307. var buttonAnchored = new Button
  308. {
  309. X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Bottom Right"
  310. };
  311. view.Margin.Data = "Margin";
  312. view.Margin.Thickness = new (0);
  313. view.Border.Data = "Border";
  314. view.Border.Thickness = new (3);
  315. view.Padding.Data = "Padding";
  316. view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
  317. var longLabel = new Label
  318. {
  319. Id = "label2",
  320. X = 0,
  321. Y = 30,
  322. Text =
  323. "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."
  324. };
  325. longLabel.TextFormatter.WordWrap = true;
  326. view.Add (longLabel);
  327. List<object> options = new () { "Option 1", "Option 2", "Option 3" };
  328. Slider slider = new (options)
  329. {
  330. X = 0,
  331. Y = Pos.Bottom (textField) + 1,
  332. Orientation = Orientation.Vertical,
  333. Type = SliderType.Multiple,
  334. AllowEmpty = false,
  335. BorderStyle = LineStyle.Double,
  336. Title = "_Slider"
  337. };
  338. view.Add (slider);
  339. editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
  340. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  341. Application.MouseEvent += ApplicationOnMouseEvent;
  342. Application.Run (app);
  343. app.Dispose ();
  344. Application.Shutdown ();
  345. return;
  346. void ApplicationOnMouseEvent (object sender, MouseEvent e)
  347. {
  348. if (!editor.AutoSelectViewToEdit || editor.FrameToScreen ().Contains (e.Position))
  349. {
  350. return;
  351. }
  352. // TODO: Add a setting (property) so only subviews of a specified view are considered.
  353. View view = e.View;
  354. if (view is { } && e.Flags == MouseFlags.Button1Clicked)
  355. {
  356. if (view is Adornment adornment)
  357. {
  358. editor.ViewToEdit = adornment.Parent;
  359. }
  360. else
  361. {
  362. editor.ViewToEdit = view;
  363. }
  364. }
  365. }
  366. }
  367. }