Window.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// A <see cref="Toplevel"/> <see cref="View"/> with <see cref="View.BorderStyle"/> set to
  5. /// <see cref="LineStyle.Single"/>. Provides a container for other views.
  6. /// </summary>
  7. /// <remarks>
  8. /// <para>
  9. /// If any subview is a button and the <see cref="Button.IsDefault"/> property is set to true, the Enter key will
  10. /// invoke the <see cref="Command.Accept"/> command on that subview.
  11. /// </para>
  12. /// </remarks>
  13. public class Window : Toplevel
  14. {
  15. /// <summary>
  16. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  17. /// </summary>
  18. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  19. public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="Window"/> class.
  22. /// </summary>
  23. public Window ()
  24. {
  25. CanFocus = true;
  26. TabStop = TabBehavior.TabGroup;
  27. ColorScheme = Colors.ColorSchemes ["Base"]; // TODO: make this a theme property
  28. BorderStyle = DefaultBorderStyle;
  29. ShadowStyle = DefaultShadow;
  30. // This enables the default button to be activated by the Enter key.
  31. AddCommand (
  32. Command.Accept,
  33. () =>
  34. {
  35. // TODO: Perhaps all views should support the concept of being default?
  36. // ReSharper disable once InvertIf
  37. if (Subviews.FirstOrDefault (v => v is Button { IsDefault: true, Enabled: true }) is Button
  38. defaultBtn)
  39. {
  40. defaultBtn.InvokeCommand (Command.Accept);
  41. return true;
  42. }
  43. return OnAccept ();
  44. }
  45. );
  46. KeyBindings.Add (Key.Enter, Command.Accept);
  47. }
  48. // TODO: enable this
  49. ///// <summary>
  50. ///// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
  51. ///// </summary>
  52. ///// <remarks>
  53. ///// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
  54. ///// </remarks>
  55. /////[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  56. ////public static ColorScheme DefaultColorScheme { get; set; } = Colors.ColorSchemes ["Base"];
  57. /// <summary>
  58. /// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is
  59. /// <see cref="LineStyle.Single"/>.
  60. /// </summary>
  61. /// <remarks>
  62. /// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>
  63. /// s.
  64. /// </remarks>
  65. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  66. [JsonConverter (typeof (JsonStringEnumConverter<LineStyle>))]
  67. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  68. }