123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- using System;
- using static Terminal.Gui.Dim;
- namespace Terminal.Gui;
- /// <summary>
- /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
- /// relative to the ending dimension. Integer values are implicitly convertible to
- /// an absolute <see cref="Pos"/>. These objects are created using the static methods Percent,
- /// AnchorEnd, and Center. The <see cref="Pos"/> objects can be combined with the addition and
- /// subtraction operators.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.
- /// </para>
- /// <para>
- /// These can be used to set the absolute position, when merely assigning an
- /// integer value (via the implicit integer to <see cref="Pos"/> conversion), and they can be combined
- /// to produce more useful layouts, like: Pos.Center - 3, which would shift the position
- /// of the <see cref="View"/> 3 characters to the left after centering for example.
- /// </para>
- /// <para>
- /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View). The X(View) and Y(View) are
- /// aliases to Left(View) and Top(View) respectively.
- /// </para>
- /// <para>
- /// <list type="table">
- /// <listheader>
- /// <term>Pos Object</term>
- /// <description>Description</description>
- /// </listheader>
- /// <item>
- /// <term><see cref="Pos.Function(Func{int})"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function will be called every time the position is needed.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Percent(float)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the SuperView.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Anchor(int)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the dimension,
- /// useful to flush the layout from the right or bottom.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Center"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.At(int)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Left(int)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.X(View)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Top(View)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Y(View)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Right(View)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Pos.Bottom(View)"/></term>
- /// <description>
- /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified <see cref="View"/>
- /// </description>
- /// </item>
- ///
- /// </list>
- /// </para>
- /// </remarks>
- public class Pos {
- internal virtual int Anchor (int width) => 0;
- /// <summary>
- /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function will be called every time the position is needed.
- /// </summary>
- /// <param name="function">The function to be executed.</param>
- /// <returns>The <see cref="Pos"/> returned from the function.</returns>
- public static Pos Function (Func<int> function) => new PosFunc (function);
- internal class PosFactor : Pos {
- readonly float _factor;
- public PosFactor (float n) => _factor = n;
- internal override int Anchor (int width) => (int)(width * _factor);
- public override string ToString () => $"Factor({_factor})";
- public override int GetHashCode () => _factor.GetHashCode ();
- public override bool Equals (object other) => other is PosFactor f && f._factor == _factor;
- }
- // Helper class to provide dynamic value by the execution of a function that returns an integer.
- internal class PosFunc : Pos {
- readonly Func<int> _function;
- public PosFunc (Func<int> n) => _function = n;
- internal override int Anchor (int width) => _function ();
- public override string ToString () => $"PosFunc({_function ()})";
- public override int GetHashCode () => _function.GetHashCode ();
- public override bool Equals (object other) => other is PosFunc f && f._function () == _function ();
- }
- /// <summary>
- /// Creates a percentage <see cref="Pos"/> object
- /// </summary>
- /// <returns>The percent <see cref="Pos"/> object.</returns>
- /// <param name="n">A value between 0 and 100 representing the percentage.</param>
- /// <example>
- /// This creates a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
- /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
- /// <code>
- /// var textView = new TextView () {
- /// X = Pos.Center (),
- /// Y = Pos.Percent (50),
- /// Width = Dim.Percent (80),
- /// Height = Dim.Percent (30),
- /// };
- /// </code>
- /// </example>
- public static Pos Percent (float n)
- {
- if (n is < 0 or > 100) {
- throw new ArgumentException ("Percent value must be between 0 and 100");
- }
- return new PosFactor (n / 100);
- }
- /// <summary>
- /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the dimension,
- /// useful to flush the layout from the right or bottom.
- /// </summary>
- /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
- /// <param name="margin">Optional margin to place to the right or below.</param>
- /// <example>
- /// This sample shows how align a <see cref="Button"/> to the bottom-right of a <see cref="View"/>.
- /// <code>
- /// // See Issue #502
- /// anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
- /// anchorButton.Y = Pos.AnchorEnd (1);
- /// </code>
- /// </example>
- public static Pos AnchorEnd (int margin = 0)
- {
- if (margin < 0) {
- throw new ArgumentException ("Margin must be positive");
- }
- return new PosAnchorEnd (margin);
- }
- internal class PosAnchorEnd : Pos {
- readonly int _p;
- public PosAnchorEnd (int n) => _p = n;
- internal override int Anchor (int width) => width - _p;
- public override string ToString () => $"AnchorEnd({_p})";
- public override int GetHashCode () => _p.GetHashCode ();
- public override bool Equals (object other) => other is PosAnchorEnd anchorEnd && anchorEnd._p == _p;
- }
- /// <summary>
- /// Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.
- /// </summary>
- /// <returns>The center Pos.</returns>
- /// <example>
- /// This creates a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
- /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
- /// <code>
- /// var textView = new TextView () {
- /// X = Pos.Center (),
- /// Y = Pos.Percent (50),
- /// Width = Dim.Percent (80),
- /// Height = Dim.Percent (30),
- /// };
- /// </code>
- /// </example>
- public static Pos Center () => new PosCenter ();
- internal class PosAbsolute : Pos {
- readonly int _n;
- public PosAbsolute (int n) => _n = n;
- public override string ToString () => $"Absolute({_n})";
- internal override int Anchor (int width) => _n;
- public override int GetHashCode () => _n.GetHashCode ();
- public override bool Equals (object other) => other is PosAbsolute abs && abs._n == _n;
- }
- internal class PosCenter : Pos {
- internal override int Anchor (int width) => width / 2;
- public override string ToString () => "Center";
- }
- /// <summary>
- /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.
- /// </summary>
- /// <returns>The Absolute <see cref="Pos"/>.</returns>
- /// <param name="n">The value to convert to the <see cref="Pos"/>.</param>
- public static Pos At (int n) => new PosAbsolute (n);
- internal class PosCombine : Pos {
- internal Pos _left, _right;
- internal bool _add;
- public PosCombine (bool add, Pos left, Pos right)
- {
- _left = left;
- _right = right;
- _add = add;
- }
- internal override int Anchor (int width)
- {
- int la = _left.Anchor (width);
- int ra = _right.Anchor (width);
- if (_add) {
- return la + ra;
- } else {
- return la - ra;
- }
- }
- public override string ToString () => $"Combine({_left}{(_add ? '+' : '-')}{_right})";
- }
- /// <summary>
- /// Creates an Absolute <see cref="Pos"/> from the specified integer value.
- /// </summary>
- /// <returns>The Absolute <see cref="Pos"/>.</returns>
- /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
- public static implicit operator Pos (int n) => new PosAbsolute (n);
- /// <summary>
- /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.
- /// </summary>
- /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
- /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
- /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
- public static Pos operator + (Pos left, Pos right)
- {
- if (left is PosAbsolute && right is PosAbsolute) {
- return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
- }
- var newPos = new PosCombine (true, left, right);
- SetPosCombine (left, newPos);
- return newPos;
- }
- /// <summary>
- /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.
- /// </summary>
- /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
- /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
- /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
- public static Pos operator - (Pos left, Pos right)
- {
- if (left is PosAbsolute && right is PosAbsolute) {
- return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
- }
- var newPos = new PosCombine (false, left, right);
- SetPosCombine (left, newPos);
- return newPos;
- }
- static void SetPosCombine (Pos left, PosCombine newPos)
- {
- var view = left as PosView;
- if (view != null) {
- view.Target.SetNeedsLayout ();
- }
- }
- internal class PosView : Pos {
- public readonly View Target;
- int side;
- public PosView (View view, int side)
- {
- Target = view;
- this.side = side;
- }
- internal override int Anchor (int width)
- {
- switch (side) {
- case 0: return Target.Frame.X;
- case 1: return Target.Frame.Y;
- case 2: return Target.Frame.Right;
- case 3: return Target.Frame.Bottom;
- default:
- return 0;
- }
- }
- public override string ToString ()
- {
- string tside;
- switch (side) {
- case 0: tside = "x"; break;
- case 1: tside = "y"; break;
- case 2: tside = "right"; break;
- case 3: tside = "bottom"; break;
- default: tside = "unknown"; break;
- }
- // Note: We do not checkt `Target` for null here to intentionally throw if so
- return $"View(side={tside},target={Target.ToString ()})";
- }
- public override int GetHashCode () => Target.GetHashCode ();
- public override bool Equals (object other) => other is PosView abs && abs.Target == Target;
- }
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos Left (View view) => new PosView (view, 0);
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos X (View view) => new PosView (view, 0);
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos Top (View view) => new PosView (view, 1);
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos Y (View view) => new PosView(view, 1);
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos Right (View view) => new PosView (view, 2);
- /// <summary>
- /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified <see cref="View"/>
- /// </summary>
- /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
- /// <param name="view">The <see cref="View"/> that will be tracked.</param>
- public static Pos Bottom (View view) => new PosView (view, 3);
- /// <summary>Serves as the default hash function. </summary>
- /// <returns>A hash code for the current object.</returns>
- public override int GetHashCode () => Anchor (0).GetHashCode ();
- /// <summary>Determines whether the specified object is equal to the current object.</summary>
- /// <param name="other">The object to compare with the current object. </param>
- /// <returns>
- /// <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns>
- public override bool Equals (object other) => other is Pos abs && abs == this;
- }
- /// <summary>
- /// <para>
- /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the <see cref="View.Width"/> and
- /// <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable Computed Layout (see <see cref="LayoutStyle.Computed"/>)
- /// to automatically manage the dimensions of a view.
- /// </para>
- /// <para>
- /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using the static methods described below.
- /// The <see cref="Dim"/> objects can be combined with the addition and subtraction operators.
- /// </para>
- /// </summary>
- /// <remarks>
- /// <para>
- /// <list type="table">
- /// <listheader>
- /// <term>Dim Object</term>
- /// <description>Description</description>
- /// </listheader>
- /// <item>
- /// <term><see cref="Dim.Function(Func{int})"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided function. The function will be called every time the dimension is needed.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Dim.Percent(float, bool)"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Dim.Fill(int)"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a margin.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Auto()"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Dim.Width(View)"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Dim.Height(View)"/></term>
- /// <description>
- /// Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.
- /// </description>
- /// </item>
- /// </list>
- /// </para>
- /// <para>
- /// </para>
- /// </remarks>
- public class Dim {
- internal virtual int Anchor (int width) => 0;
- /// <summary>
- /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
- /// The function will be called every time the dimension is needed.
- /// </summary>
- /// <param name="function">The function to be executed.</param>
- /// <returns>The <see cref="Dim"/> returned from the function.</returns>
- public static Dim Function (Func<int> function) => new DimFunc (function);
- // Helper class to provide dynamic value by the execution of a function that returns an integer.
- internal class DimFunc : Dim {
- readonly Func<int> _function;
- public DimFunc (Func<int> n) => _function = n;
- internal override int Anchor (int width) => _function ();
- public override string ToString () => $"DimFunc({_function ()})";
- public override int GetHashCode () => _function.GetHashCode ();
- public override bool Equals (object other) => other is DimFunc f && f._function () == _function ();
- }
- /// <summary>
- /// Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.
- /// </summary>
- /// <returns>The percent <see cref="Dim"/> object.</returns>
- /// <param name="n">A value between 0 and 100 representing the percentage.</param>
- /// <param name="r">If <c>true</c> the Percent is computed based on the remaining space after the X/Y anchor positions.
- /// If <c>false</c> is computed based on the whole original space.</param>
- /// <example>
- /// This initializes a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
- /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
- /// <code>
- /// var textView = new TextView () {
- /// X = Pos.Center (),
- /// Y = Pos.Percent (50),
- /// Width = Dim.Percent (80),
- /// Height = Dim.Percent (30),
- /// };
- /// </code>
- /// </example>
- public static Dim Percent (float n, bool r = false)
- {
- if (n is < 0 or > 100) {
- throw new ArgumentException ("Percent value must be between 0 and 100");
- }
- return new DimFactor (n / 100, r);
- }
- internal class DimFactor : Dim {
- readonly float _factor;
- readonly bool _remaining;
- public DimFactor (float n, bool r = false)
- {
- _factor = n;
- _remaining = r;
- }
- internal override int Anchor (int width) => (int)(width * _factor);
- public bool IsFromRemaining () => _remaining;
- public override string ToString () => $"Factor({_factor},{_remaining})";
- public override int GetHashCode () => _factor.GetHashCode ();
- public override bool Equals (object other) => other is DimFactor f && f._factor == _factor && f._remaining == _remaining;
- }
- internal class DimAbsolute : Dim {
- readonly int _n;
- public DimAbsolute (int n) => _n = n;
- public override string ToString () => $"Absolute({_n})";
- internal override int Anchor (int width) => _n;
- public override int GetHashCode () => _n.GetHashCode ();
- public override bool Equals (object other) => other is DimAbsolute abs && abs._n == _n;
- }
- internal class DimFill : Dim {
- readonly int _margin;
- public DimFill (int margin) => _margin = margin;
- public override string ToString () => $"Fill({_margin})";
- internal override int Anchor (int width) => width - _margin;
- public override int GetHashCode () => _margin.GetHashCode ();
- public override bool Equals (object other) => other is DimFill fill && fill._margin == _margin;
- }
- /// <summary>
- /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a margin.
- /// </summary>
- /// <returns>The Fill dimension.</returns>
- /// <param name="margin">Margin to use.</param>
- public static Dim Fill (int margin = 0) => new DimFill (margin);
- /// <summary>
- /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews.
- /// </summary>
- /// <example>
- /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two SubViews.
- /// <code>
- /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
- /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
- /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.AutoSize (), Height = Dim.AutoSize () };
- /// view.Add (button, textField);
- /// </code>
- /// </example>
- /// <returns>The AutoSize <see cref="Dim"/> object.</returns>
- /// <param name="style">Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Text"/>. NOT CURRENTLY SUPPORTED.</param>
- /// <param name="min">Specifies the minimum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
- /// <param name="min">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
- public static Dim Auto (DimAutoStyle style = DimAutoStyle.Subviews, Dim min = null, Dim max = null)
- {
- if (style == DimAutoStyle.Text) {
- throw new NotImplementedException (@"DimAutoStyle.Text is not implemented.");
- }
- //if (min != null) {
- // throw new NotImplementedException (@"min is not implemented");
- //}
- if (max != null) {
- throw new NotImplementedException (@"max is not implemented");
- }
- return new DimAuto (style, min, max);
- }
- /// <summary>
- /// Specifies how <see cref="DimAuto"/> will compute the dimension.
- /// </summary>
- public enum DimAutoStyle {
- /// <summary>
- /// The dimension will be computed from the view's <see cref="View.Text"/>. NOT CURRENTLY SUPPORTED.
- /// </summary>
- Text,
- /// <summary>
- /// The dimension will be computed from the view's <see cref="View.Subviews"/>.
- /// </summary>
- Subviews,
- }
- internal class DimAuto : Dim {
- internal readonly Dim _min;
- internal readonly Dim _max;
- internal readonly DimAutoStyle _style;
- public DimAuto (DimAutoStyle style, Dim min, Dim max)
- {
- _min = min;
- _max = max;
- _style = style;
- }
- public override string ToString () => $"Auto({_style},{_min},{_max})";
- public override int GetHashCode () => HashCode.Combine (base.GetHashCode (), _min, _max, _style);
- public override bool Equals (object other) => other is DimAuto auto && (auto._min == _min && auto._max == _max && auto._style == _style);
- }
- /// <summary>
- /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
- /// </summary>
- /// <returns>The Absolute <see cref="Dim"/>.</returns>
- /// <param name="n">The value to convert to the pos.</param>
- public static implicit operator Dim (int n) => new DimAbsolute (n);
- /// <summary>
- /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
- /// </summary>
- /// <returns>The Absolute <see cref="Dim"/>.</returns>
- /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
- public static Dim Sized (int n) => new DimAbsolute (n);
- internal class DimCombine : Dim {
- internal Dim _left, _right;
- internal bool _add;
- public DimCombine (bool add, Dim left, Dim right)
- {
- _left = left;
- _right = right;
- _add = add;
- }
- internal override int Anchor (int width)
- {
- int la = _left.Anchor (width);
- int ra = _right.Anchor (width);
- if (_add) {
- return la + ra;
- } else {
- return la - ra;
- }
- }
- public override string ToString () => $"Combine({_left}{(_add ? '+' : '-')}{_right})";
- }
- /// <summary>
- /// Adds a <see cref="Terminal.Gui.Dim"/> to a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
- /// </summary>
- /// <param name="left">The first <see cref="Terminal.Gui.Dim"/> to add.</param>
- /// <param name="right">The second <see cref="Terminal.Gui.Dim"/> to add.</param>
- /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
- public static Dim operator + (Dim left, Dim right)
- {
- if (left is DimAbsolute && right is DimAbsolute) {
- return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
- }
- var newDim = new DimCombine (true, left, right);
- SetDimCombine (left, newDim);
- return newDim;
- }
- /// <summary>
- /// Subtracts a <see cref="Terminal.Gui.Dim"/> from a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
- /// </summary>
- /// <param name="left">The <see cref="Terminal.Gui.Dim"/> to subtract from (the minuend).</param>
- /// <param name="right">The <see cref="Terminal.Gui.Dim"/> to subtract (the subtrahend).</param>
- /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
- public static Dim operator - (Dim left, Dim right)
- {
- if (left is DimAbsolute && right is DimAbsolute) {
- return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
- }
- var newDim = new DimCombine (false, left, right);
- SetDimCombine (left, newDim);
- return newDim;
- }
- // BUGBUG: newPos is never used.
- static void SetDimCombine (Dim left, DimCombine newPos) => (left as DimView)?.Target.SetNeedsLayout ();
- internal class DimView : Dim {
- public View Target { get; init; }
- readonly int _side;
- public DimView (View view, int side)
- {
- Target = view;
- _side = side;
- }
- internal override int Anchor (int width) => _side switch {
- 0 => Target.Frame.Height,
- 1 => Target.Frame.Width,
- _ => 0
- };
- public override string ToString ()
- {
- if (Target == null) {
- throw new NullReferenceException ();
- }
- string tside = _side switch {
- 0 => "Height",
- 1 => "Width",
- _ => "unknown"
- };
- return $"View({tside},{Target})";
- }
- public override int GetHashCode () => Target.GetHashCode ();
- public override bool Equals (object other) => other is DimView abs && abs.Target == Target;
- }
- /// <summary>
- /// Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
- /// <param name="view">The view that will be tracked.</param>
- public static Dim Width (View view) => new DimView (view, 1);
- /// <summary>
- /// Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.
- /// </summary>
- /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
- /// <param name="view">The view that will be tracked.</param>
- public static Dim Height (View view) => new DimView (view, 0);
- /// <summary>Serves as the default hash function. </summary>
- /// <returns>A hash code for the current object.</returns>
- public override int GetHashCode () => Anchor (0).GetHashCode ();
- /// <summary>Determines whether the specified object is equal to the current object.</summary>
- /// <param name="other">The object to compare with the current object. </param>
- /// <returns>
- /// <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns>
- public override bool Equals (object other) => other is Dim abs && abs == this;
- }
|