Bars.cs 13 KB

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