BordersOnContainers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. public class BordersOnContainers : Window {
  7. public BordersOnContainers (NStack.ustring title, string typeName, View smartView)
  8. {
  9. var borderStyle = BorderStyle.Double;
  10. var drawMarginFrame = false;
  11. var borderThickness = new Thickness (1, 2, 3, 4);
  12. var borderBrush = Colors.Base.HotFocus.Foreground;
  13. var padding = new Thickness (1, 2, 3, 4);
  14. var background = Colors.Base.HotNormal.Foreground;
  15. var effect3D = true;
  16. smartView.X = Pos.Center ();
  17. smartView.Y = 0;
  18. smartView.Width = 40;
  19. smartView.Height = 20;
  20. smartView.Border = new Border () {
  21. BorderStyle = borderStyle,
  22. DrawMarginFrame = drawMarginFrame,
  23. BorderThickness = borderThickness,
  24. BorderBrush = borderBrush,
  25. Padding = padding,
  26. Background = background,
  27. Effect3D = effect3D,
  28. Title = typeName
  29. };
  30. smartView.ColorScheme = Colors.TopLevel;
  31. var tf1 = new TextField ("1234567890") { Width = 10 };
  32. var button = new Button ("Press me!") {
  33. X = Pos.Center (),
  34. Y = Pos.Center (),
  35. };
  36. button.Clicked += (s,e) => MessageBox.Query (20, 7, "Hi", $"I'm a {typeName}?", "Yes", "No");
  37. var label = new Label ($"I'm a {typeName}") {
  38. X = Pos.Center (),
  39. Y = Pos.Center () - 1,
  40. };
  41. var tf2 = new TextField ("1234567890") {
  42. X = Pos.AnchorEnd (10),
  43. Y = Pos.AnchorEnd (1),
  44. Width = 10
  45. };
  46. var tv = new TextView () {
  47. Y = Pos.AnchorEnd (2),
  48. Width = 10,
  49. Height = Dim.Fill (),
  50. Text = "1234567890"
  51. };
  52. smartView.Add (tf1, button, label, tf2, tv);
  53. Add (new Label ("Padding:") {
  54. X = Pos.Center () - 23,
  55. });
  56. var paddingTopEdit = new TextField ("") {
  57. X = Pos.Center () - 22,
  58. Y = 1,
  59. Width = 5
  60. };
  61. paddingTopEdit.TextChanging += (e) => {
  62. try {
  63. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  64. int.Parse (e.NewText.ToString ()), smartView.Border.Padding.Right,
  65. smartView.Border.Padding.Bottom);
  66. } catch {
  67. if (!e.NewText.IsEmpty) {
  68. e.Cancel = true;
  69. }
  70. }
  71. };
  72. paddingTopEdit.Text = $"{smartView.Border.Padding.Top}";
  73. Add (paddingTopEdit);
  74. var paddingLeftEdit = new TextField ("") {
  75. X = Pos.Center () - 30,
  76. Y = 2,
  77. Width = 5
  78. };
  79. paddingLeftEdit.TextChanging += (e) => {
  80. try {
  81. smartView.Border.Padding = new Thickness (int.Parse (e.NewText.ToString ()),
  82. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  83. smartView.Border.Padding.Bottom);
  84. } catch {
  85. if (!e.NewText.IsEmpty) {
  86. e.Cancel = true;
  87. }
  88. }
  89. };
  90. paddingLeftEdit.Text = $"{smartView.Border.Padding.Left}";
  91. Add (paddingLeftEdit);
  92. var paddingRightEdit = new TextField ("") {
  93. X = Pos.Center () - 15,
  94. Y = 2,
  95. Width = 5
  96. };
  97. paddingRightEdit.TextChanging += (e) => {
  98. try {
  99. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  100. smartView.Border.Padding.Top, int.Parse (e.NewText.ToString ()),
  101. smartView.Border.Padding.Bottom);
  102. } catch {
  103. if (!e.NewText.IsEmpty) {
  104. e.Cancel = true;
  105. }
  106. }
  107. };
  108. paddingRightEdit.Text = $"{smartView.Border.Padding.Right}";
  109. Add (paddingRightEdit);
  110. var paddingBottomEdit = new TextField ("") {
  111. X = Pos.Center () - 22,
  112. Y = 3,
  113. Width = 5
  114. };
  115. paddingBottomEdit.TextChanging += (e) => {
  116. try {
  117. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  118. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  119. int.Parse (e.NewText.ToString ()));
  120. } catch {
  121. if (!e.NewText.IsEmpty) {
  122. e.Cancel = true;
  123. }
  124. }
  125. };
  126. paddingBottomEdit.Text = $"{smartView.Border.Padding.Bottom}";
  127. Add (paddingBottomEdit);
  128. var replacePadding = new Button ("Replace all based on top") {
  129. X = Pos.Left (paddingLeftEdit),
  130. Y = 5
  131. };
  132. replacePadding.Clicked += (s,e) => {
  133. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Top);
  134. if (paddingTopEdit.Text.IsEmpty) {
  135. paddingTopEdit.Text = "0";
  136. }
  137. paddingBottomEdit.Text = paddingLeftEdit.Text = paddingRightEdit.Text = paddingTopEdit.Text;
  138. };
  139. Add (replacePadding);
  140. Add (new Label ("Border:") {
  141. X = Pos.Center () + 11,
  142. });
  143. var borderTopEdit = new TextField ("") {
  144. X = Pos.Center () + 12,
  145. Y = 1,
  146. Width = 5
  147. };
  148. borderTopEdit.TextChanging += (e) => {
  149. try {
  150. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  151. int.Parse (e.NewText.ToString ()), smartView.Border.BorderThickness.Right,
  152. smartView.Border.BorderThickness.Bottom);
  153. } catch {
  154. if (!e.NewText.IsEmpty) {
  155. e.Cancel = true;
  156. }
  157. }
  158. };
  159. borderTopEdit.Text = $"{smartView.Border.BorderThickness.Top}";
  160. Add (borderTopEdit);
  161. var borderLeftEdit = new TextField ("") {
  162. X = Pos.Center () + 5,
  163. Y = 2,
  164. Width = 5
  165. };
  166. borderLeftEdit.TextChanging += (e) => {
  167. try {
  168. smartView.Border.BorderThickness = new Thickness (int.Parse (e.NewText.ToString ()),
  169. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  170. smartView.Border.BorderThickness.Bottom);
  171. } catch {
  172. if (!e.NewText.IsEmpty) {
  173. e.Cancel = true;
  174. }
  175. }
  176. };
  177. borderLeftEdit.Text = $"{smartView.Border.BorderThickness.Left}";
  178. Add (borderLeftEdit);
  179. var borderRightEdit = new TextField ("") {
  180. X = Pos.Center () + 19,
  181. Y = 2,
  182. Width = 5
  183. };
  184. borderRightEdit.TextChanging += (e) => {
  185. try {
  186. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  187. smartView.Border.BorderThickness.Top, int.Parse (e.NewText.ToString ()),
  188. smartView.Border.BorderThickness.Bottom);
  189. } catch {
  190. if (!e.NewText.IsEmpty) {
  191. e.Cancel = true;
  192. }
  193. }
  194. };
  195. borderRightEdit.Text = $"{smartView.Border.BorderThickness.Right}";
  196. Add (borderRightEdit);
  197. var borderBottomEdit = new TextField ("") {
  198. X = Pos.Center () + 12,
  199. Y = 3,
  200. Width = 5
  201. };
  202. borderBottomEdit.TextChanging += (e) => {
  203. try {
  204. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  205. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  206. int.Parse (e.NewText.ToString ()));
  207. } catch {
  208. if (!e.NewText.IsEmpty) {
  209. e.Cancel = true;
  210. }
  211. }
  212. };
  213. borderBottomEdit.Text = $"{smartView.Border.BorderThickness.Bottom}";
  214. Add (borderBottomEdit);
  215. var replaceBorder = new Button ("Replace all based on top") {
  216. X = Pos.Left (borderLeftEdit),
  217. Y = 5
  218. };
  219. replaceBorder.Clicked += (s,e) => {
  220. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Top);
  221. if (borderTopEdit.Text.IsEmpty) {
  222. borderTopEdit.Text = "0";
  223. }
  224. borderBottomEdit.Text = borderLeftEdit.Text = borderRightEdit.Text = borderTopEdit.Text;
  225. };
  226. Add (replaceBorder);
  227. smartView.Y = Pos.Center () + 4;
  228. Add (new Label ("BorderStyle:"));
  229. var borderStyleEnum = Enum.GetValues (typeof (BorderStyle)).Cast<BorderStyle> ().ToList ();
  230. var rbBorderStyle = new RadioGroup (borderStyleEnum.Select (
  231. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  232. X = 2,
  233. Y = 1,
  234. SelectedItem = (int)smartView.Border.BorderStyle
  235. };
  236. Add (rbBorderStyle);
  237. var cbDrawMarginFrame = new CheckBox ("Draw Margin Frame", smartView.Border.DrawMarginFrame) {
  238. X = Pos.AnchorEnd (20),
  239. Y = 0,
  240. Width = 5
  241. };
  242. cbDrawMarginFrame.Toggled += (s, e) => {
  243. try {
  244. smartView.Border.DrawMarginFrame = (bool)cbDrawMarginFrame.Checked;
  245. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  246. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  247. }
  248. } catch { }
  249. };
  250. Add (cbDrawMarginFrame);
  251. rbBorderStyle.SelectedItemChanged += (e) => {
  252. smartView.Border.BorderStyle = (BorderStyle)e.SelectedItem;
  253. smartView.SetNeedsDisplay ();
  254. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  255. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  256. }
  257. };
  258. var cbEffect3D = new CheckBox ("Draw 3D effects", smartView.Border.Effect3D) {
  259. X = Pos.AnchorEnd (20),
  260. Y = 1,
  261. Width = 5
  262. };
  263. Add (cbEffect3D);
  264. Add (new Label ("Effect3D Offset:") {
  265. X = Pos.AnchorEnd (20),
  266. Y = 2
  267. });
  268. Add (new Label ("X:") {
  269. X = Pos.AnchorEnd (19),
  270. Y = 3
  271. });
  272. var effect3DOffsetX = new TextField ("") {
  273. X = Pos.AnchorEnd (16),
  274. Y = 3,
  275. Width = 5
  276. };
  277. effect3DOffsetX.TextChanging += (e) => {
  278. try {
  279. smartView.Border.Effect3DOffset = new Point (int.Parse (e.NewText.ToString ()),
  280. smartView.Border.Effect3DOffset.Y);
  281. } catch {
  282. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  283. e.Cancel = true;
  284. }
  285. }
  286. };
  287. effect3DOffsetX.Text = $"{smartView.Border.Effect3DOffset.X}";
  288. Add (effect3DOffsetX);
  289. Add (new Label ("Y:") {
  290. X = Pos.AnchorEnd (10),
  291. Y = 3
  292. });
  293. var effect3DOffsetY = new TextField ("") {
  294. X = Pos.AnchorEnd (7),
  295. Y = 3,
  296. Width = 5
  297. };
  298. effect3DOffsetY.TextChanging += (e) => {
  299. try {
  300. smartView.Border.Effect3DOffset = new Point (smartView.Border.Effect3DOffset.X,
  301. int.Parse (e.NewText.ToString ()));
  302. } catch {
  303. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  304. e.Cancel = true;
  305. }
  306. }
  307. };
  308. effect3DOffsetY.Text = $"{smartView.Border.Effect3DOffset.Y}";
  309. Add (effect3DOffsetY);
  310. cbEffect3D.Toggled += (s, e) => {
  311. try {
  312. smartView.Border.Effect3D = effect3DOffsetX.Enabled =
  313. effect3DOffsetY.Enabled = (bool)cbEffect3D.Checked;
  314. } catch { }
  315. };
  316. Add (new Label ("Background:") {
  317. Y = 5
  318. });
  319. var colorEnum = Enum.GetValues (typeof (Color)).Cast<Color> ().ToList ();
  320. var rbBackground = new RadioGroup (colorEnum.Select (
  321. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  322. X = 2,
  323. Y = 6,
  324. SelectedItem = (int)smartView.Border.Background
  325. };
  326. rbBackground.SelectedItemChanged += (e) => {
  327. smartView.Border.Background = (Color)e.SelectedItem;
  328. };
  329. Add (rbBackground);
  330. Add (new Label ("BorderBrush:") {
  331. X = Pos.AnchorEnd (20),
  332. Y = 5
  333. });
  334. var rbBorderBrush = new RadioGroup (colorEnum.Select (
  335. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  336. X = Pos.AnchorEnd (18),
  337. Y = 6,
  338. SelectedItem = (int)smartView.Border.BorderBrush
  339. };
  340. rbBorderBrush.SelectedItemChanged += (e) => {
  341. smartView.Border.BorderBrush = (Color)e.SelectedItem;
  342. };
  343. Add (rbBorderBrush);
  344. Add (smartView);
  345. Title = title;
  346. }
  347. }
  348. }