FrameView.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. SetInitialProperties (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. SetInitialProperties (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. BorderFrame.Thickness = new Thickness (1);
  62. BorderFrame.BorderStyle = Border.BorderStyle;
  63. BorderFrame.ColorScheme = ColorScheme;
  64. BorderFrame.Data = "BorderFrame";
  65. }
  66. /// <summary>
  67. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  68. /// </summary>
  69. /// <remarks>
  70. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="FrameView"/>s.
  71. /// </remarks>
  72. [SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  73. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  74. void SetInitialProperties (Rect frame, ustring title, View [] views = null, Border border = null)
  75. {
  76. this.Title = title;
  77. if (border == null) {
  78. Border = new Border () {
  79. BorderStyle = DefaultBorderStyle,
  80. //Title = title
  81. };
  82. } else {
  83. Border = border;
  84. //if (ustring.IsNullOrEmpty (border.Title)) {
  85. // border.Title = title;
  86. //}
  87. }
  88. }
  89. public override void BeginInit ()
  90. {
  91. base.BeginInit ();
  92. }
  93. ///<inheritdoc/>
  94. public override bool OnEnter (View view)
  95. {
  96. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  97. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  98. }
  99. return base.OnEnter (view);
  100. }
  101. }
  102. }