DynamicStatusBar.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Runtime.CompilerServices;
  6. using Terminal.Gui;
  7. namespace UICatalog {
  8. [ScenarioMetadata (Name: "Dynamic StatusBar", Description: "Demonstrates how to add and remove a StatusBar and change items dynamically.")]
  9. [ScenarioCategory ("Dynamic")]
  10. class DynamicStatusBar : Scenario {
  11. public override void Init (Toplevel top, ColorScheme colorScheme)
  12. {
  13. Application.Init ();
  14. Top = Application.Top;
  15. Top.Add (new DynamicStatusBarSample ($"CTRL-Q to Close - Scenario: {GetName ()}"));
  16. }
  17. }
  18. public class DynamicStatusItemList {
  19. public ustring Title { get; set; }
  20. public StatusItem StatusItem { get; set; }
  21. public DynamicStatusItemList () { }
  22. public DynamicStatusItemList (ustring title, StatusItem statusItem)
  23. {
  24. Title = title;
  25. StatusItem = statusItem;
  26. }
  27. public override string ToString () => $"{Title}, {StatusItem}";
  28. }
  29. public class DynamicStatusItem {
  30. public ustring title = "New";
  31. public ustring action = "";
  32. public ustring shortcut;
  33. public DynamicStatusItem () { }
  34. public DynamicStatusItem (ustring title)
  35. {
  36. this.title = title;
  37. }
  38. public DynamicStatusItem (ustring title, ustring action, ustring shortcut = null)
  39. {
  40. this.title = title;
  41. this.action = action;
  42. this.shortcut = shortcut;
  43. }
  44. }
  45. public class DynamicStatusBarSample : Window {
  46. StatusBar _statusBar;
  47. StatusItem _currentStatusItem;
  48. int _currentSelectedStatusBar = -1;
  49. StatusItem _currentEditStatusItem;
  50. ListView _lstItems;
  51. public DynamicStatusItemModel DataContext { get; set; }
  52. public DynamicStatusBarSample (ustring title) : base (title)
  53. {
  54. DataContext = new DynamicStatusItemModel ();
  55. var _frmDelimiter = new FrameView ("Shortcut Delimiter:") {
  56. X = Pos.Center (),
  57. Y = 0,
  58. Width = 25,
  59. Height = 4
  60. };
  61. var _txtDelimiter = new TextField (StatusBar.ShortcutDelimiter.ToString ()) {
  62. X = Pos.Center (),
  63. Width = 2,
  64. };
  65. _txtDelimiter.TextChanged += (_) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text;
  66. _frmDelimiter.Add (_txtDelimiter);
  67. Add (_frmDelimiter);
  68. var _frmStatusBar = new FrameView ("Items:") {
  69. Y = 5,
  70. Width = Dim.Percent (50),
  71. Height = Dim.Fill (2)
  72. };
  73. var _btnAddStatusBar = new Button ("Add a StatusBar") {
  74. Y = 1,
  75. };
  76. _frmStatusBar.Add (_btnAddStatusBar);
  77. var _btnRemoveStatusBar = new Button ("Remove a StatusBar") {
  78. Y = 1
  79. };
  80. _btnRemoveStatusBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveStatusBar) - Pos.Left (_btnRemoveStatusBar));
  81. _frmStatusBar.Add (_btnRemoveStatusBar);
  82. var _btnAdd = new Button (" Add ") {
  83. Y = Pos.Top (_btnRemoveStatusBar) + 2,
  84. };
  85. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  86. _frmStatusBar.Add (_btnAdd);
  87. _lstItems = new ListView (new List<DynamicStatusItemList> ()) {
  88. ColorScheme = Colors.Dialog,
  89. Y = Pos.Top (_btnAddStatusBar) + 2,
  90. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1,
  91. Height = Dim.Fill (),
  92. };
  93. _frmStatusBar.Add (_lstItems);
  94. var _btnRemove = new Button ("Remove") {
  95. X = Pos.Left (_btnAdd),
  96. Y = Pos.Top (_btnAdd) + 1
  97. };
  98. _frmStatusBar.Add (_btnRemove);
  99. var _btnUp = new Button ("^") {
  100. X = Pos.Right (_lstItems) + 2,
  101. Y = Pos.Top (_btnRemove) + 2
  102. };
  103. _frmStatusBar.Add (_btnUp);
  104. var _btnDown = new Button ("v") {
  105. X = Pos.Right (_lstItems) + 2,
  106. Y = Pos.Top (_btnUp) + 1
  107. };
  108. _frmStatusBar.Add (_btnDown);
  109. Add (_frmStatusBar);
  110. var _frmStatusBarDetails = new DynamicStatusBarDetails ("StatusBar Item Details:") {
  111. X = Pos.Right (_frmStatusBar),
  112. Y = Pos.Top (_frmStatusBar),
  113. Width = Dim.Fill (),
  114. Height = Dim.Fill (4)
  115. };
  116. Add (_frmStatusBarDetails);
  117. _btnUp.Clicked += () => {
  118. var i = _lstItems.SelectedItem;
  119. var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
  120. if (statusItem != null) {
  121. var items = _statusBar.Items;
  122. if (i > 0) {
  123. items [i] = items [i - 1];
  124. items [i - 1] = statusItem;
  125. DataContext.Items [i] = DataContext.Items [i - 1];
  126. DataContext.Items [i - 1] = new DynamicStatusItemList (statusItem.Title, statusItem);
  127. _lstItems.SelectedItem = i - 1;
  128. _statusBar.SetNeedsDisplay ();
  129. }
  130. }
  131. };
  132. _btnDown.Clicked += () => {
  133. var i = _lstItems.SelectedItem;
  134. var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
  135. if (statusItem != null) {
  136. var items = _statusBar.Items;
  137. if (i < items.Length - 1) {
  138. items [i] = items [i + 1];
  139. items [i + 1] = statusItem;
  140. DataContext.Items [i] = DataContext.Items [i + 1];
  141. DataContext.Items [i + 1] = new DynamicStatusItemList (statusItem.Title, statusItem);
  142. _lstItems.SelectedItem = i + 1;
  143. _statusBar.SetNeedsDisplay ();
  144. }
  145. }
  146. };
  147. var _btnOk = new Button ("Ok") {
  148. X = Pos.Right (_frmStatusBar) + 20,
  149. Y = Pos.Bottom (_frmStatusBarDetails),
  150. };
  151. Add (_btnOk);
  152. var _btnCancel = new Button ("Cancel") {
  153. X = Pos.Right (_btnOk) + 3,
  154. Y = Pos.Top (_btnOk),
  155. };
  156. _btnCancel.Clicked += () => {
  157. SetFrameDetails (_currentEditStatusItem);
  158. };
  159. Add (_btnCancel);
  160. _lstItems.SelectedItemChanged += (e) => {
  161. SetFrameDetails ();
  162. };
  163. _btnOk.Clicked += () => {
  164. if (ustring.IsNullOrEmpty (_frmStatusBarDetails._txtTitle.Text) && _currentEditStatusItem != null) {
  165. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  166. } else if (_currentEditStatusItem != null) {
  167. _frmStatusBarDetails._txtTitle.Text = SetTitleText (
  168. _frmStatusBarDetails._txtTitle.Text, _frmStatusBarDetails._txtShortcut.Text);
  169. var statusItem = new DynamicStatusItem (_frmStatusBarDetails._txtTitle.Text,
  170. _frmStatusBarDetails._txtAction.Text,
  171. _frmStatusBarDetails._txtShortcut.Text);
  172. UpdateStatusItem (_currentEditStatusItem, statusItem, _lstItems.SelectedItem);
  173. }
  174. };
  175. _btnAdd.Clicked += () => {
  176. if (StatusBar == null) {
  177. MessageBox.ErrorQuery ("StatusBar Bar Error", "Must add a StatusBar first!", "Ok");
  178. _btnAddStatusBar.SetFocus ();
  179. return;
  180. }
  181. var frameDetails = new DynamicStatusBarDetails ();
  182. var item = frameDetails.EnterStatusItem ();
  183. if (item == null) {
  184. return;
  185. }
  186. StatusItem newStatusItem = CreateNewStatusBar (item);
  187. _currentSelectedStatusBar++;
  188. _statusBar.AddItemAt (_currentSelectedStatusBar, newStatusItem);
  189. DataContext.Items.Add (new DynamicStatusItemList (newStatusItem.Title, newStatusItem));
  190. _lstItems.MoveDown ();
  191. SetFrameDetails ();
  192. };
  193. _btnRemove.Clicked += () => {
  194. var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
  195. if (statusItem != null) {
  196. _statusBar.RemoveItem (_currentSelectedStatusBar);
  197. DataContext.Items.RemoveAt (_lstItems.SelectedItem);
  198. if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1) {
  199. _lstItems.SelectedItem = _lstItems.Source.Count - 1;
  200. }
  201. _lstItems.SetNeedsDisplay ();
  202. SetFrameDetails ();
  203. }
  204. };
  205. _lstItems.Enter += (_) => {
  206. var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
  207. SetFrameDetails (statusItem);
  208. };
  209. _btnAddStatusBar.Clicked += () => {
  210. if (_statusBar != null) {
  211. return;
  212. }
  213. _statusBar = new StatusBar ();
  214. Add (_statusBar);
  215. };
  216. _btnRemoveStatusBar.Clicked += () => {
  217. if (_statusBar == null) {
  218. return;
  219. }
  220. Remove (_statusBar);
  221. _statusBar = null;
  222. DataContext.Items = new List<DynamicStatusItemList> ();
  223. _currentStatusItem = null;
  224. _currentSelectedStatusBar = -1;
  225. SetListViewSource (_currentStatusItem, true);
  226. SetFrameDetails (null);
  227. };
  228. SetFrameDetails ();
  229. var ustringConverter = new UStringValueConverter ();
  230. var listWrapperConverter = new ListWrapperConverter ();
  231. var lstItems = new Binding (this, "Items", _lstItems, "Source", listWrapperConverter);
  232. void SetFrameDetails (StatusItem statusItem = null)
  233. {
  234. StatusItem newStatusItem;
  235. if (statusItem == null) {
  236. newStatusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
  237. } else {
  238. newStatusItem = statusItem;
  239. }
  240. _currentEditStatusItem = newStatusItem;
  241. _frmStatusBarDetails.EditStatusItem (newStatusItem);
  242. var f = _btnOk.Enabled == _frmStatusBarDetails.Enabled;
  243. if (!f) {
  244. _btnOk.Enabled = _frmStatusBarDetails.Enabled;
  245. _btnCancel.Enabled = _frmStatusBarDetails.Enabled;
  246. }
  247. }
  248. void SetListViewSource (StatusItem _currentStatusItem, bool fill = false)
  249. {
  250. DataContext.Items = new List<DynamicStatusItemList> ();
  251. var statusItem = _currentStatusItem;
  252. if (!fill) {
  253. return;
  254. }
  255. if (statusItem != null) {
  256. foreach (var si in _statusBar.Items) {
  257. DataContext.Items.Add (new DynamicStatusItemList (si.Title, si));
  258. }
  259. }
  260. }
  261. StatusItem CreateNewStatusBar (DynamicStatusItem item)
  262. {
  263. var newStatusItem = new StatusItem (ShortcutHelper.GetShortcutFromTag (
  264. item.shortcut, StatusBar.ShortcutDelimiter),
  265. item.title, _frmStatusBarDetails.CreateAction (item));
  266. return newStatusItem;
  267. }
  268. void UpdateStatusItem (StatusItem _currentEditStatusItem, DynamicStatusItem statusItem, int index)
  269. {
  270. _currentEditStatusItem = CreateNewStatusBar (statusItem);
  271. _statusBar.Items [index] = _currentEditStatusItem;
  272. if (DataContext.Items.Count == 0) {
  273. DataContext.Items.Add (new DynamicStatusItemList (_currentEditStatusItem.Title, _currentEditStatusItem));
  274. }
  275. DataContext.Items [index] = new DynamicStatusItemList (_currentEditStatusItem.Title, _currentEditStatusItem);
  276. SetFrameDetails (_currentEditStatusItem);
  277. }
  278. //_frmStatusBarDetails.Initialized += (s, e) => _frmStatusBarDetails.Enabled = false;
  279. }
  280. public static ustring SetTitleText (ustring title, ustring shortcut)
  281. {
  282. var txt = title;
  283. var split = title.ToString ().Split ('~');
  284. if (split.Length > 1) {
  285. txt = split [2].Trim (); ;
  286. }
  287. if (string.IsNullOrEmpty (shortcut.ToString ())) {
  288. return txt;
  289. }
  290. return $"~{shortcut}~ {txt}";
  291. }
  292. }
  293. public class DynamicStatusBarDetails : FrameView {
  294. public StatusItem _statusItem;
  295. public TextField _txtTitle;
  296. public TextView _txtAction;
  297. public TextField _txtShortcut;
  298. public DynamicStatusBarDetails (StatusItem statusItem = null) : this (statusItem == null ? "Adding New StatusBar Item." : "Editing StatusBar Item.")
  299. {
  300. _statusItem = statusItem;
  301. }
  302. public DynamicStatusBarDetails (ustring title) : base (title)
  303. {
  304. var _lblTitle = new Label ("Title:") {
  305. Y = 1
  306. };
  307. Add (_lblTitle);
  308. _txtTitle = new TextField () {
  309. X = Pos.Right (_lblTitle) + 4,
  310. Y = Pos.Top (_lblTitle),
  311. Width = Dim.Fill ()
  312. };
  313. Add (_txtTitle);
  314. var _lblAction = new Label ("Action:") {
  315. X = Pos.Left (_lblTitle),
  316. Y = Pos.Bottom (_lblTitle) + 1
  317. };
  318. Add (_lblAction);
  319. _txtAction = new TextView () {
  320. ColorScheme = Colors.Dialog,
  321. X = Pos.Left (_txtTitle),
  322. Y = Pos.Top (_lblAction),
  323. Width = Dim.Fill (),
  324. Height = 5
  325. };
  326. Add (_txtAction);
  327. var _lblShortcut = new Label ("Shortcut:") {
  328. X = Pos.Left (_lblTitle),
  329. Y = Pos.Bottom (_txtAction) + 1
  330. };
  331. Add (_lblShortcut);
  332. _txtShortcut = new TextField () {
  333. X = Pos.X (_txtAction),
  334. Y = Pos.Y (_lblShortcut),
  335. Width = Dim.Fill (),
  336. ReadOnly = true
  337. };
  338. _txtShortcut.KeyDown += (e) => {
  339. if (!ProcessKey (e.KeyEvent)) {
  340. return;
  341. }
  342. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  343. if (CheckShortcut (k, true)) {
  344. e.Handled = true;
  345. }
  346. };
  347. bool ProcessKey (KeyEvent ev)
  348. {
  349. switch (ev.Key) {
  350. case Key.CursorUp:
  351. case Key.CursorDown:
  352. case Key.Tab:
  353. case Key.BackTab:
  354. return false;
  355. }
  356. return true;
  357. }
  358. bool CheckShortcut (Key k, bool pre)
  359. {
  360. var m = _statusItem != null ? _statusItem : new StatusItem (k, "", null);
  361. if (pre && !ShortcutHelper.PreShortcutValidation (k)) {
  362. _txtShortcut.Text = "";
  363. return false;
  364. }
  365. if (!pre) {
  366. if (!ShortcutHelper.PostShortcutValidation (ShortcutHelper.GetShortcutFromTag (
  367. _txtShortcut.Text, StatusBar.ShortcutDelimiter))) {
  368. _txtShortcut.Text = "";
  369. return false;
  370. }
  371. return true;
  372. }
  373. _txtShortcut.Text = ShortcutHelper.GetShortcutTag (k, StatusBar.ShortcutDelimiter);
  374. return true;
  375. }
  376. _txtShortcut.KeyUp += (e) => {
  377. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  378. if (CheckShortcut (k, false)) {
  379. e.Handled = true;
  380. }
  381. };
  382. Add (_txtShortcut);
  383. var _btnShortcut = new Button ("Clear Shortcut") {
  384. X = Pos.X (_lblShortcut),
  385. Y = Pos.Bottom (_txtShortcut) + 1
  386. };
  387. _btnShortcut.Clicked += () => {
  388. _txtShortcut.Text = "";
  389. };
  390. Add (_btnShortcut);
  391. }
  392. public DynamicStatusItem EnterStatusItem ()
  393. {
  394. var valid = false;
  395. if (_statusItem == null) {
  396. var m = new DynamicStatusItem ();
  397. _txtTitle.Text = m.title;
  398. _txtAction.Text = m.action;
  399. } else {
  400. EditStatusItem (_statusItem);
  401. }
  402. var _btnOk = new Button ("Ok") {
  403. IsDefault = true,
  404. };
  405. _btnOk.Clicked += () => {
  406. if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
  407. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  408. } else {
  409. if (!ustring.IsNullOrEmpty (_txtShortcut.Text)) {
  410. _txtTitle.Text = DynamicStatusBarSample.SetTitleText (
  411. _txtTitle.Text, _txtShortcut.Text);
  412. }
  413. valid = true;
  414. Application.RequestStop ();
  415. }
  416. };
  417. var _btnCancel = new Button ("Cancel");
  418. _btnCancel.Clicked += () => {
  419. _txtTitle.Text = ustring.Empty;
  420. Application.RequestStop ();
  421. };
  422. var _dialog = new Dialog ("Please enter the item details.", _btnOk, _btnCancel);
  423. Width = Dim.Fill ();
  424. Height = Dim.Fill () - 1;
  425. _dialog.Add (this);
  426. _txtTitle.SetFocus ();
  427. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  428. Application.Run (_dialog);
  429. if (valid) {
  430. return new DynamicStatusItem (_txtTitle.Text, _txtAction.Text, _txtShortcut.Text);
  431. } else {
  432. return null;
  433. }
  434. }
  435. public void EditStatusItem (StatusItem statusItem)
  436. {
  437. if (statusItem == null) {
  438. Enabled = false;
  439. CleanEditStatusItem ();
  440. return;
  441. } else {
  442. Enabled = true;
  443. }
  444. _statusItem = statusItem;
  445. _txtTitle.Text = statusItem?.Title ?? "";
  446. _txtAction.Text = statusItem != null && statusItem.Action != null ? GetTargetAction (statusItem.Action) : ustring.Empty;
  447. _txtShortcut.Text = ShortcutHelper.GetShortcutTag (statusItem.Shortcut, StatusBar.ShortcutDelimiter) ?? "";
  448. }
  449. void CleanEditStatusItem ()
  450. {
  451. _txtTitle.Text = "";
  452. _txtAction.Text = "";
  453. _txtShortcut.Text = "";
  454. }
  455. ustring GetTargetAction (Action action)
  456. {
  457. var me = action.Target;
  458. if (me == null) {
  459. throw new ArgumentException ();
  460. }
  461. object v = new object ();
  462. foreach (var field in me.GetType ().GetFields ()) {
  463. if (field.Name == "item") {
  464. v = field.GetValue (me);
  465. }
  466. }
  467. return v == null || !(v is DynamicStatusItem item) ? ustring.Empty : item.action;
  468. }
  469. public Action CreateAction (DynamicStatusItem item)
  470. {
  471. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  472. }
  473. }
  474. public class DynamicStatusItemModel : INotifyPropertyChanged {
  475. public event PropertyChangedEventHandler PropertyChanged;
  476. private ustring statusBar;
  477. private List<DynamicStatusItemList> items;
  478. public ustring StatusBar {
  479. get => statusBar;
  480. set {
  481. if (value != statusBar) {
  482. statusBar = value;
  483. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  484. }
  485. }
  486. }
  487. public List<DynamicStatusItemList> Items {
  488. get => items;
  489. set {
  490. if (value != items) {
  491. items = value;
  492. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  493. }
  494. }
  495. }
  496. public DynamicStatusItemModel ()
  497. {
  498. Items = new List<DynamicStatusItemList> ();
  499. }
  500. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  501. {
  502. return propertyName;
  503. }
  504. }
  505. }