Shortcuts.cs 14 KB

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