#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 = "")] private static readonly IList _empty = new List (0).AsReadOnly (); private List? _subviews; // This is null, and allocated on demand. // 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 InternalSubviews => _subviews ?? _empty; /// This returns a list of the subviews contained by this view. /// The subviews. public IList Subviews => _subviews?.AsReadOnly () ?? _empty; private View? _superView; /// Returns the container for this view, or null if this view has not been added to a container. /// The super view. public virtual View? SuperView { get => _superView!; set => throw new NotImplementedException (); } #region AddRemove /// Indicates whether the view was added to . public bool IsAdded { get; private set; } /// Adds a subview (child) to this view. /// /// /// The Views that have been added to this view can be retrieved via the property. See also /// /// /// /// 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. /// /// /// The view to add. /// The view that was added. public virtual View? Add (View? view) { if (view is null) { return null; } if (_subviews is null) { _subviews = []; } Debug.WriteLineIf (_subviews.Contains (view), $"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; _subviews.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; } OnAdded (new (this, view)); if (IsInitialized && !view.IsInitialized) { view.BeginInit (); view.EndInit (); } SetNeedsDraw (); SetNeedsLayout (); return view; } /// Adds the specified views (children) to the view. /// Array of one or more views (can be optional parameter). /// /// /// The Views that have been added to this view can be retrieved via the property. See also /// and . /// /// /// 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. /// /// public void Add (params View []? views) { if (views is null) { return; } foreach (View view in views) { Add (view); } } /// Event fired when this view is added to another. public event EventHandler? Added; /// Method invoked when a subview is being added to this view. /// Event where is the subview being added. public virtual void OnAdded (SuperViewChangedEventArgs e) { View view = e.SubView; view.IsAdded = true; view.Added?.Invoke (this, e); } /// Method invoked when a subview is being removed from this view. /// Event args describing the subview being removed. public virtual void OnRemoved (SuperViewChangedEventArgs e) { View view = e.SubView; view.IsAdded = false; view.Removed?.Invoke (this, e); } /// Removes a subview added via or from this View. /// /// /// 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 . /// /// /// /// The removed View. if the View could not be removed. /// public virtual View? Remove (View? view) { if (view is null) { return null; } if (_subviews is null) { return view; } 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); _subviews.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 _subviews) { if (v.Frame.IntersectsWith (touched)) { view.SetNeedsDraw (); } } view.CanFocus = couldFocus; // Restore to previous value if (_previouslyFocused == view) { _previouslyFocused = null; } OnRemoved (new (this, view)); return view; } /// /// Removes all subviews (children) added via or from this View. /// /// /// /// 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 on any Views that were /// added. /// /// public virtual void RemoveAll () { if (_subviews is null) { return; } while (_subviews.Count > 0) { Remove (_subviews [0]); } } /// Event fired when this view is removed from another. public event EventHandler? Removed; #endregion AddRemove // TODO: Mark as internal. Or nuke. /// Get the top superview of a given . /// The superview view. public 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; } /// /// Gets whether is in the Subview hierarchy of . /// /// The View at the start of the hierarchy. /// The View to test. /// Will search the subview hierarchy of the adornments if true. /// 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.Subviews) { 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 /// /// Moves one position towards the end of the list. /// /// The subview to move. public void MoveSubviewTowardsEnd (View subview) { PerformActionForSubview ( subview, x => { int idx = _subviews!.IndexOf (x); if (idx + 1 < _subviews.Count) { _subviews.Remove (x); _subviews.Insert (idx + 1, x); } } ); } /// /// Moves to the end of the list. /// /// The subview to move. public void MoveSubviewToEnd (View subview) { PerformActionForSubview ( subview, x => { _subviews!.Remove (x); _subviews.Add (x); } ); } /// /// Moves one position towards the start of the list. /// /// The subview to move. public void MoveSubviewTowardsStart (View subview) { PerformActionForSubview ( subview, x => { int idx = _subviews!.IndexOf (x); if (idx > 0) { _subviews.Remove (x); _subviews.Insert (idx - 1, x); } } ); } /// /// Moves to the start of the list. /// /// The subview to move. public void MoveSubviewToStart (View subview) { PerformActionForSubview ( subview, x => { _subviews!.Remove (x); _subviews.Insert (0, subview); } ); } /// /// Internal API that runs on a subview if it is part of the list. /// /// /// private void PerformActionForSubview (View subview, Action action) { if (_subviews!.Contains (subview)) { action (subview); } // BUGBUG: this is odd. Why is this needed? SetNeedsDraw (); subview.SetNeedsDraw (); } #endregion SubViewOrdering }