ListView.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. //
  2. // ListView.cs: ListView control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - Should we support multiple columns, if so, how should that be done?
  10. // - Show mark for items that have been marked.
  11. // - Mouse support
  12. // - Scrollbars?
  13. //
  14. // Column considerations:
  15. // - Would need a way to specify widths
  16. // - Should it automatically extract data out of structs/classes based on public fields/properties?
  17. // - It seems that this would be useful just for the "simple" API, not the IListDAtaSource, as that one has full support for it.
  18. // - Should a function be specified that retrieves the individual elements?
  19. //
  20. using System;
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. using NStack;
  26. namespace Terminal.Gui {
  27. /// <summary>
  28. /// Implement <see cref="IListDataSource"/> to provide custom rendering for a <see cref="ListView"/>.
  29. /// </summary>
  30. public interface IListDataSource {
  31. /// <summary>
  32. /// Returns the number of elements to display
  33. /// </summary>
  34. int Count { get; }
  35. /// <summary>
  36. /// Returns the maximum length of elements to display
  37. /// </summary>
  38. int Length { get; }
  39. /// <summary>
  40. /// This method is invoked to render a specified item, the method should cover the entire provided width.
  41. /// </summary>
  42. /// <returns>The render.</returns>
  43. /// <param name="container">The list view to render.</param>
  44. /// <param name="driver">The console driver to render.</param>
  45. /// <param name="selected">Describes whether the item being rendered is currently selected by the user.</param>
  46. /// <param name="item">The index of the item to render, zero for the first item and so on.</param>
  47. /// <param name="col">The column where the rendering will start</param>
  48. /// <param name="line">The line where the rendering will be done.</param>
  49. /// <param name="width">The width that must be filled out.</param>
  50. /// <param name="start">The index of the string to be displayed.</param>
  51. /// <remarks>
  52. /// The default color will be set before this method is invoked, and will be based on whether the item is selected or not.
  53. /// </remarks>
  54. void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0);
  55. /// <summary>
  56. /// Should return whether the specified item is currently marked.
  57. /// </summary>
  58. /// <returns><c>true</c>, if marked, <c>false</c> otherwise.</returns>
  59. /// <param name="item">Item index.</param>
  60. bool IsMarked (int item);
  61. /// <summary>
  62. /// Flags the item as marked.
  63. /// </summary>
  64. /// <param name="item">Item index.</param>
  65. /// <param name="value">If set to <c>true</c> value.</param>
  66. void SetMark (int item, bool value);
  67. /// <summary>
  68. /// Return the source as IList.
  69. /// </summary>
  70. /// <returns></returns>
  71. IList ToList ();
  72. }
  73. /// <summary>
  74. /// ListView <see cref="View"/> renders a scrollable list of data where each item can be activated to perform an action.
  75. /// </summary>
  76. /// <remarks>
  77. /// <para>
  78. /// The <see cref="ListView"/> displays lists of data and allows the user to scroll through the data.
  79. /// Items in the can be activated firing an event (with the ENTER key or a mouse double-click).
  80. /// If the <see cref="AllowsMarking"/> property is true, elements of the list can be marked by the user.
  81. /// </para>
  82. /// <para>
  83. /// By default <see cref="ListView"/> uses <see cref="object.ToString"/> to render the items of any
  84. /// <see cref="IList"/> object (e.g. arrays, <see cref="List{T}"/>,
  85. /// and other collections). Alternatively, an object that implements the <see cref="IListDataSource"/>
  86. /// interface can be provided giving full control of what is rendered.
  87. /// </para>
  88. /// <para>
  89. /// <see cref="ListView"/> can display any object that implements the <see cref="IList"/> interface.
  90. /// <see cref="string"/> values are converted into <see cref="ustring"/> values before rendering, and other values are
  91. /// converted into <see cref="string"/> by calling <see cref="object.ToString"/> and then converting to <see cref="ustring"/> .
  92. /// </para>
  93. /// <para>
  94. /// To change the contents of the ListView, set the <see cref="Source"/> property (when
  95. /// providing custom rendering via <see cref="IListDataSource"/>) or call <see cref="SetSource"/>
  96. /// an <see cref="IList"/> is being used.
  97. /// </para>
  98. /// <para>
  99. /// When <see cref="AllowsMarking"/> is set to true the rendering will prefix the rendered items with
  100. /// [x] or [ ] and bind the SPACE key to toggle the selection. To implement a different
  101. /// marking style set <see cref="AllowsMarking"/> to false and implement custom rendering.
  102. /// </para>
  103. /// </remarks>
  104. public class ListView : View {
  105. int top, left;
  106. int selected;
  107. IListDataSource source;
  108. /// <summary>
  109. /// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ListView"/>, enabling custom rendering.
  110. /// </summary>
  111. /// <value>The source.</value>
  112. /// <remarks>
  113. /// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
  114. /// </remarks>
  115. public IListDataSource Source {
  116. get => source;
  117. set {
  118. source = value;
  119. top = 0;
  120. selected = 0;
  121. lastSelectedItem = -1;
  122. SetNeedsDisplay ();
  123. }
  124. }
  125. /// <summary>
  126. /// Sets the source of the <see cref="ListView"/> to an <see cref="IList"/>.
  127. /// </summary>
  128. /// <value>An object implementing the IList interface.</value>
  129. /// <remarks>
  130. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
  131. /// </remarks>
  132. public void SetSource (IList source)
  133. {
  134. if (source == null)
  135. Source = null;
  136. else {
  137. Source = MakeWrapper (source);
  138. }
  139. }
  140. /// <summary>
  141. /// Sets the source to an <see cref="IList"/> value asynchronously.
  142. /// </summary>
  143. /// <value>An item implementing the IList interface.</value>
  144. /// <remarks>
  145. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custom rendering.
  146. /// </remarks>
  147. public Task SetSourceAsync (IList source)
  148. {
  149. return Task.Factory.StartNew (() => {
  150. if (source == null)
  151. Source = null;
  152. else
  153. Source = MakeWrapper (source);
  154. return source;
  155. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
  156. }
  157. bool allowsMarking;
  158. /// <summary>
  159. /// Gets or sets whether this <see cref="ListView"/> allows items to be marked.
  160. /// </summary>
  161. /// <value><c>true</c> if allows marking elements of the list; otherwise, <c>false</c>.
  162. /// </value>
  163. /// <remarks>
  164. /// If set to true, <see cref="ListView"/> will render items marked items with "[x]", and unmarked items with "[ ]"
  165. /// spaces. SPACE key will toggle marking.
  166. /// </remarks>
  167. public bool AllowsMarking {
  168. get => allowsMarking;
  169. set {
  170. allowsMarking = value;
  171. SetNeedsDisplay ();
  172. }
  173. }
  174. /// <summary>
  175. /// If set to true allows more than one item to be selected. If false only allow one item selected.
  176. /// </summary>
  177. public bool AllowsMultipleSelection {
  178. get => allowsMultipleSelection;
  179. set {
  180. allowsMultipleSelection = value;
  181. if (Source != null && !allowsMultipleSelection) {
  182. // Clear all selections except selected
  183. for (int i = 0; i < Source.Count; i++) {
  184. if (Source.IsMarked (i) && i != selected) {
  185. Source.SetMark (i, false);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// Gets or sets the item that is displayed at the top of the <see cref="ListView"/>.
  193. /// </summary>
  194. /// <value>The top item.</value>
  195. public int TopItem {
  196. get => top;
  197. set {
  198. if (source == null)
  199. return;
  200. if (value < 0 || (source.Count > 0 && value >= source.Count))
  201. throw new ArgumentException ("value");
  202. top = value;
  203. SetNeedsDisplay ();
  204. }
  205. }
  206. /// <summary>
  207. /// Gets or sets the left column where the item start to be displayed at on the <see cref="ListView"/>.
  208. /// </summary>
  209. /// <value>The left position.</value>
  210. public int LeftItem {
  211. get => left;
  212. set {
  213. if (source == null)
  214. return;
  215. if (value < 0 || (Maxlength > 0 && value >= Maxlength))
  216. throw new ArgumentException ("value");
  217. left = value;
  218. SetNeedsDisplay ();
  219. }
  220. }
  221. /// <summary>
  222. /// Gets the widest item.
  223. /// </summary>
  224. public int Maxlength => (source?.Length) ?? 0;
  225. /// <summary>
  226. /// Gets or sets the index of the currently selected item.
  227. /// </summary>
  228. /// <value>The selected item.</value>
  229. public int SelectedItem {
  230. get => selected;
  231. set {
  232. if (source == null || source.Count == 0) {
  233. return;
  234. }
  235. if (value < 0 || value >= source.Count) {
  236. throw new ArgumentException ("value");
  237. }
  238. selected = value;
  239. OnSelectedChanged ();
  240. }
  241. }
  242. static IListDataSource MakeWrapper (IList source)
  243. {
  244. return new ListWrapper (source);
  245. }
  246. /// <summary>
  247. /// Initializes a new instance of <see cref="ListView"/> that will display the contents of the object implementing the <see cref="IList"/> interface,
  248. /// with relative positioning.
  249. /// </summary>
  250. /// <param name="source">An <see cref="IList"/> data source, if the elements are strings or ustrings, the string is rendered, otherwise the ToString() method is invoked on the result.</param>
  251. public ListView (IList source) : this (MakeWrapper (source))
  252. {
  253. }
  254. /// <summary>
  255. /// Initializes a new instance of <see cref="ListView"/> that will display the provided data source, using relative positioning.
  256. /// </summary>
  257. /// <param name="source"><see cref="IListDataSource"/> object that provides a mechanism to render the data.
  258. /// The number of elements on the collection should not change, if you must change, set
  259. /// the "Source" property to reset the internal settings of the ListView.</param>
  260. public ListView (IListDataSource source) : base ()
  261. {
  262. this.source = source;
  263. Initialize ();
  264. }
  265. /// <summary>
  266. /// Initializes a new instance of <see cref="ListView"/>. Set the <see cref="Source"/> property to display something.
  267. /// </summary>
  268. public ListView () : base ()
  269. {
  270. Initialize ();
  271. }
  272. /// <summary>
  273. /// Initializes a new instance of <see cref="ListView"/> that will display the contents of the object implementing the <see cref="IList"/> interface with an absolute position.
  274. /// </summary>
  275. /// <param name="rect">Frame for the listview.</param>
  276. /// <param name="source">An IList data source, if the elements of the IList are strings or ustrings, the string is rendered, otherwise the ToString() method is invoked on the result.</param>
  277. public ListView (Rect rect, IList source) : this (rect, MakeWrapper (source))
  278. {
  279. Initialize ();
  280. }
  281. /// <summary>
  282. /// Initializes a new instance of <see cref="ListView"/> with the provided data source and an absolute position
  283. /// </summary>
  284. /// <param name="rect">Frame for the listview.</param>
  285. /// <param name="source">IListDataSource object that provides a mechanism to render the data. The number of elements on the collection should not change, if you must change, set the "Source" property to reset the internal settings of the ListView.</param>
  286. public ListView (Rect rect, IListDataSource source) : base (rect)
  287. {
  288. this.source = source;
  289. Initialize ();
  290. }
  291. void Initialize ()
  292. {
  293. Source = source;
  294. CanFocus = true;
  295. }
  296. ///<inheritdoc/>
  297. public override void Redraw (Rect bounds)
  298. {
  299. var current = ColorScheme.Focus;
  300. Driver.SetAttribute (current);
  301. Move (0, 0);
  302. var f = Frame;
  303. var item = top;
  304. bool focused = HasFocus;
  305. int col = allowsMarking ? 2 : 0;
  306. int start = left;
  307. for (int row = 0; row < f.Height; row++, item++) {
  308. bool isSelected = item == selected;
  309. var newcolor = focused ? (isSelected ? ColorScheme.Focus : ColorScheme.Normal) : (isSelected ? ColorScheme.HotNormal : ColorScheme.Normal);
  310. if (newcolor != current) {
  311. Driver.SetAttribute (newcolor);
  312. current = newcolor;
  313. }
  314. Move (0, row);
  315. if (source == null || item >= source.Count) {
  316. for (int c = 0; c < f.Width; c++)
  317. Driver.AddRune (' ');
  318. } else {
  319. if (allowsMarking) {
  320. Driver.AddRune (source.IsMarked (item) ? (AllowsMultipleSelection ? Driver.Checked : Driver.Selected) : (AllowsMultipleSelection ? Driver.UnChecked : Driver.UnSelected));
  321. Driver.AddRune (' ');
  322. }
  323. Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
  324. }
  325. }
  326. }
  327. /// <summary>
  328. /// This event is raised when the selected item in the <see cref="ListView"/> has changed.
  329. /// </summary>
  330. public event Action<ListViewItemEventArgs> SelectedItemChanged;
  331. /// <summary>
  332. /// This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.
  333. /// </summary>
  334. public event Action<ListViewItemEventArgs> OpenSelectedItem;
  335. ///<inheritdoc/>
  336. public override bool ProcessKey (KeyEvent kb)
  337. {
  338. if (source == null)
  339. return base.ProcessKey (kb);
  340. switch (kb.Key) {
  341. case Key.CursorUp:
  342. case Key.P | Key.CtrlMask:
  343. return MoveUp ();
  344. case Key.CursorDown:
  345. case Key.N | Key.CtrlMask:
  346. return MoveDown ();
  347. case Key.V | Key.CtrlMask:
  348. case Key.PageDown:
  349. return MovePageDown ();
  350. case Key.PageUp:
  351. return MovePageUp ();
  352. case Key.Space:
  353. if (MarkUnmarkRow ())
  354. return true;
  355. else
  356. break;
  357. case Key.Enter:
  358. return OnOpenSelectedItem ();
  359. case Key.End:
  360. return MoveEnd ();
  361. case Key.Home:
  362. return MoveHome ();
  363. default:
  364. return false;
  365. }
  366. return true;
  367. }
  368. /// <summary>
  369. /// Prevents marking if it's not allowed mark and if it's not allows multiple selection.
  370. /// </summary>
  371. /// <returns></returns>
  372. public virtual bool AllowsAll ()
  373. {
  374. if (!allowsMarking)
  375. return false;
  376. if (!AllowsMultipleSelection) {
  377. for (int i = 0; i < Source.Count; i++) {
  378. if (Source.IsMarked (i) && i != selected) {
  379. Source.SetMark (i, false);
  380. return true;
  381. }
  382. }
  383. }
  384. return true;
  385. }
  386. /// <summary>
  387. /// Marks an unmarked row.
  388. /// </summary>
  389. /// <returns></returns>
  390. public virtual bool MarkUnmarkRow ()
  391. {
  392. if (AllowsAll ()) {
  393. Source.SetMark (SelectedItem, !Source.IsMarked (SelectedItem));
  394. SetNeedsDisplay ();
  395. return true;
  396. }
  397. return false;
  398. }
  399. /// <summary>
  400. /// Moves the selected item index to the next page.
  401. /// </summary>
  402. /// <returns></returns>
  403. public virtual bool MovePageUp ()
  404. {
  405. int n = (selected - Frame.Height);
  406. if (n < 0)
  407. n = 0;
  408. if (n != selected) {
  409. selected = n;
  410. top = selected;
  411. OnSelectedChanged ();
  412. SetNeedsDisplay ();
  413. }
  414. return true;
  415. }
  416. /// <summary>
  417. /// Moves the selected item index to the previous page.
  418. /// </summary>
  419. /// <returns></returns>
  420. public virtual bool MovePageDown ()
  421. {
  422. var n = (selected + Frame.Height);
  423. if (n >= source.Count)
  424. n = source.Count - 1;
  425. if (n != selected) {
  426. selected = n;
  427. if (source.Count >= Frame.Height)
  428. top = selected;
  429. else
  430. top = 0;
  431. OnSelectedChanged ();
  432. SetNeedsDisplay ();
  433. }
  434. return true;
  435. }
  436. /// <summary>
  437. /// Moves the selected item index to the next row.
  438. /// </summary>
  439. /// <returns></returns>
  440. public virtual bool MoveDown ()
  441. {
  442. if (source.Count == 0) {
  443. // Do we set lastSelectedItem to -1 here?
  444. return false; //Nothing for us to move to
  445. }
  446. if (selected >= source.Count) {
  447. // If for some reason we are currently outside of the
  448. // valid values range, we should select the bottommost valid value.
  449. // This can occur if the backing data source changes.
  450. selected = source.Count - 1;
  451. OnSelectedChanged ();
  452. SetNeedsDisplay ();
  453. } else if (selected + 1 < source.Count) { //can move by down by one.
  454. selected++;
  455. if (selected >= top + Frame.Height) {
  456. top++;
  457. } else if (selected < top) {
  458. top = selected;
  459. }
  460. OnSelectedChanged ();
  461. SetNeedsDisplay ();
  462. } else if (selected == 0) {
  463. OnSelectedChanged ();
  464. SetNeedsDisplay ();
  465. }
  466. return true;
  467. }
  468. /// <summary>
  469. /// Moves the selected item index to the previous row.
  470. /// </summary>
  471. /// <returns></returns>
  472. public virtual bool MoveUp ()
  473. {
  474. if (source.Count == 0) {
  475. // Do we set lastSelectedItem to -1 here?
  476. return false; //Nothing for us to move to
  477. }
  478. if (selected >= source.Count) {
  479. // If for some reason we are currently outside of the
  480. // valid values range, we should select the bottommost valid value.
  481. // This can occur if the backing data source changes.
  482. selected = source.Count - 1;
  483. OnSelectedChanged ();
  484. SetNeedsDisplay ();
  485. } else if (selected > 0) {
  486. selected--;
  487. if (selected > Source.Count) {
  488. selected = Source.Count - 1;
  489. }
  490. if (selected < top) {
  491. top = selected;
  492. } else if (selected > top + Frame.Height) {
  493. top = Math.Max (selected - Frame.Height + 1, 0);
  494. }
  495. OnSelectedChanged ();
  496. SetNeedsDisplay ();
  497. }
  498. return true;
  499. }
  500. /// <summary>
  501. /// Moves the selected item index to the last row.
  502. /// </summary>
  503. /// <returns></returns>
  504. public virtual bool MoveEnd ()
  505. {
  506. if (source.Count > 0 && selected != source.Count - 1) {
  507. selected = source.Count - 1;
  508. if (top + selected > Frame.Height - 1) {
  509. top = selected;
  510. }
  511. OnSelectedChanged ();
  512. SetNeedsDisplay ();
  513. }
  514. return true;
  515. }
  516. /// <summary>
  517. /// Moves the selected item index to the first row.
  518. /// </summary>
  519. /// <returns></returns>
  520. public virtual bool MoveHome ()
  521. {
  522. if (selected != 0) {
  523. selected = 0;
  524. top = selected;
  525. OnSelectedChanged ();
  526. SetNeedsDisplay ();
  527. }
  528. return true;
  529. }
  530. /// <summary>
  531. /// Scrolls the view down.
  532. /// </summary>
  533. /// <param name="lines">Number of lines to scroll down.</param>
  534. public virtual void ScrollDown (int lines)
  535. {
  536. top = Math.Max (Math.Min (top + lines, source.Count - 1), 0);
  537. SetNeedsDisplay ();
  538. }
  539. /// <summary>
  540. /// Scrolls the view up.
  541. /// </summary>
  542. /// <param name="lines">Number of lines to scroll up.</param>
  543. public virtual void ScrollUp (int lines)
  544. {
  545. top = Math.Max (top - lines, 0);
  546. SetNeedsDisplay ();
  547. }
  548. /// <summary>
  549. /// Scrolls the view right.
  550. /// </summary>
  551. /// <param name="cols">Number of columns to scroll right.</param>
  552. public virtual void ScrollRight (int cols)
  553. {
  554. left = Math.Max (Math.Min (left + cols, Maxlength - 1), 0);
  555. SetNeedsDisplay ();
  556. }
  557. /// <summary>
  558. /// Scrolls the view left.
  559. /// </summary>
  560. /// <param name="cols">Number of columns to scroll left.</param>
  561. public virtual void ScrollLeft (int cols)
  562. {
  563. left = Math.Max (left - cols, 0);
  564. SetNeedsDisplay ();
  565. }
  566. int lastSelectedItem = -1;
  567. private bool allowsMultipleSelection = true;
  568. /// <summary>
  569. /// Invokes the SelectedChanged event if it is defined.
  570. /// </summary>
  571. /// <returns></returns>
  572. public virtual bool OnSelectedChanged ()
  573. {
  574. if (selected != lastSelectedItem) {
  575. var value = source?.Count > 0 ? source.ToList () [selected] : null;
  576. SelectedItemChanged?.Invoke (new ListViewItemEventArgs (selected, value));
  577. if (HasFocus) {
  578. lastSelectedItem = selected;
  579. }
  580. return true;
  581. }
  582. return false;
  583. }
  584. /// <summary>
  585. /// Invokes the OnOpenSelectedItem event if it is defined.
  586. /// </summary>
  587. /// <returns></returns>
  588. public virtual bool OnOpenSelectedItem ()
  589. {
  590. if (source.Count <= selected || selected < 0 || OpenSelectedItem == null) {
  591. return false;
  592. }
  593. var value = source.ToList () [selected];
  594. OpenSelectedItem?.Invoke (new ListViewItemEventArgs (selected, value));
  595. return true;
  596. }
  597. ///<inheritdoc/>
  598. public override bool OnEnter (View view)
  599. {
  600. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  601. if (lastSelectedItem == -1) {
  602. EnsuresVisibilitySelectedItem ();
  603. OnSelectedChanged ();
  604. return true;
  605. }
  606. return base.OnEnter (view);
  607. }
  608. ///<inheritdoc/>
  609. public override bool OnLeave (View view)
  610. {
  611. if (lastSelectedItem > -1) {
  612. lastSelectedItem = -1;
  613. return true;
  614. }
  615. return false;
  616. }
  617. void EnsuresVisibilitySelectedItem ()
  618. {
  619. SuperView?.LayoutSubviews ();
  620. if (selected < top) {
  621. top = selected;
  622. } else if (Frame.Height > 0 && selected >= top + Frame.Height) {
  623. top = Math.Max (selected - Frame.Height + 2, 0);
  624. }
  625. }
  626. ///<inheritdoc/>
  627. public override void PositionCursor ()
  628. {
  629. if (allowsMarking)
  630. Move (0, selected - top);
  631. else
  632. Move (Bounds.Width - 1, selected - top);
  633. }
  634. ///<inheritdoc/>
  635. public override bool MouseEvent (MouseEvent me)
  636. {
  637. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  638. me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  639. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft)
  640. return false;
  641. if (!HasFocus && CanFocus) {
  642. SetFocus ();
  643. }
  644. if (source == null) {
  645. return false;
  646. }
  647. if (me.Flags == MouseFlags.WheeledDown) {
  648. ScrollDown (1);
  649. return true;
  650. } else if (me.Flags == MouseFlags.WheeledUp) {
  651. ScrollUp (1);
  652. return true;
  653. } else if (me.Flags == MouseFlags.WheeledRight) {
  654. ScrollRight (1);
  655. return true;
  656. } else if (me.Flags == MouseFlags.WheeledLeft) {
  657. ScrollLeft (1);
  658. return true;
  659. }
  660. if (me.Y + top >= source.Count) {
  661. return true;
  662. }
  663. selected = top + me.Y;
  664. if (AllowsAll ()) {
  665. Source.SetMark (SelectedItem, !Source.IsMarked (SelectedItem));
  666. SetNeedsDisplay ();
  667. return true;
  668. }
  669. OnSelectedChanged ();
  670. SetNeedsDisplay ();
  671. if (me.Flags == MouseFlags.Button1DoubleClicked) {
  672. OnOpenSelectedItem ();
  673. }
  674. return true;
  675. }
  676. }
  677. /// <summary>
  678. /// Implements an <see cref="IListDataSource"/> that renders arbitrary <see cref="IList"/> instances for <see cref="ListView"/>.
  679. /// </summary>
  680. /// <remarks>Implements support for rendering marked items.</remarks>
  681. public class ListWrapper : IListDataSource {
  682. IList src;
  683. BitArray marks;
  684. int count, len;
  685. /// <summary>
  686. /// Initializes a new instance of <see cref="ListWrapper"/> given an <see cref="IList"/>
  687. /// </summary>
  688. /// <param name="source"></param>
  689. public ListWrapper (IList source)
  690. {
  691. if (source != null) {
  692. count = source.Count;
  693. marks = new BitArray (count);
  694. src = source;
  695. len = GetMaxLengthItem ();
  696. }
  697. }
  698. /// <summary>
  699. /// Gets the number of items in the <see cref="IList"/>.
  700. /// </summary>
  701. public int Count => src != null ? src.Count : 0;
  702. /// <summary>
  703. /// Gets the maximum item length in the <see cref="IList"/>.
  704. /// </summary>
  705. public int Length => len;
  706. int GetMaxLengthItem ()
  707. {
  708. if (src?.Count == 0) {
  709. return 0;
  710. }
  711. int maxLength = 0;
  712. for (int i = 0; i < src.Count; i++) {
  713. var t = src [i];
  714. int l;
  715. if (t is ustring u) {
  716. l = u.RuneCount;
  717. } else if (t is string s) {
  718. l = s.Length;
  719. } else {
  720. l = t.ToString ().Length;
  721. }
  722. if (l > maxLength) {
  723. maxLength = l;
  724. }
  725. }
  726. return maxLength;
  727. }
  728. void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width, int start = 0)
  729. {
  730. int byteLen = ustr.Length;
  731. int used = 0;
  732. for (int i = start; i < byteLen;) {
  733. (var rune, var size) = Utf8.DecodeRune (ustr, i, i - byteLen);
  734. var count = Rune.ColumnWidth (rune);
  735. if (used + count > width)
  736. break;
  737. driver.AddRune (rune);
  738. used += count;
  739. i += size;
  740. }
  741. for (; used < width; used++) {
  742. driver.AddRune (' ');
  743. }
  744. }
  745. /// <summary>
  746. /// Renders a <see cref="ListView"/> item to the appropriate type.
  747. /// </summary>
  748. /// <param name="container">The ListView.</param>
  749. /// <param name="driver">The driver used by the caller.</param>
  750. /// <param name="marked">Informs if it's marked or not.</param>
  751. /// <param name="item">The item.</param>
  752. /// <param name="col">The col where to move.</param>
  753. /// <param name="line">The line where to move.</param>
  754. /// <param name="width">The item width.</param>
  755. /// <param name="start">The index of the string to be displayed.</param>
  756. public void Render (ListView container, ConsoleDriver driver, bool marked, int item, int col, int line, int width, int start = 0)
  757. {
  758. container.Move (col, line);
  759. var t = src [item];
  760. if (t == null) {
  761. RenderUstr (driver, ustring.Make (""), col, line, width);
  762. } else {
  763. if (t is ustring u) {
  764. RenderUstr (driver, u, col, line, width, start);
  765. } else if (t is string s) {
  766. RenderUstr (driver, s, col, line, width, start);
  767. } else {
  768. RenderUstr (driver, t.ToString (), col, line, width, start);
  769. }
  770. }
  771. }
  772. /// <summary>
  773. /// Returns true if the item is marked, false otherwise.
  774. /// </summary>
  775. /// <param name="item">The item.</param>
  776. /// <returns><c>true</c>If is marked.<c>false</c>otherwise.</returns>
  777. public bool IsMarked (int item)
  778. {
  779. if (item >= 0 && item < count)
  780. return marks [item];
  781. return false;
  782. }
  783. /// <summary>
  784. /// Sets the item as marked or unmarked based on the value is true or false, respectively.
  785. /// </summary>
  786. /// <param name="item">The item</param>
  787. /// <param name="value"><true>Marks the item.</true><false>Unmarked the item.</false>The value.</param>
  788. public void SetMark (int item, bool value)
  789. {
  790. if (item >= 0 && item < count)
  791. marks [item] = value;
  792. }
  793. /// <summary>
  794. /// Returns the source as IList.
  795. /// </summary>
  796. /// <returns></returns>
  797. public IList ToList ()
  798. {
  799. return src;
  800. }
  801. }
  802. /// <summary>
  803. /// <see cref="EventArgs"/> for <see cref="ListView"/> events.
  804. /// </summary>
  805. public class ListViewItemEventArgs : EventArgs {
  806. /// <summary>
  807. /// The index of the <see cref="ListView"/> item.
  808. /// </summary>
  809. public int Item { get; }
  810. /// <summary>
  811. /// The <see cref="ListView"/> item.
  812. /// </summary>
  813. public object Value { get; }
  814. /// <summary>
  815. /// Initializes a new instance of <see cref="ListViewItemEventArgs"/>
  816. /// </summary>
  817. /// <param name="item">The index of the <see cref="ListView"/> item.</param>
  818. /// <param name="value">The <see cref="ListView"/> item</param>
  819. public ListViewItemEventArgs (int item, object value)
  820. {
  821. Item = item;
  822. Value = value;
  823. }
  824. }
  825. }