123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- #nullable enable
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- namespace Terminal.Gui;
- public partial class View // SuperView/SubView hierarchy management (SuperView, SubViews, Add, Remove, etc.)
- {
- [SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
- private static readonly IReadOnlyCollection<View> _empty = [];
- private readonly List<View>? _subviews = [];
- // Internally, we use InternalSubViews rather than subviews, as we do not expect us
- // to make the same mistakes our users make when they poke at the SubViews.
- internal IList<View> InternalSubViews => _subviews ?? [];
- /// <summary>Gets the list of SubViews.</summary>
- /// <remarks>
- /// Use <see cref="Add(Terminal.Gui.View?)"/> and <see cref="Remove(Terminal.Gui.View?)"/> to add or remove subviews.
- /// </remarks>
- public IReadOnlyCollection<View> SubViews => InternalSubViews?.AsReadOnly () ?? _empty;
- private View? _superView;
- /// <summary>
- /// Gets this Views SuperView (the View's container), or <see langword="null"/> if this view has not been added as a
- /// SubView.
- /// </summary>
- /// <seealso cref="OnSuperViewChanged"/>
- /// <seealso cref="SuperViewChanged"/>
- public View? SuperView
- {
- get => _superView!;
- private set => SetSuperView (value);
- }
- private void SetSuperView (View? value)
- {
- if (_superView == value)
- {
- return;
- }
- _superView = value;
- RaiseSuperViewChanged ();
- }
- private void RaiseSuperViewChanged ()
- {
- SuperViewChangedEventArgs args = new (SuperView, this);
- OnSuperViewChanged (args);
- SuperViewChanged?.Invoke (this, args);
- }
- /// <summary>
- /// Called when the SuperView of this View has changed.
- /// </summary>
- /// <param name="e"></param>
- protected virtual void OnSuperViewChanged (SuperViewChangedEventArgs e) { }
- /// <summary>Raised when the SuperView of this View has changed.</summary>
- public event EventHandler<SuperViewChangedEventArgs>? SuperViewChanged;
- #region AddRemove
- /// <summary>Adds a SubView (child) to this view.</summary>
- /// <remarks>
- /// <para>
- /// The Views that have been added to this view can be retrieved via the <see cref="SubViews"/> property.
- /// </para>
- /// <para>
- /// To check if a View has been added to this View, compare it's <see cref="SuperView"/> property to this View.
- /// </para>
- /// <para>
- /// SubViews will be disposed when this View is disposed. In other-words, calling this method causes
- /// the lifecycle of the subviews to be transferred to this View.
- /// </para>
- /// <para>
- /// Calls/Raises the <see cref="OnSubViewAdded"/>/<see cref="SubViewAdded"/> event.
- /// </para>
- /// <para>
- /// The <see cref="OnSuperViewChanged"/>/<see cref="SuperViewChanged"/> event will be raised on the added View.
- /// </para>
- /// </remarks>
- /// <param name="view">The view to add.</param>
- /// <returns>The view that was added.</returns>
- /// <seealso cref="Remove(View)"/>
- /// <seealso cref="RemoveAll"/>
- /// <seealso cref="OnSubViewAdded"/>
- /// <seealso cref="SubViewAdded"/>
- public virtual View? Add (View? view)
- {
- if (view is null)
- {
- return null;
- }
- //Debug.Assert (view.SuperView is null, $"{view} already has a SuperView: {view.SuperView}.");
- if (view.SuperView is {})
- {
- Logging.Warning ($"{view} already has a SuperView: {view.SuperView}.");
- }
- //Debug.Assert (!InternalSubViews.Contains (view), $"{view} has already been Added to {this}.");
- if (InternalSubViews.Contains (view))
- {
- Logging.Warning ($"{view} has already been Added to {this}.");
- }
- // TileView likes to add views that were previously added and have HasFocus = true. No bueno.
- view.HasFocus = false;
- // TODO: Make this thread safe
- InternalSubViews.Add (view);
- view.SuperView = this;
- if (view is { Enabled: true, Visible: true, CanFocus: true })
- {
- // Add will cause the newly added subview to gain focus if it's focusable
- if (HasFocus)
- {
- view.SetFocus ();
- }
- }
- if (view.Enabled && !Enabled)
- {
- view.Enabled = false;
- }
- // Raise event indicating a subview has been added
- // We do this before Init.
- RaiseSubViewAdded (view);
- if (IsInitialized && !view.IsInitialized)
- {
- view.BeginInit ();
- view.EndInit ();
- }
- SetNeedsDraw ();
- SetNeedsLayout ();
- return view;
- }
- /// <summary>Adds the specified SubView (children) to the view.</summary>
- /// <param name="views">Array of one or more views (can be optional parameter).</param>
- /// <remarks>
- /// <para>
- /// The Views that have been added to this view can be retrieved via the <see cref="SubViews"/> property. See also
- /// <seealso cref="Remove(View)"/> and <seealso cref="RemoveAll"/>.
- /// </para>
- /// <para>
- /// SubViews will be disposed when this View is disposed. In other-words, calling this method causes
- /// the lifecycle of the subviews to be transferred to this View.
- /// </para>
- /// </remarks>
- public void Add (params View []? views)
- {
- if (views is null)
- {
- return;
- }
- foreach (View view in views)
- {
- Add (view);
- }
- }
- internal void RaiseSubViewAdded (View view)
- {
- OnSubViewAdded (view);
- SubViewAdded?.Invoke (this, new (this, view));
- }
- /// <summary>
- /// Called when a SubView has been added to this View.
- /// </summary>
- /// <remarks>
- /// If the SubView has not been initialized, this happens before BeginInit/EndInit is called.
- /// </remarks>
- /// <param name="view"></param>
- protected virtual void OnSubViewAdded (View view) { }
- /// <summary>Raised when a SubView has been added to this View.</summary>
- /// <remarks>
- /// If the SubView has not been initialized, this happens before BeginInit/EndInit is called.
- /// </remarks>
- public event EventHandler<SuperViewChangedEventArgs>? SubViewAdded;
- /// <summary>Removes a SubView added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
- /// <remarks>
- /// <para>
- /// Normally SubViews will be disposed when this View is disposed. Removing a SubView causes ownership of the
- /// SubView's
- /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose()"/>.
- /// </para>
- /// <para>
- /// Calls/Raises the <see cref="OnSubViewRemoved"/>/<see cref="SubViewRemoved"/> event.
- /// </para>
- /// <para>
- /// The <see cref="OnSuperViewChanged"/>/<see cref="SuperViewChanged"/> event will be raised on the removed View.
- /// </para>
- /// </remarks>
- /// <returns>
- /// The removed View. <see langword="null"/> if the View could not be removed.
- /// </returns>
- /// <seealso cref="OnSubViewRemoved"/>
- /// <seealso cref="SubViewRemoved"/>"/>
- public virtual View? Remove (View? view)
- {
- if (view is null)
- {
- return null;
- }
- if (InternalSubViews.Count == 0)
- {
- return view;
- }
- if (view.SuperView is null)
- {
- Logging.Warning ($"{view} cannot be Removed. SuperView is null.");
- }
- if (view.SuperView != this)
- {
- Logging.Warning ($"{view} cannot be Removed. SuperView is not this ({view.SuperView}.");
- }
- if (!InternalSubViews.Contains (view))
- {
- Logging.Warning ($"{view} cannot be Removed. It has not been added to {this}.");
- }
- Rectangle touched = view.Frame;
- bool hadFocus = view.HasFocus;
- bool couldFocus = view.CanFocus;
- if (hadFocus)
- {
- view.CanFocus = false; // If view had focus, this will ensure it doesn't and it stays that way
- }
- Debug.Assert (!view.HasFocus);
- InternalSubViews.Remove (view);
- // Clean up focus stuff
- _previouslyFocused = null;
- if (view.SuperView is { } && view.SuperView._previouslyFocused == this)
- {
- view.SuperView._previouslyFocused = null;
- }
- view.SuperView = null;
- SetNeedsLayout ();
- SetNeedsDraw ();
- foreach (View v in InternalSubViews)
- {
- if (v.Frame.IntersectsWith (touched))
- {
- view.SetNeedsDraw ();
- }
- }
- view.CanFocus = couldFocus; // Restore to previous value
- if (_previouslyFocused == view)
- {
- _previouslyFocused = null;
- }
- RaiseSubViewRemoved (view);
- return view;
- }
- internal void RaiseSubViewRemoved (View view)
- {
- OnSubViewRemoved (view);
- SubViewRemoved?.Invoke (this, new (this, view));
- }
- /// <summary>
- /// Called when a SubView has been removed from this View.
- /// </summary>
- /// <param name="view"></param>
- protected virtual void OnSubViewRemoved (View view) { }
- /// <summary>Raised when a SubView has been added to this View.</summary>
- public event EventHandler<SuperViewChangedEventArgs>? SubViewRemoved;
- /// <summary>
- /// Removes all SubView (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Normally SubViews will be disposed when this View is disposed. Removing a SubView causes ownership of the
- /// SubView's
- /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose()"/> on any Views that were
- /// added.
- /// </para>
- /// </remarks>
- public virtual void RemoveAll ()
- {
- while (InternalSubViews.Count > 0)
- {
- Remove (InternalSubViews [0]);
- }
- }
- #pragma warning disable CS0067 // The event is never used
- /// <summary>Raised when a SubView has been removed from this View.</summary>
- public event EventHandler<SuperViewChangedEventArgs>? Removed;
- #pragma warning restore CS0067 // The event is never used
- #endregion AddRemove
- // TODO: This drives a weird coupling of Application.Top and View. It's not clear why this is needed.
- /// <summary>Get the top superview of a given <see cref="View"/>.</summary>
- /// <returns>The superview view.</returns>
- internal View? GetTopSuperView (View? view = null, View? superview = null)
- {
- View? top = superview ?? Application.Top;
- for (View? v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView)
- {
- top = v;
- if (top == superview)
- {
- break;
- }
- }
- return top;
- }
- /// <summary>
- /// Gets whether <paramref name="view"/> is in the SubView hierarchy of <paramref name="start"/>.
- /// </summary>
- /// <param name="start">The View at the start of the hierarchy.</param>
- /// <param name="view">The View to test.</param>
- /// <param name="includeAdornments">Will search the subview hierarchy of the adornments if true.</param>
- /// <returns></returns>
- public static bool IsInHierarchy (View? start, View? view, bool includeAdornments = false)
- {
- if (view is null || start is null)
- {
- return false;
- }
- if (view == start)
- {
- return true;
- }
- foreach (View subView in start.InternalSubViews)
- {
- if (view == subView)
- {
- return true;
- }
- bool found = IsInHierarchy (subView, view, includeAdornments);
- if (found)
- {
- return found;
- }
- }
- if (includeAdornments)
- {
- bool found = IsInHierarchy (start.Padding, view, includeAdornments);
- if (found)
- {
- return found;
- }
- found = IsInHierarchy (start.Border, view, includeAdornments);
- if (found)
- {
- return found;
- }
- found = IsInHierarchy (start.Margin, view, includeAdornments);
- if (found)
- {
- return found;
- }
- }
- return false;
- }
- #region SubViewOrdering
- /// <summary>
- /// Moves <paramref name="subview"/> one position towards the end of the <see cref="SubViews"/> list.
- /// </summary>
- /// <param name="subview">The subview to move.</param>
- public void MoveSubViewTowardsEnd (View subview)
- {
- PerformActionForSubView (
- subview,
- x =>
- {
- int idx = InternalSubViews!.IndexOf (x);
- if (idx + 1 < InternalSubViews.Count)
- {
- InternalSubViews.Remove (x);
- InternalSubViews.Insert (idx + 1, x);
- }
- }
- );
- }
- /// <summary>
- /// Moves <paramref name="subview"/> to the end of the <see cref="SubViews"/> list.
- /// </summary>
- /// <param name="subview">The subview to move.</param>
- public void MoveSubViewToEnd (View subview)
- {
- PerformActionForSubView (
- subview,
- x =>
- {
- InternalSubViews!.Remove (x);
- InternalSubViews.Add (x);
- }
- );
- }
- /// <summary>
- /// Moves <paramref name="subview"/> one position towards the start of the <see cref="SubViews"/> list.
- /// </summary>
- /// <param name="subview">The subview to move.</param>
- public void MoveSubViewTowardsStart (View subview)
- {
- PerformActionForSubView (
- subview,
- x =>
- {
- int idx = InternalSubViews!.IndexOf (x);
- if (idx > 0)
- {
- InternalSubViews.Remove (x);
- InternalSubViews.Insert (idx - 1, x);
- }
- }
- );
- }
- /// <summary>
- /// Moves <paramref name="subview"/> to the start of the <see cref="SubViews"/> list.
- /// </summary>
- /// <param name="subview">The subview to move.</param>
- public void MoveSubViewToStart (View subview)
- {
- PerformActionForSubView (
- subview,
- x =>
- {
- InternalSubViews!.Remove (x);
- InternalSubViews.Insert (0, subview);
- }
- );
- }
- /// <summary>
- /// Internal API that runs <paramref name="action"/> on a subview if it is part of the <see cref="SubViews"/> list.
- /// </summary>
- /// <param name="subview"></param>
- /// <param name="action"></param>
- private void PerformActionForSubView (View subview, Action<View> action)
- {
- if (InternalSubViews.Contains (subview))
- {
- action (subview);
- }
- // BUGBUG: this is odd. Why is this needed?
- SetNeedsDraw ();
- subview.SetNeedsDraw ();
- }
- #endregion SubViewOrdering
- }
|