FrameView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Linq;
  2. using System.Text.Json.Serialization;
  3. using NStack;
  4. using static Terminal.Gui.Configuration.ConfigurationManager;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// The FrameView is a container frame that draws a frame around the contents. It is similar to
  8. /// a GroupBox in Windows.
  9. /// </summary>
  10. public class FrameView : View {
  11. //internal class FrameViewConfig : Configuration.Config<FrameViewConfig> {
  12. // /// <summary>
  13. // ///
  14. // /// </summary>
  15. // ///
  16. // [JsonConverter (typeof (JsonStringEnumConverter))]
  17. // public BorderStyle? DefaultBorderStyle { get; set; }
  18. // public override void Apply ()
  19. // {
  20. // if (DefaultBorderStyle.HasValue) {
  21. // FrameView.DefaultBorderStyle = DefaultBorderStyle.Value;
  22. // }
  23. // }
  24. // public override void CopyUpdatedProperitesFrom (FrameViewConfig changedConfig)
  25. // {
  26. // if (changedConfig.DefaultBorderStyle.HasValue) {
  27. // DefaultBorderStyle = changedConfig.DefaultBorderStyle;
  28. // }
  29. // }
  30. // public override void GetHardCodedDefaults ()
  31. // {
  32. // DefaultBorderStyle = FrameView.DefaultBorderStyle;
  33. // }
  34. //}
  35. //[Configuration.ConfigProperty]
  36. //internal static FrameViewConfig Config { get; set; } = new FrameViewConfig ();
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  39. /// </summary>
  40. /// <param name="frame">Frame.</param>
  41. /// <param name="title">Title.</param>
  42. /// <param name="views">Views.</param>
  43. /// <param name="border">The <see cref="Border"/>.</param>
  44. public FrameView (Rect frame, ustring title = null, View [] views = null, Border border = null) : base (frame)
  45. {
  46. Initialize (frame, title, views, border);
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  50. /// </summary>
  51. /// <param name="title">Title.</param>
  52. /// <param name="border">The <see cref="Border"/>.</param>
  53. public FrameView (ustring title, Border border = null)
  54. {
  55. Initialize (Rect.Empty, title, null, border);
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  59. /// </summary>
  60. public FrameView () : this (title: string.Empty) { }
  61. /// <summary>
  62. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  63. /// </summary>
  64. /// <remarks>
  65. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="FrameView"/>s.
  66. /// </remarks>
  67. [SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  68. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  69. void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
  70. {
  71. this.Title = title;
  72. if (border == null) {
  73. Border = new Border () {
  74. BorderStyle = DefaultBorderStyle,
  75. //Title = title
  76. };
  77. } else {
  78. Border = border;
  79. //if (ustring.IsNullOrEmpty (border.Title)) {
  80. // border.Title = title;
  81. //}
  82. }
  83. }
  84. void DrawFrame ()
  85. {
  86. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  87. }
  88. /// <summary>
  89. /// Add the specified <see cref="View"/> to this container.
  90. /// </summary>
  91. /// <param name="view"><see cref="View"/> to add to this container</param>
  92. public override void Add (View view)
  93. {
  94. if (view.CanFocus)
  95. CanFocus = true;
  96. }
  97. /// <summary>
  98. /// Removes a <see cref="View"/> from this container.
  99. /// </summary>
  100. /// <remarks>
  101. /// </remarks>
  102. public override void Remove (View view)
  103. {
  104. if (view == null)
  105. return;
  106. SetNeedsDisplay ();
  107. }
  108. ///<inheritdoc/>
  109. public override bool OnEnter (View view)
  110. {
  111. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  112. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  113. }
  114. return base.OnEnter (view);
  115. }
  116. }
  117. }