Partial progress in removing AutoSize

This commit is contained in:
Tig
2024-04-17 20:24:23 -06:00
parent 6f3799dd87
commit c011ea0af0
2 changed files with 129 additions and 198 deletions

View File

@@ -276,24 +276,6 @@ public partial class View
_height = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Height)} cannot be null");
if (AutoSize)
{
Debug.WriteLine (@$"Must set AutoSize to false before setting {nameof (Height)}.");
AutoSize = false;
}
//if (ValidatePosDim) {
bool isValidNewAutoSize = AutoSize && IsValidAutoSizeHeight (_height);
if (IsAdded && AutoSize && !isValidNewAutoSize)
{
Debug.WriteLine (
@$"Must set AutoSize to false before setting the {nameof (Height)}."
);
AutoSize = false;
}
//}
OnResizeNeeded ();
}
}
@@ -334,19 +316,6 @@ public partial class View
_width = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Width)} cannot be null");
if (AutoSize)
{
Debug.WriteLine ($@"Must set AutoSize to false before setting {nameof (Width)}.");
AutoSize = false;
}
bool isValidNewAutoSize = AutoSize && IsValidAutoSizeWidth (_width);
if (IsAdded && AutoSize && !isValidNewAutoSize)
{
Debug.WriteLine ($@"Must set AutoSize to false before setting {nameof (Width)}.");
AutoSize = false;
}
OnResizeNeeded ();
}
@@ -356,8 +325,6 @@ public partial class View
#region AutoSize
private bool _autoSize;
/// <summary>
/// Gets or sets a flag that determines whether the View will be automatically resized to fit the <see cref="Text"/>
/// within <see cref="Viewport"/>.
@@ -377,60 +344,24 @@ public partial class View
/// </summary>
public virtual bool AutoSize
{
get => _autoSize;
get => Height is Dim.DimAuto && Width is Dim.DimAuto;
set
{
if (Width != Dim.Sized (0) && Height != Dim.Sized (0))
if (IsInitialized)
{
Debug.WriteLine (
$@"WARNING: {GetType ().Name} - Setting {nameof (AutoSize)} invalidates {nameof (Width)} and {nameof (Height)}."
);
}
Height = Dim.Auto (Dim.DimAutoStyle.Text);
Width = Dim.Auto (Dim.DimAutoStyle.Text);
bool v = ResizeView (value);
TextFormatter.AutoSize = v;
if (_autoSize != v)
{
_autoSize = v;
TextFormatter.NeedsFormat = true;
UpdateTextFormatterText ();
OnResizeNeeded ();
}
}
}
/// <summary>If <paramref name="autoSize"/> is true, resizes the view.</summary>
/// <param name="autoSize"></param>
/// <returns></returns>
private bool ResizeView (bool autoSize)
{
if (!autoSize)
{
return false;
}
var boundsChanged = true;
Size newFrameSize = GetTextAutoSize ();
if (IsInitialized && newFrameSize != Frame.Size)
{
if (ValidatePosDim)
{
// BUGBUG: This ain't right, obviously. We need to figure out how to handle this.
boundsChanged = ResizeViewportToFit (newFrameSize);
}
else
{
Height = newFrameSize.Height;
Width = newFrameSize.Width;
_height = Dim.Auto (Dim.DimAutoStyle.Text);
_width = Dim.Auto (Dim.DimAutoStyle.Text);
}
}
return boundsChanged;
}
/// <summary>Determines if the View's <see cref="Height"/> can be set to a new value.</summary>
/// <remarks>TrySetHeight can only be called when AutoSize is true (or being set to true).</remarks>
/// <param name="desiredHeight"></param>
@@ -1050,11 +981,11 @@ public partial class View
// TODO: Determine what, if any of the below is actually needed here.
if (IsInitialized)
{
if (AutoSize)
{
SetFrameToFitText ();
SetTextFormatterSize ();
}
//if (AutoSize)
//{
// SetFrameToFitText ();
// SetTextFormatterSize ();
//}
LayoutAdornments ();
SetNeedsDisplay ();
@@ -1156,32 +1087,32 @@ public partial class View
SetNeedsDisplay ();
}
if (AutoSize)
{
if (autoSize.Width == 0 || autoSize.Height == 0)
{
// Set the frame. Do NOT use `Frame` as it overwrites X, Y, Width, and Height, making
// the view LayoutStyle.Absolute.
SetFrame (_frame with { Size = autoSize });
//if (AutoSize)
//{
// if (autoSize.Width == 0 || autoSize.Height == 0)
// {
// // Set the frame. Do NOT use `Frame` as it overwrites X, Y, Width, and Height, making
// // the view LayoutStyle.Absolute.
// SetFrame (_frame with { Size = autoSize });
if (autoSize.Width == 0)
{
_width = 0;
}
// if (autoSize.Width == 0)
// {
// _width = 0;
// }
if (autoSize.Height == 0)
{
_height = 0;
}
}
else if (!SetFrameToFitText ())
{
SetTextFormatterSize ();
}
// if (autoSize.Height == 0)
// {
// _height = 0;
// }
// }
// //else if (!SetFrameToFitText ())
// //{
// // SetTextFormatterSize ();
// //}
SetNeedsLayout ();
SetNeedsDisplay ();
}
// SetNeedsLayout ();
// SetNeedsDisplay ();
//}
}
internal void CollectAll (View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)

View File

@@ -290,18 +290,18 @@ public partial class View
TextFormatter.Size = new Size (w, h);
}
private bool IsValidAutoSize (out Size autoSize)
{
Rectangle rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
//private bool IsValidAutoSize (out Size autoSize)
//{
// Rectangle rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
autoSize = new Size (
rect.Size.Width - GetHotKeySpecifierLength (),
rect.Size.Height - GetHotKeySpecifierLength (false));
// autoSize = new Size (
// rect.Size.Width - GetHotKeySpecifierLength (),
// rect.Size.Height - GetHotKeySpecifierLength (false));
return !((ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)))
|| _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength ()
|| _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
}
// return !((ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)))
// || _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength ()
// || _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
//}
private bool IsValidAutoSizeHeight (Dim height)
{
@@ -319,113 +319,113 @@ public partial class View
return !((ValidatePosDim && !(width is Dim.DimAbsolute)) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ());
}
/// <summary>
/// Sets the size of the View to the minimum width or height required to fit <see cref="Text"/>.
/// </summary>
/// <returns>
/// <see langword="true"/> if the size was changed; <see langword="false"/> if <see cref="AutoSize"/> ==
/// <see langword="true"/> or
/// <see cref="Text"/> will not fit.
/// </returns>
/// <remarks>
/// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
/// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
/// Does not take into account word wrapping.
/// </remarks>
private bool SetFrameToFitText ()
{
if (AutoSize == false)
{
throw new InvalidOperationException ("SetFrameToFitText can only be called when AutoSize is true");
}
///// <summary>
///// Sets the size of the View to the minimum width or height required to fit <see cref="Text"/>.
///// </summary>
///// <returns>
///// <see langword="true"/> if the size was changed; <see langword="false"/> if <see cref="AutoSize"/> ==
///// <see langword="true"/> or
///// <see cref="Text"/> will not fit.
///// </returns>
///// <remarks>
///// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
///// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
///// Does not take into account word wrapping.
///// </remarks>
//private bool SetFrameToFitText ()
//{
// if (AutoSize == false)
// {
// throw new InvalidOperationException ("SetFrameToFitText can only be called when AutoSize is true");
// }
// BUGBUG: This API is broken - should not assume Frame.Height == ContentSize.Height
// <summary>
// Gets the minimum dimensions required to fit the View's <see cref="Text"/>, factoring in <see cref="TextDirection"/>.
// </summary>
// <param name="sizeRequired">The minimum dimensions required.</param>
// <returns><see langword="true"/> if the dimensions fit within the View's <see cref="ContentSize"/>, <see langword="false"/> otherwise.</returns>
// <remarks>
// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
// Does not take into account word wrapping.
// </remarks>
bool GetMinimumSizeOfText (out Size sizeRequired)
{
if (!IsInitialized)
{
sizeRequired = Size.Empty;
// // BUGBUG: This API is broken - should not assume Frame.Height == ContentSize.Height
// // <summary>
// // Gets the minimum dimensions required to fit the View's <see cref="Text"/>, factoring in <see cref="TextDirection"/>.
// // </summary>
// // <param name="sizeRequired">The minimum dimensions required.</param>
// // <returns><see langword="true"/> if the dimensions fit within the View's <see cref="ContentSize"/>, <see langword="false"/> otherwise.</returns>
// // <remarks>
// // Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
// // if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
// // Does not take into account word wrapping.
// // </remarks>
// bool GetMinimumSizeOfText (out Size sizeRequired)
// {
// if (!IsInitialized)
// {
// sizeRequired = Size.Empty;
return false;
}
// return false;
// }
sizeRequired = ContentSize;
// sizeRequired = ContentSize;
if (AutoSize || string.IsNullOrEmpty (TextFormatter.Text))
{
return false;
}
// if (AutoSize || string.IsNullOrEmpty (TextFormatter.Text))
// {
// return false;
// }
switch (TextFormatter.IsVerticalDirection (TextDirection))
{
case true:
int colWidth = TextFormatter.GetSumMaxCharWidth (TextFormatter.Text, 0, 1);
// switch (TextFormatter.IsVerticalDirection (TextDirection))
// {
// case true:
// int colWidth = TextFormatter.GetSumMaxCharWidth (TextFormatter.Text, 0, 1);
// TODO: v2 - This uses frame.Width; it should only use ContentSize
if (_frame.Width < colWidth
&& (Width is null || (ContentSize.Width >= 0 && Width is Dim.DimAbsolute && Width.Anchor (0) >= 0 && Width.Anchor (0) < colWidth)))
{
sizeRequired = new (colWidth, ContentSize.Height);
// // TODO: v2 - This uses frame.Width; it should only use ContentSize
// if (_frame.Width < colWidth
// && (Width is null || (ContentSize.Width >= 0 && Width is Dim.DimAbsolute && Width.Anchor (0) >= 0 && Width.Anchor (0) < colWidth)))
// {
// sizeRequired = new (colWidth, ContentSize.Height);
return true;
}
// return true;
// }
break;
default:
if (_frame.Height < 1 && (Height is null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0)))
{
sizeRequired = new (ContentSize.Width, 1);
// break;
// default:
// if (_frame.Height < 1 && (Height is null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0)))
// {
// sizeRequired = new (ContentSize.Width, 1);
return true;
}
// return true;
// }
break;
}
// break;
// }
return false;
}
// return false;
// }
if (GetMinimumSizeOfText (out Size size))
{
// TODO: This is a hack.
//_width = size.Width;
//_height = size.Height;
SetFrame (new (_frame.Location, size));
// if (GetMinimumSizeOfText (out Size size))
// {
// // TODO: This is a hack.
// //_width = size.Width;
// //_height = size.Height;
// SetFrame (new (_frame.Location, size));
//throw new InvalidOperationException ("This is a hack.");
return true;
}
// //throw new InvalidOperationException ("This is a hack.");
// return true;
// }
return false;
}
// return false;
//}
private void UpdateTextDirection (TextDirection newDirection)
{
bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
TextFormatter.Direction = newDirection;
bool isValidOldAutoSize = AutoSize && IsValidAutoSize (out Size _);
//bool isValidOldAutoSize = AutoSize && IsValidAutoSize (out Size _);
UpdateTextFormatterText ();
if ((!ValidatePosDim && directionChanged && AutoSize) || (ValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize))
{
OnResizeNeeded ();
}
else if (directionChanged && IsAdded)
{
ResizeViewportToFit (Viewport.Size);
}
//if ((!ValidatePosDim && directionChanged && AutoSize) || (ValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize))
//{
// OnResizeNeeded ();
//}
//else if (directionChanged && IsAdded)
//{
// ResizeViewportToFit (Viewport.Size);
//}
SetTextFormatterSize ();
SetNeedsDisplay ();