EditorBase.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  39. private readonly ExpanderButton? _expanderButton;
  40. public ExpanderButton? ExpanderButton
  41. {
  42. get => _expanderButton;
  43. init
  44. {
  45. if (_expanderButton == value)
  46. {
  47. return;
  48. }
  49. _expanderButton = value;
  50. }
  51. }
  52. public bool UpdatingSettings { get; private set; } = false;
  53. private void View_LayoutComplete (object? sender, LayoutEventArgs e)
  54. {
  55. UpdatingSettings = true;
  56. OnUpdateSettings ();
  57. UpdatingSettings = false;
  58. }
  59. private View? _viewToEdit;
  60. public View? ViewToEdit
  61. {
  62. get => _viewToEdit;
  63. set
  64. {
  65. if (_viewToEdit == value)
  66. {
  67. return;
  68. }
  69. if (value is null && _viewToEdit is { })
  70. {
  71. _viewToEdit.SubviewsLaidOut -= View_LayoutComplete;
  72. }
  73. _viewToEdit = value;
  74. if (_viewToEdit is { })
  75. {
  76. _viewToEdit.SubviewsLaidOut += View_LayoutComplete;
  77. }
  78. OnViewToEditChanged ();
  79. }
  80. }
  81. protected virtual void OnViewToEditChanged () { }
  82. protected virtual void OnUpdateSettings () { }
  83. /// <summary>
  84. /// Gets or sets whether the DimEditor should automatically select the View to edit
  85. /// based on the values of <see cref="AutoSelectSuperView"/> and <see cref="AutoSelectAdornments"/>.
  86. /// </summary>
  87. public bool AutoSelectViewToEdit { get; set; }
  88. /// <summary>
  89. /// Gets or sets the View that will scope the behavior of <see cref="AutoSelectViewToEdit"/>.
  90. /// </summary>
  91. public View? AutoSelectSuperView { get; set; }
  92. /// <summary>
  93. /// Gets or sets whether auto select with the mouse will select Adornments or just Views.
  94. /// </summary>
  95. public bool AutoSelectAdornments { get; set; }
  96. private void NavigationOnFocusedChanged (object? sender, EventArgs e)
  97. {
  98. if (AutoSelectSuperView is null)
  99. {
  100. return;
  101. }
  102. if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ()))
  103. {
  104. return;
  105. }
  106. if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ()))
  107. {
  108. return;
  109. }
  110. ViewToEdit = Application.Navigation!.GetFocused ();
  111. }
  112. private void ApplicationOnMouseEvent (object? sender, MouseEventArgs e)
  113. {
  114. if (e.Flags != MouseFlags.Button1Clicked || !AutoSelectViewToEdit)
  115. {
  116. return;
  117. }
  118. if ((AutoSelectSuperView is { } && !AutoSelectSuperView.FrameToScreen ().Contains (e.Position))
  119. || FrameToScreen ().Contains (e.Position))
  120. {
  121. return;
  122. }
  123. View? view = e.View;
  124. if (view is null)
  125. {
  126. return;
  127. }
  128. if (view is Adornment adornment)
  129. {
  130. ViewToEdit = AutoSelectAdornments ? adornment : adornment.Parent;
  131. }
  132. else
  133. {
  134. ViewToEdit = view;
  135. }
  136. }
  137. }