TextAlignmentsAndDirection.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("Text Alignment and Direction", "Demos horizontal and vertical text alignment and text direction.")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class TextAlignmentsAndDirections : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. Window app = new ()
  15. {
  16. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  17. };
  18. // string txt = ".\n...\n.....\nHELLO\n.....\n...\n.";
  19. // string txt = "┌──┴──┐\n┤HELLO├\n└──┬──┘";
  20. var txt = "HELLO WORLD";
  21. var color1 = new ColorScheme { Normal = new (Color.Black, Color.Gray) };
  22. var color2 = new ColorScheme { Normal = new (Color.Black, Color.DarkGray) };
  23. List<Label> txts = new (); // single line
  24. List<Label> mtxts = new (); // multi line
  25. // Horizontal Single-Line
  26. var labelHL = new Label
  27. {
  28. X = 1,
  29. Y = 1,
  30. AutoSize = false,
  31. Width = 9,
  32. Height = 1,
  33. TextAlignment = TextAlignment.Right,
  34. ColorScheme = Colors.ColorSchemes ["Dialog"],
  35. Text = "Left"
  36. };
  37. var labelHC = new Label
  38. {
  39. X = 1,
  40. Y = 2,
  41. AutoSize = false,
  42. Width = 9,
  43. Height = 1,
  44. TextAlignment = TextAlignment.Right,
  45. ColorScheme = Colors.ColorSchemes ["Dialog"],
  46. Text = "Centered"
  47. };
  48. var labelHR = new Label
  49. {
  50. X = 1,
  51. Y = 3,
  52. AutoSize = false,
  53. Width = 9,
  54. Height = 1,
  55. TextAlignment = TextAlignment.Right,
  56. ColorScheme = Colors.ColorSchemes ["Dialog"],
  57. Text = "Right"
  58. };
  59. var labelHJ = new Label
  60. {
  61. X = 1,
  62. Y = 4,
  63. AutoSize = false,
  64. Width = 9,
  65. Height = 1,
  66. TextAlignment = TextAlignment.Right,
  67. ColorScheme = Colors.ColorSchemes ["Dialog"],
  68. Text = "Justified"
  69. };
  70. var txtLabelHL = new Label
  71. {
  72. X = Pos.Right (labelHL) + 1,
  73. Y = Pos.Y (labelHL),
  74. AutoSize = false,
  75. Width = Dim.Fill (1) - 9,
  76. Height = 1,
  77. ColorScheme = color1,
  78. TextAlignment = TextAlignment.Left,
  79. Text = txt
  80. };
  81. var txtLabelHC = new Label
  82. {
  83. X = Pos.Right (labelHC) + 1,
  84. Y = Pos.Y (labelHC),
  85. AutoSize = false,
  86. Width = Dim.Fill (1) - 9,
  87. Height = 1,
  88. ColorScheme = color2,
  89. TextAlignment = TextAlignment.Centered,
  90. Text = txt
  91. };
  92. var txtLabelHR = new Label
  93. {
  94. X = Pos.Right (labelHR) + 1,
  95. Y = Pos.Y (labelHR),
  96. AutoSize = false,
  97. Width = Dim.Fill (1) - 9,
  98. Height = 1,
  99. ColorScheme = color1,
  100. TextAlignment = TextAlignment.Right,
  101. Text = txt
  102. };
  103. var txtLabelHJ = new Label
  104. {
  105. X = Pos.Right (labelHJ) + 1,
  106. Y = Pos.Y (labelHJ),
  107. AutoSize = false,
  108. Width = Dim.Fill (1) - 9,
  109. Height = 1,
  110. ColorScheme = color2,
  111. TextAlignment = TextAlignment.Justified,
  112. Text = txt
  113. };
  114. txts.Add (txtLabelHL);
  115. txts.Add (txtLabelHC);
  116. txts.Add (txtLabelHR);
  117. txts.Add (txtLabelHJ);
  118. app.Add (labelHL);
  119. app.Add (txtLabelHL);
  120. app.Add (labelHC);
  121. app.Add (txtLabelHC);
  122. app.Add (labelHR);
  123. app.Add (txtLabelHR);
  124. app.Add (labelHJ);
  125. app.Add (txtLabelHJ);
  126. // Vertical Single-Line
  127. var labelVT = new Label
  128. {
  129. X = Pos.AnchorEnd (8),
  130. Y = 1,
  131. AutoSize = false,
  132. Width = 2,
  133. Height = 9,
  134. ColorScheme = color1,
  135. TextDirection = TextDirection.TopBottom_LeftRight,
  136. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  137. Text = "Top"
  138. };
  139. var labelVM = new Label
  140. {
  141. X = Pos.AnchorEnd (6),
  142. Y = 1,
  143. AutoSize = false,
  144. Width = 2,
  145. Height = 9,
  146. ColorScheme = color1,
  147. TextDirection = TextDirection.TopBottom_LeftRight,
  148. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  149. Text = "Middle"
  150. };
  151. var labelVB = new Label
  152. {
  153. X = Pos.AnchorEnd (4),
  154. Y = 1,
  155. AutoSize = false,
  156. Width = 2,
  157. Height = 9,
  158. ColorScheme = color1,
  159. TextDirection = TextDirection.TopBottom_LeftRight,
  160. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  161. Text = "Bottom"
  162. };
  163. var labelVJ = new Label
  164. {
  165. X = Pos.AnchorEnd (2),
  166. Y = 1,
  167. AutoSize = false,
  168. Width = 1,
  169. Height = 9,
  170. ColorScheme = color1,
  171. TextDirection = TextDirection.TopBottom_LeftRight,
  172. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  173. Text = "Justified"
  174. };
  175. var txtLabelVT = new Label
  176. {
  177. X = Pos.X (labelVT),
  178. Y = Pos.Bottom (labelVT) + 1,
  179. AutoSize = false,
  180. Width = 1,
  181. Height = Dim.Fill (1),
  182. ColorScheme = color1,
  183. TextDirection = TextDirection.TopBottom_LeftRight,
  184. VerticalTextAlignment = VerticalTextAlignment.Top,
  185. Text = txt
  186. };
  187. var txtLabelVM = new Label
  188. {
  189. X = Pos.X (labelVM),
  190. Y = Pos.Bottom (labelVM) + 1,
  191. AutoSize = false,
  192. Width = 1,
  193. Height = Dim.Fill (1),
  194. ColorScheme = color2,
  195. TextDirection = TextDirection.TopBottom_LeftRight,
  196. VerticalTextAlignment = VerticalTextAlignment.Middle,
  197. Text = txt
  198. };
  199. var txtLabelVB = new Label
  200. {
  201. X = Pos.X (labelVB),
  202. Y = Pos.Bottom (labelVB) + 1,
  203. AutoSize = false,
  204. Width = 1,
  205. Height = Dim.Fill (1),
  206. ColorScheme = color1,
  207. TextDirection = TextDirection.TopBottom_LeftRight,
  208. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  209. Text = txt
  210. };
  211. var txtLabelVJ = new Label
  212. {
  213. X = Pos.X (labelVJ),
  214. Y = Pos.Bottom (labelVJ) + 1,
  215. AutoSize = false,
  216. Width = 1,
  217. Height = Dim.Fill (1),
  218. ColorScheme = color2,
  219. TextDirection = TextDirection.TopBottom_LeftRight,
  220. VerticalTextAlignment = VerticalTextAlignment.Justified,
  221. Text = txt
  222. };
  223. txts.Add (txtLabelVT);
  224. txts.Add (txtLabelVM);
  225. txts.Add (txtLabelVB);
  226. txts.Add (txtLabelVJ);
  227. app.Add (labelVT);
  228. app.Add (txtLabelVT);
  229. app.Add (labelVM);
  230. app.Add (txtLabelVM);
  231. app.Add (labelVB);
  232. app.Add (txtLabelVB);
  233. app.Add (labelVJ);
  234. app.Add (txtLabelVJ);
  235. // Multi-Line
  236. var container = new View
  237. {
  238. X = 0,
  239. Y = Pos.Bottom (txtLabelHJ),
  240. Width = Dim.Fill (31),
  241. Height = Dim.Fill (6),
  242. ColorScheme = color2
  243. };
  244. var txtLabelTL = new Label
  245. {
  246. X = 1 /* */,
  247. Y = 1,
  248. AutoSize = false,
  249. Width = Dim.Percent (100f / 3f),
  250. Height = Dim.Percent (100f / 3f),
  251. TextAlignment = TextAlignment.Left,
  252. VerticalTextAlignment = VerticalTextAlignment.Top,
  253. ColorScheme = color1,
  254. Text = txt
  255. };
  256. var txtLabelTC = new Label
  257. {
  258. X = Pos.Right (txtLabelTL) + 2,
  259. Y = 1,
  260. AutoSize = false,
  261. Width = Dim.Percent (100f / 3f),
  262. Height = Dim.Percent (100f / 3f),
  263. TextAlignment = TextAlignment.Centered,
  264. VerticalTextAlignment = VerticalTextAlignment.Top,
  265. ColorScheme = color1,
  266. Text = txt
  267. };
  268. var txtLabelTR = new Label
  269. {
  270. X = Pos.Right (txtLabelTC) + 2,
  271. Y = 1,
  272. AutoSize = false,
  273. Width = Dim.Percent (100f, true),
  274. Height = Dim.Percent (100f / 3f),
  275. TextAlignment = TextAlignment.Right,
  276. VerticalTextAlignment = VerticalTextAlignment.Top,
  277. ColorScheme = color1,
  278. Text = txt
  279. };
  280. var txtLabelML = new Label
  281. {
  282. X = Pos.X (txtLabelTL),
  283. Y = Pos.Bottom (txtLabelTL) + 1,
  284. AutoSize = false,
  285. Width = Dim.Width (txtLabelTL),
  286. Height = Dim.Percent (100f / 3f),
  287. TextAlignment = TextAlignment.Left,
  288. VerticalTextAlignment = VerticalTextAlignment.Middle,
  289. ColorScheme = color1,
  290. Text = txt
  291. };
  292. var txtLabelMC = new Label
  293. {
  294. X = Pos.X (txtLabelTC),
  295. Y = Pos.Bottom (txtLabelTC) + 1,
  296. AutoSize = false,
  297. Width = Dim.Width (txtLabelTC),
  298. Height = Dim.Percent (100f / 3f),
  299. TextAlignment = TextAlignment.Centered,
  300. VerticalTextAlignment = VerticalTextAlignment.Middle,
  301. ColorScheme = color1,
  302. Text = txt
  303. };
  304. var txtLabelMR = new Label
  305. {
  306. X = Pos.X (txtLabelTR),
  307. Y = Pos.Bottom (txtLabelTR) + 1,
  308. AutoSize = false,
  309. Width = Dim.Percent (100f, true),
  310. Height = Dim.Percent (100f / 3f),
  311. TextAlignment = TextAlignment.Right,
  312. VerticalTextAlignment = VerticalTextAlignment.Middle,
  313. ColorScheme = color1,
  314. Text = txt
  315. };
  316. var txtLabelBL = new Label
  317. {
  318. X = Pos.X (txtLabelML),
  319. Y = Pos.Bottom (txtLabelML) + 1,
  320. AutoSize = false,
  321. Width = Dim.Width (txtLabelML),
  322. Height = Dim.Percent (100f, true),
  323. TextAlignment = TextAlignment.Left,
  324. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  325. ColorScheme = color1,
  326. Text = txt
  327. };
  328. var txtLabelBC = new Label
  329. {
  330. X = Pos.X (txtLabelMC),
  331. Y = Pos.Bottom (txtLabelMC) + 1,
  332. AutoSize = false,
  333. Width = Dim.Width (txtLabelMC),
  334. Height = Dim.Percent (100f, true),
  335. TextAlignment = TextAlignment.Centered,
  336. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  337. ColorScheme = color1,
  338. Text = txt
  339. };
  340. var txtLabelBR = new Label
  341. {
  342. X = Pos.X (txtLabelMR),
  343. Y = Pos.Bottom (txtLabelMR) + 1,
  344. AutoSize = false,
  345. Width = Dim.Percent (100f, true),
  346. Height = Dim.Percent (100f, true),
  347. TextAlignment = TextAlignment.Right,
  348. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  349. ColorScheme = color1,
  350. Text = txt
  351. };
  352. mtxts.Add (txtLabelTL);
  353. mtxts.Add (txtLabelTC);
  354. mtxts.Add (txtLabelTR);
  355. mtxts.Add (txtLabelML);
  356. mtxts.Add (txtLabelMC);
  357. mtxts.Add (txtLabelMR);
  358. mtxts.Add (txtLabelBL);
  359. mtxts.Add (txtLabelBC);
  360. mtxts.Add (txtLabelBR);
  361. // Save Alignments in Data
  362. foreach (Label t in mtxts)
  363. {
  364. t.Data = new { h = t.TextAlignment, v = t.VerticalTextAlignment };
  365. }
  366. container.Add (txtLabelTL);
  367. container.Add (txtLabelTC);
  368. container.Add (txtLabelTR);
  369. container.Add (txtLabelML);
  370. container.Add (txtLabelMC);
  371. container.Add (txtLabelMR);
  372. container.Add (txtLabelBL);
  373. container.Add (txtLabelBC);
  374. container.Add (txtLabelBR);
  375. app.Add (container);
  376. // Edit Text
  377. var editText = new TextView
  378. {
  379. X = 1,
  380. Y = Pos.Bottom (container) + 1,
  381. Width = Dim.Fill (10),
  382. Height = Dim.Fill (1),
  383. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  384. Text = txt
  385. };
  386. editText.MouseClick += (s, m) =>
  387. {
  388. foreach (Label v in txts)
  389. {
  390. v.Text = editText.Text;
  391. }
  392. foreach (Label v in mtxts)
  393. {
  394. v.Text = editText.Text;
  395. }
  396. };
  397. app.KeyUp += (s, m) =>
  398. {
  399. foreach (Label v in txts)
  400. {
  401. v.Text = editText.Text;
  402. }
  403. foreach (Label v in mtxts)
  404. {
  405. v.Text = editText.Text;
  406. }
  407. };
  408. editText.SetFocus ();
  409. app.Add (editText);
  410. // JUSTIFY CHECKBOX
  411. var justifyCheckbox = new CheckBox
  412. {
  413. X = Pos.Right (container) + 1,
  414. Y = Pos.Y (container) + 1,
  415. AutoSize = false,
  416. Width = Dim.Fill (10),
  417. Height = 1,
  418. Text = "Justify"
  419. };
  420. justifyCheckbox.Toggled += (s, e) =>
  421. {
  422. if (e.OldValue == true)
  423. {
  424. foreach (Label t in mtxts)
  425. {
  426. t.TextAlignment = (TextAlignment)((dynamic)t.Data).h;
  427. t.VerticalTextAlignment = (VerticalTextAlignment)((dynamic)t.Data).v;
  428. }
  429. }
  430. else
  431. {
  432. foreach (Label t in mtxts)
  433. {
  434. if (TextFormatter.IsVerticalDirection (t.TextDirection))
  435. {
  436. t.VerticalTextAlignment = VerticalTextAlignment.Justified;
  437. t.TextAlignment = ((dynamic)t.Data).h;
  438. }
  439. else
  440. {
  441. t.TextAlignment = TextAlignment.Justified;
  442. t.VerticalTextAlignment = ((dynamic)t.Data).v;
  443. }
  444. }
  445. }
  446. };
  447. app.Add (justifyCheckbox);
  448. // Direction Options
  449. List<TextDirection> directionsEnum = Enum.GetValues (typeof (TextDirection)).Cast<TextDirection> ().ToList ();
  450. var directionOptions = new RadioGroup
  451. {
  452. X = Pos.Right (container) + 1,
  453. Y = Pos.Bottom (justifyCheckbox) + 1,
  454. Width = Dim.Fill (10),
  455. Height = Dim.Fill (1),
  456. HotKeySpecifier = (Rune)'\xffff',
  457. RadioLabels = directionsEnum.Select (e => e.ToString ()).ToArray ()
  458. };
  459. directionOptions.SelectedItemChanged += (s, ev) =>
  460. {
  461. foreach (Label v in mtxts)
  462. {
  463. v.TextDirection = (TextDirection)ev.SelectedItem;
  464. }
  465. };
  466. app.Add (directionOptions);
  467. Application.Run (app);
  468. app.Dispose ();
  469. }
  470. }