ContentScrolling.cs 15 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.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. view.Padding.CanFocus = true;
  105. var cbAllowNegativeX = new CheckBox
  106. {
  107. Title = "Allow _X < 0",
  108. Y = 0,
  109. CanFocus = true
  110. };
  111. cbAllowNegativeX.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowNegativeX) ? CheckState.Checked : CheckState.UnChecked;
  112. cbAllowNegativeX.CheckedStateChanging += AllowNegativeX_Toggle;
  113. void AllowNegativeX_Toggle (object sender, CancelEventArgs<CheckState> e)
  114. {
  115. if (e.NewValue == CheckState.Checked)
  116. {
  117. view.ViewportSettings |= ViewportSettings.AllowNegativeX;
  118. }
  119. else
  120. {
  121. view.ViewportSettings &= ~ViewportSettings.AllowNegativeX;
  122. }
  123. }
  124. view.Padding.Add (cbAllowNegativeX);
  125. var cbAllowNegativeY = new CheckBox
  126. {
  127. Title = "Allow _Y < 0",
  128. X = Pos.Right (cbAllowNegativeX) + 1,
  129. Y = 0,
  130. CanFocus = true
  131. };
  132. cbAllowNegativeY.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowNegativeY) ? CheckState.Checked : CheckState.UnChecked;
  133. cbAllowNegativeY.CheckedStateChanging += AllowNegativeY_Toggle;
  134. void AllowNegativeY_Toggle (object sender, CancelEventArgs<CheckState> e)
  135. {
  136. if (e.NewValue == CheckState.Checked)
  137. {
  138. view.ViewportSettings |= ViewportSettings.AllowNegativeY;
  139. }
  140. else
  141. {
  142. view.ViewportSettings &= ~ViewportSettings.AllowNegativeY;
  143. }
  144. }
  145. view.Padding.Add (cbAllowNegativeY);
  146. var cbAllowXGreaterThanContentWidth = new CheckBox
  147. {
  148. Title = "All_ow X > Content",
  149. Y = Pos.Bottom (cbAllowNegativeX),
  150. CanFocus = true
  151. };
  152. cbAllowXGreaterThanContentWidth.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowXGreaterThanContentWidth) ? CheckState.Checked : CheckState.UnChecked;
  153. cbAllowXGreaterThanContentWidth.CheckedStateChanging += AllowXGreaterThanContentWidth_Toggle;
  154. void AllowXGreaterThanContentWidth_Toggle (object sender, CancelEventArgs<CheckState> e)
  155. {
  156. if (e.NewValue == CheckState.Checked)
  157. {
  158. view.ViewportSettings |= ViewportSettings.AllowXGreaterThanContentWidth;
  159. }
  160. else
  161. {
  162. view.ViewportSettings &= ~ViewportSettings.AllowXGreaterThanContentWidth;
  163. }
  164. }
  165. view.Padding.Add (cbAllowXGreaterThanContentWidth);
  166. var cbAllowYGreaterThanContentHeight = new CheckBox
  167. {
  168. Title = "Allo_w Y > Content",
  169. X = Pos.Right (cbAllowXGreaterThanContentWidth) + 1,
  170. Y = Pos.Bottom (cbAllowNegativeX),
  171. CanFocus = true
  172. };
  173. cbAllowYGreaterThanContentHeight.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.AllowYGreaterThanContentHeight) ? CheckState.Checked : CheckState.UnChecked;
  174. cbAllowYGreaterThanContentHeight.CheckedStateChanging += AllowYGreaterThanContentHeight_Toggle;
  175. void AllowYGreaterThanContentHeight_Toggle (object sender, CancelEventArgs<CheckState> e)
  176. {
  177. if (e.NewValue == CheckState.Checked)
  178. {
  179. view.ViewportSettings |= ViewportSettings.AllowYGreaterThanContentHeight;
  180. }
  181. else
  182. {
  183. view.ViewportSettings &= ~ViewportSettings.AllowYGreaterThanContentHeight;
  184. }
  185. }
  186. view.Padding.Add (cbAllowYGreaterThanContentHeight);
  187. var labelContentSize = new Label
  188. {
  189. Title = "_ContentSize:",
  190. Y = Pos.Bottom (cbAllowYGreaterThanContentHeight)
  191. };
  192. NumericUpDown<int> contentSizeWidth = new NumericUpDown<int>
  193. {
  194. Value = view.GetContentSize ().Width,
  195. X = Pos.Right (labelContentSize) + 1,
  196. Y = Pos.Top (labelContentSize),
  197. CanFocus = true
  198. };
  199. contentSizeWidth.ValueChanging += ContentSizeWidth_ValueChanged;
  200. void ContentSizeWidth_ValueChanged (object sender, CancelEventArgs<int> e)
  201. {
  202. if (e.NewValue < 0)
  203. {
  204. e.Cancel = true;
  205. return;
  206. }
  207. // BUGBUG: set_ContentSize is supposed to be `protected`.
  208. view.SetContentSize (view.GetContentSize () with { Width = e.NewValue });
  209. }
  210. var labelComma = new Label
  211. {
  212. Title = ",",
  213. X = Pos.Right (contentSizeWidth),
  214. Y = Pos.Top (labelContentSize)
  215. };
  216. NumericUpDown<int> contentSizeHeight = new NumericUpDown<int>
  217. {
  218. Value = view.GetContentSize ().Height,
  219. X = Pos.Right (labelComma) + 1,
  220. Y = Pos.Top (labelContentSize),
  221. CanFocus = true
  222. };
  223. contentSizeHeight.ValueChanging += ContentSizeHeight_ValueChanged;
  224. void ContentSizeHeight_ValueChanged (object sender, CancelEventArgs<int> e)
  225. {
  226. if (e.NewValue < 0)
  227. {
  228. e.Cancel = true;
  229. return;
  230. }
  231. // BUGBUG: set_ContentSize is supposed to be `protected`.
  232. view.SetContentSize (view.GetContentSize () with { Height = e.NewValue });
  233. }
  234. var cbClearContentOnly = new CheckBox
  235. {
  236. Title = "ClearContentOnly",
  237. X = Pos.Right (contentSizeHeight) + 1,
  238. Y = Pos.Top (labelContentSize),
  239. CanFocus = true
  240. };
  241. cbClearContentOnly.CheckedState = view.ViewportSettings.HasFlag(ViewportSettings.ClearContentOnly) ? CheckState.Checked : CheckState.UnChecked;
  242. cbClearContentOnly.CheckedStateChanging += ClearContentOnly_Toggle;
  243. void ClearContentOnly_Toggle (object sender, CancelEventArgs<CheckState> e)
  244. {
  245. if (e.NewValue == CheckState.Checked)
  246. {
  247. view.ViewportSettings |= ViewportSettings.ClearContentOnly;
  248. }
  249. else
  250. {
  251. view.ViewportSettings &= ~ViewportSettings.ClearContentOnly;
  252. }
  253. }
  254. var cbClipContentOnly = new CheckBox
  255. {
  256. Title = "ClipContentOnly",
  257. X = Pos.Right (cbClearContentOnly) + 1,
  258. Y = Pos.Top (labelContentSize),
  259. CanFocus = true
  260. };
  261. cbClipContentOnly.CheckedState = view.ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly) ? CheckState.Checked : CheckState.UnChecked;
  262. cbClipContentOnly.CheckedStateChanging += ClipContentOnlyOnly_Toggle;
  263. void ClipContentOnlyOnly_Toggle (object sender, CancelEventArgs<CheckState> e)
  264. {
  265. if (e.NewValue == CheckState.Checked)
  266. {
  267. view.ViewportSettings |= ViewportSettings.ClipContentOnly;
  268. }
  269. else
  270. {
  271. view.ViewportSettings &= ~ViewportSettings.ClipContentOnly;
  272. }
  273. }
  274. view.Padding.Add (labelContentSize, contentSizeWidth, labelComma, contentSizeHeight, cbClearContentOnly, cbClipContentOnly);
  275. // Add demo views to show that things work correctly
  276. var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test TextField" };
  277. var colorPicker = new ColorPicker16 { Title = "BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
  278. colorPicker.BorderStyle = LineStyle.RoundedDotted;
  279. colorPicker.ColorChanged += (s, e) =>
  280. {
  281. colorPicker.SuperView.ColorScheme = new (colorPicker.SuperView.ColorScheme)
  282. {
  283. Normal = new (
  284. colorPicker.SuperView.ColorScheme.Normal.Foreground,
  285. e.CurrentValue
  286. )
  287. };
  288. };
  289. var textView = new TextView
  290. {
  291. X = Pos.Center (),
  292. Y = 10,
  293. Title = "TextView",
  294. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
  295. AllowsTab = false,
  296. Width = 30,
  297. Height = 6 // TODO: Use Dim.Auto
  298. };
  299. textView.Border.Thickness = new (1, 3, 1, 1);
  300. var charMap = new CharMap
  301. {
  302. X = Pos.Center (),
  303. Y = Pos.Bottom (textView) + 1,
  304. Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.GetContentSize ().Width)),
  305. Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
  306. };
  307. charMap.Accept += (s, e) =>
  308. MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
  309. var buttonAnchored = new Button
  310. {
  311. X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Bottom Right"
  312. };
  313. view.Margin.Data = "Margin";
  314. view.Margin.Thickness = new (0);
  315. view.Border.Data = "Border";
  316. view.Border.Thickness = new (3);
  317. view.Padding.Data = "Padding";
  318. view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
  319. var longLabel = new Label
  320. {
  321. Id = "label2",
  322. X = 0,
  323. Y = 30,
  324. Text =
  325. "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."
  326. };
  327. longLabel.TextFormatter.WordWrap = true;
  328. view.Add (longLabel);
  329. List<object> options = new () { "Option 1", "Option 2", "Option 3" };
  330. Slider slider = new (options)
  331. {
  332. X = 0,
  333. Y = Pos.Bottom (textField) + 1,
  334. Orientation = Orientation.Vertical,
  335. Type = SliderType.Multiple,
  336. AllowEmpty = false,
  337. BorderStyle = LineStyle.Double,
  338. Title = "_Slider"
  339. };
  340. view.Add (slider);
  341. editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
  342. app.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
  343. editor.AutoSelectViewToEdit = true;
  344. editor.AutoSelectSuperView = view;
  345. editor.AutoSelectAdornments = false;
  346. Application.Run (app);
  347. app.Dispose ();
  348. Application.Shutdown ();
  349. }
  350. }