TextAlignmentAndDirection.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 direction.")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class TextAlignmentAndDirection : 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. var txt = $"Hello World{Environment.NewLine}HELLO WORLD{Environment.NewLine}世界 您好";
  19. var color1 = new ColorScheme { Normal = new (Color.Black, Color.Gray) };
  20. var color2 = new ColorScheme { Normal = new (Color.Black, Color.DarkGray) };
  21. List<Label> txts = new (); // single line
  22. List<Label> mtxts = new (); // multi line
  23. // Horizontal Single-Line
  24. var labelHL = new Label
  25. {
  26. X = 1,
  27. Y = 1,
  28. Width = 9,
  29. Height = 1,
  30. TextAlignment = Alignment.Right,
  31. ColorScheme = Colors.ColorSchemes ["Dialog"],
  32. Text = "Left"
  33. };
  34. var labelHC = new Label
  35. {
  36. X = 1,
  37. Y = 2,
  38. Width = 9,
  39. Height = 1,
  40. TextAlignment = Alignment.Right,
  41. ColorScheme = Colors.ColorSchemes ["Dialog"],
  42. Text = "Centered"
  43. };
  44. var labelHR = new Label
  45. {
  46. X = 1,
  47. Y = 3,
  48. Width = 9,
  49. Height = 1,
  50. TextAlignment = Alignment.Right,
  51. ColorScheme = Colors.ColorSchemes ["Dialog"],
  52. Text = "Right"
  53. };
  54. var labelHJ = new Label
  55. {
  56. X = 1,
  57. Y = 4,
  58. Width = 9,
  59. Height = 1,
  60. TextAlignment = Alignment.Right,
  61. ColorScheme = Colors.ColorSchemes ["Dialog"],
  62. Text = "Justified"
  63. };
  64. var txtLabelHL = new Label
  65. {
  66. X = Pos.Right (labelHL) + 1,
  67. Y = Pos.Y (labelHL),
  68. Width = Dim.Fill (1) - 9,
  69. Height = 1,
  70. ColorScheme = color1,
  71. TextAlignment = Alignment.Left,
  72. Text = txt
  73. };
  74. var txtLabelHC = new Label
  75. {
  76. X = Pos.Right (labelHC) + 1,
  77. Y = Pos.Y (labelHC),
  78. Width = Dim.Fill (1) - 9,
  79. Height = 1,
  80. ColorScheme = color2,
  81. TextAlignment = Alignment.Centered,
  82. Text = txt
  83. };
  84. var txtLabelHR = new Label
  85. {
  86. X = Pos.Right (labelHR) + 1,
  87. Y = Pos.Y (labelHR),
  88. Width = Dim.Fill (1) - 9,
  89. Height = 1,
  90. ColorScheme = color1,
  91. TextAlignment = Alignment.Right,
  92. Text = txt
  93. };
  94. var txtLabelHJ = new Label
  95. {
  96. X = Pos.Right (labelHJ) + 1,
  97. Y = Pos.Y (labelHJ),
  98. Width = Dim.Fill (1) - 9,
  99. Height = 1,
  100. ColorScheme = color2,
  101. TextAlignment = Alignment.Justified,
  102. Text = txt
  103. };
  104. txts.Add (txtLabelHL);
  105. txts.Add (txtLabelHC);
  106. txts.Add (txtLabelHR);
  107. txts.Add (txtLabelHJ);
  108. app.Add (labelHL);
  109. app.Add (txtLabelHL);
  110. app.Add (labelHC);
  111. app.Add (txtLabelHC);
  112. app.Add (labelHR);
  113. app.Add (txtLabelHR);
  114. app.Add (labelHJ);
  115. app.Add (txtLabelHJ);
  116. // Vertical Single-Line
  117. var labelVT = new Label
  118. {
  119. X = Pos.AnchorEnd (8),
  120. Y = 1,
  121. Width = 2,
  122. Height = 9,
  123. ColorScheme = color1,
  124. TextDirection = TextDirection.TopBottom_LeftRight,
  125. VerticalTextAlignment = Alignment.Bottom,
  126. Text = "Top"
  127. };
  128. labelVT.TextFormatter.WordWrap = false;
  129. var labelVM = new Label
  130. {
  131. X = Pos.AnchorEnd (6),
  132. Y = 1,
  133. Width = 2,
  134. Height = 9,
  135. ColorScheme = color1,
  136. TextDirection = TextDirection.TopBottom_LeftRight,
  137. VerticalTextAlignment = Alignment.Bottom,
  138. Text = "Centered"
  139. };
  140. labelVM.TextFormatter.WordWrap = false;
  141. var labelVB = new Label
  142. {
  143. X = Pos.AnchorEnd (4),
  144. Y = 1,
  145. Width = 2,
  146. Height = 9,
  147. ColorScheme = color1,
  148. TextDirection = TextDirection.TopBottom_LeftRight,
  149. VerticalTextAlignment = Alignment.Bottom,
  150. Text = "Bottom"
  151. };
  152. labelVB.TextFormatter.WordWrap = false;
  153. var labelVJ = new Label
  154. {
  155. X = Pos.AnchorEnd (2),
  156. Y = 1,
  157. Width = 2,
  158. Height = 9,
  159. ColorScheme = color1,
  160. TextDirection = TextDirection.TopBottom_LeftRight,
  161. VerticalTextAlignment = Alignment.Bottom,
  162. Text = "Justified"
  163. };
  164. labelVJ.TextFormatter.WordWrap = false;
  165. var txtLabelVT = new Label
  166. {
  167. X = Pos.X (labelVT),
  168. Y = Pos.Bottom (labelVT) + 1,
  169. Width = 2,
  170. Height = Dim.Fill (1),
  171. ColorScheme = color1,
  172. TextDirection = TextDirection.TopBottom_LeftRight,
  173. VerticalTextAlignment = Alignment.Top,
  174. Text = txt
  175. };
  176. txtLabelVT.TextFormatter.WordWrap = false;
  177. var txtLabelVM = new Label
  178. {
  179. X = Pos.X (labelVM),
  180. Y = Pos.Bottom (labelVM) + 1,
  181. Width = 2,
  182. Height = Dim.Fill (1),
  183. ColorScheme = color2,
  184. TextDirection = TextDirection.TopBottom_LeftRight,
  185. VerticalTextAlignment = Alignment.Centered,
  186. Text = txt
  187. };
  188. txtLabelVM.TextFormatter.WordWrap = false;
  189. var txtLabelVB = new Label
  190. {
  191. X = Pos.X (labelVB),
  192. Y = Pos.Bottom (labelVB) + 1,
  193. Width = 2,
  194. Height = Dim.Fill (1),
  195. ColorScheme = color1,
  196. TextDirection = TextDirection.TopBottom_LeftRight,
  197. VerticalTextAlignment = Alignment.Bottom,
  198. Text = txt
  199. };
  200. txtLabelVB.TextFormatter.WordWrap = false;
  201. var txtLabelVJ = new Label
  202. {
  203. X = Pos.X (labelVJ),
  204. Y = Pos.Bottom (labelVJ) + 1,
  205. Width = 2,
  206. Height = Dim.Fill (1),
  207. ColorScheme = color2,
  208. TextDirection = TextDirection.TopBottom_LeftRight,
  209. VerticalTextAlignment = Alignment.Justified,
  210. Text = txt
  211. };
  212. txtLabelVJ.TextFormatter.WordWrap = false;
  213. txts.Add (txtLabelVT);
  214. txts.Add (txtLabelVM);
  215. txts.Add (txtLabelVB);
  216. txts.Add (txtLabelVJ);
  217. app.Add (labelVT);
  218. app.Add (txtLabelVT);
  219. app.Add (labelVM);
  220. app.Add (txtLabelVM);
  221. app.Add (labelVB);
  222. app.Add (txtLabelVB);
  223. app.Add (labelVJ);
  224. app.Add (txtLabelVJ);
  225. // Multi-Line
  226. var container = new View
  227. {
  228. X = 0,
  229. Y = Pos.Bottom (txtLabelHJ),
  230. Width = Dim.Fill (31),
  231. Height = Dim.Fill (4),
  232. ColorScheme = color2
  233. };
  234. var txtLabelTL = new Label
  235. {
  236. X = 1 /* */,
  237. Y = 1,
  238. Width = Dim.Percent (100f / 3f),
  239. Height = Dim.Percent (100f / 3f),
  240. TextAlignment = Alignment.Left,
  241. VerticalTextAlignment = Alignment.Top,
  242. ColorScheme = color1,
  243. Text = txt
  244. };
  245. txtLabelTL.TextFormatter.MultiLine = true;
  246. var txtLabelTC = new Label
  247. {
  248. X = Pos.Right (txtLabelTL) + 2,
  249. Y = 1,
  250. Width = Dim.Percent (100f / 3f),
  251. Height = Dim.Percent (100f / 3f),
  252. TextAlignment = Alignment.Centered,
  253. VerticalTextAlignment = Alignment.Top,
  254. ColorScheme = color1,
  255. Text = txt
  256. };
  257. txtLabelTC.TextFormatter.MultiLine = true;
  258. var txtLabelTR = new Label
  259. {
  260. X = Pos.Right (txtLabelTC) + 2,
  261. Y = 1,
  262. Width = Dim.Percent (100f, true),
  263. Height = Dim.Percent (100f / 3f),
  264. TextAlignment = Alignment.Right,
  265. VerticalTextAlignment = Alignment.Top,
  266. ColorScheme = color1,
  267. Text = txt
  268. };
  269. txtLabelTR.TextFormatter.MultiLine = true;
  270. var txtLabelML = new Label
  271. {
  272. X = Pos.X (txtLabelTL),
  273. Y = Pos.Bottom (txtLabelTL) + 1,
  274. Width = Dim.Width (txtLabelTL),
  275. Height = Dim.Percent (100f / 3f),
  276. TextAlignment = Alignment.Left,
  277. VerticalTextAlignment = Alignment.Centered,
  278. ColorScheme = color1,
  279. Text = txt
  280. };
  281. txtLabelML.TextFormatter.MultiLine = true;
  282. var txtLabelMC = new Label
  283. {
  284. X = Pos.X (txtLabelTC),
  285. Y = Pos.Bottom (txtLabelTC) + 1,
  286. Width = Dim.Width (txtLabelTC),
  287. Height = Dim.Percent (100f / 3f),
  288. TextAlignment = Alignment.Centered,
  289. VerticalTextAlignment = Alignment.Centered,
  290. ColorScheme = color1,
  291. Text = txt
  292. };
  293. txtLabelMC.TextFormatter.MultiLine = true;
  294. var txtLabelMR = new Label
  295. {
  296. X = Pos.X (txtLabelTR),
  297. Y = Pos.Bottom (txtLabelTR) + 1,
  298. Width = Dim.Percent (100f, true),
  299. Height = Dim.Percent (100f / 3f),
  300. TextAlignment = Alignment.Right,
  301. VerticalTextAlignment = Alignment.Centered,
  302. ColorScheme = color1,
  303. Text = txt
  304. };
  305. txtLabelMR.TextFormatter.MultiLine = true;
  306. var txtLabelBL = new Label
  307. {
  308. X = Pos.X (txtLabelML),
  309. Y = Pos.Bottom (txtLabelML) + 1,
  310. Width = Dim.Width (txtLabelML),
  311. Height = Dim.Percent (100f, true),
  312. TextAlignment = Alignment.Left,
  313. VerticalTextAlignment = Alignment.Bottom,
  314. ColorScheme = color1,
  315. Text = txt
  316. };
  317. txtLabelBL.TextFormatter.MultiLine = true;
  318. var txtLabelBC = new Label
  319. {
  320. X = Pos.X (txtLabelMC),
  321. Y = Pos.Bottom (txtLabelMC) + 1,
  322. Width = Dim.Width (txtLabelMC),
  323. Height = Dim.Percent (100f, true),
  324. TextAlignment = Alignment.Centered,
  325. VerticalTextAlignment = Alignment.Bottom,
  326. ColorScheme = color1,
  327. Text = txt
  328. };
  329. txtLabelBC.TextFormatter.MultiLine = true;
  330. var txtLabelBR = new Label
  331. {
  332. X = Pos.X (txtLabelMR),
  333. Y = Pos.Bottom (txtLabelMR) + 1,
  334. Width = Dim.Percent (100f, true),
  335. Height = Dim.Percent (100f, true),
  336. TextAlignment = Alignment.Right,
  337. VerticalTextAlignment = Alignment.Bottom,
  338. ColorScheme = color1,
  339. Text = txt
  340. };
  341. txtLabelBR.TextFormatter.MultiLine = true;
  342. mtxts.Add (txtLabelTL);
  343. mtxts.Add (txtLabelTC);
  344. mtxts.Add (txtLabelTR);
  345. mtxts.Add (txtLabelML);
  346. mtxts.Add (txtLabelMC);
  347. mtxts.Add (txtLabelMR);
  348. mtxts.Add (txtLabelBL);
  349. mtxts.Add (txtLabelBC);
  350. mtxts.Add (txtLabelBR);
  351. // Save Alignment in Data
  352. foreach (Label t in mtxts)
  353. {
  354. t.Data = new { h = t.TextAlignment, v = t.VerticalTextAlignment };
  355. }
  356. container.Add (txtLabelTL);
  357. container.Add (txtLabelTC);
  358. container.Add (txtLabelTR);
  359. container.Add (txtLabelML);
  360. container.Add (txtLabelMC);
  361. container.Add (txtLabelMR);
  362. container.Add (txtLabelBL);
  363. container.Add (txtLabelBC);
  364. container.Add (txtLabelBR);
  365. app.Add (container);
  366. // Edit Text
  367. var editText = new TextView
  368. {
  369. X = 1,
  370. Y = Pos.Bottom (container) + 1,
  371. Width = Dim.Fill (10),
  372. Height = Dim.Fill (1),
  373. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  374. Text = txt
  375. };
  376. editText.MouseClick += (s, m) =>
  377. {
  378. foreach (Label v in txts)
  379. {
  380. v.Text = editText.Text;
  381. }
  382. foreach (Label v in mtxts)
  383. {
  384. v.Text = editText.Text;
  385. }
  386. };
  387. app.KeyUp += (s, m) =>
  388. {
  389. foreach (Label v in txts)
  390. {
  391. v.Text = editText.Text;
  392. }
  393. foreach (Label v in mtxts)
  394. {
  395. v.Text = editText.Text;
  396. }
  397. };
  398. editText.SetFocus ();
  399. app.Add (editText);
  400. // JUSTIFY CHECKBOX
  401. var justifyCheckbox = new CheckBox
  402. {
  403. X = Pos.Right (container) + 1,
  404. Y = Pos.Y (container) + 1,
  405. Width = Dim.Fill (10),
  406. Height = 1,
  407. Text = "Justify"
  408. };
  409. app.Add (justifyCheckbox);
  410. // JUSTIFY OPTIONS
  411. var justifyOptions = new RadioGroup
  412. {
  413. X = Pos.Left (justifyCheckbox) + 1,
  414. Y = Pos.Y (justifyCheckbox) + 1,
  415. Width = Dim.Fill (11),
  416. RadioLabels = ["Current direction", "Opposite direction", "Justify Both"],
  417. Enabled = false
  418. };
  419. justifyCheckbox.Toggled += (s, e) => ToggleJustify (e.OldValue is { } && (bool)e.OldValue);
  420. justifyOptions.SelectedItemChanged += (s, e) =>
  421. {
  422. ToggleJustify (false, true);
  423. };
  424. app.Add (justifyOptions);
  425. // WRAP CHECKBOX
  426. var wrapCheckbox = new CheckBox
  427. {
  428. X = Pos.Right (container) + 1,
  429. Y = Pos.Bottom (justifyOptions),
  430. Width = Dim.Fill (10),
  431. Height = 1,
  432. Text = "Word Wrap",
  433. };
  434. wrapCheckbox.Checked = wrapCheckbox.TextFormatter.WordWrap;
  435. wrapCheckbox.Toggled += (s, e) =>
  436. {
  437. if (e.OldValue == true)
  438. {
  439. foreach (Label t in mtxts)
  440. {
  441. t.TextFormatter.WordWrap = false;
  442. }
  443. }
  444. else
  445. {
  446. foreach (Label t in mtxts)
  447. {
  448. t.TextFormatter.WordWrap = true;
  449. }
  450. }
  451. };
  452. app.Add (wrapCheckbox);
  453. // AUTOSIZE CHECKBOX
  454. var autoSizeCheckbox = new CheckBox
  455. {
  456. X = Pos.Right (container) + 1,
  457. Y = Pos.Y (wrapCheckbox) + 1,
  458. Width = Dim.Fill (10),
  459. Height = 1,
  460. Text = "AutoSize",
  461. };
  462. autoSizeCheckbox.Checked = autoSizeCheckbox.TextFormatter.AutoSize;
  463. autoSizeCheckbox.Toggled += (s, e) =>
  464. {
  465. if (e.OldValue == true)
  466. {
  467. foreach (Label t in mtxts)
  468. {
  469. t.TextFormatter.AutoSize = false;
  470. }
  471. }
  472. else
  473. {
  474. foreach (Label t in mtxts)
  475. {
  476. t.TextFormatter.AutoSize = true;
  477. }
  478. }
  479. };
  480. app.Add (autoSizeCheckbox);
  481. // Direction Options
  482. List<TextDirection> directionsEnum = Enum.GetValues (typeof (TextDirection)).Cast<TextDirection> ().ToList ();
  483. var directionOptions = new RadioGroup
  484. {
  485. X = Pos.Right (container) + 1,
  486. Y = Pos.Bottom (autoSizeCheckbox) + 1,
  487. Width = Dim.Fill (10),
  488. Height = Dim.Fill (1),
  489. HotKeySpecifier = (Rune)'\xffff',
  490. RadioLabels = directionsEnum.Select (e => e.ToString ()).ToArray ()
  491. };
  492. directionOptions.SelectedItemChanged += (s, ev) =>
  493. {
  494. var justChecked = justifyCheckbox.Checked is { } && (bool)justifyCheckbox.Checked;
  495. if (justChecked)
  496. {
  497. ToggleJustify (true);
  498. }
  499. foreach (Label v in mtxts)
  500. {
  501. v.TextDirection = (TextDirection)ev.SelectedItem;
  502. }
  503. if (justChecked)
  504. {
  505. ToggleJustify (false);
  506. }
  507. };
  508. app.Add (directionOptions);
  509. Application.Run (app);
  510. app.Dispose ();
  511. void ToggleJustify (bool oldValue, bool wasJustOptions = false)
  512. {
  513. if (oldValue == true)
  514. {
  515. if (!wasJustOptions)
  516. {
  517. justifyOptions.Enabled = false;
  518. }
  519. foreach (Label t in mtxts)
  520. {
  521. t.TextAlignment = (Alignment)((dynamic)t.Data).h;
  522. t.VerticalTextAlignment = (Alignment)((dynamic)t.Data).v;
  523. }
  524. }
  525. else
  526. {
  527. foreach (Label t in mtxts)
  528. {
  529. if (!wasJustOptions)
  530. {
  531. justifyOptions.Enabled = true;
  532. }
  533. if (TextFormatter.IsVerticalDirection (t.TextDirection))
  534. {
  535. switch (justifyOptions.SelectedItem)
  536. {
  537. case 0:
  538. t.VerticalTextAlignment = Alignment.Justified;
  539. t.TextAlignment = ((dynamic)t.Data).h;
  540. break;
  541. case 1:
  542. t.VerticalTextAlignment = (Alignment)((dynamic)t.Data).v;
  543. t.TextAlignment = Alignment.Justified;
  544. break;
  545. case 2:
  546. t.VerticalTextAlignment = Alignment.Justified;
  547. t.TextAlignment = Alignment.Justified;
  548. break;
  549. }
  550. }
  551. else
  552. {
  553. switch (justifyOptions.SelectedItem)
  554. {
  555. case 0:
  556. t.TextAlignment = Alignment.Justified;
  557. t.VerticalTextAlignment = ((dynamic)t.Data).v;
  558. break;
  559. case 1:
  560. t.TextAlignment = (Alignment)((dynamic)t.Data).h;
  561. t.VerticalTextAlignment = Alignment.Justified;
  562. break;
  563. case 2:
  564. t.TextAlignment = Alignment.Justified;
  565. t.VerticalTextAlignment = Alignment.Justified;
  566. break;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }