TextStyles.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 TestStyles : 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. args.NewValue = args.NewValue with { Style = style };
  69. args.Cancel = true;
  70. };
  71. appWindow.Add (button);
  72. }
  73. // Add a blank line
  74. y += 1;
  75. // Generate all combinations of TextStyle (excluding individual flags)
  76. int totalCombinations = 1 << allStyles.Length; // 2^n combinations
  77. for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None"
  78. {
  79. var combination = (TextStyle)0;
  80. List<string> styleNames = [];
  81. for (var bit = 0; bit < allStyles.Length; bit++)
  82. {
  83. if ((i & (1 << bit)) != 0)
  84. {
  85. combination |= allStyles [bit];
  86. styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!);
  87. }
  88. }
  89. // Skip individual flags
  90. if (styleNames.Count == 1)
  91. {
  92. continue;
  93. }
  94. y++;
  95. var button = new Button
  96. {
  97. X = 0,
  98. Y = y,
  99. Text = $"[{string.Join (" | ", styleNames)}]",
  100. Visible = _drawDirectly!.CheckedState != CheckState.Checked
  101. };
  102. button.GettingAttributeForRole += (_, args) =>
  103. {
  104. args.NewValue = args.NewValue with { Style = combination };
  105. args.Cancel = true;
  106. };
  107. appWindow.Add (button);
  108. }
  109. }
  110. private void OnAppWindowOnDrawingSubviews (object? sender, DrawEventArgs e)
  111. {
  112. if (sender is not View sendingVioew)
  113. {
  114. return;
  115. }
  116. foreach (Button view in sendingVioew.SubViews.OfType<Button> ())
  117. {
  118. view.Visible = _drawDirectly!.CheckedState != CheckState.Checked;
  119. }
  120. e.Cancel = false;
  121. }
  122. private void OnAppWindowOnDrawingContent (object? sender, DrawEventArgs args)
  123. {
  124. if (sender is View { } sendingView && _drawDirectly!.CheckedState == CheckState.Checked)
  125. {
  126. int y = 2 - args.NewViewport.Y; // Start drawing below the checkbox
  127. TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle))
  128. .Cast<TextStyle> ()
  129. .Where (style => style != TextStyle.None)
  130. .ToArray ();
  131. // Draw individual flags, one per line
  132. foreach (TextStyle style in allStyles)
  133. {
  134. string text = Enum.GetName (typeof (TextStyle), style)!;
  135. sendingView.Move (0, y);
  136. var attr = new Attribute (sendingView.GetAttributeForRole (VisualRole.Normal))
  137. {
  138. Style = style
  139. };
  140. sendingView.SetAttribute (attr);
  141. sendingView.AddStr (text);
  142. y++; // Move to the next line
  143. }
  144. // Add a blank line
  145. y++;
  146. // Generate all combinations of TextStyle (excluding individual flags)
  147. int totalCombinations = 1 << allStyles.Length; // 2^n combinations
  148. for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None"
  149. {
  150. var combination = (TextStyle)0;
  151. List<string> styleNames = [];
  152. for (var bit = 0; bit < allStyles.Length; bit++)
  153. {
  154. if ((i & (1 << bit)) != 0)
  155. {
  156. combination |= allStyles [bit];
  157. styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!);
  158. }
  159. }
  160. // Skip individual flags
  161. if (styleNames.Count == 1)
  162. {
  163. continue;
  164. }
  165. var text = $"[{string.Join (" | ", styleNames)}]";
  166. sendingView.Move (00, y);
  167. var attr = new Attribute (sendingView.GetAttributeForRole (VisualRole.Normal))
  168. {
  169. Style = combination
  170. };
  171. sendingView.SetAttribute (attr);
  172. sendingView.AddStr (text);
  173. y++; // Move to the next line
  174. }
  175. args.Cancel = true;
  176. }
  177. }
  178. }