Shortcuts.cs 24 KB

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