Bars.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}";
  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. bar.Border.Thickness = new (1);
  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. FrameView statusBarLikeExamples = new ()
  121. {
  122. Title = "StatusBar-Like Examples",
  123. X = 0,
  124. Y = Pos.AnchorEnd (),
  125. Width = Dim.Width (menuLikeExamples),
  126. Height = Dim.Percent (33),
  127. };
  128. Application.Top.Add (statusBarLikeExamples);
  129. label = new Label ()
  130. {
  131. Title = " Bar:",
  132. X = 0,
  133. Y = 0,
  134. };
  135. statusBarLikeExamples.Add (label);
  136. bar = new Bar
  137. {
  138. Id = "statusBar-like",
  139. X = Pos.Right (label),
  140. Y = Pos.Top (label),
  141. Width = Dim.Fill (),
  142. Orientation = Orientation.Horizontal,
  143. };
  144. ConfigStatusBar (bar);
  145. statusBarLikeExamples.Add (bar);
  146. label = new Label ()
  147. {
  148. Title = "StatusBar:",
  149. X = 0,
  150. Y = Pos.Bottom (bar) + 1,
  151. };
  152. statusBarLikeExamples.Add (label);
  153. bar = new StatusBar ()
  154. {
  155. Id = "statusBar",
  156. X = Pos.Right (label),
  157. Y = Pos.Top (label),
  158. Width = Dim.Fill (),
  159. };
  160. ConfigStatusBar (bar);
  161. statusBarLikeExamples.Add (bar);
  162. foreach (FrameView frameView in Application.Top.Subviews.Where (f => f is FrameView)!)
  163. {
  164. foreach (Bar barView in frameView.Subviews.Where (b => b is Bar)!)
  165. {
  166. foreach (Shortcut sh in barView.Subviews.Where (s => s is Shortcut)!)
  167. {
  168. sh.Accept += (o, args) =>
  169. {
  170. eventSource.Add ($"Accept: {sh!.SuperView.Id} {sh!.CommandView.Text}");
  171. eventLog.MoveDown ();
  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 Menu_Initialized (object sender, EventArgs e)
  292. {
  293. // BUGBUG: this should not be needed
  294. ((View)(sender)).LayoutSubviews ();
  295. }
  296. private void ConfigMenuBar (Bar bar)
  297. {
  298. var fileMenuBarItem = new Shortcut
  299. {
  300. Title = "_File",
  301. HelpText = "File Menu",
  302. Key = Key.D0.WithAlt,
  303. };
  304. var editMenuBarItem = new Shortcut
  305. {
  306. Title = "_Edit",
  307. HelpText = "Edit Menu",
  308. Key = Key.D1.WithAlt
  309. };
  310. var helpMenuBarItem = new Shortcut
  311. {
  312. Title = "_Help",
  313. HelpText = "Halp Menu",
  314. Key = Key.D2.WithAlt
  315. };
  316. bar.Add (fileMenuBarItem, editMenuBarItem, helpMenuBarItem);
  317. }
  318. private void ConfigureMenu (Bar bar)
  319. {
  320. var shortcut1 = new Shortcut
  321. {
  322. Title = "Z_igzag",
  323. Key = Key.I.WithCtrl,
  324. Text = "Gonna zig zag",
  325. };
  326. var shortcut2 = new Shortcut
  327. {
  328. Title = "Za_G",
  329. Text = "Gonna zag",
  330. Key = Key.G.WithAlt,
  331. };
  332. var shortcut3 = new Shortcut
  333. {
  334. Title = "_Three",
  335. Text = "The 3rd item",
  336. Key = Key.D3.WithAlt,
  337. };
  338. bar.Add (shortcut1, shortcut2, shortcut3);
  339. }
  340. private void ConfigStatusBar (Bar bar)
  341. {
  342. var shortcut = new Shortcut
  343. {
  344. Text = "Quit",
  345. Title = "Q_uit",
  346. Key = Key.Z.WithCtrl,
  347. };
  348. bar.Add (shortcut);
  349. shortcut = new Shortcut
  350. {
  351. Text = "Help Text",
  352. Title = "Help",
  353. Key = Key.F1,
  354. };
  355. bar.Add (shortcut);
  356. shortcut = new Shortcut
  357. {
  358. Title = "_Show/Hide",
  359. Key = Key.F10,
  360. CommandView = new CheckBox
  361. {
  362. CanFocus = false,
  363. Text = "_Show/Hide"
  364. },
  365. };
  366. bar.Add (shortcut);
  367. var button1 = new Button
  368. {
  369. Text = "I'll Hide",
  370. // Visible = false
  371. };
  372. button1.Accept += Button_Clicked;
  373. bar.Add (button1);
  374. shortcut.Accept += (s, e) =>
  375. {
  376. button1.Visible = !button1.Visible;
  377. button1.Enabled = button1.Visible;
  378. e.Cancel = false;
  379. };
  380. bar.Add (new Label
  381. {
  382. HotKeySpecifier = new Rune ('_'),
  383. Text = "Fo_cusLabel",
  384. CanFocus = true
  385. });
  386. var button2 = new Button
  387. {
  388. Text = "Or me!",
  389. };
  390. button2.Accept += (s, e) => Application.RequestStop ();
  391. bar.Add (button2);
  392. return;
  393. void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  394. }
  395. }