Skip to content

Commit

Permalink
Merge pull request gui-cs#3507 from tig/v2_3503-ViewEditors
Browse files Browse the repository at this point in the history
Fixes gui-cs#3503. Improves "View Editing" for Scenarios (e.g. `AdornmentsEditor`)
  • Loading branch information
tig committed May 27, 2024
2 parents 38ad450 + b9c5797 commit 7b3f5d2
Show file tree
Hide file tree
Showing 20 changed files with 1,042 additions and 653 deletions.
4 changes: 0 additions & 4 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,8 @@ public static RunState Begin (Toplevel toplevel)
MoveCurrent (Current);
}

//if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
toplevel.SetRelativeLayout (Driver.Screen.Size);

//}

// BUGBUG: This call is likely not needed.
toplevel.LayoutSubviews ();
toplevel.PositionToplevels ();
toplevel.FocusFirst ();
Expand Down
39 changes: 30 additions & 9 deletions Terminal.Gui/View/Adornment/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ public LineStyle LineStyle
set => _lineStyle = value;
}

private bool _showTitle = true;

/// <summary>
/// Gets or sets whether the title should be shown. The default is <see langword="true"/>.
/// </summary>
public bool ShowTitle
{
get => _showTitle;
set
{
if (value == _showTitle)
{
return;
}
_showTitle = value;

Parent?.SetNeedsDisplay ();
}
}

#region Mouse Support

private Color? _savedForeColor;
Expand Down Expand Up @@ -358,7 +378,7 @@ private void Application_UnGrabbingMouse (object sender, GrabMouseEventArgs e)
}
}

#endregion Mouse Support
#endregion Mouse Support

/// <inheritdoc/>
public override void OnDrawContent (Rectangle viewport)
Expand Down Expand Up @@ -394,12 +414,13 @@ public override void OnDrawContent (Rectangle viewport)
Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
)
);

Parent.TitleTextFormatter.Size = new (maxTitleWidth, 1);

int sideLineLength = borderBounds.Height;
bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };

if (!string.IsNullOrEmpty (Parent?.Title))
if (ShowTitle)
{
if (Thickness.Top == 2)
{
Expand Down Expand Up @@ -431,13 +452,13 @@ public override void OnDrawContent (Rectangle viewport)
}
}

if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && ShowTitle && !string.IsNullOrEmpty (Parent?.Title))
{
var focus = Parent.GetNormalColor();
var focus = Parent.GetNormalColor ();
if (Parent.SuperView is { } && Parent.SuperView?.Subviews!.Count (s => s.CanFocus) > 1)
{
// Only use focus color if there are multiple focusable views
focus = Parent.GetFocusColor() ;
focus = Parent.GetFocusColor ();
}

Parent.TitleTextFormatter.Draw (
Expand All @@ -450,9 +471,9 @@ public override void OnDrawContent (Rectangle viewport)
{
LineCanvas lc = Parent?.LineCanvas;

bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1 && Frame.Height > 1;
bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);

Attribute prevAttr = Driver.GetAttribute ();
Expand All @@ -470,7 +491,7 @@ public override void OnDrawContent (Rectangle viewport)
{
// ╔╡Title╞═════╗
// ╔╡╞═════╗
if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title))
if (borderBounds.Width < 4 || !ShowTitle || string.IsNullOrEmpty (Parent?.Title))
{
// ╔╡╞╗ should be ╔══╗
lc.AddLine (
Expand Down Expand Up @@ -620,7 +641,7 @@ public override void OnDrawContent (Rectangle viewport)
}

// Redraw title
if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
if (drawTop && maxTitleWidth > 0 && ShowTitle)
{
Parent.TitleTextFormatter.Draw (
new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
Expand Down
69 changes: 37 additions & 32 deletions Terminal.Gui/View/Layout/DimAuto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ internal override int Calculate (int location, int superviewContentSize, View us
// TODO: This whole body of code is a WIP (for https://github.com/gui-cs/Terminal.Gui/pull/3451).
subviewsSize = 0;

List<View> includedSubviews = us.Subviews.ToList();//.Where (v => !v.ExcludeFromLayout).ToList ();
List<View> subviews;

#region Not Anchored and Are Not Dependent
Expand All @@ -94,17 +95,17 @@ internal override int Calculate (int location, int superviewContentSize, View us
// [ ] DimView
if (dimension == Dimension.Width)
{
subviews = us.Subviews.Where (v => v.X is not PosAnchorEnd
&& v.X is not PosAlign
&& v.X is not PosCenter
&& v.Width is not DimFill).ToList ();
subviews = includedSubviews.Where (v => v.X is not PosAnchorEnd
&& v.X is not PosAlign
// && v.X is not PosCenter
&& v.Width is not DimFill).ToList ();
}
else
{
subviews = us.Subviews.Where (v => v.Y is not PosAnchorEnd
&& v.Y is not PosAlign
&& v.Y is not PosCenter
&& v.Height is not DimFill).ToList ();
subviews = includedSubviews.Where (v => v.Y is not PosAnchorEnd
&& v.Y is not PosAlign
// && v.Y is not PosCenter
&& v.Height is not DimFill).ToList ();
}

for (var i = 0; i < subviews.Count; i++)
Expand All @@ -126,11 +127,11 @@ internal override int Calculate (int location, int superviewContentSize, View us
// [x] PosAnchorEnd
if (dimension == Dimension.Width)
{
subviews = us.Subviews.Where (v => v.X is PosAnchorEnd).ToList ();
subviews = includedSubviews.Where (v => v.X is PosAnchorEnd).ToList ();
}
else
{
subviews = us.Subviews.Where (v => v.Y is PosAnchorEnd).ToList ();
subviews = includedSubviews.Where (v => v.Y is PosAnchorEnd).ToList ();
}

int maxAnchorEnd = 0;
Expand All @@ -143,38 +144,42 @@ internal override int Calculate (int location, int superviewContentSize, View us
subviewsSize += maxAnchorEnd;
#endregion Anchored

#region Center
// Now, handle subviews that are Centered
if (dimension == Dimension.Width)
{
subviews = us.Subviews.Where (v => v.X is PosCenter).ToList ();
}
else
{
subviews = us.Subviews.Where (v => v.Y is PosCenter).ToList ();
}

int maxCenter = 0;
for (var i = 0; i < subviews.Count; i++)
{
View v = subviews [i];
maxCenter = dimension == Dimension.Width ? v.Frame.Width : v.Frame.Height;
}

subviewsSize += maxCenter;
#endregion Center
//#region Center
//// Now, handle subviews that are Centered
//if (dimension == Dimension.Width)
//{
// subviews = us.Subviews.Where (v => v.X is PosCenter).ToList ();
//}
//else
//{
// subviews = us.Subviews.Where (v => v.Y is PosCenter).ToList ();
//}

//int maxCenter = 0;
//for (var i = 0; i < subviews.Count; i++)
//{
// View v = subviews [i];
// maxCenter = dimension == Dimension.Width ? v.Frame.Width : v.Frame.Height;
//}

//subviewsSize += maxCenter;
//#endregion Center

#region Are Dependent
// Now, go back to those that are dependent on content size
// [x] DimFill
// [ ] DimPercent
if (dimension == Dimension.Width)
{
subviews = us.Subviews.Where (v => v.Width is DimFill).ToList ();
subviews = includedSubviews.Where (v => v.Width is DimFill
// || v.X is PosCenter
).ToList ();
}
else
{
subviews = us.Subviews.Where (v => v.Height is DimFill).ToList ();
subviews = includedSubviews.Where (v => v.Height is DimFill
//|| v.Y is PosCenter
).ToList ();
}

int maxFill = 0;
Expand Down
23 changes: 23 additions & 0 deletions Terminal.Gui/View/Layout/ViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ public Pos Y

// EndLayout

private bool _excludeFromLayout;

/// <summary>
/// Gets or sets whether this View will be excluded from layout computations.
/// </summary>
/// <remarks>
/// If set to <see langword="true"/>, the layout engine will ignore this view. Any subviews of this view will still be laid out.
/// Any views that have Pos or Dim objects that reference this view must have <see cref="ExcludeFromLayout"/> set to <see langword="true"/> as well
/// </remarks>
public bool ExcludeFromLayout
{
get => _excludeFromLayout;
set
{
if (value == _excludeFromLayout)
{
return;
}
_excludeFromLayout = value;
SetNeedsLayout();
}
}

/// <summary>
/// Controls how the View's <see cref="Frame"/> is computed during <see cref="LayoutSubviews"/>. If the style is
/// set to <see cref="LayoutStyle.Absolute"/>, LayoutSubviews does not change the <see cref="Frame"/>. If the style is
Expand Down
16 changes: 13 additions & 3 deletions Terminal.Gui/View/ViewDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ public void Draw ()

OnRenderLineCanvas ();

// TODO: This is a hack to force the border subviews to draw.
if (Border?.Subviews is { })
{
foreach (View view in Border.Subviews)
{
view.SetNeedsDisplay ();
view.Draw ();
}
}

// Invoke DrawContentCompleteEvent
OnDrawContentComplete (Viewport);

Expand Down Expand Up @@ -329,7 +339,7 @@ public void DrawHotString (string text, bool focused, ColorScheme scheme)
public virtual Attribute GetFocusColor ()
{
ColorScheme cs = ColorScheme;
if (ColorScheme is null)
if (cs is null)
{
cs = new ();
}
Expand All @@ -347,7 +357,7 @@ public virtual Attribute GetHotNormalColor ()
{
ColorScheme cs = ColorScheme;

if (ColorScheme is null)
if (cs is null)
{
cs = new ();
}
Expand All @@ -365,7 +375,7 @@ public virtual Attribute GetNormalColor ()
{
ColorScheme cs = ColorScheme;

if (ColorScheme is null)
if (cs is null)
{
cs = new ();
}
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,12 @@ public override void OnDrawContent (Rectangle viewport)

if (i == _selected && IsMenuOpen)
{
hotColor = i == _selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
normalColor = i == _selected ? ColorScheme.Focus : GetNormalColor ();
hotColor = i == _selected ? ColorScheme.HotFocus : GetHotNormalColor ();
normalColor = i == _selected ? GetFocusColor() : GetNormalColor ();
}
else
{
hotColor = ColorScheme.HotNormal;
hotColor = GetHotNormalColor ();
normalColor = GetNormalColor ();
}

Expand Down
Loading

0 comments on commit 7b3f5d2

Please sign in to comment.