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