2
0

FrameView.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /// <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 SetInitialProperties (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. public override void BeginInit ()
  85. {
  86. base.BeginInit ();
  87. BorderFrame.Thickness = new Thickness (2);
  88. BorderFrame.BorderStyle = Border.BorderStyle;
  89. BorderFrame.ColorScheme = ColorScheme;
  90. BorderFrame.Data = "BorderFrame";
  91. }
  92. ///<inheritdoc/>
  93. public override bool OnEnter (View view)
  94. {
  95. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  96. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  97. }
  98. return base.OnEnter (view);
  99. }
  100. }
  101. }