Tile.cs 3.5 KB

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