Borders.cs 14 KB

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