Themes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #nullable enable
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Themes", "Shows off Themes, Schemes, and VisualRoles.")]
  5. [ScenarioCategory ("Colors")]
  6. [ScenarioCategory ("Drawing")]
  7. [ScenarioCategory ("Configuration")]
  8. public sealed class Themes : Scenario
  9. {
  10. private View? _view;
  11. public override void Main ()
  12. {
  13. // Init
  14. Application.Init ();
  15. // Setup - Create a top-level application window and configure it.
  16. Window appWindow = new ()
  17. {
  18. Title = GetQuitKeyAndName (),
  19. BorderStyle = LineStyle.None
  20. };
  21. string[] options = ThemeManager.GetThemeNames ().Select (option => option = "_" + option).ToArray ();
  22. RadioGroup themeOptionSelector = new ()
  23. {
  24. Title = "_Themes",
  25. BorderStyle = LineStyle.Rounded,
  26. Width = Dim.Auto (),
  27. Height = Dim.Auto (),
  28. RadioLabels= options,
  29. SelectedItem = ThemeManager.GetThemeNames ().IndexOf (ThemeManager.Theme)
  30. };
  31. themeOptionSelector.Border!.Thickness = new (0, 1, 0, 0);
  32. themeOptionSelector.Margin!.Thickness = new (0, 0, 1, 0);
  33. themeOptionSelector.SelectedItemChanged += (sender, args) =>
  34. {
  35. RadioGroup? optionSelector = sender as RadioGroup;
  36. if (optionSelector is null)
  37. {
  38. return;
  39. }
  40. var newTheme = optionSelector!.RadioLabels! [(int)args.SelectedItem!] as string;
  41. // strip off the leading underscore
  42. ThemeManager.Theme = newTheme!.Substring (1);
  43. ConfigurationManager.Apply ();
  44. };
  45. var themeViewer = new ThemeViewer
  46. {
  47. X = Pos.Right (themeOptionSelector)
  48. };
  49. Dictionary<string, Type> viewClasses = GetAllViewClassesCollection ()
  50. .OrderBy (t => t.Name)
  51. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  52. .ToDictionary (t => t.Key, t => t.Value);
  53. CheckBox? allViewsCheckBox = new ()
  54. {
  55. Title = "_All Views",
  56. X = Pos.Right (themeViewer),
  57. };
  58. ListView viewListView = new ()
  59. {
  60. X = Pos.Right (themeViewer),
  61. Y = Pos.Bottom(allViewsCheckBox),
  62. Title = "_Views",
  63. BorderStyle = LineStyle.Rounded,
  64. Width = Dim.Auto (),
  65. Height = Dim.Fill (),
  66. Source = new ListWrapper<string> (new (viewClasses.Keys))
  67. };
  68. viewListView.Border!.Thickness = new (0, 1, 0, 0);
  69. viewListView.Margin!.Thickness = new (0, 0, 1, 0);
  70. viewListView.VerticalScrollBar.AutoShow = true;
  71. ViewPropertiesEditor viewPropertiesEditor = new ()
  72. {
  73. X = Pos.Right (viewListView),
  74. Width = Dim.Fill (),
  75. Height = Dim.Auto (),
  76. };
  77. FrameView? viewFrame = new ()
  78. {
  79. X = Pos.Right (viewListView),
  80. Y = Pos.Bottom(viewPropertiesEditor),
  81. Title = "The View",
  82. BorderStyle = LineStyle.Rounded,
  83. Width = Dim.Fill (),
  84. Height = Dim.Fill (),
  85. TabStop = TabBehavior.TabStop
  86. };
  87. viewFrame.Border!.Thickness = new (0, 1, 0, 0);
  88. viewListView.SelectedItemChanged += (sender, args) =>
  89. {
  90. var listView = sender as ListView;
  91. if (_view is { })
  92. {
  93. viewPropertiesEditor.ViewToEdit = null;
  94. viewFrame.Remove (_view);
  95. _view.Dispose ();
  96. _view = null;
  97. }
  98. _view = CreateView (viewClasses [(args.Value as string)!]);
  99. if (_view is { })
  100. {
  101. viewFrame.Add (_view);
  102. viewPropertiesEditor.ViewToEdit = _view;
  103. }
  104. };
  105. appWindow.Add (themeOptionSelector, themeViewer, allViewsCheckBox, viewListView, viewPropertiesEditor, viewFrame);
  106. viewListView.SelectedItem = 0;
  107. themeViewer.SettingSchemeName += (sender, args) =>
  108. {
  109. if (_view is { })
  110. {
  111. Application.Top!.SchemeName = args.NewString;
  112. if (_view.HasScheme)
  113. {
  114. _view.SetScheme (null);
  115. }
  116. _view.SchemeName = args.NewString;
  117. }
  118. };
  119. AllViewsView? allViewsView = null;
  120. allViewsCheckBox.CheckedStateChanged += (sender, args) =>
  121. {
  122. if (args.CurrentValue == CheckState.Checked)
  123. {
  124. viewListView.Visible = false;
  125. appWindow.Remove (viewFrame);
  126. allViewsView = new AllViewsView ()
  127. {
  128. X = Pos.Right (themeViewer),
  129. Y = Pos.Bottom (viewPropertiesEditor),
  130. Title = "All Views - Focused: {None}",
  131. BorderStyle = LineStyle.Rounded,
  132. Width = Dim.Fill (),
  133. Height = Dim.Fill (),
  134. TabStop = TabBehavior.TabStop
  135. };
  136. allViewsView.FocusedChanged += (s, args) =>
  137. {
  138. allViewsView.Title =
  139. $"All Views - Focused: {args.NewFocused.Title}";
  140. viewPropertiesEditor.ViewToEdit = args.NewFocused.SubViews.ElementAt(0);
  141. };
  142. appWindow.Add (allViewsView);
  143. }
  144. else
  145. {
  146. appWindow.Remove (allViewsView);
  147. allViewsView.Dispose ();
  148. allViewsView = null;
  149. appWindow.Add (viewFrame);
  150. viewListView.Visible = true;
  151. }
  152. };
  153. // Run - Start the application.
  154. Application.Run (appWindow);
  155. viewFrame.Dispose ();
  156. appWindow.Dispose ();
  157. // Shutdown - Calling Application.Shutdown is required.
  158. Application.Shutdown ();
  159. }
  160. private static List<Type> GetAllViewClassesCollection ()
  161. {
  162. List<Type> types = typeof (View).Assembly.GetTypes ()
  163. .Where (
  164. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  165. && myType.IsSubclassOf (typeof (View)))
  166. .ToList ();
  167. types.Add (typeof (View));
  168. return types;
  169. }
  170. private View? CreateView (Type type)
  171. {
  172. // If we are to create a generic Type
  173. if (type.IsGenericType)
  174. {
  175. // For each of the <T> arguments
  176. List<Type> typeArguments = new ();
  177. // use <object> or the original type if applicable
  178. foreach (Type arg in type.GetGenericArguments ())
  179. {
  180. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  181. {
  182. typeArguments.Add (arg);
  183. }
  184. else
  185. {
  186. typeArguments.Add (typeof (object));
  187. }
  188. }
  189. // And change what type we are instantiating from MyClass<T> to MyClass<object> or MyClass<T>
  190. type = type.MakeGenericType (typeArguments.ToArray ());
  191. }
  192. // Ensure the type does not contain any generic parameters
  193. if (type.ContainsGenericParameters)
  194. {
  195. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  196. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  197. return null;
  198. }
  199. // Instantiate view
  200. var view = (View)Activator.CreateInstance (type)!;
  201. var demoText = "This, that, and the other thing.";
  202. if (view is IDesignable designable)
  203. {
  204. designable.EnableForDesign (ref demoText);
  205. }
  206. else
  207. {
  208. view.Text = demoText;
  209. view.Title = "_Test Title";
  210. }
  211. view.Initialized += OnViewInitialized;
  212. return view;
  213. }
  214. private void OnViewInitialized (object? sender, EventArgs e)
  215. {
  216. if (sender is not View view)
  217. {
  218. return;
  219. }
  220. if (view.Width == Dim.Absolute (0) || view.Width is null)
  221. {
  222. view.Width = Dim.Fill ();
  223. }
  224. if (view.Height == Dim.Absolute (0) || view.Height is null)
  225. {
  226. view.Height = Dim.Fill ();
  227. }
  228. }
  229. }