TextStyles.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #nullable enable
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Text Styles", "Shows Attribute.TextStyles including bold, italic, etc...")]
  4. [ScenarioCategory ("Text and Formatting")]
  5. [ScenarioCategory ("Colors")]
  6. public sealed class TextStyles : Scenario
  7. {
  8. private CheckBox? _drawDirectly;
  9. public override void Main ()
  10. {
  11. // Init
  12. Application.Init ();
  13. // Setup - Create a top-level application window and configure it.
  14. Window appWindow = new ()
  15. {
  16. Id = "appWindow",
  17. Title = GetQuitKeyAndName ()
  18. };
  19. //appWindow.ContentSizeTracksViewport = false;
  20. appWindow.VerticalScrollBar.AutoShow = true;
  21. appWindow.HorizontalScrollBar.AutoShow = true;
  22. appWindow.SubViewsLaidOut += (sender, _) =>
  23. {
  24. if (sender is View sendingView)
  25. {
  26. sendingView.SetContentSize (new Size(sendingView.GetContentSize().Width, sendingView.GetHeightRequiredForSubViews()));
  27. }
  28. };
  29. appWindow.DrawingContent += OnAppWindowOnDrawingContent;
  30. appWindow.DrawingSubViews += OnAppWindowOnDrawingSubviews;
  31. _drawDirectly = new ()
  32. {
  33. Title = "_Draw styled text directly using DrawingContent vs. Buttons",
  34. CheckedState = CheckState.UnChecked
  35. };
  36. appWindow.Add (_drawDirectly);
  37. AddButtons (appWindow);
  38. // Run - Start the application.
  39. Application.Run (appWindow);
  40. appWindow.Dispose ();
  41. // Shutdown - Calling Application.Shutdown is required.
  42. Application.Shutdown ();
  43. }
  44. private void AddButtons (Window appWindow)
  45. {
  46. var y = 1;
  47. TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle))
  48. .Cast<TextStyle> ()
  49. .Where (style => style != TextStyle.None)
  50. .ToArray ();
  51. // Add individual flags as labels
  52. foreach (TextStyle style in allStyles)
  53. {
  54. y++;
  55. var button = new Button
  56. {
  57. X = 0,
  58. Y = y,
  59. Title = $"{Enum.GetName (typeof (TextStyle), style)}",
  60. Visible = _drawDirectly!.CheckedState != CheckState.Checked
  61. };
  62. button.GettingAttributeForRole += (sender, args) =>
  63. {
  64. if (sender is not Button buttonSender)
  65. {
  66. return;
  67. }
  68. if (args.Result is { })
  69. {
  70. args.Result = args.Result.Value with { Style = style };
  71. }
  72. args.Handled = true;
  73. };
  74. appWindow.Add (button);
  75. }
  76. // Add a blank line
  77. y += 1;
  78. // Generate all combinations of TextStyle (excluding individual flags)
  79. int totalCombinations = 1 << allStyles.Length; // 2^n combinations
  80. for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None"
  81. {
  82. var combination = (TextStyle)0;
  83. List<string> styleNames = [];
  84. for (var bit = 0; bit < allStyles.Length; bit++)
  85. {
  86. if ((i & (1 << bit)) != 0)
  87. {
  88. combination |= allStyles [bit];
  89. styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!);
  90. }
  91. }
  92. // Skip individual flags
  93. if (styleNames.Count == 1)
  94. {
  95. continue;
  96. }
  97. y++;
  98. var button = new Button
  99. {
  100. X = 0,
  101. Y = y,
  102. Text = $"[{string.Join (" | ", styleNames)}]",
  103. Visible = _drawDirectly!.CheckedState != CheckState.Checked
  104. };
  105. button.GettingAttributeForRole += (_, args) =>
  106. {
  107. if (args.Result is { })
  108. {
  109. args.Result = args.Result.Value with { Style = combination };
  110. }
  111. args.Handled = true;
  112. };
  113. appWindow.Add (button);
  114. }
  115. }
  116. private void OnAppWindowOnDrawingSubviews (object? sender, DrawEventArgs e)
  117. {
  118. if (sender is not View sendingVioew)
  119. {
  120. return;
  121. }
  122. foreach (Button view in sendingVioew.SubViews.OfType<Button> ())
  123. {
  124. view.Visible = _drawDirectly!.CheckedState != CheckState.Checked;
  125. }
  126. e.Cancel = false;
  127. }
  128. private void OnAppWindowOnDrawingContent (object? sender, DrawEventArgs args)
  129. {
  130. if (sender is View { } sendingView && _drawDirectly!.CheckedState == CheckState.Checked)
  131. {
  132. int y = 2 - args.NewViewport.Y; // Start drawing below the checkbox
  133. TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle))
  134. .Cast<TextStyle> ()
  135. .Where (style => style != TextStyle.None)
  136. .ToArray ();
  137. // Draw individual flags, one per line
  138. foreach (TextStyle style in allStyles)
  139. {
  140. string text = Enum.GetName (typeof (TextStyle), style)!;
  141. sendingView.Move (0, y);
  142. var attr = new Attribute (sendingView.GetAttributeForRole (VisualRole.Normal))
  143. {
  144. Style = style
  145. };
  146. sendingView.SetAttribute (attr);
  147. sendingView.AddStr (text);
  148. y++; // Move to the next line
  149. }
  150. // Add a blank line
  151. y++;
  152. // Generate all combinations of TextStyle (excluding individual flags)
  153. int totalCombinations = 1 << allStyles.Length; // 2^n combinations
  154. for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None"
  155. {
  156. var combination = (TextStyle)0;
  157. List<string> styleNames = [];
  158. for (var bit = 0; bit < allStyles.Length; bit++)
  159. {
  160. if ((i & (1 << bit)) != 0)
  161. {
  162. combination |= allStyles [bit];
  163. styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!);
  164. }
  165. }
  166. // Skip individual flags
  167. if (styleNames.Count == 1)
  168. {
  169. continue;
  170. }
  171. var text = $"[{string.Join (" | ", styleNames)}]";
  172. sendingView.Move (00, y);
  173. var attr = new Attribute (sendingView.GetAttributeForRole (VisualRole.Normal))
  174. {
  175. Style = combination
  176. };
  177. sendingView.SetAttribute (attr);
  178. sendingView.AddStr (text);
  179. y++; // Move to the next line
  180. }
  181. args.Cancel = true;
  182. }
  183. }
  184. }