ICollectionNavigator.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Navigates a collection of items using keystrokes. The keystrokes are used to build a search string. The
  5. /// <see cref="SearchString"/> is used to find the next item in the collection that matches the search string when
  6. /// <see cref="GetNextMatchingItem(int, char)"/> is called.
  7. /// <para>
  8. /// If the user types keystrokes that can't be found in the collection, the search string is cleared and the next
  9. /// item is found that starts with the last keystroke.
  10. /// </para>
  11. /// <para>If the user pauses keystrokes for a short time (see <see cref="TypingDelay"/>), the search string is cleared.</para>
  12. /// </summary>
  13. public interface ICollectionNavigator
  14. {
  15. /// <summary>
  16. /// Gets or sets the number of milliseconds to delay before clearing the search string. The delay is reset on each
  17. /// call to <see cref="GetNextMatchingItem(int, char)"/>. The default is 500ms.
  18. /// </summary>
  19. public int TypingDelay { get; set; }
  20. /// <summary>This event is invoked when <see cref="SearchString"/> changes. Useful for debugging.</summary>
  21. public event EventHandler<KeystrokeNavigatorEventArgs>? SearchStringChanged;
  22. /// <summary>
  23. /// Gets the current search string. This includes the set of keystrokes that have been pressed since the last
  24. /// unsuccessful match or after <see cref="TypingDelay"/>) milliseconds. Useful for debugging.
  25. /// </summary>
  26. string SearchString { get; }
  27. /// <summary>
  28. /// Class responsible for deciding whether given entries in the collection match
  29. /// the search term the user is typing.
  30. /// </summary>
  31. ICollectionNavigatorMatcher Matcher { get; set; }
  32. /// <summary>
  33. /// Gets the index of the next item in the collection that matches the current <see cref="SearchString"/> plus the
  34. /// provided character (typically from a key press).
  35. /// </summary>
  36. /// <param name="currentIndex">The index in the collection to start the search from.</param>
  37. /// <param name="keyStruck">The character of the key the user pressed.</param>
  38. /// <returns>
  39. /// The index of the item that matches what the user has typed. Returns <see langword="-1"/> if no item in the
  40. /// collection matched.
  41. /// </returns>
  42. int GetNextMatchingItem (int currentIndex, char keyStruck);
  43. }