Tile.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// A single <see cref="ContentView"/> presented in a <see cref="TileView"/>. To create new instances use
  6. /// <see cref="TileView.RebuildForTileCount(int)"/> or <see cref="TileView.InsertTile(int)"/>.
  7. /// </summary>
  8. public class Tile
  9. {
  10. private string _title = string.Empty;
  11. /// <summary>Creates a new instance of the <see cref="Tile"/> class.</summary>
  12. public Tile ()
  13. {
  14. ContentView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  15. #if DEBUG_IDISPOSABLE
  16. ContentView.Data = "Tile.ContentView";
  17. #endif
  18. Title = string.Empty;
  19. MinSize = 0;
  20. }
  21. /// <summary>
  22. /// The <see cref="ContentView"/> that is contained in this <see cref="TileView"/>. Add new child views to this
  23. /// member for multiple <see cref="ContentView"/>s within the <see cref="Tile"/>.
  24. /// </summary>
  25. public View ContentView { get; internal set; }
  26. /// <summary>
  27. /// Gets or Sets the minimum size you to allow when splitter resizing along parent
  28. /// <see cref="TileView.Orientation"/> direction.
  29. /// </summary>
  30. public int MinSize { get; set; }
  31. /// <summary>
  32. /// The text that should be displayed above the <see cref="ContentView"/>. This will appear over the splitter line
  33. /// or border (above the view client area).
  34. /// </summary>
  35. /// <remarks>Title are not rendered for root level tiles <see cref="Gui.LineStyle"/> is <see cref="LineStyle.None"/>.</remarks>
  36. public string Title
  37. {
  38. get => _title;
  39. set
  40. {
  41. if (!OnTitleChanging (_title, value))
  42. {
  43. string old = _title;
  44. _title = value;
  45. OnTitleChanged (old, _title);
  46. return;
  47. }
  48. _title = value;
  49. }
  50. }
  51. /// <summary>Called when the <see cref="Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
  52. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  53. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  54. public virtual void OnTitleChanged (string oldTitle, string newTitle)
  55. {
  56. var args = new EventArgs<string> (in newTitle);
  57. TitleChanged?.Invoke (this, args);
  58. }
  59. /// <summary>
  60. /// Called before the <see cref="Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be
  61. /// cancelled.
  62. /// </summary>
  63. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  64. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  65. /// <returns><c>true</c> if an event handler cancelled the Title change.</returns>
  66. public virtual bool OnTitleChanging (string oldTitle, string newTitle)
  67. {
  68. var args = new CancelEventArgs<string> (ref oldTitle, ref newTitle);
  69. TitleChanging?.Invoke (this, args);
  70. return args.Cancel;
  71. }
  72. /// <summary>Event fired after the <see cref="Title"/> has been changed.</summary>
  73. public event EventHandler? TitleChanged;
  74. /// <summary>
  75. /// Event fired when the <see cref="Title"/> is changing.
  76. /// <see cref="CancelEventArgs.Cancel"/> can be set to <c>true</c> to cancel the change.
  77. /// </summary>
  78. public event EventHandler<CancelEventArgs<string>>? TitleChanging;
  79. }