Window.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped;
  28. ColorScheme = Colors.ColorSchemes ["Base"]; // TODO: make this a theme property
  29. BorderStyle = DefaultBorderStyle;
  30. ShadowStyle = DefaultShadow;
  31. // This enables the default button to be activated by the Enter key.
  32. AddCommand (
  33. Command.Accept,
  34. () =>
  35. {
  36. // TODO: Perhaps all views should support the concept of being default?
  37. // ReSharper disable once InvertIf
  38. if (Subviews.FirstOrDefault (v => v is Button { IsDefault: true, Enabled: true }) is Button
  39. defaultBtn)
  40. {
  41. defaultBtn.InvokeCommand (Command.Accept);
  42. return true;
  43. }
  44. return OnAccept ();
  45. }
  46. );
  47. KeyBindings.Add (Key.Enter, Command.Accept);
  48. }
  49. // TODO: enable this
  50. ///// <summary>
  51. ///// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
  52. ///// </summary>
  53. ///// <remarks>
  54. ///// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
  55. ///// </remarks>
  56. /////[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  57. ////public static ColorScheme DefaultColorScheme { get; set; } = Colors.ColorSchemes ["Base"];
  58. /// <summary>
  59. /// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is
  60. /// <see cref="LineStyle.Single"/>.
  61. /// </summary>
  62. /// <remarks>
  63. /// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>
  64. /// s.
  65. /// </remarks>
  66. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  67. [JsonConverter (typeof (JsonStringEnumConverter<LineStyle>))]
  68. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  69. }