Shortcuts.cs 19 KB

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