EditorBase.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #nullable enable
  2. using System;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. public abstract class EditorBase : View
  8. {
  9. protected EditorBase ()
  10. {
  11. Width = Dim.Auto (DimAutoStyle.Content);
  12. Height = Dim.Auto (DimAutoStyle.Content);
  13. CanFocus = true;
  14. ExpanderButton = new ()
  15. {
  16. Orientation = Orientation.Vertical
  17. };
  18. TabStop = TabBehavior.TabStop;
  19. Initialized += OnInitialized;
  20. void OnInitialized (object? sender, EventArgs e)
  21. {
  22. if (Border is { })
  23. {
  24. Border.Add (ExpanderButton);
  25. if (ExpanderButton.Orientation == Orientation.Vertical)
  26. {
  27. ExpanderButton.X = Pos.AnchorEnd () - 1;
  28. }
  29. else
  30. {
  31. ExpanderButton.Y = Pos.AnchorEnd () - 1;
  32. }
  33. }
  34. Application.MouseEvent += ApplicationOnMouseEvent;
  35. Application.Navigation!.FocusedChanged += NavigationOnFocusedChanged;
  36. }
  37. AddCommand (Command.Accept, () => true);
  38. SchemeName = "Dialog";
  39. }
  40. private readonly ExpanderButton? _expanderButton;
  41. public ExpanderButton? ExpanderButton
  42. {
  43. get => _expanderButton;
  44. init
  45. {
  46. if (_expanderButton == value)
  47. {
  48. return;
  49. }
  50. _expanderButton = value;
  51. }
  52. }
  53. public bool UpdatingLayoutSettings { get; private set; } = false;
  54. private void View_LayoutComplete (object? sender, LayoutEventArgs e)
  55. {
  56. UpdatingLayoutSettings = true;
  57. OnUpdateLayoutSettings ();
  58. UpdatingLayoutSettings = false;
  59. }
  60. private View? _viewToEdit;
  61. public View? ViewToEdit
  62. {
  63. get => _viewToEdit;
  64. set
  65. {
  66. if (_viewToEdit == value)
  67. {
  68. return;
  69. }
  70. if (value is null && _viewToEdit is { })
  71. {
  72. _viewToEdit.SubViewsLaidOut -= View_LayoutComplete;
  73. }
  74. _viewToEdit = value;
  75. if (_viewToEdit is { })
  76. {
  77. _viewToEdit.SubViewsLaidOut += View_LayoutComplete;
  78. }
  79. OnViewToEditChanged ();
  80. }
  81. }
  82. protected virtual void OnViewToEditChanged () { }
  83. protected virtual void OnUpdateLayoutSettings () { }
  84. /// <summary>
  85. /// Gets or sets whether the DimEditor should automatically select the View to edit
  86. /// based on the values of <see cref="AutoSelectSuperView"/> and <see cref="AutoSelectAdornments"/>.
  87. /// </summary>
  88. public bool AutoSelectViewToEdit { get; set; }
  89. /// <summary>
  90. /// Gets or sets the View that will scope the behavior of <see cref="AutoSelectViewToEdit"/>.
  91. /// </summary>
  92. public View? AutoSelectSuperView { get; set; }
  93. /// <summary>
  94. /// Gets or sets whether auto select with the mouse will select Adornments or just Views.
  95. /// </summary>
  96. public bool AutoSelectAdornments { get; set; }
  97. private void NavigationOnFocusedChanged (object? sender, EventArgs e)
  98. {
  99. if (AutoSelectSuperView is null)
  100. {
  101. return;
  102. }
  103. if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ()))
  104. {
  105. return;
  106. }
  107. if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ()))
  108. {
  109. return;
  110. }
  111. ViewToEdit = Application.Navigation!.GetFocused ();
  112. }
  113. private void ApplicationOnMouseEvent (object? sender, MouseEventArgs e)
  114. {
  115. if (e.Flags != MouseFlags.Button1Clicked || !AutoSelectViewToEdit)
  116. {
  117. return;
  118. }
  119. if ((AutoSelectSuperView is { } && !AutoSelectSuperView.FrameToScreen ().Contains (e.Position))
  120. || FrameToScreen ().Contains (e.Position))
  121. {
  122. return;
  123. }
  124. View? view = e.View;
  125. if (view is null)
  126. {
  127. return;
  128. }
  129. if (view is Adornment adornment)
  130. {
  131. ViewToEdit = AutoSelectAdornments ? adornment : adornment.Parent;
  132. }
  133. else
  134. {
  135. ViewToEdit = view;
  136. }
  137. }
  138. }