TextAlignmentsAndDirection.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. 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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = TextAlignment.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 = VerticalTextAlignment.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 = VerticalTextAlignment.Bottom,
  138. Text = "Middle"
  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 = VerticalTextAlignment.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 = VerticalTextAlignment.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 = VerticalTextAlignment.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 = VerticalTextAlignment.Middle,
  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 = VerticalTextAlignment.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 = VerticalTextAlignment.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 = TextAlignment.Left,
  241. VerticalTextAlignment = VerticalTextAlignment.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 = TextAlignment.Centered,
  253. VerticalTextAlignment = VerticalTextAlignment.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 = TextAlignment.Right,
  265. VerticalTextAlignment = VerticalTextAlignment.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 = TextAlignment.Left,
  277. VerticalTextAlignment = VerticalTextAlignment.Middle,
  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 = TextAlignment.Centered,
  289. VerticalTextAlignment = VerticalTextAlignment.Middle,
  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 = TextAlignment.Right,
  301. VerticalTextAlignment = VerticalTextAlignment.Middle,
  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 = TextAlignment.Left,
  313. VerticalTextAlignment = VerticalTextAlignment.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 = TextAlignment.Centered,
  325. VerticalTextAlignment = VerticalTextAlignment.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 = TextAlignment.Right,
  337. VerticalTextAlignment = VerticalTextAlignment.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 Alignments 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. justifyCheckbox.Toggled += (s, e) => ToggleJustify (e.OldValue is { } && (bool)e.OldValue);
  410. app.Add (justifyCheckbox);
  411. // WRAP CHECKBOX
  412. var wrapCheckbox = new CheckBox
  413. {
  414. X = Pos.Right (container) + 1,
  415. Y = Pos.Y (justifyCheckbox) + 1,
  416. Width = Dim.Fill (10),
  417. Height = 1,
  418. Text = "Word Wrap",
  419. };
  420. wrapCheckbox.Checked = wrapCheckbox.TextFormatter.WordWrap;
  421. wrapCheckbox.Toggled += (s, e) =>
  422. {
  423. if (e.OldValue == true)
  424. {
  425. foreach (Label t in mtxts)
  426. {
  427. t.TextFormatter.WordWrap = false;
  428. }
  429. }
  430. else
  431. {
  432. foreach (Label t in mtxts)
  433. {
  434. t.TextFormatter.WordWrap = true;
  435. }
  436. }
  437. };
  438. app.Add (wrapCheckbox);
  439. // AUTOSIZE CHECKBOX
  440. var autoSizeCheckbox = new CheckBox
  441. {
  442. X = Pos.Right (container) + 1,
  443. Y = Pos.Y (wrapCheckbox) + 1,
  444. Width = Dim.Fill (10),
  445. Height = 1,
  446. Text = "AutoSize",
  447. };
  448. autoSizeCheckbox.Checked = autoSizeCheckbox.TextFormatter.AutoSize;
  449. autoSizeCheckbox.Toggled += (s, e) =>
  450. {
  451. if (e.OldValue == true)
  452. {
  453. foreach (Label t in mtxts)
  454. {
  455. t.TextFormatter.AutoSize = false;
  456. }
  457. }
  458. else
  459. {
  460. foreach (Label t in mtxts)
  461. {
  462. t.TextFormatter.AutoSize = true;
  463. }
  464. }
  465. };
  466. app.Add (autoSizeCheckbox);
  467. // Direction Options
  468. List<TextDirection> directionsEnum = Enum.GetValues (typeof (TextDirection)).Cast<TextDirection> ().ToList ();
  469. var directionOptions = new RadioGroup
  470. {
  471. X = Pos.Right (container) + 1,
  472. Y = Pos.Bottom (autoSizeCheckbox) + 1,
  473. Width = Dim.Fill (10),
  474. Height = Dim.Fill (1),
  475. HotKeySpecifier = (Rune)'\xffff',
  476. RadioLabels = directionsEnum.Select (e => e.ToString ()).ToArray ()
  477. };
  478. directionOptions.SelectedItemChanged += (s, ev) =>
  479. {
  480. var justChecked = justifyCheckbox.Checked is { } && (bool)justifyCheckbox.Checked;
  481. if (justChecked)
  482. {
  483. ToggleJustify (true);
  484. }
  485. foreach (Label v in mtxts)
  486. {
  487. v.TextDirection = (TextDirection)ev.SelectedItem;
  488. }
  489. if (justChecked)
  490. {
  491. ToggleJustify (false);
  492. }
  493. };
  494. app.Add (directionOptions);
  495. Application.Run (app);
  496. app.Dispose ();
  497. void ToggleJustify (bool oldValue)
  498. {
  499. if (oldValue == true)
  500. {
  501. foreach (Label t in mtxts)
  502. {
  503. t.TextAlignment = (TextAlignment)((dynamic)t.Data).h;
  504. t.VerticalTextAlignment = (VerticalTextAlignment)((dynamic)t.Data).v;
  505. }
  506. }
  507. else
  508. {
  509. foreach (Label t in mtxts)
  510. {
  511. if (TextFormatter.IsVerticalDirection (t.TextDirection))
  512. {
  513. t.VerticalTextAlignment = VerticalTextAlignment.Justified;
  514. t.TextAlignment = ((dynamic)t.Data).h;
  515. }
  516. else
  517. {
  518. t.TextAlignment = TextAlignment.Justified;
  519. t.VerticalTextAlignment = ((dynamic)t.Data).v;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. }