Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3407 - Border with Subviews #3862

Draft
wants to merge 4 commits into
base: v2_develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ protected override bool OnClearingViewport ()

/// <summary>Does nothing for Adornment</summary>
/// <returns></returns>
protected override bool OnRenderingLineCanvas () { return true; }

/// <summary>
/// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
/// property throws an <see cref="InvalidOperationException"/>.
/// </summary>
public override bool SuperViewRendersLineCanvas
{
get => false;
set => throw new InvalidOperationException (@"Adornment can only render to their Parent or Parent's Superview.");
}
protected override bool OnRenderingLineCanvas () { return false; }

///// <summary>
///// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
///// property throws an <see cref="InvalidOperationException"/>.
///// </summary>
//public override bool SuperViewRendersLineCanvas
//{
// get => false;
// set => throw new InvalidOperationException (@"Adornment can only render to their Parent or Parent's Superview.");
//}

/// <inheritdoc/>
protected override void OnDrawComplete () { }
Expand Down
153 changes: 112 additions & 41 deletions Terminal.Gui/View/Adornment/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
private LineStyle? _lineStyle;

/// <inheritdoc/>
public Border ()

Check warning on line 51 in Terminal.Gui/View/Adornment/Border.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Non-nullable property 'TopLeft' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{ /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
}

Expand Down Expand Up @@ -111,64 +111,126 @@
}
}

#if SUBVIEW_BASED_BORDER
private Line _left;
public Line TopLeft { get; internal set; }
public Line TopRight { get; internal set; }
public Line Left { get; internal set; }
public Line Right { get; internal set; }
public Line Bottom { get; internal set; }
public View TitleLabel { get; internal set; }

/// <summary>
/// The close button for the border. Set to <see cref="View.Visible"/>, to <see langword="true"/> to enable.
/// </summary>
public Button CloseButton { get; internal set; }
#endif

/// <inheritdoc/>
public override void BeginInit ()
{
base.BeginInit ();

//SuperViewRendersLineCanvas = true;

if (Thickness == Thickness.Empty)
{
return;
}
ShowHideDrawIndicator ();
#if SUBVIEW_BASED_BORDER
if (Parent is { })

TopLeft = new ()
{
// Left
_left = new ()
{
Orientation = Orientation.Vertical,
};
Add (_left);
Id = "TopLeft",
Orientation = Orientation.Horizontal,
};
Add (TopLeft);

CloseButton = new Button ()
{
Text = "X",
CanFocus = true,
Visible = false,
};
CloseButton.Accept += (s, e) =>
{
e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
};
Add (CloseButton);
TopRight = new ()
{
Id = "TopRight",
Orientation = Orientation.Horizontal,
};
Add (TopRight);

LayoutStarted += OnLayoutStarted;
}
#endif
}
Left = new ()
{
Id = "Left",
Orientation = Orientation.Vertical,
};
Add (Left);

#if SUBVIEW_BASED_BORDER
private void OnLayoutStarted (object sender, LayoutEventArgs e)
{
_left.Border.LineStyle = LineStyle;
Right = new ()
{
Id = "Right",
Orientation = Orientation.Vertical,
};

_left.X = Thickness.Left - 1;
_left.Y = Thickness.Top - 1;
_left.Width = 1;
_left.Height = Height;
Add (Right);

CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) -
(Pos.Right (CloseButton) -
Pos.Left (CloseButton));
CloseButton.Y = 0;
}
#endif
Bottom = new ()
{
Id = "Bottom",
Orientation = Orientation.Horizontal,
};
Add (Bottom);

TitleLabel = new View ()
{
Id = "TitleLabel",
Text = Parent.Title,
CanFocus = false,
TextAlignment = Alignment.Center,
VerticalTextAlignment = Alignment.Center,
};
Add (TitleLabel);

SetSubviewLayout ();
}

private void SetSubviewLayout ()
{
TopLeft.X = Pos.Func (() => Thickness.Left / 2);
TopLeft.Y = Pos.Func (() => Thickness.Top / 2);
TopLeft.Width = 2;
TopLeft.Height = 1;
TopLeft.Visible = Thickness.Top > 0;

TopRight.X = Pos.Right (TitleLabel);
TopRight.Y = Pos.Func (() => Thickness.Top / 2);
TopRight.Width = Dim.Fill () - Dim.Func (() => Thickness.Right / 2);
TopRight.Height = 1;
TopRight.Visible = Thickness.Top > 0;

Left.X = Pos.Func (() => Thickness.Left / 2);
Left.Y = Pos.Top (TopRight);
Left.Height = Dim.Fill () - Dim.Func (() => Thickness.Bottom / 2);
Left.Width = 1;
Left.Visible = Thickness.Left > 0;

Right.X = Pos.Right (TopRight) - 1;
Right.Y = Pos.Top (TopRight);
Right.Height = Dim.Fill () - Dim.Func (() => Thickness.Bottom / 2);
Right.Width = 1;
Right.Visible = Thickness.Right > 0;

Bottom.X = Pos.Func (() => Thickness.Left / 2);
Bottom.Y = Pos.Bottom (Left) - 1;
Bottom.Width = Dim.Fill () - Dim.Func (() => Thickness.Right / 2);
Bottom.Height = 1;
Bottom.Visible = Thickness.Bottom > 0;

TitleLabel.X = Pos.Right (TopLeft);
TitleLabel.Y = Pos.Func (() => Thickness.Top / 2 - TitleLabel.Frame.Height / 2);
TitleLabel.Height = _settings.FastHasFlags (BorderSettings.Title) ? Thickness.Top : 0;
TitleLabel.Width = Dim.Func (() => _settings.FastHasFlags (BorderSettings.Title) ? TitleLabel.TextFormatter.FormatAndGetSize ().Width + TitleLabel.GetAdornmentsThickness ().Horizontal : 0);
//TitleLabel.Border.Thickness = new (1, 0, 1, 0);
//TitleLabel.Border.LineStyle = LineStyle.Dotted;
//TitleLabel.SuperViewRendersLineCanvas = true;

//CloseButton.X = Pos.Left (Right) - 1;
//CloseButton.Y = Pos.Func (() => Thickness.Top / 2);
//CloseButton.Width = 1;
//CloseButton.Height = 1;
//CloseButton.Visible = false;
}

/// <summary>
/// The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
Expand Down Expand Up @@ -640,6 +702,13 @@

#endregion Mouse Support

/// <inheritdoc />
protected override bool OnDrawingSubviews ()
{
return base.OnDrawingSubviews ();

}

/// <inheritdoc/>
protected override bool OnDrawingContent ()
{
Expand All @@ -648,6 +717,8 @@
return true;
}

return true;

Rectangle screenBounds = ViewportToScreen (Viewport);

// TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
Expand Down Expand Up @@ -727,7 +798,7 @@
,
Parent.HasFocus ? focus : GetNormalColor (),
Parent.HasFocus ? focus : GetHotNormalColor ());
Parent?.LineCanvas.Exclude(new(titleRect));
Parent?.LineCanvas.Exclude (new (titleRect));
}

if (canDrawBorder && LineStyle != LineStyle.None)
Expand Down
11 changes: 8 additions & 3 deletions Terminal.Gui/View/View.Drawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void DoDrawBorderAndPaddingSubViews ()
if (Border?.Subviews is { } && Border.Thickness != Thickness.Empty)
{
// PERFORMANCE: Get the check for DrawIndicator out of this somehow.
foreach (View subview in Border.Subviews.Where (v => v.Visible || v.Id == "DrawIndicator"))
foreach (View subview in Border.Subviews.Where (v => (v.Visible || v.Id == "DrawIndicator") && v.SuperViewRendersLineCanvas == false))
{
if (subview.Id != "DrawIndicator")
{
Expand Down Expand Up @@ -585,12 +585,12 @@ private void DoRenderLineCanvas ()
/// </summary>
public void RenderLineCanvas ()
{
if (Driver is null)
if (Driver is null || LineCanvas.Bounds == Rectangle.Empty)
{
return;
}

if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
if (!SuperViewRendersLineCanvas)
{
foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
{
Expand All @@ -607,6 +607,11 @@ public void RenderLineCanvas ()

LineCanvas.Clear ();
}
else
{
//SuperView?.LineCanvas.Merge (LineCanvas);
//LineCanvas.Clear ();
}
}

#endregion DrawLineCanvas
Expand Down
30 changes: 27 additions & 3 deletions Terminal.Gui/Views/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,27 @@ public Line ()

_orientationHelper = new (this);
_orientationHelper.Orientation = Orientation.Horizontal;
OnOrientationChanged(Orientation);
OnOrientationChanged (Orientation);
}

private LineStyle? _lineStyle;

/// <summary>
/// Gets or sets the style of the Line. The default is the <see cref="Border.BorderStyle"/> of the SuperView.
/// </summary>
public LineStyle LineStyle
{
get
{
if (_lineStyle.HasValue)
{
return _lineStyle.Value;
}

return SuperView?.BorderStyle ?? LineStyle.Single;
}
set => _lineStyle = value;
}

#region IOrientation members
/// <summary>
Expand Down Expand Up @@ -61,18 +79,24 @@ public void OnOrientationChanged (Orientation newOrientation)
}
#endregion

/// <inheritdoc />
protected override bool OnClearingViewport () { return true; }

/// <inheritdoc/>
protected override bool OnDrawingContent ()
{
Point pos = ViewportToScreen (Viewport).Location;
int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;

if (length == 0)
{
return true;
}
LineCanvas?.AddLine (
pos,
length,
Orientation,
BorderStyle
);
LineStyle);

//SuperView?.SetNeedsDraw ();
return true;
Expand Down
14 changes: 11 additions & 3 deletions UICatalog/Scenarios/Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ public override void Main ()
Window appWindow = new ()
{
Title = GetQuitKeyAndName (),
//BorderStyle = LineStyle.None
};

var button = new Button { Id = "button", X = Pos.Center (), Y = 1, Text = "_Press me!" };
//var button = new Button { Id = "button", X = Pos.Center (), Y = 1, Text = "_Press me!" };

button.Accepting += (s, e) => MessageBox.ErrorQuery ("Error", "You pressed the button!", "_Ok");
appWindow.Add (button);
//button.Accepting += (s, e) => MessageBox.ErrorQuery ("Error", "You pressed the button!", "_Ok");
//appWindow.Add (button);

//Line line = new ()
//{
// LineStyle = LineStyle.Double,
// Width = 10,
//};
//appWindow.Add (line);

// Run - Start the application.
Application.Run (appWindow);
Expand Down
Loading