Selectors.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #nullable enable
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Selectors", "Demonstrates OptionSelector and FlagSelector.")]
  4. [ScenarioCategory ("Controls")]
  5. public sealed class Selectors : Scenario
  6. {
  7. public override void Main ()
  8. {
  9. // Init
  10. Application.Init ();
  11. // Setup - Create a top-level application window and configure it.
  12. Window appWindow = new ()
  13. {
  14. Title = GetQuitKeyAndName (),
  15. BorderStyle = LineStyle.None
  16. };
  17. FrameView? optionSelectorsFrame = null;
  18. FrameView? flagSelectorsFrame = null;
  19. OptionSelector<Orientation> orientationSelector = new ()
  20. {
  21. Orientation = Orientation.Horizontal,
  22. BorderStyle = LineStyle.Dotted,
  23. Title = "Selector Or_ientation",
  24. Value = Orientation.Vertical
  25. };
  26. orientationSelector.ValueChanged += OrientationSelectorOnSelectedItemChanged;
  27. FlagSelector<SelectorStyles> stylesSelector = new ()
  28. {
  29. X = Pos.Right (orientationSelector) + 1,
  30. Orientation = Orientation.Horizontal,
  31. BorderStyle = LineStyle.Dotted,
  32. Title = "Selector St_yles",
  33. };
  34. stylesSelector.ValueChanged += StylesSelectorOnValueChanged;
  35. NumericUpDown<int> horizontalSpace = new ()
  36. {
  37. X = 0,
  38. Y = Pos.Bottom (orientationSelector),
  39. Width = 11,
  40. Title = "H_. Space",
  41. Value = stylesSelector.HorizontalSpace,
  42. BorderStyle = LineStyle.Dotted,
  43. };
  44. horizontalSpace.ValueChanging += HorizontalSpaceOnValueChanging;
  45. CheckBox showBorderAndTitle = new ()
  46. {
  47. X = Pos.Right (horizontalSpace) + 1,
  48. Y = Pos.Top (horizontalSpace),
  49. Title = "Border _& Title",
  50. CheckedState = CheckState.Checked,
  51. BorderStyle = LineStyle.Dotted,
  52. };
  53. showBorderAndTitle.CheckedStateChanged += ShowBorderAndTitleOnCheckedStateChanged;
  54. CheckBox canFocus = new ()
  55. {
  56. X = Pos.Right (showBorderAndTitle) + 1,
  57. Y = Pos.Top (horizontalSpace),
  58. Title = "_CanFocus",
  59. CheckedState = CheckState.Checked,
  60. BorderStyle = LineStyle.Dotted,
  61. };
  62. canFocus.CheckedStateChanged += CanFocusOnCheckedStateChanged;
  63. optionSelectorsFrame = new ()
  64. {
  65. Y = Pos.Bottom (canFocus),
  66. Width = Dim.Percent (50),
  67. Height = Dim.Fill (),
  68. Title = "O_ptionSelectors",
  69. TabStop = TabBehavior.TabStop,
  70. //InvertFocusAttribute = true
  71. };
  72. optionSelectorsFrame.ClearingViewport += (sender, args) =>
  73. {
  74. // optionSelectorsFrame.SetAttributeForRole (optionSelectorsFrame.HasFocus ? VisualRole.Focus : VisualRole.Normal);
  75. };
  76. Label label = new ()
  77. {
  78. Title = "Fo_ur Options:"
  79. };
  80. OptionSelector optionSelector = new ()
  81. {
  82. X = Pos.Right (label) + 1,
  83. Title = "Fou_r Options",
  84. BorderStyle = LineStyle.Dotted,
  85. UsedHotKeys = { label.HotKey },
  86. AssignHotKeys = true,
  87. Labels = ["Option _1 (0)", "Option _2 (1)", "Option _3 (5) 你", "Option _Quattro (4) 你"],
  88. Values = [0, 1, 5, 4],
  89. Arrangement = ViewArrangement.Resizable,
  90. };
  91. optionSelectorsFrame.Add (label, optionSelector);
  92. label = new ()
  93. {
  94. Y = Pos.Bottom (optionSelector),
  95. Title = "<VisualRole_>:"
  96. };
  97. OptionSelector<VisualRole> optionSelectorT = new ()
  98. {
  99. X = Pos.Right (label) + 1,
  100. Y = Pos.Bottom (optionSelector),
  101. Title = "<Vi_sualRole>",
  102. BorderStyle = LineStyle.Dotted,
  103. UsedHotKeys = optionSelector.UsedHotKeys,
  104. AssignHotKeys = true,
  105. };
  106. optionSelectorsFrame.Add (label, optionSelectorT);
  107. flagSelectorsFrame = new ()
  108. {
  109. Y = Pos.Top (optionSelectorsFrame),
  110. X = Pos.Right (optionSelectorsFrame),
  111. Width = Dim.Fill (),
  112. Height = Dim.Fill (),
  113. Title = "_FlagSelectors",
  114. TabStop = TabBehavior.TabStop
  115. };
  116. label = new ()
  117. {
  118. Title = "FlagSelector _(uint):"
  119. };
  120. FlagSelector flagSelector = new ()
  121. {
  122. X = Pos.Right (label) + 1,
  123. UsedHotKeys = optionSelectorT.UsedHotKeys,
  124. BorderStyle = LineStyle.Dotted,
  125. Title = "FlagSe_lector (uint)",
  126. AssignHotKeys = true,
  127. Values =
  128. [
  129. 0b_0001,
  130. 0b_0010,
  131. 0b_0100,
  132. 0b_1000,
  133. 0b_1111
  134. ],
  135. Labels =
  136. [
  137. "0x0001 One",
  138. "0x0010 Two",
  139. "0x0100 Quattro",
  140. "0x1000 8",
  141. "0x1111 Fifteen"
  142. ]
  143. };
  144. flagSelectorsFrame.Add (label, flagSelector);
  145. label = new ()
  146. {
  147. Y = Pos.Bottom (flagSelector),
  148. Title = "_<ViewDiagnosticFlags>:"
  149. };
  150. FlagSelector<ViewDiagnosticFlags> flagSelectorT = new ()
  151. {
  152. X = Pos.Right (label) + 1,
  153. BorderStyle = LineStyle.Dotted,
  154. Title = "<ViewD_iagnosticFlags>",
  155. Y = Pos.Bottom (flagSelector),
  156. UsedHotKeys = flagSelector.UsedHotKeys,
  157. AssignHotKeys = true,
  158. Value = View.Diagnostics
  159. };
  160. flagSelectorsFrame.Add (label, flagSelectorT);
  161. flagSelectorT.ValueChanged += (s, a) =>
  162. {
  163. View.Diagnostics = (ViewDiagnosticFlags)a.Value!;
  164. };
  165. appWindow.Add (orientationSelector, stylesSelector, horizontalSpace, showBorderAndTitle, canFocus, optionSelectorsFrame, flagSelectorsFrame);
  166. // Run - Start the application.
  167. Application.Run (appWindow);
  168. appWindow.Dispose ();
  169. // Shutdown - Calling Application.Shutdown is required.
  170. Application.Shutdown ();
  171. return;
  172. void OrientationSelectorOnSelectedItemChanged (object? sender, EventArgs<Orientation?> e)
  173. {
  174. if (sender is not OptionSelector<Orientation> s)
  175. {
  176. return;
  177. }
  178. List<SelectorBase> selectors = GetAllSelectors ();
  179. foreach (SelectorBase selector in selectors)
  180. {
  181. selector.Orientation = s.Value!.Value;
  182. }
  183. }
  184. void StylesSelectorOnValueChanged (object? sender, EventArgs<SelectorStyles?> e)
  185. {
  186. if (sender is not FlagSelector<SelectorStyles> s)
  187. {
  188. return;
  189. }
  190. List<SelectorBase> selectors = GetAllSelectors ();
  191. foreach (SelectorBase selector in selectors)
  192. {
  193. selector.Styles = s.Value!.Value;
  194. }
  195. }
  196. void HorizontalSpaceOnValueChanging (object? sender, CancelEventArgs<int> e)
  197. {
  198. if (sender is not NumericUpDown<int> upDown || e.NewValue < 0)
  199. {
  200. e.Cancel = true;
  201. return;
  202. }
  203. List<SelectorBase> selectors = GetAllSelectors ();
  204. foreach (SelectorBase selector in selectors)
  205. {
  206. selector.HorizontalSpace = e.NewValue;
  207. }
  208. }
  209. void ShowBorderAndTitleOnCheckedStateChanged (object? sender, EventArgs<CheckState> e)
  210. {
  211. if (sender is not CheckBox cb)
  212. {
  213. return;
  214. }
  215. List<SelectorBase> selectors = GetAllSelectors ();
  216. foreach (SelectorBase selector in selectors)
  217. {
  218. selector.Border!.Thickness = cb.CheckedState == CheckState.Checked ? new (1) : new Thickness (0);
  219. }
  220. }
  221. void CanFocusOnCheckedStateChanged (object? sender, EventArgs<CheckState> e)
  222. {
  223. if (sender is not CheckBox cb)
  224. {
  225. return;
  226. }
  227. List<SelectorBase> selectors = GetAllSelectors ();
  228. foreach (SelectorBase selector in selectors)
  229. {
  230. selector.CanFocus = cb.CheckedState == CheckState.Checked;
  231. }
  232. }
  233. List<SelectorBase> GetAllSelectors ()
  234. {
  235. List<SelectorBase> optionSelectors = [];
  236. // ReSharper disable once AccessToModifiedClosure
  237. optionSelectors.AddRange (optionSelectorsFrame!.SubViews.OfType<SelectorBase> ());
  238. // ReSharper disable once AccessToModifiedClosure
  239. optionSelectors.AddRange (flagSelectorsFrame!.SubViews.OfType<FlagSelector> ());
  240. return optionSelectors;
  241. }
  242. }
  243. }