DynamicStatusBar.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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> ().Dispose ();
  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 ("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 (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 ("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, Application.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. int i = _lstItems.SelectedItem;
  259. Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
  260. if (statusItem != null)
  261. {
  262. Shortcut [] items = _statusBar.SubViews.OfType<Shortcut> ().ToArray ();
  263. if (i > 0)
  264. {
  265. items [i] = items [i - 1];
  266. items [i - 1] = statusItem;
  267. DataContext.Items [i] = DataContext.Items [i - 1];
  268. DataContext.Items [i - 1] =
  269. new DynamicStatusItemList (statusItem.Title, statusItem);
  270. _lstItems.SelectedItem = i - 1;
  271. _statusBar.SetNeedsDraw ();
  272. }
  273. }
  274. };
  275. btnDown.Accepting += (s, e) =>
  276. {
  277. int i = _lstItems.SelectedItem;
  278. Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
  279. if (statusItem != null)
  280. {
  281. Shortcut [] items = _statusBar.SubViews.OfType<Shortcut> ().ToArray ();
  282. if (i < items.Length - 1)
  283. {
  284. items [i] = items [i + 1];
  285. items [i + 1] = statusItem;
  286. DataContext.Items [i] = DataContext.Items [i + 1];
  287. DataContext.Items [i + 1] =
  288. new DynamicStatusItemList (statusItem.Title, statusItem);
  289. _lstItems.SelectedItem = i + 1;
  290. _statusBar.SetNeedsDraw ();
  291. }
  292. }
  293. };
  294. var btnOk = new Button
  295. {
  296. X = Pos.Right (frmStatusBar) + 20, Y = Pos.Bottom (frmStatusBarDetails), Text = "Ok"
  297. };
  298. Add (btnOk);
  299. var btnCancel = new Button { X = Pos.Right (btnOk) + 3, Y = Pos.Top (btnOk), Text = "Cancel" };
  300. btnCancel.Accepting += (s, e) => { SetFrameDetails (_currentEditStatusItem); };
  301. Add (btnCancel);
  302. _lstItems.SelectedItemChanged += (s, e) => { SetFrameDetails (); };
  303. btnOk.Accepting += (s, e) =>
  304. {
  305. if (string.IsNullOrEmpty (frmStatusBarDetails.TextTitle.Text) && _currentEditStatusItem != null)
  306. {
  307. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  308. }
  309. else if (_currentEditStatusItem != null)
  310. {
  311. var statusItem = new DynamicStatusItem
  312. {
  313. Title = frmStatusBarDetails.TextTitle.Text,
  314. Action = frmStatusBarDetails.TextAction.Text,
  315. Shortcut = frmStatusBarDetails.TextShortcut.Text
  316. };
  317. UpdateStatusItem (_currentEditStatusItem, statusItem, _lstItems.SelectedItem);
  318. }
  319. };
  320. btnAdd.Accepting += (s, e) =>
  321. {
  322. //if (StatusBar == null)
  323. //{
  324. // MessageBox.ErrorQuery (
  325. // "StatusBar Bar Error",
  326. // "Must add a StatusBar first!",
  327. // "Ok"
  328. // );
  329. // _btnAddStatusBar.SetFocus ();
  330. // return;
  331. //}
  332. var frameDetails = new DynamicStatusBarDetails ();
  333. DynamicStatusItem item = frameDetails.EnterStatusItem ();
  334. if (item == null)
  335. {
  336. return;
  337. }
  338. Shortcut newStatusItem = CreateNewStatusBar (item);
  339. _currentSelectedStatusBar++;
  340. _statusBar.AddShortcutAt (_currentSelectedStatusBar, newStatusItem);
  341. DataContext.Items.Add (new DynamicStatusItemList (newStatusItem.Title, newStatusItem));
  342. _lstItems.MoveDown ();
  343. SetFrameDetails ();
  344. };
  345. btnRemove.Accepting += (s, e) =>
  346. {
  347. Shortcut statusItem = DataContext.Items.Count > 0
  348. ? DataContext.Items [_lstItems.SelectedItem].Shortcut
  349. : null;
  350. if (statusItem != null)
  351. {
  352. _statusBar.RemoveShortcut (_currentSelectedStatusBar);
  353. statusItem.Dispose ();
  354. DataContext.Items.RemoveAt (_lstItems.SelectedItem);
  355. if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1)
  356. {
  357. _lstItems.SelectedItem = _lstItems.Source.Count - 1;
  358. }
  359. _lstItems.SetNeedsDraw ();
  360. SetFrameDetails ();
  361. }
  362. };
  363. _lstItems.HasFocusChanging += (s, e) =>
  364. {
  365. Shortcut statusItem = DataContext.Items.Count > 0
  366. ? DataContext.Items [_lstItems.SelectedItem].Shortcut
  367. : null;
  368. SetFrameDetails (statusItem);
  369. };
  370. btnAddStatusBar.Accepting += (s, e) =>
  371. {
  372. if (_statusBar != null)
  373. {
  374. return;
  375. }
  376. _statusBar = new StatusBar ();
  377. Add (_statusBar);
  378. };
  379. btnRemoveStatusBar.Accepting += (s, e) =>
  380. {
  381. if (_statusBar == null)
  382. {
  383. return;
  384. }
  385. Remove (_statusBar);
  386. _statusBar.Dispose ();
  387. _statusBar = null;
  388. DataContext.Items = [];
  389. _currentStatusItem = null;
  390. _currentSelectedStatusBar = -1;
  391. SetListViewSource (_currentStatusItem, true);
  392. SetFrameDetails ();
  393. };
  394. SetFrameDetails ();
  395. var ustringConverter = new UStringValueConverter ();
  396. var listWrapperConverter = new ListWrapperConverter<DynamicStatusItemList> ();
  397. var lstItems = new Binding (this, "Items", _lstItems, "Source", listWrapperConverter);
  398. void SetFrameDetails (Shortcut statusItem = null)
  399. {
  400. Shortcut newStatusItem;
  401. if (statusItem == null)
  402. {
  403. newStatusItem = DataContext.Items.Count > 0
  404. ? DataContext.Items [_lstItems.SelectedItem].Shortcut
  405. : null;
  406. }
  407. else
  408. {
  409. newStatusItem = statusItem;
  410. }
  411. _currentEditStatusItem = newStatusItem;
  412. frmStatusBarDetails.EditStatusItem (newStatusItem);
  413. bool f = btnOk.Enabled == frmStatusBarDetails.Enabled;
  414. if (!f)
  415. {
  416. btnOk.Enabled = frmStatusBarDetails.Enabled;
  417. btnCancel.Enabled = frmStatusBarDetails.Enabled;
  418. }
  419. }
  420. void SetListViewSource (Shortcut currentStatusItem, bool fill = false)
  421. {
  422. DataContext.Items = [];
  423. Shortcut statusItem = currentStatusItem;
  424. if (!fill)
  425. {
  426. return;
  427. }
  428. if (statusItem != null)
  429. {
  430. foreach (Shortcut si in _statusBar.SubViews.OfType<Shortcut> ())
  431. {
  432. DataContext.Items.Add (new DynamicStatusItemList (si.Title, si));
  433. }
  434. }
  435. }
  436. Shortcut CreateNewStatusBar (DynamicStatusItem item)
  437. {
  438. var newStatusItem = new Shortcut (item.Shortcut, item.Title, frmStatusBarDetails.CreateAction (item));
  439. return newStatusItem;
  440. }
  441. void UpdateStatusItem (
  442. Shortcut currentEditStatusItem,
  443. DynamicStatusItem statusItem,
  444. int index
  445. )
  446. {
  447. _statusBar.SubViews.ElementAt (index).Title = statusItem.Title;
  448. ((Shortcut)_statusBar.SubViews.ElementAt (index)).Action = frmStatusBarDetails.CreateAction (statusItem);
  449. ((Shortcut)_statusBar.SubViews.ElementAt (index)).Key = statusItem.Shortcut;
  450. if (DataContext.Items.Count == 0)
  451. {
  452. DataContext.Items.Add (
  453. new DynamicStatusItemList (
  454. currentEditStatusItem.Title,
  455. currentEditStatusItem
  456. )
  457. );
  458. }
  459. DataContext.Items [index] = new DynamicStatusItemList (
  460. currentEditStatusItem.Title,
  461. currentEditStatusItem
  462. );
  463. SetFrameDetails (currentEditStatusItem);
  464. }
  465. //_frmStatusBarDetails.Initialized += (s, e) => _frmStatusBarDetails.Enabled = false;
  466. }
  467. public DynamicStatusItemModel DataContext { get; set; }
  468. }
  469. public class DynamicStatusItem
  470. {
  471. public string Action { get; set; } = "";
  472. public string Shortcut { get; set; }
  473. public string Title { get; set; } = "New";
  474. }
  475. public class DynamicStatusItemList
  476. {
  477. public DynamicStatusItemList () { }
  478. public DynamicStatusItemList (string title, Shortcut statusItem)
  479. {
  480. Title = title;
  481. Shortcut = statusItem;
  482. }
  483. public Shortcut Shortcut { get; set; }
  484. public string Title { get; set; }
  485. public override string ToString () { return $"{Title}, {Shortcut.Key}"; }
  486. }
  487. public class DynamicStatusItemModel : INotifyPropertyChanged
  488. {
  489. private ObservableCollection<DynamicStatusItemList> _items;
  490. private string _statusBar;
  491. public DynamicStatusItemModel () { Items = []; }
  492. public ObservableCollection<DynamicStatusItemList> Items
  493. {
  494. get => _items;
  495. set
  496. {
  497. if (value == _items)
  498. {
  499. return;
  500. }
  501. _items = value;
  502. PropertyChanged?.Invoke (
  503. this,
  504. new PropertyChangedEventArgs (GetPropertyName ())
  505. );
  506. }
  507. }
  508. public string StatusBar
  509. {
  510. get => _statusBar;
  511. set
  512. {
  513. if (value == _statusBar)
  514. {
  515. return;
  516. }
  517. _statusBar = value;
  518. PropertyChanged?.Invoke (
  519. this,
  520. new PropertyChangedEventArgs (GetPropertyName ())
  521. );
  522. }
  523. }
  524. public event PropertyChangedEventHandler PropertyChanged;
  525. public string GetPropertyName ([CallerMemberName] string propertyName = null) { return propertyName; }
  526. }
  527. public interface IValueConverter
  528. {
  529. object Convert (object value, object parameter = null);
  530. }
  531. public class ListWrapperConverter<T> : IValueConverter
  532. {
  533. public object Convert (object value, object parameter = null) { return new ListWrapper<T> ((ObservableCollection<T>)value); }
  534. }
  535. public class UStringValueConverter : IValueConverter
  536. {
  537. public object Convert (object value, object parameter = null)
  538. {
  539. byte [] data = Encoding.ASCII.GetBytes (value.ToString () ?? string.Empty);
  540. return StringExtensions.ToString (data);
  541. }
  542. }
  543. }