Shortcuts.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9. using Terminal.Gui;
  10. namespace UICatalog.Scenarios;
  11. [ScenarioMetadata ("Shortcuts", "Illustrates Shortcut class.")]
  12. [ScenarioCategory ("Controls")]
  13. public class Shortcuts : Scenario
  14. {
  15. public override void Main ()
  16. {
  17. Application.Init ();
  18. Window app = new ();
  19. app.Loaded += App_Loaded;
  20. Application.Run (app);
  21. app.Dispose ();
  22. Application.Shutdown ();
  23. }
  24. // Setting everything up in Loaded handler because we change the
  25. // QuitKey and it only sticks if changed after init
  26. private void App_Loaded (object sender, EventArgs e)
  27. {
  28. Application.QuitKey = Key.Z.WithCtrl;
  29. Application.Top.Title = GetQuitKeyAndName ();
  30. ObservableCollection<string> eventSource = new ();
  31. var eventLog = new ListView
  32. {
  33. X = Pos.AnchorEnd (),
  34. Width = 40,
  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. Application.Top.Add (eventLog);
  42. var vShortcut1 = new Shortcut
  43. {
  44. Orientation = Orientation.Vertical,
  45. X = 0,
  46. Width = 35,
  47. Title = "A_pp Shortcut",
  48. Key = Key.F1,
  49. Text = "Width is 35",
  50. KeyBindingScope = KeyBindingScope.Application,
  51. };
  52. Application.Top.Add (vShortcut1);
  53. var vShortcut2 = new Shortcut
  54. {
  55. Orientation = Orientation.Vertical,
  56. X = 0,
  57. Y = Pos.Bottom (vShortcut1),
  58. Width = 35,
  59. Key = Key.F2,
  60. Text = "Width is 35",
  61. KeyBindingScope = KeyBindingScope.HotKey,
  62. CommandView = new RadioGroup
  63. {
  64. Orientation = Orientation.Vertical,
  65. RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"]
  66. }
  67. };
  68. ((RadioGroup)vShortcut2.CommandView).SelectedItemChanged += (o, args) =>
  69. {
  70. eventSource.Add ($"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
  71. eventLog.MoveDown ();
  72. };
  73. vShortcut2.Accept += (o, args) =>
  74. {
  75. // Cycle to next item. If at end, set 0
  76. if (((RadioGroup)vShortcut2.CommandView).SelectedItem < ((RadioGroup)vShortcut2.CommandView).RadioLabels.Length - 1)
  77. {
  78. ((RadioGroup)vShortcut2.CommandView).SelectedItem++;
  79. }
  80. else
  81. {
  82. ((RadioGroup)vShortcut2.CommandView).SelectedItem = 0;
  83. }
  84. };
  85. Application.Top.Add (vShortcut2);
  86. var vShortcut3 = new Shortcut
  87. {
  88. Orientation = Orientation.Vertical,
  89. X = 0,
  90. Y = Pos.Bottom (vShortcut2),
  91. CommandView = new CheckBox { Text = "_Align" },
  92. Key = Key.F5.WithCtrl.WithAlt.WithShift,
  93. HelpText = "Width is Fill",
  94. Width = Dim.Fill () - Dim.Width (eventLog),
  95. KeyBindingScope = KeyBindingScope.HotKey,
  96. };
  97. ((CheckBox)vShortcut3.CommandView).Toggle += (s, e) =>
  98. {
  99. if (vShortcut3.CommandView is CheckBox cb)
  100. {
  101. eventSource.Add ($"Toggle: {cb.Text}");
  102. eventLog.MoveDown ();
  103. var max = 0;
  104. var toAlign = Application.Top.Subviews.Where (v => v is Shortcut { Orientation: Orientation.Vertical, Width: not DimAbsolute });
  105. if (e.NewValue == CheckState.Checked)
  106. {
  107. foreach (Shortcut peer in toAlign)
  108. {
  109. // DANGER: KeyView is internal so we can't access it. So we assume this is how it works.
  110. max = Math.Max (max, peer.Key.ToString ().GetColumns ());
  111. }
  112. }
  113. foreach (Shortcut peer in toAlign)
  114. {
  115. peer.MinimumKeyTextSize = max;
  116. }
  117. }
  118. };
  119. Application.Top.Add (vShortcut3);
  120. var vShortcut4 = new Shortcut
  121. {
  122. Orientation = Orientation.Vertical,
  123. X = 0,
  124. Y = Pos.Bottom (vShortcut3),
  125. Width = Dim.Width (vShortcut3),
  126. CommandView = new Button
  127. {
  128. Title = "_Button",
  129. },
  130. HelpText = "Width is Fill",
  131. Key = Key.K,
  132. KeyBindingScope = KeyBindingScope.HotKey,
  133. };
  134. Button button = (Button)vShortcut4.CommandView;
  135. vShortcut4.CommandView.Accept += Button_Clicked;
  136. Application.Top.Add (vShortcut4);
  137. var vShortcut5 = new Shortcut
  138. {
  139. Orientation = Orientation.Vertical,
  140. X = 0,
  141. Y = Pos.Bottom (vShortcut4),
  142. Width = Dim.Width (vShortcut4),
  143. Key = Key.F4,
  144. HelpText = "CommandView.CanFocus",
  145. KeyBindingScope = KeyBindingScope.HotKey,
  146. CommandView = new CheckBox { Text = "_CanFocus" },
  147. };
  148. ((CheckBox)vShortcut5.CommandView).Toggle += (s, e) =>
  149. {
  150. if (vShortcut5.CommandView is CheckBox cb)
  151. {
  152. eventSource.Add ($"Toggle: {cb.Text}");
  153. eventLog.MoveDown ();
  154. foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
  155. {
  156. if (peer.CanFocus)
  157. {
  158. peer.CommandView.CanFocus = e.NewValue == CheckState.Checked;
  159. }
  160. }
  161. }
  162. };
  163. Application.Top.Add (vShortcut5);
  164. var vShortcutSlider = new Shortcut
  165. {
  166. Orientation = Orientation.Vertical,
  167. X = 0,
  168. Y = Pos.Bottom (vShortcut5),
  169. HelpText = "Width is Fill",
  170. Width = Dim.Width (vShortcut5),
  171. KeyBindingScope = KeyBindingScope.HotKey,
  172. CommandView = new Slider<string>
  173. {
  174. Orientation = Orientation.Vertical,
  175. AllowEmpty = true
  176. },
  177. Key = Key.F5,
  178. };
  179. ((Slider<string>)vShortcutSlider.CommandView).Options = new () { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } };
  180. ((Slider<string>)vShortcutSlider.CommandView).SetOption (0);
  181. ((Slider<string>)vShortcutSlider.CommandView).OptionsChanged += (o, args) =>
  182. {
  183. eventSource.Add ($"OptionsChanged: {o.GetType ().Name} - {string.Join (",", ((Slider<string>)o).GetSetOptions ())}");
  184. eventLog.MoveDown ();
  185. };
  186. Application.Top.Add (vShortcutSlider);
  187. var vShortcut6 = new Shortcut
  188. {
  189. Orientation = Orientation.Vertical,
  190. X = 0,
  191. Y = Pos.Bottom (vShortcutSlider),
  192. Width = Dim.Width (vShortcutSlider),
  193. Title = "_No Key",
  194. HelpText = "Keyless",
  195. };
  196. Application.Top.Add (vShortcut6);
  197. var vShortcut7 = new Shortcut
  198. {
  199. Orientation = Orientation.Vertical,
  200. X = 0,
  201. Y = Pos.Bottom (vShortcut6),
  202. Width = Dim.Width (vShortcutSlider),
  203. Key = Key.F6,
  204. Title = "Not _very much help",
  205. HelpText = "",
  206. };
  207. Application.Top.Add (vShortcut7);
  208. vShortcut7.SetFocus ();
  209. // Horizontal
  210. var hShortcut1 = new Shortcut
  211. {
  212. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  213. Y = Pos.Bottom (eventLog) + 1,
  214. Key = Key.F7,
  215. HelpText = "Horizontal",
  216. CanFocus = false
  217. };
  218. hShortcut1.CommandView = new ProgressBar
  219. {
  220. Text = "Progress",
  221. Title = "P",
  222. Fraction = 0.5f,
  223. Width = 10,
  224. Height = 1,
  225. ProgressBarStyle = ProgressBarStyle.Continuous
  226. };
  227. hShortcut1.CommandView.Width = 10;
  228. hShortcut1.CommandView.Height = 1;
  229. hShortcut1.CommandView.CanFocus = false;
  230. Timer timer = new (10)
  231. {
  232. AutoReset = true,
  233. };
  234. timer.Elapsed += (o, args) =>
  235. {
  236. if (hShortcut1.CommandView is ProgressBar pb)
  237. {
  238. if (pb.Fraction == 1.0)
  239. {
  240. pb.Fraction = 0;
  241. }
  242. pb.Fraction += 0.01f;
  243. Application.Wakeup ();
  244. pb.SetNeedsDisplay ();
  245. }
  246. };
  247. timer.Start ();
  248. Application.Top.Add (hShortcut1);
  249. var textField = new TextField ()
  250. {
  251. Text = "Edit me",
  252. Width = 10,
  253. Height = 1,
  254. CanFocus = true
  255. };
  256. var hShortcut2 = new Shortcut
  257. {
  258. Orientation = Orientation.Horizontal,
  259. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  260. Y = Pos.Top (hShortcut1),
  261. Key = Key.F8,
  262. HelpText = "TextField",
  263. CanFocus = true,
  264. CommandView = textField,
  265. };
  266. Application.Top.Add (hShortcut2);
  267. var hShortcutBG = new Shortcut
  268. {
  269. Orientation = Orientation.Horizontal,
  270. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1) - 1,
  271. Y = Pos.Top (hShortcut2),
  272. Key = Key.F9,
  273. HelpText = "BG Color",
  274. CanFocus = false
  275. };
  276. var bgColor = new ColorPicker ()
  277. {
  278. CanFocus = false,
  279. BoxHeight = 1,
  280. BoxWidth = 1,
  281. };
  282. bgColor.ColorChanged += (o, args) =>
  283. {
  284. Application.Top.ColorScheme = new ColorScheme (Application.Top.ColorScheme)
  285. {
  286. Normal = new Attribute (Application.Top.ColorScheme.Normal.Foreground, args.Color),
  287. };
  288. };
  289. hShortcutBG.CommandView = bgColor;
  290. Application.Top.Add (hShortcutBG);
  291. var hShortcut3 = new Shortcut
  292. {
  293. Orientation = Orientation.Horizontal,
  294. X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
  295. Y = Pos.Top (hShortcut2),
  296. Key = Key.Esc,
  297. KeyBindingScope = KeyBindingScope.Application,
  298. Title = "Quit",
  299. HelpText = "App Scope",
  300. CanFocus = false
  301. };
  302. hShortcut3.Accept += (o, args) =>
  303. {
  304. Application.RequestStop ();
  305. };
  306. Application.Top.Add (hShortcut3);
  307. foreach (View sh in Application.Top.Subviews.Where (v => v is Shortcut)!)
  308. {
  309. if (sh is Shortcut shortcut)
  310. {
  311. shortcut.Accept += (o, args) =>
  312. {
  313. eventSource.Add ($"Accept: {shortcut!.CommandView.Text}");
  314. eventLog.MoveDown ();
  315. args.Handled = true;
  316. };
  317. shortcut.CommandView.Accept += (o, args) =>
  318. {
  319. eventSource.Add ($"CommandView.Accept: {shortcut!.CommandView.Text}");
  320. eventLog.MoveDown ();
  321. };
  322. }
  323. }
  324. //((CheckBox)vShortcut5.CommandView).OnToggle ();
  325. }
  326. private void Button_Clicked (object sender, HandledEventArgs e)
  327. {
  328. //e.Cancel = true;
  329. MessageBox.Query ("Hi", $"You clicked {sender}");
  330. }
  331. }