Skip to content

Commit c0ee541

Browse files
committed
Code cleanup and API docs
1 parent a5badb8 commit c0ee541

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

Terminal.Gui/View/View.Drawing.cs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static void Draw (IEnumerable<View> views, bool force)
3232
/// or <see cref="NeedsLayout"/> set.
3333
/// </para>
3434
/// <para>
35-
/// // TODO: Add docs for the drawing process.
35+
/// See the View Drawing Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/drawing.html"/>.
3636
/// </para>
3737
/// </remarks>
3838
public void Draw ()
@@ -44,32 +44,40 @@ public void Draw ()
4444

4545
Region? saved = GetClip ();
4646

47+
// TODO: This can be further optimized by checking NeedsDraw below and only clearing, drawing text, drawing content, etc. if it is true.
4748
if (NeedsDraw || SubViewNeedsDraw)
4849
{
50+
// Draw the Border and Padding.
51+
// We clip to the frame to prevent drawing outside the frame.
4952
saved = ClipFrame ();
5053
DoDrawBorderAndPadding ();
5154
SetClip (saved);
5255

56+
// Draw the content within the Viewport
5357
// By default, we clip to the viewport preventing drawing outside the viewport
5458
// We also clip to the content, but if a developer wants to draw outside the viewport, they can do
5559
// so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
5660
// Get our Viewport in screen coordinates
5761

5862
saved = ClipViewport ();
5963

64+
// Clear the viewport
6065
// TODO: Simplify/optimize SetAttribute system.
6166
DoSetAttribute ();
6267
DoClearViewport ();
6368

69+
// Draw the subviews
6470
if (SubViewNeedsDraw)
6571
{
6672
DoSetAttribute ();
6773
DoDrawSubviews ();
6874
}
6975

76+
// Draw the text
7077
DoSetAttribute ();
7178
DoDrawText ();
7279

80+
// Draw the content
7381
DoSetAttribute ();
7482
DoDrawContent ();
7583

@@ -79,10 +87,14 @@ public void Draw ()
7987

8088
saved = ClipFrame ();
8189

90+
// Draw the line canvas
8291
DoRenderLineCanvas ();
8392

93+
// Re-draw the border and padding subviews
94+
// HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas.
8495
DoDrawBorderAndPaddingSubViews ();
8596

97+
// Advance the diagnostics draw indicator
8698
Border?.AdvanceDrawIndicator ();
8799

88100
ClearNeedsDraw ();
@@ -99,7 +111,7 @@ public void Draw ()
99111
SetClip (saved);
100112

101113
// Exclude this view (not including Margin) from the Clip
102-
if (this is not Adornment && GetClip () is { })
114+
if (this is not Adornment)
103115
{
104116
Rectangle borderFrame = FrameToScreen ();
105117

@@ -463,6 +475,7 @@ public void DrawSubviews ()
463475
return;
464476
}
465477

478+
// Draw the subviews in reverse order to leverage clipping.
466479
foreach (View view in _subviews.Where (view => view.Visible).Reverse ())
467480
{
468481
view.Draw ();
@@ -592,11 +605,10 @@ protected virtual void OnDrawComplete () { }
592605
#region NeedsDraw
593606

594607
// TODO: Change NeedsDraw to use a Region instead of Rectangle
595-
596608
// TODO: Make _needsDrawRect nullable instead of relying on Empty
597-
// TODO: If null, it means ?
598-
// TODO: If Empty, it means no need to redraw
599-
// TODO: If not Empty, it means the region that needs to be redrawn
609+
// TODO: If null, it means ?
610+
// TODO: If Empty, it means no need to redraw
611+
// TODO: If not Empty, it means the region that needs to be redrawn
600612
// The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
601613
internal Rectangle _needsDrawRect = Rectangle.Empty;
602614

@@ -612,7 +624,7 @@ protected virtual void OnDrawComplete () { }
612624
/// </remarks>
613625
public bool NeedsDraw
614626
{
615-
// TODO: Figure out if we can decouple NeedsDraw from NeedsLayout. This is a temporary fix.
627+
// TODO: Figure out if we can decouple NeedsDraw from NeedsLayout.
616628
get => Visible && (_needsDrawRect != Rectangle.Empty || NeedsLayout);
617629
set
618630
{
@@ -627,22 +639,8 @@ public bool NeedsDraw
627639
}
628640
}
629641

630-
private bool _subViewNeedsDraw;
631-
632642
/// <summary>Gets whether any Subviews need to be redrawn.</summary>
633-
public bool SubViewNeedsDraw
634-
{
635-
get => _subViewNeedsDraw;
636-
private set
637-
{
638-
//if (!Visible)
639-
//{
640-
// _subViewNeedsDraw = false;
641-
// return;
642-
//}
643-
_subViewNeedsDraw = value;
644-
}
645-
}
643+
public bool SubViewNeedsDraw { get; private set; }
646644

647645
/// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
648646
/// <remarks>
@@ -653,7 +651,7 @@ public void SetNeedsDraw ()
653651
{
654652
Rectangle viewport = Viewport;
655653

656-
if (/*!Visible || */(_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
654+
if (!Visible || (_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
657655
{
658656
// This handles the case where the view has not been initialized yet
659657
return;
@@ -675,10 +673,11 @@ public void SetNeedsDraw ()
675673
/// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
676674
public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
677675
{
678-
//if (!Visible)
679-
//{
680-
// return;
681-
//}
676+
if (!Visible)
677+
{
678+
return;
679+
}
680+
682681
if (_needsDrawRect.IsEmpty)
683682
{
684683
_needsDrawRect = viewPortRelativeRegion;
@@ -692,10 +691,7 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
692691
_needsDrawRect = new (x, y, w, h);
693692
}
694693

695-
//if (Margin is { } && Margin.Thickness != Thickness.Empty)
696-
//{
697-
// Margin?.SetNeedsDraw ();
698-
//}
694+
// Do not set on Margin - it will be drawn in a separate pass.
699695

700696
if (Border is { } && Border.Thickness != Thickness.Empty)
701697
{
@@ -709,7 +705,7 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
709705

710706
SuperView?.SetSubViewNeedsDraw ();
711707

712-
if (this is Adornment adornment /*and (Gui.Padding or Gui.Border)*/)
708+
if (this is Adornment adornment)
713709
{
714710
adornment.Parent?.SetSubViewNeedsDraw ();
715711
}
@@ -729,11 +725,10 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
729725
/// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
730726
public void SetSubViewNeedsDraw ()
731727
{
732-
//if (!Visible)
733-
//{
734-
// SubViewNeedsDraw = false;
735-
// return;
736-
//}
728+
if (!Visible)
729+
{
730+
return;
731+
}
737732

738733
SubViewNeedsDraw = true;
739734

0 commit comments

Comments
 (0)