DynamicMenuBar.cs 35 KB

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