DynamicMenuBar.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using Terminal.Gui;
  10. namespace UICatalog.Scenarios {
  11. [ScenarioMetadata (Name: "Dynamic MenuBar", Description: "Demonstrates how to change a MenuBar dynamically.")]
  12. [ScenarioCategory ("Top Level Windows")]
  13. [ScenarioCategory ("Menus")]
  14. public class DynamicMenuBar : Scenario {
  15. public override void Init (ColorScheme colorScheme)
  16. {
  17. Application.Init ();
  18. Application.Top.Add (new DynamicMenuBarSample ($"CTRL-Q to Close - Scenario: {GetName ()}"));
  19. }
  20. public class DynamicMenuItemList {
  21. public ustring Title { get; set; }
  22. public MenuItem MenuItem { get; set; }
  23. public DynamicMenuItemList () { }
  24. public DynamicMenuItemList (ustring title, MenuItem menuItem)
  25. {
  26. Title = title;
  27. MenuItem = menuItem;
  28. }
  29. public override string ToString () => $"{Title}, {MenuItem}";
  30. }
  31. public class DynamicMenuItem {
  32. public ustring title = "_New";
  33. public ustring help = "";
  34. public ustring action = "";
  35. public bool isTopLevel;
  36. public bool hasSubMenu;
  37. public MenuItemCheckStyle checkStyle;
  38. public ustring shortcut;
  39. public DynamicMenuItem () { }
  40. public DynamicMenuItem (ustring title, bool hasSubMenu = false)
  41. {
  42. this.title = title;
  43. this.hasSubMenu = hasSubMenu;
  44. }
  45. public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortcut = null)
  46. {
  47. this.title = title;
  48. this.help = help;
  49. this.action = action;
  50. this.isTopLevel = isTopLevel;
  51. this.hasSubMenu = hasSubMenu;
  52. this.checkStyle = checkStyle;
  53. this.shortcut = shortcut;
  54. }
  55. }
  56. public class DynamicMenuBarSample : Window {
  57. MenuBar _menuBar;
  58. MenuItem _currentMenuBarItem;
  59. int _currentSelectedMenuBar;
  60. MenuItem _currentEditMenuBarItem;
  61. ListView _lstMenus;
  62. public DynamicMenuItemModel DataContext { get; set; }
  63. public DynamicMenuBarSample (ustring title) : base (title)
  64. {
  65. DataContext = new DynamicMenuItemModel ();
  66. var _frmDelimiter = new FrameView ("Shortcut Delimiter:") {
  67. X = Pos.Center (),
  68. Y = 3,
  69. Width = 25,
  70. Height = 4
  71. };
  72. var _txtDelimiter = new TextField (MenuBar.ShortcutDelimiter.ToString ()) {
  73. X = Pos.Center (),
  74. Width = 2,
  75. };
  76. _txtDelimiter.TextChanged += (_) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text;
  77. _frmDelimiter.Add (_txtDelimiter);
  78. Add (_frmDelimiter);
  79. var _frmMenu = new FrameView ("Menus:") {
  80. Y = 7,
  81. Width = Dim.Percent (50),
  82. Height = Dim.Fill ()
  83. };
  84. var _btnAddMenuBar = new Button ("Add a MenuBar") {
  85. Y = 1,
  86. };
  87. _frmMenu.Add (_btnAddMenuBar);
  88. var _btnMenuBarUp = new Button ("^") {
  89. X = Pos.Center ()
  90. };
  91. _frmMenu.Add (_btnMenuBarUp);
  92. var _btnMenuBarDown = new Button ("v") {
  93. X = Pos.Center (),
  94. Y = Pos.Bottom (_btnMenuBarUp)
  95. };
  96. _frmMenu.Add (_btnMenuBarDown);
  97. var _btnRemoveMenuBar = new Button ("Remove a MenuBar") {
  98. Y = 1
  99. };
  100. _btnRemoveMenuBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveMenuBar) - Pos.Left (_btnRemoveMenuBar));
  101. _frmMenu.Add (_btnRemoveMenuBar);
  102. var _btnPrevious = new Button ("<") {
  103. X = Pos.Left (_btnAddMenuBar),
  104. Y = Pos.Top (_btnAddMenuBar) + 2
  105. };
  106. _frmMenu.Add (_btnPrevious);
  107. var _btnAdd = new Button (" Add ") {
  108. Y = Pos.Top (_btnPrevious) + 2,
  109. };
  110. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  111. _frmMenu.Add (_btnAdd);
  112. var _btnNext = new Button (">") {
  113. X = Pos.X (_btnAdd),
  114. Y = Pos.Top (_btnPrevious),
  115. };
  116. _frmMenu.Add (_btnNext);
  117. var _lblMenuBar = new Label () {
  118. ColorScheme = Colors.Dialog,
  119. TextAlignment = TextAlignment.Centered,
  120. X = Pos.Right (_btnPrevious) + 1,
  121. Y = Pos.Top (_btnPrevious),
  122. Width = Dim.Fill () - Dim.Function (() => _btnAdd.Frame.Width + 1),
  123. Height = 1
  124. };
  125. _frmMenu.Add (_lblMenuBar);
  126. _lblMenuBar.WantMousePositionReports = true;
  127. _lblMenuBar.CanFocus = true;
  128. var _lblParent = new Label () {
  129. TextAlignment = TextAlignment.Centered,
  130. X = Pos.Right (_btnPrevious) + 1,
  131. Y = Pos.Top (_btnPrevious) + 1,
  132. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1
  133. };
  134. _frmMenu.Add (_lblParent);
  135. var _btnPreviowsParent = new Button ("..") {
  136. X = Pos.Left (_btnAddMenuBar),
  137. Y = Pos.Top (_btnPrevious) + 1
  138. };
  139. _frmMenu.Add (_btnPreviowsParent);
  140. _lstMenus = new ListView (new List<DynamicMenuItemList> ()) {
  141. ColorScheme = Colors.Dialog,
  142. X = Pos.Right (_btnPrevious) + 1,
  143. Y = Pos.Top (_btnPrevious) + 2,
  144. Width = _lblMenuBar.Width,
  145. Height = Dim.Fill (),
  146. };
  147. _frmMenu.Add (_lstMenus);
  148. _lblMenuBar.TabIndex = _btnPrevious.TabIndex + 1;
  149. _lstMenus.TabIndex = _lblMenuBar.TabIndex + 1;
  150. _btnNext.TabIndex = _lstMenus.TabIndex + 1;
  151. _btnAdd.TabIndex = _btnNext.TabIndex + 1;
  152. var _btnRemove = new Button ("Remove") {
  153. X = Pos.Left (_btnAdd),
  154. Y = Pos.Top (_btnAdd) + 1
  155. };
  156. _frmMenu.Add (_btnRemove);
  157. var _btnUp = new Button ("^") {
  158. X = Pos.Right (_lstMenus) + 2,
  159. Y = Pos.Top (_btnRemove) + 2
  160. };
  161. _frmMenu.Add (_btnUp);
  162. var _btnDown = new Button ("v") {
  163. X = Pos.Right (_lstMenus) + 2,
  164. Y = Pos.Top (_btnUp) + 1
  165. };
  166. _frmMenu.Add (_btnDown);
  167. Add (_frmMenu);
  168. var _frmMenuDetails = new DynamicMenuBarDetails ("Menu Details:") {
  169. X = Pos.Right (_frmMenu),
  170. Y = Pos.Top (_frmMenu),
  171. Width = Dim.Fill (),
  172. Height = Dim.Fill (2)
  173. };
  174. Add (_frmMenuDetails);
  175. _btnMenuBarUp.Clicked += () => {
  176. var i = _currentSelectedMenuBar;
  177. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  178. if (menuItem != null) {
  179. var menus = _menuBar.Menus;
  180. if (i > 0) {
  181. menus [i] = menus [i - 1];
  182. menus [i - 1] = menuItem;
  183. _currentSelectedMenuBar = i - 1;
  184. _menuBar.SetNeedsDisplay ();
  185. }
  186. }
  187. };
  188. _btnMenuBarDown.Clicked += () => {
  189. var i = _currentSelectedMenuBar;
  190. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  191. if (menuItem != null) {
  192. var menus = _menuBar.Menus;
  193. if (i < menus.Length - 1) {
  194. menus [i] = menus [i + 1];
  195. menus [i + 1] = menuItem;
  196. _currentSelectedMenuBar = i + 1;
  197. _menuBar.SetNeedsDisplay ();
  198. }
  199. }
  200. };
  201. _btnUp.Clicked += () => {
  202. var i = _lstMenus.SelectedItem;
  203. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  204. if (menuItem != null) {
  205. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  206. if (i > 0) {
  207. childrens [i] = childrens [i - 1];
  208. childrens [i - 1] = menuItem;
  209. DataContext.Menus [i] = DataContext.Menus [i - 1];
  210. DataContext.Menus [i - 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  211. _lstMenus.SelectedItem = i - 1;
  212. }
  213. }
  214. };
  215. _btnDown.Clicked += () => {
  216. var i = _lstMenus.SelectedItem;
  217. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  218. if (menuItem != null) {
  219. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  220. if (i < childrens.Length - 1) {
  221. childrens [i] = childrens [i + 1];
  222. childrens [i + 1] = menuItem;
  223. DataContext.Menus [i] = DataContext.Menus [i + 1];
  224. DataContext.Menus [i + 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  225. _lstMenus.SelectedItem = i + 1;
  226. }
  227. }
  228. };
  229. _btnPreviowsParent.Clicked += () => {
  230. if (_currentMenuBarItem != null && _currentMenuBarItem.Parent != null) {
  231. var mi = _currentMenuBarItem;
  232. _currentMenuBarItem = _currentMenuBarItem.Parent as MenuBarItem;
  233. SetListViewSource (_currentMenuBarItem, true);
  234. var i = ((MenuBarItem)_currentMenuBarItem).GetChildrenIndex (mi);
  235. if (i > -1) {
  236. _lstMenus.SelectedItem = i;
  237. }
  238. if (_currentMenuBarItem.Parent != null) {
  239. DataContext.Parent = _currentMenuBarItem.Title;
  240. } else {
  241. DataContext.Parent = ustring.Empty;
  242. }
  243. } else {
  244. DataContext.Parent = ustring.Empty;
  245. }
  246. };
  247. var _btnOk = new Button ("Ok") {
  248. X = Pos.Right (_frmMenu) + 20,
  249. Y = Pos.Bottom (_frmMenuDetails),
  250. };
  251. Add (_btnOk);
  252. var _btnCancel = new Button ("Cancel") {
  253. X = Pos.Right (_btnOk) + 3,
  254. Y = Pos.Top (_btnOk),
  255. };
  256. _btnCancel.Clicked += () => {
  257. SetFrameDetails (_currentEditMenuBarItem);
  258. };
  259. Add (_btnCancel);
  260. _lstMenus.SelectedItemChanged += (e) => {
  261. SetFrameDetails ();
  262. };
  263. _btnOk.Clicked += () => {
  264. if (ustring.IsNullOrEmpty (_frmMenuDetails._txtTitle.Text) && _currentEditMenuBarItem != null) {
  265. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  266. } else if (_currentEditMenuBarItem != null) {
  267. var menuItem = new DynamicMenuItem (_frmMenuDetails._txtTitle.Text, _frmMenuDetails._txtHelp.Text,
  268. _frmMenuDetails._txtAction.Text,
  269. _frmMenuDetails._ckbIsTopLevel != null ? _frmMenuDetails._ckbIsTopLevel.Checked : false,
  270. _frmMenuDetails._ckbSubMenu != null ? _frmMenuDetails._ckbSubMenu.Checked : false,
  271. _frmMenuDetails._rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  272. _frmMenuDetails._rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked :
  273. MenuItemCheckStyle.Radio,
  274. _frmMenuDetails._txtShortcut.Text);
  275. UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
  276. }
  277. };
  278. _btnAdd.Clicked += () => {
  279. if (MenuBar == null) {
  280. MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
  281. _btnAddMenuBar.SetFocus ();
  282. return;
  283. }
  284. var frameDetails = new DynamicMenuBarDetails (null, _currentMenuBarItem != null);
  285. var item = frameDetails.EnterMenuItem ();
  286. if (item == null) {
  287. return;
  288. }
  289. if (!(_currentMenuBarItem is MenuBarItem)) {
  290. var parent = _currentMenuBarItem.Parent as MenuBarItem;
  291. var idx = parent.GetChildrenIndex (_currentMenuBarItem);
  292. _currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { }, _currentMenuBarItem.Parent);
  293. _currentMenuBarItem.CheckType = item.checkStyle;
  294. parent.Children [idx] = _currentMenuBarItem;
  295. } else {
  296. MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
  297. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  298. if (menuBarItem == null) {
  299. menuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { newMenu }, _currentMenuBarItem.Parent);
  300. } else if (menuBarItem.Children == null) {
  301. menuBarItem.Children = new MenuItem [] { newMenu };
  302. } else {
  303. var childrens = menuBarItem.Children;
  304. Array.Resize (ref childrens, childrens.Length + 1);
  305. childrens [childrens.Length - 1] = newMenu;
  306. menuBarItem.Children = childrens;
  307. }
  308. DataContext.Menus.Add (new DynamicMenuItemList (newMenu.Title, newMenu));
  309. _lstMenus.MoveDown ();
  310. }
  311. };
  312. _btnRemove.Clicked += () => {
  313. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  314. if (menuItem != null) {
  315. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  316. childrens [_lstMenus.SelectedItem] = null;
  317. int i = 0;
  318. foreach (var c in childrens) {
  319. if (c != null) {
  320. childrens [i] = c;
  321. i++;
  322. }
  323. }
  324. Array.Resize (ref childrens, childrens.Length - 1);
  325. if (childrens.Length == 0) {
  326. if (_currentMenuBarItem.Parent == null) {
  327. ((MenuBarItem)_currentMenuBarItem).Children = null;
  328. //_currentMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
  329. } else {
  330. _currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
  331. }
  332. } else {
  333. ((MenuBarItem)_currentMenuBarItem).Children = childrens;
  334. }
  335. DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
  336. if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1) {
  337. _lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
  338. }
  339. _lstMenus.SetNeedsDisplay ();
  340. SetFrameDetails ();
  341. }
  342. };
  343. _lstMenus.OpenSelectedItem += (e) => {
  344. _currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
  345. if (!(_currentMenuBarItem is MenuBarItem)) {
  346. MessageBox.ErrorQuery ("Menu Open Error", "Must allows sub menus first!", "Ok");
  347. return;
  348. }
  349. DataContext.Parent = _currentMenuBarItem.Title;
  350. DataContext.Menus = new List<DynamicMenuItemList> ();
  351. SetListViewSource (_currentMenuBarItem, true);
  352. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
  353. SetFrameDetails (menuBarItem);
  354. };
  355. _lstMenus.Enter += (_) => {
  356. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  357. SetFrameDetails (menuBarItem);
  358. };
  359. _btnNext.Clicked += () => {
  360. if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
  361. _currentSelectedMenuBar++;
  362. }
  363. SelectCurrentMenuBarItem ();
  364. };
  365. _btnPrevious.Clicked += () => {
  366. if (_currentSelectedMenuBar - 1 > -1) {
  367. _currentSelectedMenuBar--;
  368. }
  369. SelectCurrentMenuBarItem ();
  370. };
  371. _lblMenuBar.Enter += (e) => {
  372. if (_menuBar?.Menus != null) {
  373. _currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  374. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  375. }
  376. };
  377. _btnAddMenuBar.Clicked += () => {
  378. var frameDetails = new DynamicMenuBarDetails (null, false);
  379. var item = frameDetails.EnterMenuItem ();
  380. if (item == null) {
  381. return;
  382. }
  383. if (MenuBar == null) {
  384. _menuBar = new MenuBar ();
  385. Add (_menuBar);
  386. }
  387. var newMenu = CreateNewMenu (item) as MenuBarItem;
  388. var menus = _menuBar.Menus;
  389. Array.Resize (ref menus, menus.Length + 1);
  390. menus [^1] = newMenu;
  391. _menuBar.Menus = menus;
  392. _currentMenuBarItem = newMenu;
  393. _currentMenuBarItem.CheckType = item.checkStyle;
  394. _currentSelectedMenuBar = menus.Length - 1;
  395. _menuBar.Menus [_currentSelectedMenuBar] = newMenu;
  396. _lblMenuBar.Text = newMenu.Title;
  397. SetListViewSource (_currentMenuBarItem, true);
  398. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  399. _menuBar.SetNeedsDisplay ();
  400. };
  401. _btnRemoveMenuBar.Clicked += () => {
  402. if (_menuBar == null || _menuBar.Menus.Length == 0) {
  403. return;
  404. }
  405. if (_menuBar != null && _menuBar.Menus.Length > 0) {
  406. _menuBar.Menus [_currentSelectedMenuBar] = null;
  407. int i = 0;
  408. foreach (var m in _menuBar.Menus) {
  409. if (m != null) {
  410. _menuBar.Menus [i] = m;
  411. i++;
  412. }
  413. }
  414. var menus = _menuBar.Menus;
  415. Array.Resize (ref menus, menus.Length - 1);
  416. _menuBar.Menus = menus;
  417. if (_currentSelectedMenuBar - 1 >= 0 && _menuBar.Menus.Length > 0) {
  418. _currentSelectedMenuBar--;
  419. }
  420. _currentMenuBarItem = _menuBar.Menus?.Length > 0 ? _menuBar.Menus [_currentSelectedMenuBar] : null;
  421. }
  422. if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0) {
  423. Remove (_menuBar);
  424. _menuBar = null;
  425. DataContext.Menus = new List<DynamicMenuItemList> ();
  426. _currentMenuBarItem = null;
  427. _currentSelectedMenuBar = -1;
  428. _lblMenuBar.Text = ustring.Empty;
  429. } else {
  430. _lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
  431. }
  432. SetListViewSource (_currentMenuBarItem, true);
  433. SetFrameDetails (null);
  434. };
  435. SetFrameDetails ();
  436. var ustringConverter = new UStringValueConverter ();
  437. var listWrapperConverter = new ListWrapperConverter ();
  438. var lblMenuBar = new Binding (this, "MenuBar", _lblMenuBar, "Text", ustringConverter);
  439. var lblParent = new Binding (this, "Parent", _lblParent, "Text", ustringConverter);
  440. var lstMenus = new Binding (this, "Menus", _lstMenus, "Source", listWrapperConverter);
  441. void SetFrameDetails (MenuItem menuBarItem = null)
  442. {
  443. MenuItem menuItem;
  444. if (menuBarItem == null) {
  445. menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  446. } else {
  447. menuItem = menuBarItem;
  448. }
  449. _currentEditMenuBarItem = menuItem;
  450. _frmMenuDetails.EditMenuBarItem (menuItem);
  451. var f = _btnOk.Enabled == _frmMenuDetails.Enabled;
  452. if (!f) {
  453. _btnOk.Enabled = _frmMenuDetails.Enabled;
  454. _btnCancel.Enabled = _frmMenuDetails.Enabled;
  455. }
  456. }
  457. void SelectCurrentMenuBarItem ()
  458. {
  459. MenuBarItem menuBarItem = null;
  460. if (_menuBar?.Menus != null) {
  461. menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  462. _lblMenuBar.Text = menuBarItem.Title;
  463. }
  464. SetFrameDetails (menuBarItem);
  465. _currentMenuBarItem = menuBarItem;
  466. DataContext.Menus = new List<DynamicMenuItemList> ();
  467. SetListViewSource (_currentMenuBarItem, true);
  468. _lblParent.Text = ustring.Empty;
  469. }
  470. void SetListViewSource (MenuItem _currentMenuBarItem, bool fill = false)
  471. {
  472. DataContext.Menus = new List<DynamicMenuItemList> ();
  473. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  474. if (menuBarItem != null && menuBarItem?.Children == null) {
  475. return;
  476. }
  477. if (!fill) {
  478. return;
  479. }
  480. if (menuBarItem != null) {
  481. foreach (var child in menuBarItem?.Children) {
  482. var m = new DynamicMenuItemList (child.Title, child);
  483. DataContext.Menus.Add (m);
  484. }
  485. }
  486. }
  487. MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
  488. {
  489. MenuItem newMenu;
  490. if (item.hasSubMenu) {
  491. newMenu = new MenuBarItem (item.title, new MenuItem [] { }, parent);
  492. } else if (parent != null) {
  493. newMenu = new MenuItem (item.title, item.help, null, null, parent);
  494. newMenu.CheckType = item.checkStyle;
  495. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  496. newMenu.Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  497. } else if (item.isTopLevel) {
  498. newMenu = new MenuBarItem (item.title, item.help, null);
  499. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  500. } else {
  501. newMenu = new MenuBarItem (item.title, item.help, null);
  502. ((MenuBarItem)newMenu).Children [0].Action = _frmMenuDetails.CreateAction (newMenu, item);
  503. ((MenuBarItem)newMenu).Children [0].Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  504. }
  505. return newMenu;
  506. }
  507. void UpdateMenuItem (MenuItem _currentEditMenuBarItem, DynamicMenuItem menuItem, int index)
  508. {
  509. _currentEditMenuBarItem.Title = menuItem.title;
  510. _currentEditMenuBarItem.Help = menuItem.help;
  511. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  512. var parent = _currentEditMenuBarItem.Parent as MenuBarItem;
  513. if (parent != null && parent.Children.Length == 1 && _currentEditMenuBarItem.CheckType == MenuItemCheckStyle.Radio) {
  514. _currentEditMenuBarItem.Checked = true;
  515. }
  516. if (menuItem.isTopLevel && _currentEditMenuBarItem is MenuBarItem) {
  517. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  518. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  519. SetListViewSource (_currentEditMenuBarItem, true);
  520. } else if (menuItem.hasSubMenu) {
  521. _currentEditMenuBarItem.Action = null;
  522. if (_currentEditMenuBarItem is MenuBarItem && ((MenuBarItem)_currentEditMenuBarItem).Children == null) {
  523. ((MenuBarItem)_currentEditMenuBarItem).Children = new MenuItem [] { };
  524. } else if (_currentEditMenuBarItem.Parent != null) {
  525. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  526. } else {
  527. _currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { }, _currentEditMenuBarItem.Parent);
  528. }
  529. SetListViewSource (_currentEditMenuBarItem, true);
  530. } else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
  531. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  532. _currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
  533. } else {
  534. if (_currentEditMenuBarItem is MenuBarItem) {
  535. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  536. DataContext.Menus = new List<DynamicMenuItemList> ();
  537. }
  538. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  539. _currentEditMenuBarItem.Shortcut = ShortcutHelper.GetShortcutFromTag (menuItem.shortcut);
  540. }
  541. if (_currentEditMenuBarItem.Parent == null) {
  542. DataContext.MenuBar = _currentEditMenuBarItem.Title;
  543. } else {
  544. if (DataContext.Menus.Count == 0) {
  545. DataContext.Menus.Add (new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem));
  546. }
  547. DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
  548. }
  549. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  550. SetFrameDetails (_currentEditMenuBarItem);
  551. }
  552. //_frmMenuDetails.Initialized += (s, e) => _frmMenuDetails.Enabled = false;
  553. }
  554. }
  555. public class DynamicMenuBarDetails : FrameView {
  556. public MenuItem _menuItem;
  557. public TextField _txtTitle;
  558. public TextField _txtHelp;
  559. public TextView _txtAction;
  560. public CheckBox _ckbIsTopLevel;
  561. public CheckBox _ckbSubMenu;
  562. public RadioGroup _rbChkStyle;
  563. public TextField _txtShortcut;
  564. bool hasParent;
  565. public DynamicMenuBarDetails (MenuItem menuItem = null, bool hasParent = false) : this (menuItem == null ? "Adding New Menu." : "Editing Menu.")
  566. {
  567. _menuItem = menuItem;
  568. this.hasParent = hasParent;
  569. }
  570. public DynamicMenuBarDetails (ustring title) : base (title)
  571. {
  572. var _lblTitle = new Label ("Title:") {
  573. Y = 1
  574. };
  575. Add (_lblTitle);
  576. _txtTitle = new TextField () {
  577. X = Pos.Right (_lblTitle) + 2,
  578. Y = Pos.Top (_lblTitle),
  579. Width = Dim.Fill ()
  580. };
  581. Add (_txtTitle);
  582. var _lblHelp = new Label ("Help:") {
  583. X = Pos.Left (_lblTitle),
  584. Y = Pos.Bottom (_lblTitle) + 1
  585. };
  586. Add (_lblHelp);
  587. _txtHelp = new TextField () {
  588. X = Pos.Left (_txtTitle),
  589. Y = Pos.Top (_lblHelp),
  590. Width = Dim.Fill ()
  591. };
  592. Add (_txtHelp);
  593. var _lblAction = new Label ("Action:") {
  594. X = Pos.Left (_lblTitle),
  595. Y = Pos.Bottom (_lblHelp) + 1
  596. };
  597. Add (_lblAction);
  598. _txtAction = new TextView () {
  599. X = Pos.Left (_txtTitle),
  600. Y = Pos.Top (_lblAction),
  601. Width = Dim.Fill (),
  602. Height = 5
  603. };
  604. Add (_txtAction);
  605. _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
  606. X = Pos.Left (_lblTitle),
  607. Y = Pos.Bottom (_lblAction) + 5
  608. };
  609. Add (_ckbIsTopLevel);
  610. _ckbSubMenu = new CheckBox ("Has sub-menus") {
  611. X = Pos.Left (_lblTitle),
  612. Y = Pos.Bottom (_ckbIsTopLevel),
  613. Checked = _menuItem == null ? !hasParent : HasSubMenus (_menuItem)
  614. };
  615. Add (_ckbSubMenu);
  616. var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
  617. _rbChkStyle = new RadioGroup (_rChkLabels) {
  618. X = Pos.Left (_lblTitle),
  619. Y = Pos.Bottom (_ckbSubMenu) + 1,
  620. };
  621. Add (_rbChkStyle);
  622. var _lblShortcut = new Label ("Shortcut:") {
  623. X = Pos.Right (_ckbSubMenu) + 10,
  624. Y = Pos.Top (_ckbSubMenu)
  625. };
  626. Add (_lblShortcut);
  627. _txtShortcut = new TextField () {
  628. X = Pos.X (_lblShortcut),
  629. Y = Pos.Bottom (_lblShortcut),
  630. Width = Dim.Fill (),
  631. ReadOnly = true
  632. };
  633. _txtShortcut.KeyDown += (e) => {
  634. if (!ProcessKey (e.KeyEvent)) {
  635. return;
  636. }
  637. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  638. if (CheckShortcut (k, true)) {
  639. e.Handled = true;
  640. }
  641. };
  642. bool ProcessKey (KeyEvent ev)
  643. {
  644. switch (ev.Key) {
  645. case Key.CursorUp:
  646. case Key.CursorDown:
  647. case Key.Tab:
  648. case Key.BackTab:
  649. return false;
  650. }
  651. return true;
  652. }
  653. bool CheckShortcut (Key k, bool pre)
  654. {
  655. var m = _menuItem != null ? _menuItem : new MenuItem ();
  656. if (pre && !ShortcutHelper.PreShortcutValidation (k)) {
  657. _txtShortcut.Text = "";
  658. return false;
  659. }
  660. if (!pre) {
  661. if (!ShortcutHelper.PostShortcutValidation (ShortcutHelper.GetShortcutFromTag (_txtShortcut.Text))) {
  662. _txtShortcut.Text = "";
  663. return false;
  664. }
  665. return true;
  666. }
  667. _txtShortcut.Text = ShortcutHelper.GetShortcutTag (k);
  668. return true;
  669. }
  670. _txtShortcut.KeyUp += (e) => {
  671. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  672. if (CheckShortcut (k, false)) {
  673. e.Handled = true;
  674. }
  675. };
  676. Add (_txtShortcut);
  677. var _btnShortcut = new Button ("Clear Shortcut") {
  678. X = Pos.X (_lblShortcut),
  679. Y = Pos.Bottom (_txtShortcut) + 1
  680. };
  681. _btnShortcut.Clicked += () => {
  682. _txtShortcut.Text = "";
  683. };
  684. Add (_btnShortcut);
  685. _ckbIsTopLevel.Toggled += (e) => {
  686. if ((_menuItem != null && _menuItem.Parent != null && _ckbIsTopLevel.Checked) ||
  687. _menuItem == null && hasParent && _ckbIsTopLevel.Checked) {
  688. MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
  689. _ckbIsTopLevel.Checked = false;
  690. return;
  691. }
  692. if (_ckbIsTopLevel.Checked) {
  693. _ckbSubMenu.Checked = false;
  694. _ckbSubMenu.SetNeedsDisplay ();
  695. _txtHelp.Enabled = true;
  696. _txtAction.Enabled = true;
  697. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  698. } else {
  699. if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
  700. _ckbSubMenu.Checked = true;
  701. _ckbSubMenu.SetNeedsDisplay ();
  702. _txtShortcut.Enabled = false;
  703. }
  704. _txtHelp.Text = "";
  705. _txtHelp.Enabled = false;
  706. _txtAction.Text = "";
  707. _txtAction.Enabled = false;
  708. }
  709. };
  710. _ckbSubMenu.Toggled += (e) => {
  711. if (_ckbSubMenu.Checked) {
  712. _ckbIsTopLevel.Checked = false;
  713. _ckbIsTopLevel.SetNeedsDisplay ();
  714. _txtHelp.Text = "";
  715. _txtHelp.Enabled = false;
  716. _txtAction.Text = "";
  717. _txtAction.Enabled = false;
  718. _txtShortcut.Text = "";
  719. _txtShortcut.Enabled = false;
  720. } else {
  721. if (!hasParent) {
  722. _ckbIsTopLevel.Checked = true;
  723. _ckbIsTopLevel.SetNeedsDisplay ();
  724. _txtShortcut.Enabled = false;
  725. }
  726. _txtHelp.Enabled = true;
  727. _txtAction.Enabled = true;
  728. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  729. }
  730. };
  731. //Add (_frmMenuDetails);
  732. }
  733. public DynamicMenuItem EnterMenuItem ()
  734. {
  735. var valid = false;
  736. if (_menuItem == null) {
  737. var m = new DynamicMenuItem ();
  738. _txtTitle.Text = m.title;
  739. _txtHelp.Text = m.help;
  740. _txtAction.Text = m.action;
  741. _ckbIsTopLevel.Checked = false;
  742. _ckbSubMenu.Checked = !hasParent;
  743. _txtHelp.Enabled = hasParent;
  744. _txtAction.Enabled = hasParent;
  745. _txtShortcut.Enabled = hasParent;
  746. } else {
  747. EditMenuBarItem (_menuItem);
  748. }
  749. var _btnOk = new Button ("Ok") {
  750. IsDefault = true,
  751. };
  752. _btnOk.Clicked += () => {
  753. if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
  754. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  755. } else {
  756. valid = true;
  757. Application.RequestStop ();
  758. }
  759. };
  760. var _btnCancel = new Button ("Cancel");
  761. _btnCancel.Clicked += () => {
  762. _txtTitle.Text = ustring.Empty;
  763. Application.RequestStop ();
  764. };
  765. var _dialog = new Dialog ("Please enter the menu details.", _btnOk, _btnCancel);
  766. Width = Dim.Fill ();
  767. Height = Dim.Fill () - 1;
  768. _dialog.Add (this);
  769. _txtTitle.SetFocus ();
  770. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  771. Application.Run (_dialog);
  772. if (valid) {
  773. return new DynamicMenuItem (_txtTitle.Text, _txtHelp.Text, _txtAction.Text,
  774. _ckbIsTopLevel != null ? _ckbIsTopLevel.Checked : false,
  775. _ckbSubMenu != null ? _ckbSubMenu.Checked : false,
  776. _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  777. _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio,
  778. _txtShortcut.Text);
  779. } else {
  780. return null;
  781. }
  782. }
  783. public void EditMenuBarItem (MenuItem menuItem)
  784. {
  785. if (menuItem == null) {
  786. hasParent = false;
  787. Enabled = false;
  788. CleanEditMenuBarItem ();
  789. return;
  790. } else {
  791. hasParent = menuItem.Parent != null;
  792. Enabled = true;
  793. }
  794. _menuItem = menuItem;
  795. _txtTitle.Text = menuItem?.Title ?? "";
  796. _txtHelp.Text = menuItem?.Help ?? "";
  797. _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : ustring.Empty;
  798. _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
  799. _ckbSubMenu.Checked = HasSubMenus (menuItem);
  800. _txtHelp.Enabled = !_ckbSubMenu.Checked;
  801. _txtAction.Enabled = !_ckbSubMenu.Checked;
  802. _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
  803. _txtShortcut.Text = menuItem?.ShortcutTag ?? "";
  804. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  805. }
  806. void CleanEditMenuBarItem ()
  807. {
  808. _txtTitle.Text = "";
  809. _txtHelp.Text = "";
  810. _txtAction.Text = "";
  811. _ckbIsTopLevel.Checked = false;
  812. _ckbSubMenu.Checked = false;
  813. _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
  814. _txtShortcut.Text = "";
  815. }
  816. ustring GetTargetAction (Action action)
  817. {
  818. var me = action.Target;
  819. if (me == null) {
  820. throw new ArgumentException ();
  821. }
  822. object v = new object ();
  823. foreach (var field in me.GetType ().GetFields ()) {
  824. if (field.Name == "item") {
  825. v = field.GetValue (me);
  826. }
  827. }
  828. return v == null || !(v is DynamicMenuItem item) ? ustring.Empty : item.action;
  829. }
  830. bool IsTopLevel (MenuItem menuItem)
  831. {
  832. var topLevel = menuItem as MenuBarItem;
  833. if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
  834. return true;
  835. } else {
  836. return false;
  837. }
  838. }
  839. bool HasSubMenus (MenuItem menuItem)
  840. {
  841. var menuBarItem = menuItem as MenuBarItem;
  842. if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
  843. return true;
  844. } else {
  845. return false;
  846. }
  847. }
  848. public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
  849. {
  850. switch (menuItem.CheckType) {
  851. case MenuItemCheckStyle.NoCheck:
  852. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  853. case MenuItemCheckStyle.Checked:
  854. return new Action (() => menuItem.Checked = !menuItem.Checked);
  855. case MenuItemCheckStyle.Radio:
  856. break;
  857. }
  858. return new Action (() => {
  859. menuItem.Checked = true;
  860. var parent = menuItem?.Parent as MenuBarItem;
  861. if (parent != null) {
  862. var childrens = parent.Children;
  863. for (int i = 0; i < childrens.Length; i++) {
  864. var child = childrens [i];
  865. if (child != menuItem) {
  866. child.Checked = false;
  867. }
  868. }
  869. }
  870. });
  871. }
  872. public void UpdateParent (ref MenuItem menuItem)
  873. {
  874. var parent = menuItem.Parent as MenuBarItem;
  875. var idx = parent.GetChildrenIndex (menuItem);
  876. if (!(menuItem is MenuBarItem)) {
  877. menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
  878. if (idx > -1) {
  879. parent.Children [idx] = menuItem;
  880. }
  881. } else {
  882. menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
  883. if (idx > -1) {
  884. parent.Children [idx] = menuItem;
  885. }
  886. }
  887. }
  888. }
  889. public class DynamicMenuItemModel : INotifyPropertyChanged {
  890. public event PropertyChangedEventHandler PropertyChanged;
  891. private ustring menuBar;
  892. private ustring parent;
  893. private List<DynamicMenuItemList> menus;
  894. public ustring MenuBar {
  895. get => menuBar;
  896. set {
  897. if (value != menuBar) {
  898. menuBar = value;
  899. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  900. }
  901. }
  902. }
  903. public ustring Parent {
  904. get => parent;
  905. set {
  906. if (value != parent) {
  907. parent = value;
  908. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  909. }
  910. }
  911. }
  912. public List<DynamicMenuItemList> Menus {
  913. get => menus;
  914. set {
  915. if (value != menus) {
  916. menus = value;
  917. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  918. }
  919. }
  920. }
  921. public DynamicMenuItemModel ()
  922. {
  923. Menus = new List<DynamicMenuItemList> ();
  924. }
  925. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  926. {
  927. return propertyName;
  928. }
  929. }
  930. public interface IValueConverter {
  931. object Convert (object value, object parameter = null);
  932. }
  933. public class Binding {
  934. public View Target { get; private set; }
  935. public View Source { get; private set; }
  936. public string SourcePropertyName { get; private set; }
  937. public string TargetPropertyName { get; private set; }
  938. private object sourceDataContext;
  939. private PropertyInfo sourceBindingProperty;
  940. private IValueConverter valueConverter;
  941. public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
  942. {
  943. Target = target;
  944. Source = source;
  945. SourcePropertyName = sourcePropertyName;
  946. TargetPropertyName = targetPropertyName;
  947. sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  948. sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  949. this.valueConverter = valueConverter;
  950. UpdateTarget ();
  951. var notifier = ((INotifyPropertyChanged)sourceDataContext);
  952. if (notifier != null) {
  953. notifier.PropertyChanged += (s, e) => {
  954. if (e.PropertyName == SourcePropertyName) {
  955. UpdateTarget ();
  956. }
  957. };
  958. }
  959. }
  960. private void UpdateTarget ()
  961. {
  962. try {
  963. var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
  964. if (sourceValue == null) {
  965. return;
  966. }
  967. var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
  968. var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  969. targetProperty.SetValue (Target, finalValue);
  970. } catch (Exception ex) {
  971. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  972. }
  973. }
  974. }
  975. public class ListWrapperConverter : IValueConverter {
  976. public object Convert (object value, object parameter = null)
  977. {
  978. return new ListWrapper ((IList)value);
  979. }
  980. }
  981. public class UStringValueConverter : IValueConverter {
  982. public object Convert (object value, object parameter = null)
  983. {
  984. var data = Encoding.ASCII.GetBytes (value.ToString ());
  985. return ustring.Make (data);
  986. }
  987. }
  988. }
  989. }