Window.cs 2.6 KB

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