Themes.cs 11 KB

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