ViewportSettingsEditor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #nullable enable
  2. using System;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. /// <summary>
  6. /// Provides an editor UI for View.ViewportSettings.
  7. /// </summary>
  8. public sealed class ViewportSettingsEditor : EditorBase
  9. {
  10. public ViewportSettingsEditor ()
  11. {
  12. Title = "ViewportSettingsEditor";
  13. TabStop = TabBehavior.TabGroup;
  14. Initialized += ViewportSettingsEditor_Initialized;
  15. }
  16. protected override void OnUpdateSettings ()
  17. {
  18. foreach (View subview in Subviews)
  19. {
  20. subview.Enabled = ViewToEdit is not Adornment;
  21. }
  22. if (ViewToEdit is null)
  23. { }
  24. }
  25. protected override void OnViewToEditChanged ()
  26. {
  27. if (ViewToEdit is { } and not Adornment)
  28. {
  29. //ViewToEdit.VerticalScrollBar.AutoShow = true;
  30. //ViewToEdit.HorizontalScrollBar.AutoShow = true;
  31. _contentSizeWidth!.Value = ViewToEdit.GetContentSize ().Width;
  32. _contentSizeHeight!.Value = ViewToEdit.GetContentSize ().Height;
  33. _cbAllowNegativeX!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowNegativeX)
  34. ? CheckState.Checked
  35. : CheckState.UnChecked;
  36. _cbAllowNegativeY!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowNegativeY)
  37. ? CheckState.Checked
  38. : CheckState.UnChecked;
  39. _cbAllowXGreaterThanContentWidth!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth)
  40. ? CheckState.Checked
  41. : CheckState.UnChecked;
  42. _cbAllowYGreaterThanContentHeight!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight)
  43. ? CheckState.Checked
  44. : CheckState.UnChecked;
  45. _cbClearContentOnly!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.ClearContentOnly)
  46. ? CheckState.Checked
  47. : CheckState.UnChecked;
  48. _cbClipContentOnly!.CheckedState = ViewToEdit.ViewportSettings.HasFlag (Terminal.Gui.ViewportSettings.ClipContentOnly)
  49. ? CheckState.Checked
  50. : CheckState.UnChecked;
  51. _cbTransparent!.CheckedState = ViewToEdit.ViewportSettings.HasFlag(Terminal.Gui.ViewportSettings.Transparent)
  52. ? CheckState.Checked
  53. : CheckState.UnChecked;
  54. _cbVerticalScrollBar!.CheckedState = ViewToEdit.VerticalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked;
  55. _cbAutoShowVerticalScrollBar!.CheckedState = ViewToEdit.VerticalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked;
  56. _cbHorizontalScrollBar!.CheckedState = ViewToEdit.HorizontalScrollBar.Visible ? CheckState.Checked : CheckState.UnChecked;
  57. _cbAutoShowHorizontalScrollBar!.CheckedState = ViewToEdit.HorizontalScrollBar.AutoShow ? CheckState.Checked : CheckState.UnChecked;
  58. }
  59. }
  60. private CheckBox? _cbAllowNegativeX;
  61. private CheckBox? _cbAllowNegativeY;
  62. private CheckBox? _cbAllowXGreaterThanContentWidth;
  63. private CheckBox? _cbAllowYGreaterThanContentHeight;
  64. private NumericUpDown<int>? _contentSizeWidth;
  65. private NumericUpDown<int>? _contentSizeHeight;
  66. private CheckBox? _cbClearContentOnly;
  67. private CheckBox? _cbClipContentOnly;
  68. private CheckBox? _cbTransparent;
  69. private CheckBox? _cbVerticalScrollBar;
  70. private CheckBox? _cbAutoShowVerticalScrollBar;
  71. private CheckBox? _cbHorizontalScrollBar;
  72. private CheckBox? _cbAutoShowHorizontalScrollBar;
  73. private void ViewportSettingsEditor_Initialized (object? s, EventArgs e)
  74. {
  75. _cbAllowNegativeX = new()
  76. {
  77. Title = "Allow X < 0",
  78. CanFocus = true
  79. };
  80. Add (_cbAllowNegativeX);
  81. _cbAllowNegativeY = new()
  82. {
  83. Title = "Allow Y < 0",
  84. CanFocus = true
  85. };
  86. Add (_cbAllowNegativeY);
  87. _cbAllowXGreaterThanContentWidth = new()
  88. {
  89. Title = "Allow X > Content Width",
  90. Y = Pos.Bottom (_cbAllowNegativeX),
  91. CanFocus = true
  92. };
  93. _cbAllowNegativeX.CheckedStateChanging += AllowNegativeXToggle;
  94. _cbAllowXGreaterThanContentWidth.CheckedStateChanging += AllowXGreaterThanContentWidthToggle;
  95. Add (_cbAllowXGreaterThanContentWidth);
  96. void AllowNegativeXToggle (object? sender, CancelEventArgs<CheckState> e)
  97. {
  98. if (e.NewValue == CheckState.Checked)
  99. {
  100. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowNegativeX;
  101. }
  102. else
  103. {
  104. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowNegativeX;
  105. }
  106. }
  107. void AllowXGreaterThanContentWidthToggle (object? sender, CancelEventArgs<CheckState> e)
  108. {
  109. if (e.NewValue == CheckState.Checked)
  110. {
  111. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth;
  112. }
  113. else
  114. {
  115. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowXGreaterThanContentWidth;
  116. }
  117. }
  118. _cbAllowYGreaterThanContentHeight = new()
  119. {
  120. Title = "Allow Y > Content Height",
  121. X = Pos.Right (_cbAllowXGreaterThanContentWidth) + 1,
  122. Y = Pos.Bottom (_cbAllowNegativeX),
  123. CanFocus = true
  124. };
  125. _cbAllowNegativeY.CheckedStateChanging += AllowNegativeYToggle;
  126. _cbAllowYGreaterThanContentHeight.CheckedStateChanging += AllowYGreaterThanContentHeightToggle;
  127. Add (_cbAllowYGreaterThanContentHeight);
  128. void AllowNegativeYToggle (object? sender, CancelEventArgs<CheckState> e)
  129. {
  130. if (e.NewValue == CheckState.Checked)
  131. {
  132. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowNegativeY;
  133. }
  134. else
  135. {
  136. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowNegativeY;
  137. }
  138. }
  139. void AllowYGreaterThanContentHeightToggle (object? sender, CancelEventArgs<CheckState> e)
  140. {
  141. if (e.NewValue == CheckState.Checked)
  142. {
  143. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight;
  144. }
  145. else
  146. {
  147. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.AllowYGreaterThanContentHeight;
  148. }
  149. }
  150. _cbAllowNegativeY.X = Pos.Left (_cbAllowYGreaterThanContentHeight);
  151. var labelContentSize = new Label
  152. {
  153. Title = "ContentSize:",
  154. Y = Pos.Bottom (_cbAllowYGreaterThanContentHeight)
  155. };
  156. _contentSizeWidth = new()
  157. {
  158. X = Pos.Right (labelContentSize) + 1,
  159. Y = Pos.Top (labelContentSize),
  160. CanFocus = true
  161. };
  162. _contentSizeWidth.ValueChanging += ContentSizeWidthValueChanged;
  163. void ContentSizeWidthValueChanged (object? sender, CancelEventArgs<int> e)
  164. {
  165. if (e.NewValue < 0)
  166. {
  167. e.Cancel = true;
  168. return;
  169. }
  170. // BUGBUG: set_ContentSize is supposed to be `protected`.
  171. ViewToEdit!.SetContentSize (ViewToEdit.GetContentSize () with { Width = e.NewValue });
  172. }
  173. var labelComma = new Label
  174. {
  175. Title = ",",
  176. X = Pos.Right (_contentSizeWidth),
  177. Y = Pos.Top (labelContentSize)
  178. };
  179. _contentSizeHeight = new()
  180. {
  181. X = Pos.Right (labelComma) + 1,
  182. Y = Pos.Top (labelContentSize),
  183. CanFocus = true
  184. };
  185. _contentSizeHeight.ValueChanging += ContentSizeHeightValueChanged;
  186. void ContentSizeHeightValueChanged (object? sender, CancelEventArgs<int> e)
  187. {
  188. if (e.NewValue < 0)
  189. {
  190. e.Cancel = true;
  191. return;
  192. }
  193. // BUGBUG: set_ContentSize is supposed to be `protected`.
  194. ViewToEdit?.SetContentSize (ViewToEdit.GetContentSize () with { Height = e.NewValue });
  195. }
  196. _cbClearContentOnly = new()
  197. {
  198. Title = "ClearContentOnly",
  199. X = 0,
  200. Y = Pos.Bottom (labelContentSize),
  201. CanFocus = true
  202. };
  203. _cbClearContentOnly.CheckedStateChanging += ClearContentOnlyToggle;
  204. void ClearContentOnlyToggle (object? sender, CancelEventArgs<CheckState> e)
  205. {
  206. if (e.NewValue == CheckState.Checked)
  207. {
  208. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.ClearContentOnly;
  209. }
  210. else
  211. {
  212. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.ClearContentOnly;
  213. }
  214. }
  215. _cbClipContentOnly = new()
  216. {
  217. Title = "ClipContentOnly",
  218. X = Pos.Right (_cbClearContentOnly) + 1,
  219. Y = Pos.Bottom (labelContentSize),
  220. CanFocus = true
  221. };
  222. _cbClipContentOnly.CheckedStateChanging += ClipContentOnlyToggle;
  223. void ClipContentOnlyToggle (object? sender, CancelEventArgs<CheckState> e)
  224. {
  225. if (e.NewValue == CheckState.Checked)
  226. {
  227. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.ClipContentOnly;
  228. }
  229. else
  230. {
  231. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.ClipContentOnly;
  232. }
  233. }
  234. _cbTransparent = new ()
  235. {
  236. Title = "Transparent",
  237. X = Pos.Right (_cbClipContentOnly) + 1,
  238. Y = Pos.Bottom (labelContentSize),
  239. CanFocus = true
  240. };
  241. _cbTransparent.CheckedStateChanging += TransparentToggle;
  242. void TransparentToggle (object? sender, CancelEventArgs<CheckState> e)
  243. {
  244. if (e.NewValue == CheckState.Checked)
  245. {
  246. ViewToEdit!.ViewportSettings |= Terminal.Gui.ViewportSettings.Transparent;
  247. }
  248. else
  249. {
  250. ViewToEdit!.ViewportSettings &= ~Terminal.Gui.ViewportSettings.Transparent;
  251. }
  252. }
  253. _cbVerticalScrollBar = new()
  254. {
  255. Title = "VerticalScrollBar",
  256. X = 0,
  257. Y = Pos.Bottom (_cbClearContentOnly),
  258. CanFocus = false
  259. };
  260. _cbVerticalScrollBar.CheckedStateChanging += VerticalScrollBarToggle;
  261. void VerticalScrollBarToggle (object? sender, CancelEventArgs<CheckState> e)
  262. {
  263. ViewToEdit!.VerticalScrollBar.Visible = e.NewValue == CheckState.Checked;
  264. }
  265. _cbAutoShowVerticalScrollBar = new()
  266. {
  267. Title = "AutoShow",
  268. X = Pos.Right (_cbVerticalScrollBar) + 1,
  269. Y = Pos.Top (_cbVerticalScrollBar),
  270. CanFocus = false
  271. };
  272. _cbAutoShowVerticalScrollBar.CheckedStateChanging += AutoShowVerticalScrollBarToggle;
  273. void AutoShowVerticalScrollBarToggle (object? sender, CancelEventArgs<CheckState> e)
  274. {
  275. ViewToEdit!.VerticalScrollBar.AutoShow = e.NewValue == CheckState.Checked;
  276. }
  277. _cbHorizontalScrollBar = new()
  278. {
  279. Title = "HorizontalScrollBar",
  280. X = 0,
  281. Y = Pos.Bottom (_cbVerticalScrollBar),
  282. CanFocus = false
  283. };
  284. _cbHorizontalScrollBar.CheckedStateChanging += HorizontalScrollBarToggle;
  285. void HorizontalScrollBarToggle (object? sender, CancelEventArgs<CheckState> e)
  286. {
  287. ViewToEdit!.HorizontalScrollBar.Visible = e.NewValue == CheckState.Checked;
  288. }
  289. _cbAutoShowHorizontalScrollBar = new()
  290. {
  291. Title = "AutoShow ",
  292. X = Pos.Right (_cbHorizontalScrollBar) + 1,
  293. Y = Pos.Top (_cbHorizontalScrollBar),
  294. CanFocus = false
  295. };
  296. _cbAutoShowHorizontalScrollBar.CheckedStateChanging += AutoShowHorizontalScrollBarToggle;
  297. void AutoShowHorizontalScrollBarToggle (object? sender, CancelEventArgs<CheckState> e)
  298. {
  299. ViewToEdit!.HorizontalScrollBar.AutoShow = e.NewValue == CheckState.Checked;
  300. }
  301. Add (
  302. labelContentSize,
  303. _contentSizeWidth,
  304. labelComma,
  305. _contentSizeHeight,
  306. _cbClearContentOnly,
  307. _cbClipContentOnly,
  308. _cbTransparent,
  309. _cbVerticalScrollBar,
  310. _cbHorizontalScrollBar,
  311. _cbAutoShowVerticalScrollBar,
  312. _cbAutoShowHorizontalScrollBar);
  313. }
  314. }