Shortcuts.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #nullable enable
  2. using System.Collections.ObjectModel;
  3. using Timer = System.Timers.Timer;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Shortcuts", "Illustrates Shortcut class.")]
  6. [ScenarioCategory ("Controls")]
  7. public class Shortcuts : Scenario
  8. {
  9. public override void Main ()
  10. {
  11. Application.Init ();
  12. var quitKey = Application.QuitKey;
  13. Window app = new ();
  14. app.Loaded += App_Loaded;
  15. Application.Run (app);
  16. app.Dispose ();
  17. Application.Shutdown ();
  18. Application.QuitKey = quitKey;
  19. }
  20. // Setting everything up in Loaded handler because we change the
  21. // QuitKey and it only sticks if changed after init
  22. private void App_Loaded (object? sender, EventArgs e)
  23. {
  24. Application.QuitKey = Key.F4.WithCtrl;
  25. Application.Top!.Title = GetQuitKeyAndName ();
  26. ObservableCollection<string> eventSource = new ();
  27. var eventLog = new ListView
  28. {
  29. Id = "eventLog",
  30. X = Pos.AnchorEnd (),
  31. Y = 0,
  32. Height = Dim.Fill (4),
  33. SchemeName = "TopLevel",
  34. Source = new ListWrapper<string> (eventSource),
  35. BorderStyle = LineStyle.Double,
  36. Title = "E_vents"
  37. };
  38. eventLog.Width = Dim.Func (
  39. _ => Math.Min (
  40. Application.Top.Viewport.Width / 2,
  41. eventLog?.MaxLength + eventLog!.GetAdornmentsThickness ().Horizontal ?? 0));
  42. eventLog.Width = Dim.Func (
  43. _ => Math.Min (
  44. eventLog.SuperView!.Viewport.Width / 2,
  45. eventLog?.MaxLength + eventLog!.GetAdornmentsThickness ().Horizontal ?? 0));
  46. Application.Top.Add (eventLog);
  47. var alignKeysShortcut = new Shortcut
  48. {
  49. Id = "alignKeysShortcut",
  50. X = 0,
  51. Y = 0,
  52. Width = Dim.Fill ()! - Dim.Width (eventLog),
  53. HelpText = "Fill to log",
  54. CommandView = new CheckBox
  55. {
  56. Text = "_Align Keys",
  57. CanFocus = false,
  58. HighlightStates = MouseState.None
  59. },
  60. Key = Key.F5.WithCtrl.WithAlt.WithShift
  61. };
  62. // ((CheckBox)vShortcut3.CommandView).CheckedStateChanging += (_, args) =>
  63. ((CheckBox)alignKeysShortcut.CommandView).CheckedStateChanging += (s, e) =>
  64. {
  65. if (alignKeysShortcut.CommandView is CheckBox cb)
  66. {
  67. eventSource.Add (
  68. $"{alignKeysShortcut.Id}.CommandView.CheckedStateChanging: {cb.Text}");
  69. eventLog.MoveDown ();
  70. var max = 0;
  71. IEnumerable<View> toAlign =
  72. Application.Top.SubViews.Where (
  73. v => v is Shortcut { Width: not DimAbsolute });
  74. IEnumerable<View> enumerable = toAlign as View [] ?? toAlign.ToArray ();
  75. if (e.Result == CheckState.Checked)
  76. {
  77. max = (from Shortcut? peer in enumerable
  78. select peer.Key.ToString ().GetColumns ()).Prepend (max)
  79. .Max ();
  80. foreach (View view in enumerable)
  81. {
  82. var peer = (Shortcut)view;
  83. max = Math.Max (max, peer.KeyView.Text.GetColumns ());
  84. }
  85. }
  86. foreach (View view in enumerable)
  87. {
  88. var peer = (Shortcut)view;
  89. peer.MinimumKeyTextSize = max;
  90. }
  91. }
  92. };
  93. Application.Top.Add (alignKeysShortcut);
  94. var commandFirstShortcut = new Shortcut
  95. {
  96. Id = "commandFirstShortcut",
  97. X = 0,
  98. Y = Pos.Bottom (alignKeysShortcut),
  99. Width = Dim.Fill ()! - Dim.Width (eventLog),
  100. HelpText = "Show _Command first",
  101. CommandView = new CheckBox
  102. {
  103. Text = "Command _First",
  104. CanFocus = false,
  105. HighlightStates = MouseState.None
  106. },
  107. Key = Key.F.WithCtrl
  108. };
  109. ((CheckBox)commandFirstShortcut.CommandView).CheckedState =
  110. commandFirstShortcut.AlignmentModes.HasFlag (AlignmentModes.EndToStart) ? CheckState.UnChecked : CheckState.Checked;
  111. ((CheckBox)commandFirstShortcut.CommandView).CheckedStateChanging += (s, e) =>
  112. {
  113. if (commandFirstShortcut.CommandView is CheckBox cb)
  114. {
  115. eventSource.Add (
  116. $"{commandFirstShortcut.Id}.CommandView.CheckedStateChanging: {cb.Text}");
  117. eventLog.MoveDown ();
  118. IEnumerable<View> toAlign =
  119. Application.Top.SubViews.Where (
  120. v => v is Shortcut { Width: not DimAbsolute });
  121. IEnumerable<View> enumerable = toAlign as View [] ?? toAlign.ToArray ();
  122. foreach (View view in enumerable)
  123. {
  124. var peer = (Shortcut)view;
  125. if (e.Result == CheckState.Checked)
  126. {
  127. peer.AlignmentModes &= ~AlignmentModes.EndToStart;
  128. }
  129. else
  130. {
  131. peer.AlignmentModes |= AlignmentModes.EndToStart;
  132. }
  133. }
  134. }
  135. };
  136. Application.Top.Add (commandFirstShortcut);
  137. var canFocusShortcut = new Shortcut
  138. {
  139. Id = "canFocusShortcut",
  140. X = 0,
  141. Y = Pos.Bottom (commandFirstShortcut),
  142. Width = Dim.Fill ()! - Dim.Width (eventLog),
  143. Key = Key.F4,
  144. HelpText = "Changes all Command.CanFocus",
  145. CommandView = new CheckBox { Text = "_CanFocus" }
  146. };
  147. ((CheckBox)canFocusShortcut.CommandView).CheckedStateChanging += (s, e) =>
  148. {
  149. if (canFocusShortcut.CommandView is CheckBox cb)
  150. {
  151. eventSource.Add ($"Toggle: {cb.Text}");
  152. eventLog.MoveDown ();
  153. //cb.CanFocus = e.NewValue == CheckState.Checked;
  154. foreach (Shortcut peer in Application.Top.SubViews.Where (v => v is Shortcut)!)
  155. {
  156. if (peer.CanFocus)
  157. {
  158. peer.CommandView.CanFocus = e.Result == CheckState.Checked;
  159. }
  160. }
  161. }
  162. };
  163. Application.Top.Add (canFocusShortcut);
  164. var appShortcut = new Shortcut
  165. {
  166. Id = "appShortcut",
  167. X = 0,
  168. Y = Pos.Bottom (canFocusShortcut),
  169. Width = Dim.Fill (Dim.Func (_ => eventLog.Frame.Width)),
  170. Title = "A_pp Shortcut",
  171. Key = Key.F1,
  172. Text = "Width is DimFill",
  173. BindKeyToApplication = true
  174. };
  175. Application.Top.Add (appShortcut);
  176. var buttonShortcut = new Shortcut
  177. {
  178. Id = "buttonShortcut",
  179. X = 0,
  180. Y = Pos.Bottom (appShortcut),
  181. Width = Dim.Fill ()! - Dim.Width (eventLog),
  182. HelpText = "Accepting pops MB",
  183. CommandView = new Button
  184. {
  185. Title = "_Button",
  186. ShadowStyle = ShadowStyle.None,
  187. HighlightStates = MouseState.None
  188. },
  189. Key = Key.K
  190. };
  191. var button = (Button)buttonShortcut.CommandView;
  192. buttonShortcut.Accepting += Button_Clicked;
  193. Application.Top.Add (buttonShortcut);
  194. var radioGroupShortcut = new Shortcut
  195. {
  196. Id = "radioGroupShortcut",
  197. X = 0,
  198. Y = Pos.Bottom (buttonShortcut),
  199. Key = Key.F2,
  200. Width = Dim.Fill ()! - Dim.Width (eventLog),
  201. CommandView = new RadioGroup
  202. {
  203. Orientation = Orientation.Vertical,
  204. RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"]
  205. }
  206. };
  207. ((RadioGroup)radioGroupShortcut.CommandView).SelectedItemChanged += (o, args) =>
  208. {
  209. if (o is { })
  210. {
  211. eventSource.Add (
  212. $"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
  213. eventLog.MoveDown ();
  214. }
  215. };
  216. Application.Top.Add (radioGroupShortcut);
  217. var sliderShortcut = new Shortcut
  218. {
  219. Id = "sliderShortcut",
  220. X = 0,
  221. Y = Pos.Bottom (radioGroupShortcut),
  222. Width = Dim.Fill ()! - Dim.Width (eventLog),
  223. HelpText = "Sliders work!",
  224. CommandView = new Slider<string>
  225. {
  226. Orientation = Orientation.Horizontal,
  227. AllowEmpty = true
  228. },
  229. Key = Key.F5
  230. };
  231. ((Slider<string>)sliderShortcut.CommandView).Options = [new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" }];
  232. ((Slider<string>)sliderShortcut.CommandView).SetOption (0);
  233. ((Slider<string>)sliderShortcut.CommandView).OptionsChanged += (o, args) =>
  234. {
  235. eventSource.Add (
  236. $"OptionsChanged: {o?.GetType ().Name} - {string.Join (",", ((Slider<string>)o!)!.GetSetOptions ())}");
  237. eventLog.MoveDown ();
  238. };
  239. Application.Top.Add (sliderShortcut);
  240. var noCommandShortcut = new Shortcut
  241. {
  242. Id = "noCommandShortcut",
  243. X = 0,
  244. Y = Pos.Bottom (sliderShortcut),
  245. Width = Dim.Width (sliderShortcut),
  246. HelpText = "No Command",
  247. Key = Key.D0
  248. };
  249. Application.Top.Add (noCommandShortcut);
  250. var noKeyShortcut = new Shortcut
  251. {
  252. Id = "noKeyShortcut",
  253. X = 0,
  254. Y = Pos.Bottom (noCommandShortcut),
  255. Width = Dim.Width (noCommandShortcut),
  256. Title = "No Ke_y",
  257. HelpText = "Keyless"
  258. };
  259. Application.Top.Add (noKeyShortcut);
  260. var noHelpShortcut = new Shortcut
  261. {
  262. Id = "noHelpShortcut",
  263. X = 0,
  264. Y = Pos.Bottom (noKeyShortcut),
  265. Width = Dim.Width (noKeyShortcut),
  266. Key = Key.F6,
  267. Title = "Not _very much help",
  268. HelpText = ""
  269. };
  270. Application.Top.Add (noHelpShortcut);
  271. noHelpShortcut.SetFocus ();
  272. var framedShortcut = new Shortcut
  273. {
  274. Id = "framedShortcut",
  275. X = 0,
  276. Y = Pos.Bottom (noHelpShortcut) + 1,
  277. Title = "Framed",
  278. Key = Key.K.WithCtrl,
  279. Text = "Resize frame",
  280. BorderStyle = LineStyle.Dotted,
  281. Arrangement = ViewArrangement.RightResizable | ViewArrangement.BottomResizable
  282. };
  283. framedShortcut.Orientation = Orientation.Horizontal;
  284. if (framedShortcut.Padding is { })
  285. {
  286. framedShortcut.Padding.Thickness = new (0, 1, 0, 0);
  287. framedShortcut.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
  288. }
  289. if (framedShortcut.CommandView.Margin is { })
  290. {
  291. framedShortcut.CommandView.Margin!.SchemeName = framedShortcut.CommandView.SchemeName = "Error";
  292. framedShortcut.HelpView.Margin!.SchemeName = framedShortcut.HelpView.SchemeName = "Dialog";
  293. framedShortcut.KeyView.Margin!.SchemeName = framedShortcut.KeyView.SchemeName = "Menu";
  294. }
  295. framedShortcut.SchemeName = "TopLevel";
  296. Application.Top.Add (framedShortcut);
  297. // Horizontal
  298. var progressShortcut = new Shortcut
  299. {
  300. Id = "progressShortcut",
  301. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  302. Y = Pos.AnchorEnd () - 1,
  303. Key = Key.F7,
  304. HelpText = "Horizontal"
  305. };
  306. progressShortcut.CommandView = new ProgressBar
  307. {
  308. Text = "Progress",
  309. Title = "P",
  310. Fraction = 0.5f,
  311. Width = 10,
  312. Height = 1,
  313. ProgressBarStyle = ProgressBarStyle.Continuous
  314. };
  315. progressShortcut.CommandView.Width = 10;
  316. progressShortcut.CommandView.Height = 1;
  317. progressShortcut.CommandView.CanFocus = false;
  318. Timer timer = new (10)
  319. {
  320. AutoReset = true
  321. };
  322. timer.Elapsed += (o, args) =>
  323. {
  324. if (progressShortcut.CommandView is ProgressBar pb)
  325. {
  326. if (pb.Fraction == 1.0)
  327. {
  328. pb.Fraction = 0;
  329. }
  330. pb.Fraction += 0.01f;
  331. pb.SetNeedsDraw ();
  332. }
  333. };
  334. timer.Start ();
  335. Application.Top.Add (progressShortcut);
  336. var textField = new TextField
  337. {
  338. Text = "Edit me",
  339. Width = 10,
  340. Height = 1
  341. };
  342. var textFieldShortcut = new Shortcut
  343. {
  344. Id = "textFieldShortcut",
  345. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  346. Y = Pos.AnchorEnd () - 1,
  347. Key = Key.F8,
  348. HelpText = "TextField",
  349. CanFocus = true,
  350. CommandView = textField
  351. };
  352. textField.CanFocus = true;
  353. Application.Top.Add (textFieldShortcut);
  354. var bgColorShortcut = new Shortcut
  355. {
  356. Id = "bgColorShortcut",
  357. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  358. Y = Pos.AnchorEnd (),
  359. Key = Key.F9,
  360. HelpText = "Cycles BG Color"
  361. };
  362. var bgColor = new ColorPicker16
  363. {
  364. BoxHeight = 1,
  365. BoxWidth = 1
  366. };
  367. bgColorShortcut.Selecting += (o, args) =>
  368. {
  369. //args.Cancel = true;
  370. };
  371. bgColorShortcut.Accepting += (o, args) =>
  372. {
  373. if (bgColor.SelectedColor == ColorName16.White)
  374. {
  375. bgColor.SelectedColor = ColorName16.Black;
  376. return;
  377. }
  378. bgColor.SelectedColor++;
  379. args.Handled = true;
  380. };
  381. bgColor.ColorChanged += (o, args) =>
  382. {
  383. if (o is { })
  384. {
  385. eventSource.Add ($"ColorChanged: {o.GetType ().Name} - {args.Result}");
  386. eventLog.MoveDown ();
  387. Application.Top.SetScheme (
  388. new (Application.Top.GetScheme ())
  389. {
  390. Normal = new (
  391. Application.Top!.GetAttributeForRole (VisualRole.Normal).Foreground,
  392. args.Result,
  393. Application.Top!.GetAttributeForRole (VisualRole.Normal).Style)
  394. });
  395. }
  396. };
  397. bgColorShortcut.CommandView = bgColor;
  398. Application.Top.Add (bgColorShortcut);
  399. var appQuitShortcut = new Shortcut
  400. {
  401. Id = "appQuitShortcut",
  402. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  403. Y = Pos.AnchorEnd () - 1,
  404. Key = Key.Esc,
  405. BindKeyToApplication = true,
  406. Title = "Quit",
  407. HelpText = "App Scope"
  408. };
  409. appQuitShortcut.Accepting += (o, args) => { Application.RequestStop (); };
  410. Application.Top.Add (appQuitShortcut);
  411. foreach (Shortcut shortcut in Application.Top.SubViews.OfType<Shortcut> ())
  412. {
  413. shortcut.Selecting += (o, args) =>
  414. {
  415. if (args.Handled)
  416. {
  417. return;
  418. }
  419. eventSource.Add ($"{shortcut!.Id}.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
  420. eventLog.MoveDown ();
  421. };
  422. shortcut.CommandView.Selecting += (o, args) =>
  423. {
  424. if (args.Handled)
  425. {
  426. return;
  427. }
  428. eventSource.Add (
  429. $"{shortcut!.Id}.CommandView.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
  430. eventLog.MoveDown ();
  431. args.Handled = true;
  432. };
  433. shortcut.Accepting += (o, args) =>
  434. {
  435. eventSource.Add ($"{shortcut!.Id}.Accepting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
  436. eventLog.MoveDown ();
  437. // We don't want this to exit the Scenario
  438. args.Handled = true;
  439. };
  440. shortcut.CommandView.Accepting += (o, args) =>
  441. {
  442. eventSource.Add (
  443. $"{shortcut!.Id}.CommandView.Accepting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
  444. eventLog.MoveDown ();
  445. };
  446. }
  447. }
  448. private void Button_Clicked (object? sender, CommandEventArgs e)
  449. {
  450. e.Handled = true;
  451. var view = sender as View;
  452. MessageBox.Query ("Hi", $"You clicked {view?.Text}", "_Ok");
  453. }
  454. }