#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
using Terminal.Gui;
namespace UICatalog.Scenarios;
public abstract class EditorBase : View
{
protected EditorBase ()
{
Width = Dim.Auto (DimAutoStyle.Content);
Height = Dim.Auto (DimAutoStyle.Content);
CanFocus = true;
ExpanderButton = new ()
{
Orientation = Orientation.Vertical
};
TabStop = TabBehavior.TabStop;
Initialized += OnInitialized;
void OnInitialized (object? sender, EventArgs e)
{
if (Border is { })
{
Border.Add (ExpanderButton);
if (ExpanderButton.Orientation == Orientation.Vertical)
{
ExpanderButton.X = Pos.AnchorEnd () - 1;
}
else
{
ExpanderButton.Y = Pos.AnchorEnd () - 1;
}
}
Application.MouseEvent += ApplicationOnMouseEvent;
Application.Navigation!.FocusedChanged += NavigationOnFocusedChanged;
}
AddCommand (Command.Accept, () => true);
}
private readonly ExpanderButton? _expanderButton;
public ExpanderButton? ExpanderButton
{
get => _expanderButton;
init
{
if (_expanderButton == value)
{
return;
}
_expanderButton = value;
}
}
public bool UpdatingSettings { get; private set; } = false;
private void View_LayoutComplete (object? sender, LayoutEventArgs e)
{
UpdatingSettings = true;
OnUpdateSettings ();
UpdatingSettings = false;
}
private View? _viewToEdit;
public View? ViewToEdit
{
get => _viewToEdit;
set
{
if (_viewToEdit == value)
{
return;
}
if (value is null && _viewToEdit is { })
{
_viewToEdit.SubviewsLaidOut -= View_LayoutComplete;
}
_viewToEdit = value;
if (_viewToEdit is { })
{
_viewToEdit.SubviewsLaidOut += View_LayoutComplete;
}
OnViewToEditChanged ();
}
}
protected virtual void OnViewToEditChanged () { }
protected virtual void OnUpdateSettings () { }
///
/// Gets or sets whether the DimEditor should automatically select the View to edit
/// based on the values of and .
///
public bool AutoSelectViewToEdit { get; set; }
///
/// Gets or sets the View that will scope the behavior of .
///
public View? AutoSelectSuperView { get; set; }
///
/// Gets or sets whether auto select with the mouse will select Adornments or just Views.
///
public bool AutoSelectAdornments { get; set; }
private void NavigationOnFocusedChanged (object? sender, EventArgs e)
{
if (AutoSelectSuperView is null)
{
return;
}
if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ()))
{
return;
}
if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ()))
{
return;
}
ViewToEdit = Application.Navigation!.GetFocused ();
}
private void ApplicationOnMouseEvent (object? sender, MouseEventArgs e)
{
if (e.Flags != MouseFlags.Button1Clicked || !AutoSelectViewToEdit)
{
return;
}
if ((AutoSelectSuperView is { } && !AutoSelectSuperView.FrameToScreen ().Contains (e.Position))
|| FrameToScreen ().Contains (e.Position))
{
return;
}
View? view = e.View;
if (view is null)
{
return;
}
if (view is Adornment adornment)
{
ViewToEdit = AutoSelectAdornments ? adornment : adornment.Parent;
}
else
{
ViewToEdit = view;
}
}
}