Bars.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using Terminal.Gui;
  8. namespace UICatalog.Scenarios;
  9. [ScenarioMetadata ("Bars", "Illustrates Bar views (e.g. StatusBar)")]
  10. [ScenarioCategory ("Controls")]
  11. public class Bars : Scenario
  12. {
  13. public override void Main ()
  14. {
  15. Application.Init ();
  16. Toplevel 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.Top!.Title = GetQuitKeyAndName ();
  27. ObservableCollection<string> eventSource = new ();
  28. ListView eventLog = new ListView ()
  29. {
  30. Title = "Event Log",
  31. X = Pos.AnchorEnd (),
  32. Width = Dim.Auto (),
  33. Height = Dim.Fill (), // Make room for some wide things
  34. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  35. Source = new ListWrapper<string> (eventSource)
  36. };
  37. eventLog.Border.Thickness = new (0, 1, 0, 0);
  38. Application.Top.Add (eventLog);
  39. FrameView menuBarLikeExamples = new ()
  40. {
  41. Title = "MenuBar-Like Examples",
  42. X = 0,
  43. Y = 0,
  44. Width = Dim.Fill () - Dim.Width (eventLog),
  45. Height = Dim.Percent(33),
  46. };
  47. Application.Top.Add (menuBarLikeExamples);
  48. Label label = new Label ()
  49. {
  50. Title = " Bar:",
  51. X = 0,
  52. Y = 0,
  53. };
  54. menuBarLikeExamples.Add (label);
  55. Bar bar = new Bar
  56. {
  57. Id = "menuBar-like",
  58. X = Pos.Right (label),
  59. Y = Pos.Top (label),
  60. Width = Dim.Fill (),
  61. };
  62. ConfigMenuBar (bar);
  63. menuBarLikeExamples.Add (bar);
  64. label = new Label ()
  65. {
  66. Title = " MenuBar:",
  67. X = 0,
  68. Y = Pos.Bottom (bar) + 1
  69. };
  70. menuBarLikeExamples.Add (label);
  71. //bar = new MenuBarv2
  72. //{
  73. // Id = "menuBar",
  74. // X = Pos.Right (label),
  75. // Y = Pos.Top (label),
  76. //};
  77. //ConfigMenuBar (bar);
  78. //menuBarLikeExamples.Add (bar);
  79. FrameView menuLikeExamples = new ()
  80. {
  81. Title = "Menu-Like Examples",
  82. X = 0,
  83. Y = Pos.Center (),
  84. Width = Dim.Fill () - Dim.Width (eventLog),
  85. Height = Dim.Percent (33),
  86. };
  87. Application.Top.Add (menuLikeExamples);
  88. label = new Label ()
  89. {
  90. Title = "Bar:",
  91. X = 0,
  92. Y = 0,
  93. };
  94. menuLikeExamples.Add (label);
  95. bar = new Bar
  96. {
  97. Id = "menu-like",
  98. X = 0,
  99. Y = Pos.Bottom(label),
  100. //Width = Dim.Percent (40),
  101. Orientation = Orientation.Vertical,
  102. };
  103. ConfigureMenu (bar);
  104. menuLikeExamples.Add (bar);
  105. label = new Label ()
  106. {
  107. Title = "Menu:",
  108. X = Pos.Right(bar) + 1,
  109. Y = Pos.Top (label),
  110. };
  111. menuLikeExamples.Add (label);
  112. bar = new Menuv2
  113. {
  114. Id = "menu",
  115. X = Pos.Left (label),
  116. Y = Pos.Bottom (label),
  117. };
  118. ConfigureMenu (bar);
  119. bar.Arrangement = ViewArrangement.RightResizable;
  120. menuLikeExamples.Add (bar);
  121. label = new Label ()
  122. {
  123. Title = "PopOver Menu (Right click to show):",
  124. X = Pos.Right (bar) + 1,
  125. Y = Pos.Top (label),
  126. };
  127. menuLikeExamples.Add (label);
  128. Menuv2 popOverMenu = new Menuv2
  129. {
  130. Id = "popupMenu",
  131. X = Pos.Left (label),
  132. Y = Pos.Bottom (label),
  133. };
  134. ConfigureMenu (popOverMenu);
  135. popOverMenu.Arrangement = ViewArrangement.Overlapped;
  136. popOverMenu.Visible = false;
  137. //popOverMenu.Enabled = false;
  138. var toggleShortcut = new Shortcut
  139. {
  140. Title = "Toggle Hide",
  141. Text = "App",
  142. BindKeyToApplication = true,
  143. Key = Key.F4.WithCtrl,
  144. };
  145. popOverMenu.Add (toggleShortcut);
  146. popOverMenu.Accepting += PopOverMenuOnAccept;
  147. void PopOverMenuOnAccept (object o, CommandEventArgs args)
  148. {
  149. if (popOverMenu.Visible)
  150. {
  151. popOverMenu.Visible = false;
  152. }
  153. else
  154. {
  155. popOverMenu.Visible = true;
  156. popOverMenu.SetFocus ();
  157. }
  158. }
  159. menuLikeExamples.Add (popOverMenu);
  160. menuLikeExamples.MouseClick += MenuLikeExamplesMouseClick;
  161. void MenuLikeExamplesMouseClick (object sender, MouseEventArgs e)
  162. {
  163. if (e.Flags.HasFlag (MouseFlags.Button3Clicked))
  164. {
  165. popOverMenu.X = e.Position.X;
  166. popOverMenu.Y = e.Position.Y;
  167. popOverMenu.Visible = true;
  168. //popOverMenu.Enabled = popOverMenu.Visible;
  169. popOverMenu.SetFocus ();
  170. }
  171. else
  172. {
  173. popOverMenu.Visible = false;
  174. //popOverMenu.Enabled = popOverMenu.Visible;
  175. }
  176. }
  177. FrameView statusBarLikeExamples = new ()
  178. {
  179. Title = "StatusBar-Like Examples",
  180. X = 0,
  181. Y = Pos.AnchorEnd (),
  182. Width = Dim.Width (menuLikeExamples),
  183. Height = Dim.Percent (33),
  184. };
  185. Application.Top.Add (statusBarLikeExamples);
  186. label = new Label ()
  187. {
  188. Title = " Bar:",
  189. X = 0,
  190. Y = 0,
  191. };
  192. statusBarLikeExamples.Add (label);
  193. bar = new Bar
  194. {
  195. Id = "statusBar-like",
  196. X = Pos.Right (label),
  197. Y = Pos.Top (label),
  198. Width = Dim.Fill (),
  199. Orientation = Orientation.Horizontal,
  200. };
  201. ConfigStatusBar (bar);
  202. statusBarLikeExamples.Add (bar);
  203. label = new Label ()
  204. {
  205. Title = "StatusBar:",
  206. X = 0,
  207. Y = Pos.Bottom (bar) + 1,
  208. };
  209. statusBarLikeExamples.Add (label);
  210. bar = new StatusBar ()
  211. {
  212. Id = "statusBar",
  213. X = Pos.Right (label),
  214. Y = Pos.Top (label),
  215. Width = Dim.Fill (),
  216. };
  217. ConfigStatusBar (bar);
  218. statusBarLikeExamples.Add (bar);
  219. foreach (FrameView frameView in Application.Top.SubViews.Where (f => f is FrameView)!)
  220. {
  221. foreach (Bar barView in frameView.SubViews.Where (b => b is Bar)!)
  222. {
  223. foreach (Shortcut sh in barView.SubViews.Where (s => s is Shortcut)!)
  224. {
  225. sh.Accepting += (o, args) =>
  226. {
  227. eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}");
  228. eventLog.MoveDown ();
  229. //args.Handled = true;
  230. };
  231. }
  232. }
  233. }
  234. }
  235. //private void SetupContentMenu ()
  236. //{
  237. // Application.Top.Add (new Label { Text = "Right Click for Context Menu", X = Pos.Center (), Y = 4 });
  238. // Application.Top.MouseClick += ShowContextMenu;
  239. //}
  240. //private void ShowContextMenu (object s, MouseEventEventArgs e)
  241. //{
  242. // if (e.Flags != MouseFlags.Button3Clicked)
  243. // {
  244. // return;
  245. // }
  246. // var contextMenu = new Bar
  247. // {
  248. // Id = "contextMenu",
  249. // X = e.Position.X,
  250. // Y = e.Position.Y,
  251. // Width = Dim.Auto (DimAutoStyle.Content),
  252. // Height = Dim.Auto (DimAutoStyle.Content),
  253. // Orientation = Orientation.Vertical,
  254. // StatusBarStyle = false,
  255. // BorderStyle = LineStyle.Rounded,
  256. // Modal = true,
  257. // };
  258. // var newMenu = new Shortcut
  259. // {
  260. // Title = "_New...",
  261. // Text = "Create a new file",
  262. // Key = Key.N.WithCtrl,
  263. // CanFocus = true
  264. // };
  265. // newMenu.Accept += (s, e) =>
  266. // {
  267. // contextMenu.RequestStop ();
  268. // Application.AddTimeout (
  269. // new TimeSpan (0),
  270. // () =>
  271. // {
  272. // MessageBox.Query ("File", "New");
  273. // return false;
  274. // });
  275. // };
  276. // var open = new Shortcut
  277. // {
  278. // Title = "_Open...",
  279. // Text = "Show the File Open Dialog",
  280. // Key = Key.O.WithCtrl,
  281. // CanFocus = true
  282. // };
  283. // open.Accept += (s, e) =>
  284. // {
  285. // contextMenu.RequestStop ();
  286. // Application.AddTimeout (
  287. // new TimeSpan (0),
  288. // () =>
  289. // {
  290. // MessageBox.Query ("File", "Open");
  291. // return false;
  292. // });
  293. // };
  294. // var save = new Shortcut
  295. // {
  296. // Title = "_Save...",
  297. // Text = "Save",
  298. // Key = Key.S.WithCtrl,
  299. // CanFocus = true
  300. // };
  301. // save.Accept += (s, e) =>
  302. // {
  303. // contextMenu.RequestStop ();
  304. // Application.AddTimeout (
  305. // new TimeSpan (0),
  306. // () =>
  307. // {
  308. // MessageBox.Query ("File", "Save");
  309. // return false;
  310. // });
  311. // };
  312. // var saveAs = new Shortcut
  313. // {
  314. // Title = "Save _As...",
  315. // Text = "Save As",
  316. // Key = Key.A.WithCtrl,
  317. // CanFocus = true
  318. // };
  319. // saveAs.Accept += (s, e) =>
  320. // {
  321. // contextMenu.RequestStop ();
  322. // Application.AddTimeout (
  323. // new TimeSpan (0),
  324. // () =>
  325. // {
  326. // MessageBox.Query ("File", "Save As");
  327. // return false;
  328. // });
  329. // };
  330. // contextMenu.Add (newMenu, open, save, saveAs);
  331. // contextMenu.KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  332. // contextMenu.Initialized += Menu_Initialized;
  333. // void Application_MouseEvent (object sender, MouseEventArgs e)
  334. // {
  335. // // If user clicks outside of the menuWindow, close it
  336. // if (!contextMenu.Frame.Contains (e.Position.X, e.Position.Y))
  337. // {
  338. // if (e.Flags is (MouseFlags.Button1Clicked or MouseFlags.Button3Clicked))
  339. // {
  340. // contextMenu.RequestStop ();
  341. // }
  342. // }
  343. // }
  344. // Application.MouseEvent += Application_MouseEvent;
  345. // Application.Run (contextMenu);
  346. // contextMenu.Dispose ();
  347. // Application.MouseEvent -= Application_MouseEvent;
  348. //}
  349. private void ConfigMenuBar (Bar bar)
  350. {
  351. var fileMenuBarItem = new Shortcut
  352. {
  353. Title = "_File",
  354. HelpText = "File Menu",
  355. Key = Key.D0.WithAlt,
  356. HighlightStyle = HighlightStyle.Hover
  357. };
  358. var editMenuBarItem = new Shortcut
  359. {
  360. Title = "_Edit",
  361. HelpText = "Edit Menu",
  362. Key = Key.D1.WithAlt,
  363. HighlightStyle = HighlightStyle.Hover
  364. };
  365. var helpMenuBarItem = new Shortcut
  366. {
  367. Title = "_Help",
  368. HelpText = "Halp Menu",
  369. Key = Key.D2.WithAlt,
  370. HighlightStyle = HighlightStyle.Hover
  371. };
  372. bar.Add (fileMenuBarItem, editMenuBarItem, helpMenuBarItem);
  373. }
  374. private void ConfigureMenu (Bar bar)
  375. {
  376. var shortcut1 = new Shortcut
  377. {
  378. Title = "Z_igzag",
  379. Key = Key.I.WithCtrl,
  380. Text = "Gonna zig zag",
  381. HighlightStyle = HighlightStyle.Hover
  382. };
  383. var shortcut2 = new Shortcut
  384. {
  385. Title = "Za_G",
  386. Text = "Gonna zag",
  387. Key = Key.G.WithAlt,
  388. HighlightStyle = HighlightStyle.Hover
  389. };
  390. var shortcut3 = new Shortcut
  391. {
  392. Title = "_Three",
  393. Text = "The 3rd item",
  394. Key = Key.D3.WithAlt,
  395. HighlightStyle = HighlightStyle.Hover
  396. };
  397. var line = new Line ()
  398. {
  399. X = -1,
  400. Width = Dim.Fill ()! + 1
  401. };
  402. var shortcut4 = new Shortcut
  403. {
  404. Title = "_Four",
  405. Text = "Below the line",
  406. Key = Key.D3.WithAlt,
  407. HighlightStyle = HighlightStyle.Hover
  408. };
  409. shortcut4.CommandView = new CheckBox ()
  410. {
  411. Title = shortcut4.Title,
  412. HighlightStyle = HighlightStyle.None,
  413. CanFocus = false
  414. };
  415. // This ensures the checkbox state toggles when the hotkey of Title is pressed.
  416. shortcut4.Accepting += (sender, args) => args.Cancel = true;
  417. bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4);
  418. }
  419. public void ConfigStatusBar (Bar bar)
  420. {
  421. var shortcut = new Shortcut
  422. {
  423. Text = "Quit",
  424. Title = "Q_uit",
  425. Key = Key.Z.WithCtrl,
  426. };
  427. bar.Add (shortcut);
  428. shortcut = new Shortcut
  429. {
  430. Text = "Help Text",
  431. Title = "Help",
  432. Key = Key.F1,
  433. };
  434. bar.Add (shortcut);
  435. shortcut = new Shortcut
  436. {
  437. Title = "_Show/Hide",
  438. Key = Key.F10,
  439. CommandView = new CheckBox
  440. {
  441. CanFocus = false,
  442. Text = "_Show/Hide"
  443. },
  444. };
  445. bar.Add (shortcut);
  446. var button1 = new Button
  447. {
  448. Text = "I'll Hide",
  449. // Visible = false
  450. };
  451. button1.Accepting += Button_Clicked;
  452. bar.Add (button1);
  453. shortcut.Accepting += (s, e) =>
  454. {
  455. button1.Visible = !button1.Visible;
  456. button1.Enabled = button1.Visible;
  457. e.Cancel = false;
  458. };
  459. bar.Add (new Label
  460. {
  461. HotKeySpecifier = new Rune ('_'),
  462. Text = "Fo_cusLabel",
  463. CanFocus = true
  464. });
  465. var button2 = new Button
  466. {
  467. Text = "Or me!",
  468. };
  469. button2.Accepting += (s, e) => Application.RequestStop ();
  470. bar.Add (button2);
  471. return;
  472. void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  473. }
  474. }