Window.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/>
  17. /// positioning.
  18. /// </summary>
  19. public Window ()
  20. {
  21. CanFocus = true;
  22. ColorScheme = Colors.ColorSchemes ["Base"]; // TODO: make this a theme property
  23. BorderStyle = DefaultBorderStyle;
  24. // This enables the default button to be activated by the Enter key.
  25. AddCommand (
  26. Command.Accept,
  27. () =>
  28. {
  29. // TODO: Perhaps all views should support the concept of being default?
  30. // ReSharper disable once InvertIf
  31. if (Subviews.FirstOrDefault (v => v is Button { IsDefault: true, Enabled: true }) is Button
  32. defaultBtn)
  33. {
  34. defaultBtn.InvokeCommand (Command.Accept);
  35. return true;
  36. }
  37. return OnAccept ();
  38. }
  39. );
  40. KeyBindings.Add (Key.Enter, Command.Accept);
  41. }
  42. // TODO: enable this
  43. ///// <summary>
  44. ///// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
  45. ///// </summary>
  46. ///// <remarks>
  47. ///// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
  48. ///// </remarks>
  49. /////[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  50. ////public static ColorScheme DefaultColorScheme { get; set; } = Colors.ColorSchemes ["Base"];
  51. /// <summary>
  52. /// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is
  53. /// <see cref="LineStyle.Single"/>.
  54. /// </summary>
  55. /// <remarks>
  56. /// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>
  57. /// s.
  58. /// </remarks>
  59. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  60. [JsonConverter (typeof (JsonStringEnumConverter))]
  61. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  62. }