123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #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 () { }
- /// <summary>
- /// Gets or sets whether the DimEditor should automatically select the View to edit
- /// based on the values of <see cref="AutoSelectSuperView"/> and <see cref="AutoSelectAdornments"/>.
- /// </summary>
- public bool AutoSelectViewToEdit { get; set; }
- /// <summary>
- /// Gets or sets the View that will scope the behavior of <see cref="AutoSelectViewToEdit"/>.
- /// </summary>
- public View? AutoSelectSuperView { get; set; }
- /// <summary>
- /// Gets or sets whether auto select with the mouse will select Adornments or just Views.
- /// </summary>
- 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;
- }
- }
- }
|