using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; namespace Terminal.Gui { /// /// Determines the LayoutStyle for a , if Absolute, during , the /// value from the will be used, if the value is Computed, then /// will be updated from the X, Y objects and the Width and Height objects. /// public enum LayoutStyle { /// /// The position and size of the view are based . /// Absolute, /// /// The position and size of the view will be computed based on /// , , , and . will /// provide the absolute computed values. /// Computed } public partial class View { // The frame for the object. Relative to the SuperView's Bounds. Rect _frame; /// /// Gets or sets the frame for the view. The frame is relative to the 's . /// /// The frame. /// /// /// Change the Frame when using the layout style to move or resize views. /// /// /// Altering the Frame of a view will trigger the redrawing of the /// view as well as the redrawing of the affected regions of the . /// /// public virtual Rect Frame { get => _frame; set { _frame = new Rect (value.X, value.Y, Math.Max (value.Width, 0), Math.Max (value.Height, 0)); if (IsInitialized || LayoutStyle == LayoutStyle.Absolute) { LayoutFrames (); TextFormatter.Size = GetTextFormatterSizeNeededForTextAndHotKey (); SetNeedsLayout (); SetNeedsDisplay (); } } } /// /// The frame (specified as a ) that separates a View from other SubViews of the same SuperView. /// The margin offsets the from the . /// /// /// /// The frames (, , and ) are not part of the View's content /// and are not clipped by the View's Clip Area. /// /// /// Changing the size of a frame (, , or ) /// will change the size of the and trigger to update the layout of the /// and its . /// /// public Frame Margin { get; private set; } /// /// The frame (specified as a ) inside of the view that offsets the from the . /// The Border provides the space for a visual border (drawn using line-drawing glyphs) and the Title. /// The Border expands inward; in other words if `Border.Thickness.Top == 2` the border and /// title will take up the first row and the second row will be filled with spaces. /// /// /// /// provides a simple helper for turning a simple border frame on or off. /// /// /// The frames (, , and ) are not part of the View's content /// and are not clipped by the View's Clip Area. /// /// /// Changing the size of a frame (, , or ) /// will change the size of the and trigger to update the layout of the /// and its . /// /// public Frame Border { get; private set; } /// /// Gets or sets whether the view has a one row/col thick border. /// /// /// /// This is a helper for manipulating the view's . Setting this property to any value other than /// is equivalent to setting 's /// to `1` and to the value. /// /// /// Setting this property to is equivalent to setting 's /// to `0` and to . /// /// /// For more advanced customization of the view's border, manipulate see directly. /// /// public LineStyle BorderStyle { get { return Border?.BorderStyle ?? LineStyle.None; } set { if (Border == null) { throw new InvalidOperationException ("Border is null; this is likely a bug."); } if (value != LineStyle.None) { Border.Thickness = new Thickness (1); } else { Border.Thickness = new Thickness (0); } Border.BorderStyle = value; LayoutFrames (); SetNeedsLayout (); } } /// /// The frame (specified as a ) inside of the view that offsets the from the . /// /// /// /// The frames (, , and ) are not part of the View's content /// and are not clipped by the View's Clip Area. /// /// /// Changing the size of a frame (, , or ) /// will change the size of the and trigger to update the layout of the /// and its . /// /// public Frame Padding { get; private set; } /// /// Helper to get the total thickness of the , , and . /// /// A thickness that describes the sum of the Frames' thicknesses. public Thickness GetFramesThickness () { var left = Margin.Thickness.Left + Border.Thickness.Left + Padding.Thickness.Left; var top = Margin.Thickness.Top + Border.Thickness.Top + Padding.Thickness.Top; var right = Margin.Thickness.Right + Border.Thickness.Right + Padding.Thickness.Right; var bottom = Margin.Thickness.Bottom + Border.Thickness.Bottom + Padding.Thickness.Bottom; return new Thickness (left, top, right, bottom); } /// /// Helper to get the X and Y offset of the Bounds from the Frame. This is the sum of the Left and Top properties of /// , and . /// public Point GetBoundsOffset () => new Point (Padding?.Thickness.GetInside (Padding.Frame).X ?? 0, Padding?.Thickness.GetInside (Padding.Frame).Y ?? 0); /// /// Creates the view's objects. This internal method is overridden by Frame to do nothing /// to prevent recursion during View construction. /// internal virtual void CreateFrames () { void ThicknessChangedHandler (object sender, EventArgs e) { LayoutFrames (); SetNeedsLayout (); SetNeedsDisplay (); } if (Margin != null) { Margin.ThicknessChanged -= ThicknessChangedHandler; Margin.Dispose (); } Margin = new Frame () { Id = "Margin", Thickness = new Thickness (0) }; Margin.ThicknessChanged += ThicknessChangedHandler; Margin.Parent = this; if (Border != null) { Border.ThicknessChanged -= ThicknessChangedHandler; Border.Dispose (); } Border = new Frame () { Id = "Border", Thickness = new Thickness (0) }; Border.ThicknessChanged += ThicknessChangedHandler; Border.Parent = this; // TODO: Create View.AddAdornment if (Padding != null) { Padding.ThicknessChanged -= ThicknessChangedHandler; Padding.Dispose (); } Padding = new Frame () { Id = "Padding", Thickness = new Thickness (0) }; Padding.ThicknessChanged += ThicknessChangedHandler; Padding.Parent = this; } LayoutStyle _layoutStyle; /// /// Controls how the View's is computed during . If the style is set to /// , /// LayoutSubviews does not change the . If the style is /// the is updated using /// the , , , and properties. /// /// The layout style. public LayoutStyle LayoutStyle { get => _layoutStyle; set { _layoutStyle = value; SetNeedsLayout (); } } /// /// The view's content area. /// /// SubViews are positioned relative to Bounds. /// /// /// Drawing is clipped to Bounds ( clips drawing to Bounds.Size). /// /// /// Mouse events are reported relative to Bounds. /// /// /// The view's content area. /// /// /// The of Bounds is always (0, 0). To obtain the offset of the Bounds from the Frame use /// . /// /// /// When using , Bounds is not valid until after the view has been initialized (after has been called and /// has fired). Accessing this property before the view is initialized is considered an error./> /// /// public virtual Rect Bounds { get { #if DEBUG if (LayoutStyle == LayoutStyle.Computed && !IsInitialized) { Debug.WriteLine ($"WARNING: Bounds is being accessed before the View has been initialized. This is likely a bug. View: {this}"); Debug.WriteLine ($"The Frame is set before the View has been initialized. So it isn't a bug.Is by design."); } #endif // DEBUG //var frameRelativeBounds = Padding?.Thickness.GetInside (Padding.Frame) ?? new Rect (default, Frame.Size); var frameRelativeBounds = FrameGetInsideBounds (); return new Rect (default, frameRelativeBounds.Size); } set { // BUGBUG: Margin etc.. can be null (if typeof(Frame)) Frame = new Rect (Frame.Location, new Size ( value.Size.Width + Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal, value.Size.Height + Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical ) ); } } private Rect FrameGetInsideBounds () { if (Margin == null || Border == null || Padding == null) { return new Rect (default, Frame.Size); } var width = Math.Max (0, Frame.Size.Width - Margin.Thickness.Horizontal - Border.Thickness.Horizontal - Padding.Thickness.Horizontal); var height = Math.Max (0, Frame.Size.Height - Margin.Thickness.Vertical - Border.Thickness.Vertical - Padding.Thickness.Vertical); return new Rect (Point.Empty, new Size (width, height)); } // Diagnostics to highlight when X or Y is read before the view has been initialized Pos VerifyIsInitialized (Pos pos) { #if DEBUG if (LayoutStyle == LayoutStyle.Computed && (!IsInitialized)) { Debug.WriteLine ($"WARNING: \"{this}\" has not been initialized; position is indeterminate {pos}. This is likely a bug."); } #endif // DEBUG return pos; } // Diagnostics to highlight when Width or Height is read before the view has been initialized Dim VerifyIsInitialized (Dim dim) { #if DEBUG if (LayoutStyle == LayoutStyle.Computed && (!IsInitialized)) { Debug.WriteLine ($"WARNING: \"{this}\" has not been initialized; dimension is indeterminate: {dim}. This is likely a bug."); } #endif // DEBUG return dim; } Pos _x, _y; /// /// Gets or sets the X position for the view (the column). Only used if the is . /// /// The X Position. /// /// If is changing this property has no effect and its value is indeterminate. /// public Pos X { get => VerifyIsInitialized (_x); set { if (ForceValidatePosDim && !ValidatePosDim (_x, value)) { throw new ArgumentException (); } _x = value; OnResizeNeeded (); } } /// /// Gets or sets the Y position for the view (the row). Only used if the is . /// /// The y position (line). /// /// If is changing this property has no effect and its value is indeterminate. /// public Pos Y { get => VerifyIsInitialized (_y); set { if (ForceValidatePosDim && !ValidatePosDim (_y, value)) { throw new ArgumentException (); } _y = value; OnResizeNeeded (); } } Dim _width, _height; /// /// Gets or sets the width of the view. Only used the is . /// /// The width. /// /// If is changing this property has no effect and its value is indeterminate. /// public Dim Width { get => VerifyIsInitialized (_width); set { if (ForceValidatePosDim && !ValidatePosDim (_width, value)) { throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Width)); } _width = value; if (ForceValidatePosDim) { var isValidNewAutSize = AutoSize && IsValidAutoSizeWidth (_width); if (IsAdded && AutoSize && !isValidNewAutSize) { throw new InvalidOperationException ("Must set AutoSize to false before set the Width."); } } OnResizeNeeded (); } } /// /// Gets or sets the height of the view. Only used the is . /// /// The height. /// If is changing this property has no effect and its value is indeterminate. public Dim Height { get => VerifyIsInitialized (_height); set { if (ForceValidatePosDim && !ValidatePosDim (_height, value)) { throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Height)); } _height = value; if (ForceValidatePosDim) { var isValidNewAutSize = AutoSize && IsValidAutoSizeHeight (_height); if (IsAdded && AutoSize && !isValidNewAutSize) { throw new InvalidOperationException ("Must set AutoSize to false before set the Height."); } } OnResizeNeeded (); } } /// /// Forces validation with layout /// to avoid breaking the and settings. /// public bool ForceValidatePosDim { get; set; } bool ValidatePosDim (object oldValue, object newValue) { if (!IsInitialized || _layoutStyle == LayoutStyle.Absolute || oldValue == null || oldValue.GetType () == newValue.GetType () || this is Toplevel) { return true; } if (_layoutStyle == LayoutStyle.Computed) { if (oldValue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) { return true; } } return false; } // BUGBUG: This API is broken - should not assume Frame.Height == Bounds.Height // BUGBUG: this function does not belong in ViewLayout.cs - it should be in ViewText.cs /// /// Gets the minimum dimensions required to fit the View's , factoring in . /// /// The minimum dimensions required. /// if the dimensions fit within the View's , otherwise. /// /// Always returns if is or /// if (Horizontal) or (Vertical) are not not set or zero. /// Does not take into account word wrapping. /// bool GetMinimumBoundsForFrame (out Size size) { if (!IsInitialized) { size = new Size (0, 0); return false; } size = Bounds.Size; if (!AutoSize && !string.IsNullOrEmpty (TextFormatter.Text)) { switch (TextFormatter.IsVerticalDirection (TextDirection)) { case true: var colWidth = TextFormatter.GetSumMaxCharWidth (new List { TextFormatter.Text }, 0, 1); // TODO: v2 - This uses frame.Width; it should only use Bounds if (_frame.Width < colWidth && (Width == null || (Bounds.Width >= 0 && Width is Dim.DimAbsolute && Width.Anchor (0) >= 0 && Width.Anchor (0) < colWidth))) { size = new Size (colWidth, Bounds.Height); return true; } break; default: if (_frame.Height < 1 && (Height == null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0))) { size = new Size (Bounds.Width, 1); return true; } break; } } return false; } // BUGBUG: this function does not belong in ViewLayout.cs - it should be in ViewText.cs /// /// Sets the size of the View to the minimum width or height required to fit (see . /// /// if the size was changed, if /// will not fit. bool SetBoundsToFitFrame () { if (GetMinimumBoundsForFrame (out Size size)) { _frame = new Rect (_frame.Location, size); return true; } return false; } /// /// Called whenever the view needs to be resized. Sets and /// triggers a call. /// /// /// Can be overridden if the view resize behavior is different than the default. /// protected virtual void OnResizeNeeded () { var actX = _x is Pos.PosAbsolute ? _x.Anchor (0) : _frame.X; var actY = _y is Pos.PosAbsolute ? _y.Anchor (0) : _frame.Y; if (AutoSize) { //if (TextAlignment == TextAlignment.Justified) { // throw new InvalidOperationException ("TextAlignment.Justified cannot be used with AutoSize"); //} var s = GetAutoSize (); var w = _width is Dim.DimAbsolute && _width.Anchor (0) > s.Width ? _width.Anchor (0) : s.Width; var h = _height is Dim.DimAbsolute && _height.Anchor (0) > s.Height ? _height.Anchor (0) : s.Height; _frame = new Rect (new Point (actX, actY), new Size (w, h)); // Set frame, not Frame! } else { var w = _width is Dim.DimAbsolute ? _width.Anchor (0) : _frame.Width; var h = _height is Dim.DimAbsolute ? _height.Anchor (0) : _frame.Height; // BUGBUG: v2 - ? - If layoutstyle is absolute, this overwrites the current frame h/w with 0. Hmmm... // This is needed for DimAbsolute values by setting the frame before LayoutSubViews. _frame = new Rect (new Point (actX, actY), new Size (w, h)); // Set frame, not Frame! } //// BUGBUG: I think these calls are redundant or should be moved into just the AutoSize case if (IsInitialized || LayoutStyle == LayoutStyle.Absolute) { SetBoundsToFitFrame (); LayoutFrames (); TextFormatter.Size = GetTextFormatterSizeNeededForTextAndHotKey (); SetNeedsLayout (); SetNeedsDisplay (); } } internal bool LayoutNeeded { get; private set; } = true; internal void SetNeedsLayout () { if (LayoutNeeded) { return; } LayoutNeeded = true; foreach (var view in Subviews) { view.SetNeedsLayout (); } TextFormatter.NeedsFormat = true; SuperView?.SetNeedsLayout (); } /// /// Removes the setting on this view. /// protected void ClearLayoutNeeded () { LayoutNeeded = false; } /// /// Converts a screen-relative coordinate to a Frame-relative coordinate. Frame-relative means /// relative to the View's 's . /// /// The coordinate relative to the 's . /// Screen-relative column. /// Screen-relative row. public Point ScreenToFrame (int x, int y) { Point superViewBoundsOffset = SuperView?.GetBoundsOffset () ?? Point.Empty; var ret = new Point (x - Frame.X - superViewBoundsOffset.X, y - Frame.Y - superViewBoundsOffset.Y); if (SuperView != null) { var superFrame = SuperView.ScreenToFrame (x - superViewBoundsOffset.X, y - superViewBoundsOffset.Y); ret = new Point (superFrame.X - Frame.X, superFrame.Y - Frame.Y); } return ret; } /// /// Converts a screen-relative coordinate to a bounds-relative coordinate. /// /// The coordinate relative to this view's . /// Screen-relative column. /// Screen-relative row. public Point ScreenToBounds (int x, int y) { var screen = ScreenToFrame (x, y); var boundsOffset = GetBoundsOffset (); return new Point (screen.X - boundsOffset.X, screen.Y - boundsOffset.Y); } /// /// Converts a -relative coordinate to a screen-relative coordinate. The output is optionally clamped to the screen dimensions. /// /// -relative column. /// -relative row. /// Absolute column; screen-relative. /// Absolute row; screen-relative. /// If , and will be clamped to the /// screen dimensions (will never be negative and will always be less than and /// , respectively. public virtual void BoundsToScreen (int x, int y, out int rx, out int ry, bool clamped = true) { var boundsOffset = GetBoundsOffset (); rx = x + Frame.X + boundsOffset.X; ry = y + Frame.Y + boundsOffset.Y; var super = SuperView; while (super != null) { boundsOffset = super.GetBoundsOffset (); rx += super.Frame.X + boundsOffset.X; ry += super.Frame.Y + boundsOffset.Y; super = super.SuperView; } // The following ensures that the cursor is always in the screen boundaries. if (clamped) { ry = Math.Min (ry, Driver.Rows - 1); rx = Math.Min (rx, Driver.Cols - 1); } } /// /// Converts a -relative region to a screen-relative region. /// public Rect BoundsToScreen (Rect region) { BoundsToScreen (region.X, region.Y, out var x, out var y, clamped: false); return new Rect (x, y, region.Width, region.Height); } /// /// Gets the with a screen-relative location. /// /// The location and size of the view in screen-relative coordinates. public virtual Rect FrameToScreen () { var ret = Frame; var super = SuperView; while (super != null) { var boundsOffset = super.GetBoundsOffset (); ret.X += super.Frame.X + boundsOffset.X; ret.Y += super.Frame.Y + boundsOffset.Y; super = super.SuperView; } return ret; } /// /// Sets the View's to the frame-relative coordinates if its container. The /// container size and location are specified by and are relative to the /// View's superview. /// /// The SuperView-relative rectangle describing View's container (nominally the /// same as this.SuperView.Frame). internal void SetRelativeLayout (Rect superviewFrame) { int newX, newW, newY, newH; var autosize = Size.Empty; if (AutoSize) { // Note this is global to this function and used as such within the local functions defined // below. In v2 AutoSize will be re-factored to not need to be dealt with in this function. autosize = GetAutoSize (); } // Returns the new dimension (width or height) and location (x or y) for the View given // the superview's Frame.X or Frame.Y // the superview's width or height // the current Pos (View.X or View.Y) // the current Dim (View.Width or View.Height) (int newLocation, int newDimension) GetNewLocationAndDimension (int superviewLocation, int superviewDimension, Pos pos, Dim dim, int autosizeDimension) { int newDimension, newLocation; switch (pos) { case Pos.PosCenter: if (dim == null) { newDimension = AutoSize ? autosizeDimension : superviewDimension; } else { newDimension = dim.Anchor (superviewDimension); newDimension = AutoSize && autosizeDimension > newDimension ? autosizeDimension : newDimension; } newLocation = pos.Anchor (superviewDimension - newDimension); break; case Pos.PosCombine combine: int left, right; (left, newDimension) = GetNewLocationAndDimension (superviewLocation, superviewDimension, combine.left, dim, autosizeDimension); (right, newDimension) = GetNewLocationAndDimension (superviewLocation, superviewDimension, combine.right, dim, autosizeDimension); if (combine.add) { newLocation = left + right; } else { newLocation = left - right; } newDimension = Math.Max (CalculateNewDimension (dim, newLocation, superviewDimension, autosizeDimension), 0); break; case Pos.PosAbsolute: case Pos.PosAnchorEnd: case Pos.PosFactor: case Pos.PosFunc: case Pos.PosView: default: newLocation = pos?.Anchor (superviewDimension) ?? 0; newDimension = Math.Max (CalculateNewDimension (dim, newLocation, superviewDimension, autosizeDimension), 0); break; } return (newLocation, newDimension); } // Recursively calculates the new dimension (width or height) of the given Dim given: // the current location (x or y) // the current dimension (width or height) int CalculateNewDimension (Dim d, int location, int dimension, int autosize) { int newDimension; switch (d) { case null: newDimension = AutoSize ? autosize : dimension; break; case Dim.DimCombine combine: int leftNewDim = CalculateNewDimension (combine.left, location, dimension, autosize); int rightNewDim = CalculateNewDimension (combine.right, location, dimension, autosize); if (combine.add) { newDimension = leftNewDim + rightNewDim; } else { newDimension = leftNewDim - rightNewDim; } newDimension = AutoSize && autosize > newDimension ? autosize : newDimension; break; case Dim.DimFactor factor when !factor.IsFromRemaining (): newDimension = d.Anchor (dimension); newDimension = AutoSize && autosize > newDimension ? autosize : newDimension; break; case Dim.DimFill: default: newDimension = Math.Max (d.Anchor (dimension - location), 0); newDimension = AutoSize && autosize > newDimension ? autosize : newDimension; break; } return newDimension; } // horizontal (newX, newW) = GetNewLocationAndDimension (superviewFrame.X, superviewFrame.Width, _x, _width, autosize.Width); // vertical (newY, newH) = GetNewLocationAndDimension (superviewFrame.Y, superviewFrame.Height, _y, _height, autosize.Height); var r = new Rect (newX, newY, newW, newH); if (Frame != r) { Frame = r; // BUGBUG: Why is this AFTER setting Frame? Seems duplicative. if (!SetBoundsToFitFrame ()) { TextFormatter.Size = GetTextFormatterSizeNeededForTextAndHotKey (); } } } /// /// Fired after the View's method has completed. /// /// /// Subscribe to this event to perform tasks when the has been resized or the layout has otherwise changed. /// public event EventHandler LayoutStarted; /// /// Raises the event. Called from before any subviews have been laid out. /// internal virtual void OnLayoutStarted (LayoutEventArgs args) { LayoutStarted?.Invoke (this, args); } /// /// Fired after the View's method has completed. /// /// /// Subscribe to this event to perform tasks when the has been resized or the layout has otherwise changed. /// public event EventHandler LayoutComplete; /// /// Event called only once when the is being initialized for the first time. /// Allows configurations and assignments to be performed before the being shown. /// This derived from to allow notify all the views that are being initialized. /// public event EventHandler Initialized; /// /// Raises the event. Called from before all sub-views have been laid out. /// internal virtual void OnLayoutComplete (LayoutEventArgs args) { LayoutComplete?.Invoke (this, args); } internal void CollectPos (Pos pos, View from, ref HashSet nNodes, ref HashSet<(View, View)> nEdges) { switch (pos) { case Pos.PosView pv: // See #2461 //if (!from.InternalSubviews.Contains (pv.Target)) { // throw new InvalidOperationException ($"View {pv.Target} is not a subview of {from}"); //} if (pv.Target != this) { nEdges.Add ((pv.Target, from)); } return; case Pos.PosCombine pc: CollectPos (pc.left, from, ref nNodes, ref nEdges); CollectPos (pc.right, from, ref nNodes, ref nEdges); break; } } internal void CollectDim (Dim dim, View from, ref HashSet nNodes, ref HashSet<(View, View)> nEdges) { switch (dim) { case Dim.DimView dv: // See #2461 //if (!from.InternalSubviews.Contains (dv.Target)) { // throw new InvalidOperationException ($"View {dv.Target} is not a subview of {from}"); //} if (dv.Target != this) { nEdges.Add ((dv.Target, from)); } return; case Dim.DimCombine dc: CollectDim (dc.left, from, ref nNodes, ref nEdges); CollectDim (dc.right, from, ref nNodes, ref nEdges); break; } } internal void CollectAll (View from, ref HashSet nNodes, ref HashSet<(View, View)> nEdges) { foreach (var v in from.InternalSubviews) { nNodes.Add (v); if (v._layoutStyle != LayoutStyle.Computed) { continue; } CollectPos (v.X, v, ref nNodes, ref nEdges); CollectPos (v.Y, v, ref nNodes, ref nEdges); CollectDim (v.Width, v, ref nNodes, ref nEdges); CollectDim (v.Height, v, ref nNodes, ref nEdges); } } // https://en.wikipedia.org/wiki/Topological_sorting internal static List TopologicalSort (View superView, IEnumerable nodes, ICollection<(View From, View To)> edges) { var result = new List (); // Set of all nodes with no incoming edges var noEdgeNodes = new HashSet (nodes.Where (n => edges.All (e => !e.To.Equals (n)))); while (noEdgeNodes.Any ()) { // remove a node n from S var n = noEdgeNodes.First (); noEdgeNodes.Remove (n); // add n to tail of L if (n != superView) result.Add (n); // for each node m with an edge e from n to m do foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) { var m = e.To; // remove edge e from the graph edges.Remove (e); // if m has no other incoming edges then if (edges.All (me => !me.To.Equals (m)) && m != superView) { // insert m into S noEdgeNodes.Add (m); } } } if (edges.Any ()) { foreach ((var from, var to) in edges) { if (from == to) { // if not yet added to the result, add it and remove from edge if (result.Find (v => v == from) == null) { result.Add (from); } edges.Remove ((from, to)); } else if (from.SuperView == to.SuperView) { // if 'from' is not yet added to the result, add it if (result.Find (v => v == from) == null) { result.Add (from); } // if 'to' is not yet added to the result, add it if (result.Find (v => v == to) == null) { result.Add (to); } // remove from edge edges.Remove ((from, to)); } else if (from != superView?.GetTopSuperView (to, from) && !ReferenceEquals (from, to)) { if (ReferenceEquals (from.SuperView, to)) { throw new InvalidOperationException ($"ComputedLayout for \"{superView}\": \"{to}\" references a SubView (\"{from}\")."); } else { throw new InvalidOperationException ($"ComputedLayout for \"{superView}\": \"{from}\" linked with \"{to}\" was not found. Did you forget to add it to {superView}?"); } } } } // return L (a topologically sorted order) return result; } // TopologicalSort /// /// Overriden by to do nothing, as the does not have frames. /// internal virtual void LayoutFrames () { if (Margin == null) return; // CreateFrames() has not been called yet if (Margin.Frame.Size != Frame.Size) { Margin._frame = new Rect (Point.Empty, Frame.Size); Margin.X = 0; Margin.Y = 0; Margin.Width = Frame.Size.Width; Margin.Height = Frame.Size.Height; Margin.SetNeedsLayout (); Margin.LayoutSubviews (); Margin.SetNeedsDisplay (); } var border = Margin.Thickness.GetInside (Margin.Frame); if (border != Border.Frame) { Border._frame = new Rect (new Point (border.Location.X, border.Location.Y), border.Size); Border.X = border.Location.X; Border.Y = border.Location.Y; Border.Width = border.Size.Width; Border.Height = border.Size.Height; Border.SetNeedsLayout (); Border.LayoutSubviews (); Border.SetNeedsDisplay (); } var padding = Border.Thickness.GetInside (Border.Frame); if (padding != Padding.Frame) { Padding._frame = new Rect (new Point (padding.Location.X, padding.Location.Y), padding.Size); Padding.X = padding.Location.X; Padding.Y = padding.Location.Y; Padding.Width = padding.Size.Width; Padding.Height = padding.Size.Height; Padding.SetNeedsLayout (); Padding.LayoutSubviews (); Padding.SetNeedsDisplay (); } } /// /// Invoked when a view starts executing or when the dimensions of the view have changed, for example in /// response to the container view or terminal resizing. /// /// /// Calls (which raises the event) before it returns. /// public virtual void LayoutSubviews () { if (!LayoutNeeded) { return; } LayoutFrames (); var oldBounds = Bounds; OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds }); TextFormatter.Size = GetTextFormatterSizeNeededForTextAndHotKey (); // Sort out the dependencies of the X, Y, Width, Height properties var nodes = new HashSet (); var edges = new HashSet<(View, View)> (); CollectAll (this, ref nodes, ref edges); var ordered = View.TopologicalSort (SuperView, nodes, edges); foreach (var v in ordered) { LayoutSubview (v, new Rect (GetBoundsOffset (), Bounds.Size)); } // If the 'to' is rooted to 'from' and the layoutstyle is Computed it's a special-case. // Use LayoutSubview with the Frame of the 'from' if (SuperView != null && GetTopSuperView () != null && LayoutNeeded && edges.Count > 0) { foreach ((var from, var to) in edges) { LayoutSubview (to, from.Frame); } } LayoutNeeded = false; OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds }); } private void LayoutSubview (View v, Rect contentArea) { if (v.LayoutStyle == LayoutStyle.Computed) { v.SetRelativeLayout (contentArea); } v.LayoutSubviews (); v.LayoutNeeded = false; } bool _autoSize; /// /// Gets or sets a flag that determines whether the View will be automatically resized to fit the /// within /// /// The default is . Set to to turn on AutoSize. If then /// and will be used if can fit; /// if won't fit the view will be resized as needed. /// /// /// In addition, if is the new values of and /// must be of the same types of the existing one to avoid breaking the settings. /// /// public virtual bool AutoSize { get => _autoSize; set { var v = ResizeView (value); TextFormatter.AutoSize = v; if (_autoSize != v) { _autoSize = v; TextFormatter.NeedsFormat = true; UpdateTextFormatterText (); OnResizeNeeded (); } } } bool ResizeView (bool autoSize) { if (!autoSize) { return false; } var boundsChanged = true; var newFrameSize = GetAutoSize (); if (IsInitialized && newFrameSize != Frame.Size) { if (ForceValidatePosDim) { // BUGBUG: This ain't right, obviously. We need to figure out how to handle this. boundsChanged = ResizeBoundsToFit (newFrameSize); } else { Height = newFrameSize.Height; Width = newFrameSize.Width; } } // BUGBUG: This call may be redundant TextFormatter.Size = GetTextFormatterSizeNeededForTextAndHotKey (); return boundsChanged; } /// /// Resizes the View to fit the specified size. Factors in the HotKey. /// /// /// whether the Bounds was changed or not bool ResizeBoundsToFit (Size size) { var boundsChanged = false; var canSizeW = TrySetWidth (size.Width - GetHotKeySpecifierLength (), out var rW); var canSizeH = TrySetHeight (size.Height - GetHotKeySpecifierLength (false), out var rH); if (canSizeW) { boundsChanged = true; _width = rW; } if (canSizeH) { boundsChanged = true; _height = rH; } if (boundsChanged) { Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height); } return boundsChanged; } /// /// Gets the Frame dimensions required to fit within using the text specified by the /// property and accounting for any characters. /// /// The of the view required to fit the text. public Size GetAutoSize () { int x = 0; int y = 0; if (IsInitialized) { x = Bounds.X; y = Bounds.Y; } var rect = TextFormatter.CalcRect (x, y,TextFormatter.Text, TextFormatter.Direction); var newWidth = rect.Size.Width - GetHotKeySpecifierLength () + Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal; var newHeight = rect.Size.Height - GetHotKeySpecifierLength (false) + Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical; return new Size (newWidth, newHeight); } bool IsValidAutoSize (out Size autoSize) { var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection); autoSize = new Size (rect.Size.Width - GetHotKeySpecifierLength (), rect.Size.Height - GetHotKeySpecifierLength (false)); return !(ForceValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)) || _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength () || _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false)); } bool IsValidAutoSizeWidth (Dim width) { var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection); var dimValue = width.Anchor (0); return !(ForceValidatePosDim && (!(width is Dim.DimAbsolute)) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ()); } bool IsValidAutoSizeHeight (Dim height) { var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection); var dimValue = height.Anchor (0); return !(ForceValidatePosDim && (!(height is Dim.DimAbsolute)) || dimValue != rect.Size.Height - GetHotKeySpecifierLength (false)); } /// /// Determines if the View's can be set to a new value. /// /// /// Contains the width that would result if were set to "/> /// if the View's can be changed to the specified value. False otherwise. internal bool TrySetWidth (int desiredWidth, out int resultWidth) { var w = desiredWidth; bool canSetWidth; switch (Width) { case Dim.DimCombine _: case Dim.DimView _: case Dim.DimFill _: // It's a Dim.DimCombine and so can't be assigned. Let it have it's Width anchored. w = Width.Anchor (w); canSetWidth = !ForceValidatePosDim; break; case Dim.DimFactor factor: // Tries to get the SuperView Width otherwise the view Width. var sw = SuperView != null ? SuperView.Frame.Width : w; if (factor.IsFromRemaining ()) { sw -= Frame.X; } w = Width.Anchor (sw); canSetWidth = !ForceValidatePosDim; break; default: canSetWidth = true; break; } resultWidth = w; return canSetWidth; } /// /// Determines if the View's can be set to a new value. /// /// /// Contains the width that would result if were set to "/> /// if the View's can be changed to the specified value. False otherwise. internal bool TrySetHeight (int desiredHeight, out int resultHeight) { var h = desiredHeight; bool canSetHeight; switch (Height) { case Dim.DimCombine _: case Dim.DimView _: case Dim.DimFill _: // It's a Dim.DimCombine and so can't be assigned. Let it have it's height anchored. h = Height.Anchor (h); canSetHeight = !ForceValidatePosDim; break; case Dim.DimFactor factor: // Tries to get the SuperView height otherwise the view height. var sh = SuperView != null ? SuperView.Frame.Height : h; if (factor.IsFromRemaining ()) { sh -= Frame.Y; } h = Height.Anchor (sh); canSetHeight = !ForceValidatePosDim; break; default: canSetHeight = true; break; } resultHeight = h; return canSetHeight; } /// /// Finds which view that belong to the superview at the provided location. /// /// The superview where to look for. /// The column location in the superview. /// The row location in the superview. /// The found view screen relative column location. /// The found view screen relative row location. /// /// The view that was found at the and coordinates. /// if no view was found. /// public static View FindDeepestView (View start, int x, int y, out int resx, out int resy) { resy = resx = 0; if (start == null || !start.Frame.Contains (x, y)) { return null; } var startFrame = start.Frame; if (start.InternalSubviews != null) { int count = start.InternalSubviews.Count; if (count > 0) { var boundsOffset = start.GetBoundsOffset (); var rx = x - (startFrame.X + boundsOffset.X); var ry = y - (startFrame.Y + boundsOffset.Y); for (int i = count - 1; i >= 0; i--) { View v = start.InternalSubviews [i]; if (v.Visible && v.Frame.Contains (rx, ry)) { var deep = FindDeepestView (v, rx, ry, out resx, out resy); if (deep == null) return v; return deep; } } } } resx = x - startFrame.X; resy = y - startFrame.Y; return start; } } }