Borders.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Borders with/without PanelView", Description: "Demonstrate with/without PanelView borders manipulation.")]
  7. [ScenarioCategory ("Layout")]
  8. [ScenarioCategory ("Borders")]
  9. public class Borders : Scenario {
  10. public override void Setup ()
  11. {
  12. var borderStyle = BorderStyle.Single;
  13. var drawMarginFrame = true;
  14. var borderThickness = new Thickness (2);
  15. var borderBrush = Color.Red;
  16. var padding = new Thickness (2);
  17. var background = Color.BrightGreen;
  18. var effect3D = true;
  19. var smartPanel = new PanelView () {
  20. X = Pos.Center () - 38,
  21. Y = Pos.Center () - 3,
  22. Width = 24,
  23. Height = 13,
  24. Border = new Border () {
  25. BorderStyle = borderStyle,
  26. DrawMarginFrame = drawMarginFrame,
  27. BorderThickness = borderThickness,
  28. BorderBrush = borderBrush,
  29. Padding = padding,
  30. Background = background,
  31. Effect3D = effect3D
  32. },
  33. };
  34. smartPanel.Add (new Label () { // Or smartPanel.Child =
  35. X = 0,
  36. Y = 0,
  37. //Width = 24, commenting because now setting the size disable auto-size
  38. //Height = 13,
  39. ColorScheme = Colors.TopLevel,
  40. Text = "This is a test\nwith a \nPanelView",
  41. TextAlignment = TextAlignment.Centered
  42. });
  43. // Can be initialized this way too.
  44. //var smartPanel = new PanelView (new Label () {
  45. // X = Pos.Center () - 38,
  46. // Y = Pos.Center () - 3,
  47. // Width = 24,
  48. // Height = 13,
  49. // Border = new Border () {
  50. // BorderStyle = borderStyle,
  51. // DrawMarginFrame = drawMarginFrame,
  52. // BorderThickness = borderThickness,
  53. // BorderBrush = borderBrush,
  54. // Padding = padding,
  55. // Background = background,
  56. // Effect3D = effect3D
  57. // },
  58. // ColorScheme = Colors.TopLevel,
  59. // Text = "This is a test\nwith a \nPanelView",
  60. // TextAlignment = TextAlignment.Centered
  61. //}) {
  62. // X = Pos.Center () - 38,
  63. // Y = Pos.Center () - 3,
  64. // Width = 24,
  65. // Height = 13
  66. //};
  67. var smartView = new Label () {
  68. X = Pos.Center () + 10,
  69. Y = Pos.Center () + 2,
  70. Border = new Border () {
  71. BorderStyle = borderStyle,
  72. DrawMarginFrame = drawMarginFrame,
  73. BorderThickness = borderThickness,
  74. BorderBrush = borderBrush,
  75. Padding = padding,
  76. Background = background,
  77. Effect3D = effect3D
  78. },
  79. ColorScheme = Colors.TopLevel,
  80. Text = "This is a test\nwithout a \nPanelView",
  81. TextAlignment = TextAlignment.Centered
  82. };
  83. smartView.Border.Child = smartView;
  84. Win.Add (new Label ("Padding:") {
  85. X = Pos.Center () - 23,
  86. });
  87. var paddingTopEdit = new TextField ("") {
  88. X = Pos.Center () - 22,
  89. Y = 1,
  90. Width = 5
  91. };
  92. paddingTopEdit.TextChanging += (e) => {
  93. try {
  94. smartPanel.Child.Border.Padding = new Thickness (smartPanel.Child.Border.Padding.Left,
  95. int.Parse (e.NewText.ToString ()), smartPanel.Child.Border.Padding.Right,
  96. smartPanel.Child.Border.Padding.Bottom);
  97. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  98. int.Parse (e.NewText.ToString ()), smartView.Border.Padding.Right,
  99. smartView.Border.Padding.Bottom);
  100. } catch {
  101. if (!e.NewText.IsEmpty) {
  102. e.Cancel = true;
  103. }
  104. }
  105. };
  106. paddingTopEdit.Text = $"{smartView.Border.Padding.Top}";
  107. Win.Add (paddingTopEdit);
  108. var paddingLeftEdit = new TextField ("") {
  109. X = Pos.Center () - 30,
  110. Y = 2,
  111. Width = 5
  112. };
  113. paddingLeftEdit.TextChanging += (e) => {
  114. try {
  115. smartPanel.Child.Border.Padding = new Thickness (int.Parse (e.NewText.ToString ()),
  116. smartPanel.Child.Border.Padding.Top, smartPanel.Child.Border.Padding.Right,
  117. smartPanel.Child.Border.Padding.Bottom);
  118. smartView.Border.Padding = new Thickness (int.Parse (e.NewText.ToString ()),
  119. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  120. smartView.Border.Padding.Bottom);
  121. } catch {
  122. if (!e.NewText.IsEmpty) {
  123. e.Cancel = true;
  124. }
  125. }
  126. };
  127. paddingLeftEdit.Text = $"{smartView.Border.Padding.Left}";
  128. Win.Add (paddingLeftEdit);
  129. var paddingRightEdit = new TextField ("") {
  130. X = Pos.Center () - 15,
  131. Y = 2,
  132. Width = 5
  133. };
  134. paddingRightEdit.TextChanging += (e) => {
  135. try {
  136. smartPanel.Child.Border.Padding = new Thickness (smartPanel.Child.Border.Padding.Left,
  137. smartPanel.Child.Border.Padding.Top, int.Parse (e.NewText.ToString ()),
  138. smartPanel.Child.Border.Padding.Bottom);
  139. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  140. smartView.Border.Padding.Top, int.Parse (e.NewText.ToString ()),
  141. smartView.Border.Padding.Bottom);
  142. } catch {
  143. if (!e.NewText.IsEmpty) {
  144. e.Cancel = true;
  145. }
  146. }
  147. };
  148. paddingRightEdit.Text = $"{smartView.Border.Padding.Right}";
  149. Win.Add (paddingRightEdit);
  150. var paddingBottomEdit = new TextField ("") {
  151. X = Pos.Center () - 22,
  152. Y = 3,
  153. Width = 5
  154. };
  155. paddingBottomEdit.TextChanging += (e) => {
  156. try {
  157. smartPanel.Child.Border.Padding = new Thickness (smartPanel.Child.Border.Padding.Left,
  158. smartPanel.Child.Border.Padding.Top, smartPanel.Child.Border.Padding.Right,
  159. int.Parse (e.NewText.ToString ()));
  160. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  161. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  162. int.Parse (e.NewText.ToString ()));
  163. } catch {
  164. if (!e.NewText.IsEmpty) {
  165. e.Cancel = true;
  166. }
  167. }
  168. };
  169. paddingBottomEdit.Text = $"{smartView.Border.Padding.Bottom}";
  170. Win.Add (paddingBottomEdit);
  171. var replacePadding = new Button ("Replace all based on top") {
  172. X = Pos.Center () - 35,
  173. Y = 5
  174. };
  175. replacePadding.Clicked += () => {
  176. smartPanel.Child.Border.Padding = new Thickness (smartPanel.Child.Border.Padding.Top);
  177. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Top);
  178. if (paddingTopEdit.Text.IsEmpty) {
  179. paddingTopEdit.Text = "0";
  180. }
  181. paddingBottomEdit.Text = paddingLeftEdit.Text = paddingRightEdit.Text = paddingTopEdit.Text;
  182. };
  183. Win.Add (replacePadding);
  184. var cbUseUsePanelFrame = new CheckBox ("UsePanelFrame") {
  185. X = Pos.X (replacePadding),
  186. Y = Pos.Y (replacePadding) + 3,
  187. Checked = smartPanel.UsePanelFrame
  188. };
  189. cbUseUsePanelFrame.Toggled += (e) => smartPanel.UsePanelFrame = !e;
  190. Win.Add (cbUseUsePanelFrame);
  191. Win.Add (new Label ("Border:") {
  192. X = Pos.Center () + 11,
  193. });
  194. var borderTopEdit = new TextField ("") {
  195. X = Pos.Center () + 12,
  196. Y = 1,
  197. Width = 5
  198. };
  199. borderTopEdit.TextChanging += (e) => {
  200. try {
  201. smartPanel.Child.Border.BorderThickness = new Thickness (smartPanel.Child.Border.BorderThickness.Left,
  202. int.Parse (e.NewText.ToString ()), smartPanel.Child.Border.BorderThickness.Right,
  203. smartPanel.Child.Border.BorderThickness.Bottom);
  204. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  205. int.Parse (e.NewText.ToString ()), smartView.Border.BorderThickness.Right,
  206. smartView.Border.BorderThickness.Bottom);
  207. } catch {
  208. if (!e.NewText.IsEmpty) {
  209. e.Cancel = true;
  210. }
  211. }
  212. };
  213. borderTopEdit.Text = $"{smartView.Border.BorderThickness.Top}";
  214. Win.Add (borderTopEdit);
  215. var borderLeftEdit = new TextField ("") {
  216. X = Pos.Center () + 5,
  217. Y = 2,
  218. Width = 5
  219. };
  220. borderLeftEdit.TextChanging += (e) => {
  221. try {
  222. smartPanel.Child.Border.BorderThickness = new Thickness (int.Parse (e.NewText.ToString ()),
  223. smartPanel.Child.Border.BorderThickness.Top, smartPanel.Child.Border.BorderThickness.Right,
  224. smartPanel.Child.Border.BorderThickness.Bottom);
  225. smartView.Border.BorderThickness = new Thickness (int.Parse (e.NewText.ToString ()),
  226. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  227. smartView.Border.BorderThickness.Bottom);
  228. } catch {
  229. if (!e.NewText.IsEmpty) {
  230. e.Cancel = true;
  231. }
  232. }
  233. };
  234. borderLeftEdit.Text = $"{smartView.Border.BorderThickness.Left}";
  235. Win.Add (borderLeftEdit);
  236. var borderRightEdit = new TextField ("") {
  237. X = Pos.Center () + 19,
  238. Y = 2,
  239. Width = 5
  240. };
  241. borderRightEdit.TextChanging += (e) => {
  242. try {
  243. smartPanel.Child.Border.BorderThickness = new Thickness (smartPanel.Child.Border.BorderThickness.Left,
  244. smartPanel.Child.Border.BorderThickness.Top, int.Parse (e.NewText.ToString ()),
  245. smartPanel.Child.Border.BorderThickness.Bottom);
  246. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  247. smartView.Border.BorderThickness.Top, int.Parse (e.NewText.ToString ()),
  248. smartView.Border.BorderThickness.Bottom);
  249. } catch {
  250. if (!e.NewText.IsEmpty) {
  251. e.Cancel = true;
  252. }
  253. }
  254. };
  255. borderRightEdit.Text = $"{smartView.Border.BorderThickness.Right}";
  256. Win.Add (borderRightEdit);
  257. var borderBottomEdit = new TextField ("") {
  258. X = Pos.Center () + 12,
  259. Y = 3,
  260. Width = 5
  261. };
  262. borderBottomEdit.TextChanging += (e) => {
  263. try {
  264. smartPanel.Child.Border.BorderThickness = new Thickness (smartPanel.Child.Border.BorderThickness.Left,
  265. smartPanel.Child.Border.BorderThickness.Top, smartPanel.Child.Border.BorderThickness.Right,
  266. int.Parse (e.NewText.ToString ()));
  267. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  268. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  269. int.Parse (e.NewText.ToString ()));
  270. } catch {
  271. if (!e.NewText.IsEmpty) {
  272. e.Cancel = true;
  273. }
  274. }
  275. };
  276. borderBottomEdit.Text = $"{smartView.Border.BorderThickness.Bottom}";
  277. Win.Add (borderBottomEdit);
  278. var replaceBorder = new Button ("Replace all based on top") {
  279. X = Pos.Center () + 1,
  280. Y = 5
  281. };
  282. replaceBorder.Clicked += () => {
  283. smartPanel.Child.Border.BorderThickness = new Thickness (smartPanel.Child.Border.BorderThickness.Top);
  284. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Top);
  285. if (borderTopEdit.Text.IsEmpty) {
  286. borderTopEdit.Text = "0";
  287. }
  288. borderBottomEdit.Text = borderLeftEdit.Text = borderRightEdit.Text = borderTopEdit.Text;
  289. };
  290. Win.Add (replaceBorder);
  291. Win.Add (new Label ("BorderStyle:"));
  292. var borderStyleEnum = Enum.GetValues (typeof (BorderStyle)).Cast<BorderStyle> ().ToList ();
  293. var rbBorderStyle = new RadioGroup (borderStyleEnum.Select (
  294. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  295. X = 2,
  296. Y = 1,
  297. SelectedItem = (int)smartView.Border.BorderStyle
  298. };
  299. Win.Add (rbBorderStyle);
  300. var cbDrawMarginFrame = new CheckBox ("Draw Margin Frame", smartView.Border.DrawMarginFrame) {
  301. X = Pos.AnchorEnd (20),
  302. Y = 0,
  303. Width = 5
  304. };
  305. cbDrawMarginFrame.Toggled += (e) => {
  306. try {
  307. smartPanel.Child.Border.DrawMarginFrame = cbDrawMarginFrame.Checked;
  308. smartView.Border.DrawMarginFrame = cbDrawMarginFrame.Checked;
  309. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  310. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  311. }
  312. } catch { }
  313. };
  314. Win.Add (cbDrawMarginFrame);
  315. rbBorderStyle.SelectedItemChanged += (e) => {
  316. smartPanel.Child.Border.BorderStyle = (BorderStyle)e.SelectedItem;
  317. smartView.Border.BorderStyle = (BorderStyle)e.SelectedItem;
  318. smartView.SetNeedsDisplay ();
  319. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  320. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  321. }
  322. };
  323. var cbEffect3D = new CheckBox ("Draw 3D effects", smartView.Border.Effect3D) {
  324. X = Pos.AnchorEnd (20),
  325. Y = 1,
  326. Width = 5
  327. };
  328. Win.Add (cbEffect3D);
  329. Win.Add (new Label ("Effect3D Offset:") {
  330. X = Pos.AnchorEnd (20),
  331. Y = 2
  332. });
  333. Win.Add (new Label ("X:") {
  334. X = Pos.AnchorEnd (19),
  335. Y = 3
  336. });
  337. var effect3DOffsetX = new TextField ("") {
  338. X = Pos.AnchorEnd (16),
  339. Y = 3,
  340. Width = 5
  341. };
  342. effect3DOffsetX.TextChanging += (e) => {
  343. try {
  344. smartPanel.Child.Border.Effect3DOffset = new Point (int.Parse (e.NewText.ToString ()),
  345. smartPanel.Child.Border.Effect3DOffset.Y);
  346. smartView.Border.Effect3DOffset = new Point (int.Parse (e.NewText.ToString ()),
  347. smartView.Border.Effect3DOffset.Y);
  348. } catch {
  349. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  350. e.Cancel = true;
  351. }
  352. }
  353. };
  354. effect3DOffsetX.Text = $"{smartView.Border.Effect3DOffset.X}";
  355. Win.Add (effect3DOffsetX);
  356. Win.Add (new Label ("Y:") {
  357. X = Pos.AnchorEnd (10),
  358. Y = 3
  359. });
  360. var effect3DOffsetY = new TextField ("") {
  361. X = Pos.AnchorEnd (7),
  362. Y = 3,
  363. Width = 5
  364. };
  365. effect3DOffsetY.TextChanging += (e) => {
  366. try {
  367. smartPanel.Child.Border.Effect3DOffset = new Point (smartPanel.Child.Border.Effect3DOffset.X,
  368. int.Parse (e.NewText.ToString ()));
  369. smartView.Border.Effect3DOffset = new Point (smartView.Border.Effect3DOffset.X,
  370. int.Parse (e.NewText.ToString ()));
  371. } catch {
  372. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  373. e.Cancel = true;
  374. }
  375. }
  376. };
  377. effect3DOffsetY.Text = $"{smartView.Border.Effect3DOffset.Y}";
  378. Win.Add (effect3DOffsetY);
  379. cbEffect3D.Toggled += (e) => {
  380. try {
  381. smartPanel.Child.Border.Effect3D = smartView.Border.Effect3D = effect3DOffsetX.Enabled =
  382. effect3DOffsetY.Enabled = cbEffect3D.Checked;
  383. } catch { }
  384. };
  385. Win.Add (new Label ("Background:") {
  386. Y = 5
  387. });
  388. var colorEnum = Enum.GetValues (typeof (Color)).Cast<Color> ().ToList ();
  389. var rbBackground = new RadioGroup (colorEnum.Select (
  390. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  391. X = 2,
  392. Y = 6,
  393. SelectedItem = (int)smartView.Border.Background
  394. };
  395. rbBackground.SelectedItemChanged += (e) => {
  396. smartPanel.Child.Border.Background = smartView.Border.Background = (Color)e.SelectedItem;
  397. };
  398. Win.Add (rbBackground);
  399. Win.Add (new Label ("BorderBrush:") {
  400. X = Pos.AnchorEnd (20),
  401. Y = 5
  402. });
  403. var rbBorderBrush = new RadioGroup (colorEnum.Select (
  404. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  405. X = Pos.AnchorEnd (18),
  406. Y = 6,
  407. SelectedItem = (int)smartView.Border.BorderBrush
  408. };
  409. rbBorderBrush.SelectedItemChanged += (e) => {
  410. smartPanel.Child.Border.BorderBrush = smartView.Border.BorderBrush = (Color)e.SelectedItem;
  411. };
  412. Win.Add (rbBorderBrush);
  413. Win.Add (smartPanel);
  414. Win.Add (smartView);
  415. Win.BringSubviewToFront (smartPanel);
  416. }
  417. }
  418. }