DynamicStatusBar.cs 26 KB

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