StatusItem.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// <see cref="StatusItem"/> objects are contained by <see cref="StatusBar"/> <see cref="View"/>s. Each
  4. /// <see cref="StatusItem"/> has a title, a shortcut (hotkey), and an <see cref="Command"/> that will be invoked when
  5. /// the <see cref="StatusItem.Shortcut"/> is pressed. The <see cref="StatusItem.Shortcut"/> will be a global hotkey for
  6. /// the application in the current context of the screen. The color of the <see cref="StatusItem.Title"/> will be
  7. /// changed after each ~. A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using
  8. /// <see cref="ColorScheme.HotNormal"/> and *Help* as <see cref="ColorScheme.HotNormal"/>.
  9. /// </summary>
  10. public class StatusItem
  11. {
  12. /// <summary>Initializes a new <see cref="StatusItem"/>.</summary>
  13. /// <param name="shortcut">Shortcut to activate the <see cref="StatusItem"/>.</param>
  14. /// <param name="title">Title for the <see cref="StatusItem"/>.</param>
  15. /// <param name="action">Action to invoke when the <see cref="StatusItem"/> is activated.</param>
  16. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  17. public StatusItem (Key shortcut, string title, Action action, Func<bool> canExecute = null)
  18. {
  19. Title = title ?? "";
  20. Shortcut = shortcut;
  21. Action = action;
  22. CanExecute = canExecute;
  23. }
  24. /// <summary>Gets or sets the action to be invoked when the <see cref="StatusItem"/> is triggered</summary>
  25. /// <value>Action to invoke.</value>
  26. public Action Action { get; set; }
  27. /// <summary>
  28. /// Gets or sets the action to be invoked to determine if the <see cref="StatusItem"/> can be triggered. If
  29. /// <see cref="CanExecute"/> returns <see langword="true"/> the status item will be enabled. Otherwise, it will be
  30. /// disabled.
  31. /// </summary>
  32. /// <value>Function to determine if the action is can be executed or not.</value>
  33. public Func<bool> CanExecute { get; set; }
  34. /// <summary>Gets or sets arbitrary data for the status item.</summary>
  35. /// <remarks>This property is not used internally.</remarks>
  36. public object Data { get; set; }
  37. /// <summary>Gets the global shortcut to invoke the action on the menu.</summary>
  38. public Key Shortcut { get; set; }
  39. /// <summary>Gets or sets the title.</summary>
  40. /// <value>The title.</value>
  41. /// <remarks>
  42. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~. A
  43. /// <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  44. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  45. /// </remarks>
  46. public string Title { get; set; }
  47. /// <summary>
  48. /// Returns <see langword="true"/> if the status item is enabled. This method is a wrapper around
  49. /// <see cref="CanExecute"/>.
  50. /// </summary>
  51. public bool IsEnabled () { return CanExecute?.Invoke () ?? true; }
  52. }