DynamicStatusBar.cs 25 KB

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