Bars.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. menuLikeExamples.Add (bar);
  120. label = new Label ()
  121. {
  122. Title = "PopOver Menu (Right click to show):",
  123. X = Pos.Right (bar) + 1,
  124. Y = Pos.Top (label),
  125. };
  126. menuLikeExamples.Add (label);
  127. Menuv2 popOverMenu = new Menuv2
  128. {
  129. Id = "popupMenu",
  130. X = Pos.Left (label),
  131. Y = Pos.Bottom (label),
  132. };
  133. ConfigureMenu (popOverMenu);
  134. popOverMenu.Arrangement = ViewArrangement.Overlapped;
  135. popOverMenu.Visible = false;
  136. //popOverMenu.Enabled = false;
  137. var toggleShortcut = new Shortcut
  138. {
  139. Title = "Toggle Hide",
  140. Text = "App",
  141. KeyBindingScope = KeyBindingScope.Application,
  142. Key = Key.F4.WithCtrl,
  143. };
  144. popOverMenu.Add (toggleShortcut);
  145. popOverMenu.Accepting += PopOverMenuOnAccept;
  146. void PopOverMenuOnAccept (object o, CommandEventArgs args)
  147. {
  148. if (popOverMenu.Visible)
  149. {
  150. popOverMenu.Visible = false;
  151. }
  152. else
  153. {
  154. popOverMenu.Visible = true;
  155. popOverMenu.SetFocus ();
  156. }
  157. }
  158. menuLikeExamples.Add (popOverMenu);
  159. menuLikeExamples.MouseClick += MenuLikeExamplesMouseClick;
  160. void MenuLikeExamplesMouseClick (object sender, MouseEventEventArgs e)
  161. {
  162. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
  163. {
  164. popOverMenu.X = e.MouseEvent.Position.X;
  165. popOverMenu.Y = e.MouseEvent.Position.Y;
  166. popOverMenu.Visible = true;
  167. //popOverMenu.Enabled = popOverMenu.Visible;
  168. popOverMenu.SetFocus ();
  169. }
  170. else
  171. {
  172. popOverMenu.Visible = false;
  173. //popOverMenu.Enabled = popOverMenu.Visible;
  174. }
  175. }
  176. FrameView statusBarLikeExamples = new ()
  177. {
  178. Title = "StatusBar-Like Examples",
  179. X = 0,
  180. Y = Pos.AnchorEnd (),
  181. Width = Dim.Width (menuLikeExamples),
  182. Height = Dim.Percent (33),
  183. };
  184. Application.Top.Add (statusBarLikeExamples);
  185. label = new Label ()
  186. {
  187. Title = " Bar:",
  188. X = 0,
  189. Y = 0,
  190. };
  191. statusBarLikeExamples.Add (label);
  192. bar = new Bar
  193. {
  194. Id = "statusBar-like",
  195. X = Pos.Right (label),
  196. Y = Pos.Top (label),
  197. Width = Dim.Fill (),
  198. Orientation = Orientation.Horizontal,
  199. };
  200. ConfigStatusBar (bar);
  201. statusBarLikeExamples.Add (bar);
  202. label = new Label ()
  203. {
  204. Title = "StatusBar:",
  205. X = 0,
  206. Y = Pos.Bottom (bar) + 1,
  207. };
  208. statusBarLikeExamples.Add (label);
  209. bar = new StatusBar ()
  210. {
  211. Id = "statusBar",
  212. X = Pos.Right (label),
  213. Y = Pos.Top (label),
  214. Width = Dim.Fill (),
  215. };
  216. ConfigStatusBar (bar);
  217. statusBarLikeExamples.Add (bar);
  218. foreach (FrameView frameView in Application.Top.Subviews.Where (f => f is FrameView)!)
  219. {
  220. foreach (Bar barView in frameView.Subviews.Where (b => b is Bar)!)
  221. {
  222. foreach (Shortcut sh in barView.Subviews.Where (s => s is Shortcut)!)
  223. {
  224. sh.Accepting += (o, args) =>
  225. {
  226. eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}");
  227. eventLog.MoveDown ();
  228. //args.Handled = true;
  229. };
  230. }
  231. }
  232. }
  233. }
  234. //private void SetupContentMenu ()
  235. //{
  236. // Application.Top.Add (new Label { Text = "Right Click for Context Menu", X = Pos.Center (), Y = 4 });
  237. // Application.Top.MouseClick += ShowContextMenu;
  238. //}
  239. //private void ShowContextMenu (object s, MouseEventEventArgs e)
  240. //{
  241. // if (e.MouseEvent.Flags != MouseFlags.Button3Clicked)
  242. // {
  243. // return;
  244. // }
  245. // var contextMenu = new Bar
  246. // {
  247. // Id = "contextMenu",
  248. // X = e.MouseEvent.Position.X,
  249. // Y = e.MouseEvent.Position.Y,
  250. // Width = Dim.Auto (DimAutoStyle.Content),
  251. // Height = Dim.Auto (DimAutoStyle.Content),
  252. // Orientation = Orientation.Vertical,
  253. // StatusBarStyle = false,
  254. // BorderStyle = LineStyle.Rounded,
  255. // Modal = true,
  256. // };
  257. // var newMenu = new Shortcut
  258. // {
  259. // Title = "_New...",
  260. // Text = "Create a new file",
  261. // Key = Key.N.WithCtrl,
  262. // CanFocus = true
  263. // };
  264. // newMenu.Accept += (s, e) =>
  265. // {
  266. // contextMenu.RequestStop ();
  267. // Application.AddTimeout (
  268. // new TimeSpan (0),
  269. // () =>
  270. // {
  271. // MessageBox.Query ("File", "New");
  272. // return false;
  273. // });
  274. // };
  275. // var open = new Shortcut
  276. // {
  277. // Title = "_Open...",
  278. // Text = "Show the File Open Dialog",
  279. // Key = Key.O.WithCtrl,
  280. // CanFocus = true
  281. // };
  282. // open.Accept += (s, e) =>
  283. // {
  284. // contextMenu.RequestStop ();
  285. // Application.AddTimeout (
  286. // new TimeSpan (0),
  287. // () =>
  288. // {
  289. // MessageBox.Query ("File", "Open");
  290. // return false;
  291. // });
  292. // };
  293. // var save = new Shortcut
  294. // {
  295. // Title = "_Save...",
  296. // Text = "Save",
  297. // Key = Key.S.WithCtrl,
  298. // CanFocus = true
  299. // };
  300. // save.Accept += (s, e) =>
  301. // {
  302. // contextMenu.RequestStop ();
  303. // Application.AddTimeout (
  304. // new TimeSpan (0),
  305. // () =>
  306. // {
  307. // MessageBox.Query ("File", "Save");
  308. // return false;
  309. // });
  310. // };
  311. // var saveAs = new Shortcut
  312. // {
  313. // Title = "Save _As...",
  314. // Text = "Save As",
  315. // Key = Key.A.WithCtrl,
  316. // CanFocus = true
  317. // };
  318. // saveAs.Accept += (s, e) =>
  319. // {
  320. // contextMenu.RequestStop ();
  321. // Application.AddTimeout (
  322. // new TimeSpan (0),
  323. // () =>
  324. // {
  325. // MessageBox.Query ("File", "Save As");
  326. // return false;
  327. // });
  328. // };
  329. // contextMenu.Add (newMenu, open, save, saveAs);
  330. // contextMenu.KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  331. // contextMenu.Initialized += Menu_Initialized;
  332. // void Application_MouseEvent (object sender, MouseEvent e)
  333. // {
  334. // // If user clicks outside of the menuWindow, close it
  335. // if (!contextMenu.Frame.Contains (e.Position.X, e.Position.Y))
  336. // {
  337. // if (e.Flags is (MouseFlags.Button1Clicked or MouseFlags.Button3Clicked))
  338. // {
  339. // contextMenu.RequestStop ();
  340. // }
  341. // }
  342. // }
  343. // Application.MouseEvent += Application_MouseEvent;
  344. // Application.Run (contextMenu);
  345. // contextMenu.Dispose ();
  346. // Application.MouseEvent -= Application_MouseEvent;
  347. //}
  348. private void ConfigMenuBar (Bar bar)
  349. {
  350. var fileMenuBarItem = new Shortcut
  351. {
  352. Title = "_File",
  353. HelpText = "File Menu",
  354. Key = Key.D0.WithAlt,
  355. HighlightStyle = HighlightStyle.Hover
  356. };
  357. var editMenuBarItem = new Shortcut
  358. {
  359. Title = "_Edit",
  360. HelpText = "Edit Menu",
  361. Key = Key.D1.WithAlt,
  362. HighlightStyle = HighlightStyle.Hover
  363. };
  364. var helpMenuBarItem = new Shortcut
  365. {
  366. Title = "_Help",
  367. HelpText = "Halp Menu",
  368. Key = Key.D2.WithAlt,
  369. HighlightStyle = HighlightStyle.Hover
  370. };
  371. bar.Add (fileMenuBarItem, editMenuBarItem, helpMenuBarItem);
  372. }
  373. private void ConfigureMenu (Bar bar)
  374. {
  375. var shortcut1 = new Shortcut
  376. {
  377. Title = "Z_igzag",
  378. Key = Key.I.WithCtrl,
  379. Text = "Gonna zig zag",
  380. HighlightStyle = HighlightStyle.Hover
  381. };
  382. var shortcut2 = new Shortcut
  383. {
  384. Title = "Za_G",
  385. Text = "Gonna zag",
  386. Key = Key.G.WithAlt,
  387. HighlightStyle = HighlightStyle.Hover
  388. };
  389. var shortcut3 = new Shortcut
  390. {
  391. Title = "_Three",
  392. Text = "The 3rd item",
  393. Key = Key.D3.WithAlt,
  394. HighlightStyle = HighlightStyle.Hover
  395. };
  396. var line = new Line ()
  397. {
  398. BorderStyle = LineStyle.Dotted,
  399. Orientation = Orientation.Horizontal,
  400. CanFocus = false,
  401. };
  402. // HACK: Bug in Line
  403. line.Orientation = Orientation.Vertical;
  404. line.Orientation = Orientation.Horizontal;
  405. var shortcut4 = new Shortcut
  406. {
  407. Title = "_Four",
  408. Text = "Below the line",
  409. Key = Key.D3.WithAlt,
  410. HighlightStyle = HighlightStyle.Hover
  411. };
  412. bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4);
  413. }
  414. public void ConfigStatusBar (Bar bar)
  415. {
  416. var shortcut = new Shortcut
  417. {
  418. Text = "Quit",
  419. Title = "Q_uit",
  420. Key = Key.Z.WithCtrl,
  421. };
  422. bar.Add (shortcut);
  423. shortcut = new Shortcut
  424. {
  425. Text = "Help Text",
  426. Title = "Help",
  427. Key = Key.F1,
  428. };
  429. bar.Add (shortcut);
  430. shortcut = new Shortcut
  431. {
  432. Title = "_Show/Hide",
  433. Key = Key.F10,
  434. CommandView = new CheckBox
  435. {
  436. CanFocus = false,
  437. Text = "_Show/Hide"
  438. },
  439. };
  440. bar.Add (shortcut);
  441. var button1 = new Button
  442. {
  443. Text = "I'll Hide",
  444. // Visible = false
  445. };
  446. button1.Accepting += Button_Clicked;
  447. bar.Add (button1);
  448. shortcut.Accepting += (s, e) =>
  449. {
  450. button1.Visible = !button1.Visible;
  451. button1.Enabled = button1.Visible;
  452. e.Cancel = false;
  453. };
  454. bar.Add (new Label
  455. {
  456. HotKeySpecifier = new Rune ('_'),
  457. Text = "Fo_cusLabel",
  458. CanFocus = true
  459. });
  460. var button2 = new Button
  461. {
  462. Text = "Or me!",
  463. };
  464. button2.Accepting += (s, e) => Application.RequestStop ();
  465. bar.Add (button2);
  466. return;
  467. void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  468. }
  469. }