ViewportSettings.cs 9.0 KB

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