ViewportSettings.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("ViewportSettings", "Demonstrates manipulating Viewport, ViewportSettings, and ContentSize to scroll content.")]
  7. [ScenarioCategory ("Layout")]
  8. [ScenarioCategory ("Drawing")]
  9. [ScenarioCategory ("Scrolling")]
  10. [ScenarioCategory ("Adornments")]
  11. public class ViewportSettings : Scenario
  12. {
  13. public class ViewportSettingsDemoView : FrameView
  14. {
  15. public ViewportSettingsDemoView ()
  16. {
  17. Id = "ViewportSettingsDemoView";
  18. Width = Dim.Fill ();
  19. Height = Dim.Fill ();
  20. SchemeName = "base";
  21. base.Text =
  22. "Text (ViewportSettingsDemoView.Text). This is long text.\nThe second line.\n3\n4\n5th line\nLine 6. This is a longer line that should wrap automatically.";
  23. CanFocus = true;
  24. BorderStyle = LineStyle.Rounded;
  25. Arrangement = ViewArrangement.Resizable;
  26. SetContentSize (new (60, 40));
  27. ViewportSettings |= Terminal.Gui.ViewportSettings.ClearContentOnly;
  28. ViewportSettings |= Terminal.Gui.ViewportSettings.ClipContentOnly;
  29. VerticalScrollBar.Visible = true;
  30. // Things this view knows how to do
  31. AddCommand (Command.ScrollDown, () => ScrollVertical (1));
  32. AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
  33. AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
  34. AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
  35. // Default keybindings for all ListViews
  36. KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
  37. KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
  38. KeyBindings.Add (Key.CursorLeft, Command.ScrollLeft);
  39. KeyBindings.Add (Key.CursorRight, Command.ScrollRight);
  40. // Add a status label to the border that shows Viewport and ContentSize values. Bit of a hack.
  41. // TODO: Move to Padding with controls
  42. Border?.Add (new Label { X = 20 });
  43. ViewportChanged += VirtualDemoView_LayoutComplete;
  44. MouseEvent += VirtualDemoView_MouseEvent;
  45. }
  46. private void VirtualDemoView_MouseEvent (object sender, MouseEventArgs e)
  47. {
  48. if (e.Flags == MouseFlags.WheeledDown)
  49. {
  50. ScrollVertical (1);
  51. return;
  52. }
  53. if (e.Flags == MouseFlags.WheeledUp)
  54. {
  55. ScrollVertical (-1);
  56. return;
  57. }
  58. if (e.Flags == MouseFlags.WheeledRight)
  59. {
  60. ScrollHorizontal (1);
  61. return;
  62. }
  63. if (e.Flags == MouseFlags.WheeledLeft)
  64. {
  65. ScrollHorizontal (-1);
  66. }
  67. }
  68. private void VirtualDemoView_LayoutComplete (object sender, DrawEventArgs drawEventArgs)
  69. {
  70. Label frameLabel = Padding?.SubViews.OfType<Label> ().FirstOrDefault ();
  71. if (frameLabel is { })
  72. {
  73. frameLabel.Text = $"Viewport: {Viewport}\nFrame: {Frame}";
  74. }
  75. }
  76. }
  77. public override void Main ()
  78. {
  79. Application.Init ();
  80. Window app = new ()
  81. {
  82. Title = GetQuitKeyAndName (),
  83. // Use a different colorscheme so ViewSettings.ClearContentOnly is obvious
  84. SchemeName = "Toplevel",
  85. BorderStyle = LineStyle.None
  86. };
  87. var adornmentsEditor = new AdornmentsEditor
  88. {
  89. X = Pos.AnchorEnd (),
  90. AutoSelectViewToEdit = true,
  91. ShowViewIdentifier = true
  92. };
  93. app.Add (adornmentsEditor);
  94. ViewportSettingsEditor viewportSettingsEditor = new ViewportSettingsEditor ()
  95. {
  96. Y = Pos.AnchorEnd (),
  97. //X = Pos.Right (adornmentsEditor),
  98. };
  99. app.Add (viewportSettingsEditor);
  100. var view = new ViewportSettingsDemoView
  101. {
  102. Title = "ViewportSettings Demo View",
  103. Width = Dim.Fill (Dim.Func (() => app.IsInitialized ? adornmentsEditor.Frame.Width + 1 : 1)),
  104. Height = Dim.Fill (Dim.Func (() => app.IsInitialized ? viewportSettingsEditor.Frame.Height : 1))
  105. };
  106. app.Add (view);
  107. // Add demo views to show that things work correctly
  108. var textField = new TextField { X = 20, Y = 7, Width = 15, Text = "Test Te_xtField" };
  109. var colorPicker = new ColorPicker16 { Title = "_BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd (), Y = 10 };
  110. colorPicker.BorderStyle = LineStyle.RoundedDotted;
  111. colorPicker.ColorChanged += (s, e) =>
  112. {
  113. colorPicker.SuperView!.SetScheme (
  114. new (colorPicker.SuperView.GetScheme ())
  115. {
  116. Normal = new (
  117. colorPicker.SuperView.GetAttributeForRole (VisualRole.Normal).Foreground,
  118. e.CurrentValue
  119. )
  120. });
  121. };
  122. var textView = new TextView
  123. {
  124. X = Pos.Center (),
  125. Y = 10,
  126. Title = "TextVie_w",
  127. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.\nI have 3 lines of text with room for 2.",
  128. AllowsTab = false,
  129. Width = 30,
  130. Height = 6 // TODO: Use Dim.Auto
  131. };
  132. textView.Border!.Thickness = new (1, 3, 1, 1);
  133. var charMap = new CharMap
  134. {
  135. X = Pos.Center (),
  136. Y = Pos.Bottom (textView) + 1,
  137. Width = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Func (() => view.GetContentSize ().Width)),
  138. Height = Dim.Auto (DimAutoStyle.Content, maximumContentDim: Dim.Percent (20)),
  139. };
  140. charMap.Accepting += (s, e) =>
  141. MessageBox.Query (20, 7, "Hi", $"Am I a {view.GetType ().Name}?", "Yes", "No");
  142. var buttonAnchored = new Button
  143. {
  144. X = Pos.AnchorEnd () - 10, Y = Pos.AnchorEnd () - 4, Text = "Bottom Rig_ht"
  145. };
  146. buttonAnchored.Accepting += (sender, args) => MessageBox.Query ("Hi", $"You pressed {((Button)sender)?.Text}", "_Ok");
  147. view.Margin!.Data = "Margin";
  148. view.Margin.Thickness = new (0);
  149. view.Border!.Data = "Border";
  150. view.Border.Thickness = new (3);
  151. view.Padding.Data = "Padding";
  152. view.Add (buttonAnchored, textField, colorPicker, charMap, textView);
  153. var longLabel = new Label
  154. {
  155. Id = "label2",
  156. X = 0,
  157. Y = 30,
  158. Text =
  159. "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."
  160. };
  161. longLabel.TextFormatter.WordWrap = true;
  162. view.Add (longLabel);
  163. List<object> options = new () { "Option 1", "Option 2", "Option 3" };
  164. Slider slider = new (options)
  165. {
  166. X = 0,
  167. Y = Pos.Bottom (textField) + 1,
  168. Orientation = Orientation.Vertical,
  169. Type = SliderType.Multiple,
  170. AllowEmpty = false,
  171. BorderStyle = LineStyle.Double,
  172. Title = "_Slider"
  173. };
  174. view.Add (slider);
  175. adornmentsEditor.Initialized += (s, e) =>
  176. {
  177. adornmentsEditor.ViewToEdit = view;
  178. };
  179. adornmentsEditor.AutoSelectViewToEdit = true;
  180. adornmentsEditor.AutoSelectSuperView = view;
  181. adornmentsEditor.AutoSelectAdornments = false;
  182. view.Initialized += (s, e) =>
  183. {
  184. viewportSettingsEditor.ViewToEdit = view;
  185. };
  186. view.SetFocus ();
  187. Application.Run (app);
  188. app.Dispose ();
  189. Application.Shutdown ();
  190. }
  191. public override List<Key> GetDemoKeyStrokes ()
  192. {
  193. var keys = new List<Key> ();
  194. for (int i = 0; i < 50; i++)
  195. {
  196. keys.Add (Key.CursorRight);
  197. }
  198. for (int i = 0; i < 25; i++)
  199. {
  200. keys.Add (Key.CursorLeft);
  201. }
  202. for (int i = 0; i < 50; i++)
  203. {
  204. keys.Add (Key.CursorDown);
  205. }
  206. for (int i = 0; i < 25; i++)
  207. {
  208. keys.Add (Key.CursorUp);
  209. }
  210. return keys;
  211. }
  212. }