BordersOnFrameView.cs 11 KB

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