Bars.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("Bars", "Illustrates Bar views (e.g. StatusBar)")]
  7. [ScenarioCategory ("Controls")]
  8. public class Bars : Scenario
  9. {
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. Window app = new ();
  14. app.Loaded += App_Loaded;
  15. Application.Run (app);
  16. app.Dispose ();
  17. Application.Shutdown ();
  18. }
  19. // Setting everything up in Loaded handler because we change the
  20. // QuitKey and it only sticks if changed after init
  21. private void App_Loaded (object sender, EventArgs e)
  22. {
  23. Application.QuitKey = Key.Z.WithCtrl;
  24. Application.Top.Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}";
  25. List<string> eventSource = new ();
  26. ListView eventLog = new ListView ()
  27. {
  28. X = Pos.AnchorEnd (),
  29. Width = 50,
  30. Height = Dim.Fill (),
  31. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  32. Source = new ListWrapper (eventSource)
  33. };
  34. Application.Top.Add (eventLog);
  35. var shortcut1 = new Shortcut
  36. {
  37. Title = "_Zigzag",
  38. Key = Key.Z.WithAlt,
  39. Text = "Gonna zig zag",
  40. KeyBindingScope = KeyBindingScope.HotKey,
  41. Command = Command.Accept,
  42. };
  43. shortcut1.Accept += (s, e) =>
  44. {
  45. eventSource.Add ($"Accept: {s}");
  46. eventLog.MoveDown ();
  47. };
  48. Application.Top.Add (shortcut1);
  49. shortcut1.SetFocus ();
  50. //var shortcut2 = new Shortcut
  51. //{
  52. // Title = "Za_G",
  53. // Text = "Gonna zag",
  54. // Key = Key.G.WithAlt,
  55. // KeyBindingScope = KeyBindingScope.HotKey,
  56. // Command = Command.Accept,
  57. // X = Pos.Left (shortcut1),
  58. // Y = Pos.Bottom (shortcut1),
  59. // //Width = 50,
  60. //};
  61. //var shortcut3 = new Shortcut
  62. //{
  63. // Title = "Shortcut3",
  64. // Key = Key.D3.WithCtrl,
  65. // Text = "Number Three",
  66. // KeyBindingScope = KeyBindingScope.Application,
  67. // Command = Command.Accept,
  68. //};
  69. //shortcut3.Accept += (s, e) =>
  70. // {
  71. // eventSource.Add ($"Accept: {s}");
  72. // eventLog.MoveDown ();
  73. // };
  74. //var shortcut4 = new Shortcut
  75. //{
  76. // Title = "Shortcut4",
  77. // Text = "Number 4",
  78. // Key = Key.F4,
  79. // KeyBindingScope = KeyBindingScope.Application,
  80. // Command = Command.Accept,
  81. //};
  82. //var cb = new CheckBox ()
  83. //{
  84. // Title = "Hello",// shortcut4.Text
  85. //};
  86. //cb.Toggled += (s, e) =>
  87. // {
  88. // eventSource.Add ($"Toggled: {s}");
  89. // eventLog.MoveDown ();
  90. // };
  91. //shortcut4.CommandView = cb;
  92. //shortcut4.Accept += (s, e) =>
  93. // {
  94. // eventSource.Add ($"Accept: {s}");
  95. // eventLog.MoveDown ();
  96. // };
  97. //var bar = new Bar
  98. //{
  99. // X = 2,
  100. // Y = Pos.Bottom(shortcut1),
  101. // Orientation = Orientation.Vertical,
  102. // StatusBarStyle = false,
  103. // Width = Dim.Percent(40)
  104. //};
  105. //bar.Add (shortcut3, shortcut4);
  106. ////CheckBox hello = new ()
  107. ////{
  108. //// Title = "Hello",
  109. //// X = 0,
  110. //// Y = 1,
  111. ////};
  112. ////Application.Top.Add (hello);
  113. ////hello.Toggled += (s, e) =>
  114. //// {
  115. //// eventSource.Add ($"Toggled: {s}");
  116. //// eventLog.MoveDown ();
  117. //// };
  118. //Application.Top.Add (bar);
  119. // BUGBUG: This should not be needed
  120. //Application.Top.LayoutSubviews ();
  121. //SetupMenuBar ();
  122. //SetupContentMenu ();
  123. // SetupStatusBar ();
  124. }
  125. private void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  126. //private void SetupContentMenu ()
  127. //{
  128. // Application.Top.Add (new Label { Text = "Right Click for Context Menu", X = Pos.Center (), Y = 4 });
  129. // Application.Top.MouseClick += ShowContextMenu;
  130. //}
  131. //private void ShowContextMenu (object s, MouseEventEventArgs e)
  132. //{
  133. // if (e.MouseEvent.Flags != MouseFlags.Button3Clicked)
  134. // {
  135. // return;
  136. // }
  137. // var contextMenu = new Bar
  138. // {
  139. // Id = "contextMenu",
  140. // X = e.MouseEvent.Position.X,
  141. // Y = e.MouseEvent.Position.Y,
  142. // Width = Dim.Auto (DimAutoStyle.Content),
  143. // Height = Dim.Auto (DimAutoStyle.Content),
  144. // Orientation = Orientation.Vertical,
  145. // StatusBarStyle = false,
  146. // BorderStyle = LineStyle.Rounded,
  147. // Modal = true,
  148. // };
  149. // var newMenu = new Shortcut
  150. // {
  151. // Title = "_New...",
  152. // Text = "Create a new file",
  153. // Key = Key.N.WithCtrl,
  154. // CanFocus = true
  155. // };
  156. // newMenu.Accept += (s, e) =>
  157. // {
  158. // contextMenu.RequestStop ();
  159. // Application.AddTimeout (
  160. // new TimeSpan (0),
  161. // () =>
  162. // {
  163. // MessageBox.Query ("File", "New");
  164. // return false;
  165. // });
  166. // };
  167. // var open = new Shortcut
  168. // {
  169. // Title = "_Open...",
  170. // Text = "Show the File Open Dialog",
  171. // Key = Key.O.WithCtrl,
  172. // CanFocus = true
  173. // };
  174. // open.Accept += (s, e) =>
  175. // {
  176. // contextMenu.RequestStop ();
  177. // Application.AddTimeout (
  178. // new TimeSpan (0),
  179. // () =>
  180. // {
  181. // MessageBox.Query ("File", "Open");
  182. // return false;
  183. // });
  184. // };
  185. // var save = new Shortcut
  186. // {
  187. // Title = "_Save...",
  188. // Text = "Save",
  189. // Key = Key.S.WithCtrl,
  190. // CanFocus = true
  191. // };
  192. // save.Accept += (s, e) =>
  193. // {
  194. // contextMenu.RequestStop ();
  195. // Application.AddTimeout (
  196. // new TimeSpan (0),
  197. // () =>
  198. // {
  199. // MessageBox.Query ("File", "Save");
  200. // return false;
  201. // });
  202. // };
  203. // var saveAs = new Shortcut
  204. // {
  205. // Title = "Save _As...",
  206. // Text = "Save As",
  207. // Key = Key.A.WithCtrl,
  208. // CanFocus = true
  209. // };
  210. // saveAs.Accept += (s, e) =>
  211. // {
  212. // contextMenu.RequestStop ();
  213. // Application.AddTimeout (
  214. // new TimeSpan (0),
  215. // () =>
  216. // {
  217. // MessageBox.Query ("File", "Save As");
  218. // return false;
  219. // });
  220. // };
  221. // contextMenu.Add (newMenu, open, save, saveAs);
  222. // contextMenu.KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  223. // contextMenu.Initialized += Menu_Initialized;
  224. // void Application_MouseEvent (object sender, MouseEvent e)
  225. // {
  226. // // If user clicks outside of the menuWindow, close it
  227. // if (!contextMenu.Frame.Contains (e.Position.X, e.Position.Y))
  228. // {
  229. // if (e.Flags is (MouseFlags.Button1Clicked or MouseFlags.Button3Clicked))
  230. // {
  231. // contextMenu.RequestStop ();
  232. // }
  233. // }
  234. // }
  235. // Application.MouseEvent += Application_MouseEvent;
  236. // Application.Run (contextMenu);
  237. // contextMenu.Dispose ();
  238. // Application.MouseEvent -= Application_MouseEvent;
  239. //}
  240. private void Menu_Initialized (object sender, EventArgs e)
  241. {
  242. // BUGBUG: this should not be needed
  243. ((View)(sender)).LayoutSubviews ();
  244. }
  245. //private void SetupMenuBar ()
  246. //{
  247. // var menuBar = new Bar
  248. // {
  249. // Id = "menuBar",
  250. // X = 0,
  251. // Y = 0,
  252. // Width = Dim.Fill (),
  253. // Height = Dim.Auto (DimAutoStyle.Content),
  254. // StatusBarStyle = true
  255. // };
  256. // var fileMenu = new Shortcut
  257. // {
  258. // Title = "_File",
  259. // Key = Key.F.WithAlt,
  260. // KeyBindingScope = KeyBindingScope.HotKey,
  261. // Command = Command.Accept,
  262. // };
  263. // fileMenu.HelpView.Visible = false;
  264. // fileMenu.KeyView.Visible = false;
  265. // fileMenu.Accept += (s, e) =>
  266. // {
  267. // fileMenu.SetFocus ();
  268. // if (s is View view)
  269. // {
  270. // var menu = new Bar
  271. // {
  272. // X = view.Frame.X + 1,
  273. // Y = view.Frame.Y + 1,
  274. // ColorScheme = view.ColorScheme,
  275. // Orientation = Orientation.Vertical,
  276. // StatusBarStyle = false,
  277. // BorderStyle = LineStyle.Dotted,
  278. // Width = Dim.Auto (DimAutoStyle.Content),
  279. // Height = Dim.Auto (DimAutoStyle.Content),
  280. // };
  281. // menu.KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  282. // var newMenu = new Shortcut
  283. // {
  284. // Title = "_New...",
  285. // Text = "Create a new file",
  286. // Key = Key.N.WithCtrl
  287. // };
  288. // var open = new Shortcut
  289. // {
  290. // Title = "_Open...",
  291. // Text = "Show the File Open Dialog",
  292. // Key = Key.O.WithCtrl
  293. // };
  294. // var save = new Shortcut
  295. // {
  296. // Title = "_Save...",
  297. // Text = "Save",
  298. // Key = Key.S.WithCtrl
  299. // };
  300. // menu.Add (newMenu, open, save);
  301. // // BUGBUG: this is all bad
  302. // menu.Initialized += Menu_Initialized;
  303. // open.Initialized += Menu_Initialized;
  304. // save.Initialized += Menu_Initialized;
  305. // newMenu.Initialized += Menu_Initialized;
  306. // Application.Run (menu);
  307. // menu.Dispose ();
  308. // Application.Refresh ();
  309. // }
  310. // };
  311. // var editMenu = new Shortcut
  312. // {
  313. // Title = "_Edit",
  314. // //Key = Key.E.WithAlt,
  315. // KeyBindingScope = KeyBindingScope.HotKey,
  316. // Command = Command.Accept
  317. // };
  318. // editMenu.Accept += (s, e) => { };
  319. // editMenu.HelpView.Visible = false;
  320. // editMenu.KeyView.Visible = false;
  321. // menuBar.Add (fileMenu, editMenu);
  322. // menuBar.Initialized += Menu_Initialized;
  323. // Application.Top.Add (menuBar);
  324. //}
  325. private void SetupStatusBar ()
  326. {
  327. var statusBar = new Bar
  328. {
  329. Id = "statusBar",
  330. X = 0,
  331. Y = Pos.AnchorEnd (),
  332. Width = Dim.Fill (),
  333. };
  334. var shortcut = new Shortcut
  335. {
  336. Text = "Quit Application",
  337. Title = "Q_uit",
  338. Key = Application.QuitKey,
  339. KeyBindingScope = KeyBindingScope.Application,
  340. Command = Command.QuitToplevel,
  341. CanFocus = false
  342. };
  343. statusBar.Add (shortcut);
  344. shortcut = new Shortcut
  345. {
  346. Text = "Help Text",
  347. Title = "Help",
  348. Key = Key.F1,
  349. KeyBindingScope = KeyBindingScope.HotKey,
  350. Command = Command.Accept,
  351. CanFocus = false
  352. };
  353. var labelHelp = new Label
  354. {
  355. X = Pos.Center (),
  356. Y = Pos.Top (statusBar) - 1,
  357. Text = "Help"
  358. };
  359. Application.Top.Add (labelHelp);
  360. shortcut.Accept += (s, e) =>
  361. {
  362. labelHelp.Text = labelHelp.Text + "!";
  363. e.Handled = true;
  364. };
  365. statusBar.Add (shortcut);
  366. shortcut = new Shortcut
  367. {
  368. Title = "_Show/Hide",
  369. Key = Key.F10,
  370. KeyBindingScope = KeyBindingScope.HotKey,
  371. Command = Command.ToggleExpandCollapse,
  372. CommandView = new CheckBox
  373. {
  374. Text = "_Show/Hide"
  375. },
  376. CanFocus = false
  377. };
  378. statusBar.Add (shortcut);
  379. var button1 = new Button
  380. {
  381. Text = "I'll Hide",
  382. Visible = false
  383. };
  384. button1.Accept += Button_Clicked;
  385. statusBar.Add (button1);
  386. ((CheckBox)shortcut.CommandView).Toggled += (s, e) =>
  387. {
  388. button1.Visible = !button1.Visible;
  389. button1.Enabled = button1.Visible;
  390. };
  391. statusBar.Add (new Label { HotKeySpecifier = new Rune ('_'), Text = "Fo_cusLabel", CanFocus = true });
  392. var button2 = new Button
  393. {
  394. Text = "Or me!",
  395. };
  396. button2.Accept += (s, e) => Application.RequestStop ();
  397. statusBar.Add (button2);
  398. statusBar.Initialized += Menu_Initialized;
  399. Application.Top.Add (statusBar);
  400. }
  401. }