Bars.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. 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. Runnable app = new ();
  16. app.IsModalChanged += 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.TopRunnableView!.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. SchemeName = "Runnable",
  34. Source = new ListWrapper<string> (eventSource)
  35. };
  36. eventLog.Border!.Thickness = new (0, 1, 0, 0);
  37. Application.TopRunnableView.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.TopRunnableView.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 MenuBar
  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.TopRunnableView.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 Menu
  112. {
  113. Id = "menu",
  114. X = Pos.Left (label),
  115. Y = Pos.Bottom (label),
  116. };
  117. ConfigureMenu (bar);
  118. bar.Arrangement = ViewArrangement.RightResizable;
  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. Menu popOverMenu = new Menu
  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. BindKeyToApplication = true,
  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.MouseEvent += MenuLikeExamplesMouseEvent;
  160. void MenuLikeExamplesMouseEvent (object _, MouseEventArgs e)
  161. {
  162. if (e.Flags.HasFlag (MouseFlags.Button3Clicked))
  163. {
  164. popOverMenu.X = e.Position.X;
  165. popOverMenu.Y = e.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.TopRunnableView.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.TopRunnableView.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.TopRunnable.Add (new Label { Text = "Right Click for Context Menu", X = Pos.Center (), Y = 4 });
  237. // Application.TopRunnable.MouseClick += ShowContextMenu;
  238. //}
  239. //private void ShowContextMenu (object s, MouseEventEventArgs e)
  240. //{
  241. // if (e.Flags != MouseFlags.Button3Clicked)
  242. // {
  243. // return;
  244. // }
  245. // var contextMenu = new Bar
  246. // {
  247. // Id = "contextMenu",
  248. // X = e.Position.X,
  249. // Y = e.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 (App, "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 (App, "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 (App, "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 (App, "File", "Save As");
  326. // return false;
  327. // });
  328. // };
  329. // contextMenu.Add (newMenu, open, save, saveAs);
  330. // contextMenu.KeyBindings.Add (Key.Esc, Command.Quit);
  331. // contextMenu.Initialized += Menu_Initialized;
  332. // void Application_MouseEvent (object sender, MouseEventArgs 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. HighlightStates = MouseState.In
  356. };
  357. var editMenuBarItem = new Shortcut
  358. {
  359. Title = "_Edit",
  360. HelpText = "Edit Menu",
  361. Key = Key.D1.WithAlt,
  362. HighlightStates = MouseState.In
  363. };
  364. var helpMenuBarItem = new Shortcut
  365. {
  366. Title = "_Help",
  367. HelpText = "Halp Menu",
  368. Key = Key.D2.WithAlt,
  369. HighlightStates = MouseState.In
  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. HighlightStates = MouseState.In
  381. };
  382. var shortcut2 = new Shortcut
  383. {
  384. Title = "Za_G",
  385. Text = "Gonna zag",
  386. Key = Key.G.WithAlt,
  387. HighlightStates = MouseState.In
  388. };
  389. var shortcut3 = new Shortcut
  390. {
  391. Title = "_Three",
  392. Text = "The 3rd item",
  393. Key = Key.D3.WithAlt,
  394. HighlightStates = MouseState.In
  395. };
  396. var line = new Line ()
  397. {
  398. X = -1,
  399. Width = Dim.Fill ()! + 1
  400. };
  401. var shortcut4 = new Shortcut
  402. {
  403. Title = "_Four",
  404. Text = "Below the line",
  405. Key = Key.D3.WithAlt,
  406. HighlightStates = MouseState.In
  407. };
  408. shortcut4.CommandView = new CheckBox ()
  409. {
  410. Title = shortcut4.Title,
  411. HighlightStates = MouseState.None,
  412. CanFocus = false
  413. };
  414. // This ensures the checkbox state toggles when the hotkey of Title is pressed.
  415. shortcut4.Accepting += (sender, args) => args.Handled = true;
  416. bar.Add (shortcut1, shortcut2, shortcut3, line, shortcut4);
  417. }
  418. public void ConfigStatusBar (Bar bar)
  419. {
  420. var shortcut = new Shortcut
  421. {
  422. Text = "Quit",
  423. Title = "Q_uit",
  424. Key = Key.Z.WithCtrl,
  425. };
  426. bar.Add (shortcut);
  427. shortcut = new Shortcut
  428. {
  429. Text = "Help Text",
  430. Title = "Help",
  431. Key = Key.F1,
  432. };
  433. bar.Add (shortcut);
  434. shortcut = new Shortcut
  435. {
  436. Title = "_Show/Hide",
  437. Key = Key.F10,
  438. CommandView = new CheckBox
  439. {
  440. CanFocus = false,
  441. Text = "_Show/Hide"
  442. },
  443. };
  444. bar.Add (shortcut);
  445. var button1 = new Button
  446. {
  447. Text = "I'll Hide",
  448. // Visible = false
  449. };
  450. button1.Accepting += Button_Clicked;
  451. bar.Add (button1);
  452. shortcut.Accepting += (s, e) =>
  453. {
  454. button1.Visible = !button1.Visible;
  455. button1.Enabled = button1.Visible;
  456. e.Handled = true;
  457. };
  458. bar.Add (new Label
  459. {
  460. HotKeySpecifier = new Rune ('_'),
  461. Text = "Fo_cusLabel",
  462. CanFocus = true
  463. });
  464. var button2 = new Button
  465. {
  466. Text = "Or me!",
  467. };
  468. button2.Accepting += (s, e) => Application.RequestStop ();
  469. bar.Add (button2);
  470. return;
  471. void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ((sender as View)?.App, "Hi", $"You clicked {sender}"); }
  472. }
  473. }