Shortcuts.cs 19 KB

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