2
0

EditorBase.cs 4.1 KB

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