Skip to content

Commit ab591d1

Browse files
tigBDisp
authored andcommitted
Fixes gui-cs#3984 - Margin w/out shadow should not force draw (gui-cs#3985)
* shortcut tests * Generic demos * Optimize Margin to not defer draw if there's no shadow
1 parent bdaa466 commit ab591d1

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

Terminal.Gui/View/Adornment/Margin.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Margin (View parent) : base (parent)
4848

4949
internal void CacheClip ()
5050
{
51-
if (Thickness != Thickness.Empty)
51+
if (Thickness != Thickness.Empty && ShadowStyle != ShadowStyle.None)
5252
{
5353
// PERFORMANCE: How expensive are these clones?
5454
_cachedClip = GetClip ()?.Clone ();

Terminal.Gui/View/View.Drawing.cs

+24-14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal static void Draw (IEnumerable<View> views, bool force)
2727
view.Draw (context);
2828
}
2929

30+
// Draw the margins (those whith Shadows) last to ensure they are drawn on top of the content.
3031
Margin.DrawMargins (viewsArray);
3132
}
3233

@@ -57,9 +58,9 @@ public void Draw (DrawContext? context = null)
5758
{
5859
// ------------------------------------
5960
// Draw the Border and Padding.
60-
// Note Margin is special-cased and drawn in a separate pass to support
61+
// Note Margin with a Shadow is special-cased and drawn in a separate pass to support
6162
// transparent shadows.
62-
DoDrawBorderAndPadding (originalClip);
63+
DoDrawAdornments (originalClip);
6364
SetClip (originalClip);
6465

6566
// ------------------------------------
@@ -106,7 +107,7 @@ public void Draw (DrawContext? context = null)
106107
// ------------------------------------
107108
// Re-draw the border and padding subviews
108109
// HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas.
109-
DoDrawBorderAndPaddingSubViews ();
110+
DoDrawAdornmentsSubViews ();
110111

111112
// ------------------------------------
112113
// Advance the diagnostics draw indicator
@@ -116,8 +117,8 @@ public void Draw (DrawContext? context = null)
116117
}
117118

118119
// ------------------------------------
119-
// This causes the Margin to be drawn in a second pass
120-
// PERFORMANCE: If there is a Margin, it will be redrawn each iteration of the main loop.
120+
// This causes the Margin to be drawn in a second pass if it has a ShadowStyle
121+
// PERFORMANCE: If there is a Margin w/ Shadow, it will be redrawn each iteration of the main loop.
121122
Margin?.CacheClip ();
122123

123124
// ------------------------------------
@@ -131,8 +132,11 @@ public void Draw (DrawContext? context = null)
131132

132133
#region DrawAdornments
133134

134-
private void DoDrawBorderAndPaddingSubViews ()
135+
private void DoDrawAdornmentsSubViews ()
135136
{
137+
138+
// NOTE: We do not support subviews of Margin?
139+
136140
if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty)
137141
{
138142
// PERFORMANCE: Get the check for DrawIndicator out of this somehow.
@@ -164,7 +168,7 @@ private void DoDrawBorderAndPaddingSubViews ()
164168
}
165169
}
166170

167-
private void DoDrawBorderAndPadding (Region? originalClip)
171+
private void DoDrawAdornments (Region? originalClip)
168172
{
169173
if (this is Adornment)
170174
{
@@ -194,27 +198,28 @@ private void DoDrawBorderAndPadding (Region? originalClip)
194198
// A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen.
195199
Border?.SetNeedsDraw ();
196200
Padding?.SetNeedsDraw ();
201+
Margin?.SetNeedsDraw ();
197202
}
198203

199-
if (OnDrawingBorderAndPadding ())
204+
if (OnDrawingAdornments ())
200205
{
201206
return;
202207
}
203208

204209
// TODO: add event.
205210

206-
DrawBorderAndPadding ();
211+
DrawAdornments ();
207212
}
208213

209214
/// <summary>
210-
/// Causes <see cref="Border"/> and <see cref="Padding"/> to be drawn.
215+
/// Causes <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> to be drawn.
211216
/// </summary>
212217
/// <remarks>
213218
/// <para>
214-
/// <see cref="Margin"/> is drawn in a separate pass.
219+
/// <see cref="Margin"/> is drawn in a separate pass if <see cref="ShadowStyle"/> is set.
215220
/// </para>
216221
/// </remarks>
217-
public void DrawBorderAndPadding ()
222+
public void DrawAdornments ()
218223
{
219224
// We do not attempt to draw Margin. It is drawn in a separate pass.
220225

@@ -230,6 +235,11 @@ public void DrawBorderAndPadding ()
230235
Padding?.Draw ();
231236
}
232237

238+
239+
if (Margin is { } && Margin.Thickness != Thickness.Empty && Margin.ShadowStyle == ShadowStyle.None)
240+
{
241+
Margin?.Draw ();
242+
}
233243
}
234244

235245
private void ClearFrame ()
@@ -255,7 +265,7 @@ private void ClearFrame ()
255265
/// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
256266
/// </summary>
257267
/// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
258-
protected virtual bool OnDrawingBorderAndPadding () { return false; }
268+
protected virtual bool OnDrawingAdornments () { return false; }
259269

260270
#endregion DrawAdornments
261271

@@ -635,7 +645,7 @@ private void DoRenderLineCanvas ()
635645
/// <summary>
636646
/// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
637647
/// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
638-
/// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingBorderAndPadding"/> method will
648+
/// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will
639649
/// be
640650
/// called to render the borders.
641651
/// </summary>

Terminal.Gui/Views/GraphView/Annotations.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void Render (GraphView graph)
150150

151151
if (BorderStyle != LineStyle.None)
152152
{
153-
DrawBorderAndPadding ();
153+
DrawAdornments ();
154154
RenderLineCanvas ();
155155
}
156156

Terminal.Gui/Views/Menu/Menu.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ private void Top_DrawComplete (object? sender, DrawEventArgs e)
831831
return;
832832
}
833833

834-
DrawBorderAndPadding ();
834+
DrawAdornments ();
835835
RenderLineCanvas ();
836836

837837
// BUGBUG: Views should not change the clip. Doing so is an indcation of poor design or a bug in the framework.

Terminal.Gui/Views/TileView.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public int IndexOf (View toFind, bool recursive = false)
173173

174174
/// <summary>Overridden so no Frames get drawn</summary>
175175
/// <returns></returns>
176-
protected override bool OnDrawingBorderAndPadding () { return true; }
176+
protected override bool OnDrawingAdornments () { return true; }
177177

178178
/// <inheritdoc/>
179179
protected override bool OnRenderingLineCanvas () { return false; }

UICatalog/Scenarios/Generic.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ public override void Main ()
1818
Title = GetQuitKeyAndName (),
1919
};
2020

21-
var button = new Button { Id = "button", X = Pos.Center (), Y = 1, Text = "_Press me!" };
21+
FrameView frame = new ()
22+
{
23+
Height = Dim.Fill (),
24+
Width = Dim.Fill (),
25+
Title = "Frame"
26+
};
27+
appWindow.Add (frame);
28+
29+
var button = new Shortcut ()
30+
{
31+
Id = "button",
32+
X = Pos.Center (),
33+
Y = 1,
34+
Text = "_Press me!"
35+
};
2236

2337
button.Accepting += (s, e) =>
2438
{
@@ -27,7 +41,7 @@ public override void Main ()
2741
MessageBox.ErrorQuery ("Error", "You pressed the button!", "_Ok");
2842
};
2943

30-
appWindow.Add (button);
44+
frame.Add (button);
3145

3246
// Run - Start the application.
3347
Application.Run (appWindow);

0 commit comments

Comments
 (0)