@@ -32,7 +32,7 @@ internal static void Draw (IEnumerable<View> views, bool force)
32
32
/// or <see cref="NeedsLayout"/> set.
33
33
/// </para>
34
34
/// <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"/> .
36
36
/// </para>
37
37
/// </remarks>
38
38
public void Draw ( )
@@ -44,32 +44,40 @@ public void Draw ()
44
44
45
45
Region ? saved = GetClip ( ) ;
46
46
47
+ // TODO: This can be further optimized by checking NeedsDraw below and only clearing, drawing text, drawing content, etc. if it is true.
47
48
if ( NeedsDraw || SubViewNeedsDraw )
48
49
{
50
+ // Draw the Border and Padding.
51
+ // We clip to the frame to prevent drawing outside the frame.
49
52
saved = ClipFrame ( ) ;
50
53
DoDrawBorderAndPadding ( ) ;
51
54
SetClip ( saved ) ;
52
55
56
+ // Draw the content within the Viewport
53
57
// By default, we clip to the viewport preventing drawing outside the viewport
54
58
// We also clip to the content, but if a developer wants to draw outside the viewport, they can do
55
59
// so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
56
60
// Get our Viewport in screen coordinates
57
61
58
62
saved = ClipViewport ( ) ;
59
63
64
+ // Clear the viewport
60
65
// TODO: Simplify/optimize SetAttribute system.
61
66
DoSetAttribute ( ) ;
62
67
DoClearViewport ( ) ;
63
68
69
+ // Draw the subviews
64
70
if ( SubViewNeedsDraw )
65
71
{
66
72
DoSetAttribute ( ) ;
67
73
DoDrawSubviews ( ) ;
68
74
}
69
75
76
+ // Draw the text
70
77
DoSetAttribute ( ) ;
71
78
DoDrawText ( ) ;
72
79
80
+ // Draw the content
73
81
DoSetAttribute ( ) ;
74
82
DoDrawContent ( ) ;
75
83
@@ -79,10 +87,14 @@ public void Draw ()
79
87
80
88
saved = ClipFrame ( ) ;
81
89
90
+ // Draw the line canvas
82
91
DoRenderLineCanvas ( ) ;
83
92
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.
84
95
DoDrawBorderAndPaddingSubViews ( ) ;
85
96
97
+ // Advance the diagnostics draw indicator
86
98
Border ? . AdvanceDrawIndicator ( ) ;
87
99
88
100
ClearNeedsDraw ( ) ;
@@ -99,7 +111,7 @@ public void Draw ()
99
111
SetClip ( saved ) ;
100
112
101
113
// Exclude this view (not including Margin) from the Clip
102
- if ( this is not Adornment && GetClip ( ) is { } )
114
+ if ( this is not Adornment )
103
115
{
104
116
Rectangle borderFrame = FrameToScreen ( ) ;
105
117
@@ -463,6 +475,7 @@ public void DrawSubviews ()
463
475
return ;
464
476
}
465
477
478
+ // Draw the subviews in reverse order to leverage clipping.
466
479
foreach ( View view in _subviews . Where ( view => view . Visible ) . Reverse ( ) )
467
480
{
468
481
view . Draw ( ) ;
@@ -592,11 +605,10 @@ protected virtual void OnDrawComplete () { }
592
605
#region NeedsDraw
593
606
594
607
// TODO: Change NeedsDraw to use a Region instead of Rectangle
595
-
596
608
// 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
600
612
// The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
601
613
internal Rectangle _needsDrawRect = Rectangle . Empty ;
602
614
@@ -612,7 +624,7 @@ protected virtual void OnDrawComplete () { }
612
624
/// </remarks>
613
625
public bool NeedsDraw
614
626
{
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.
616
628
get => Visible && ( _needsDrawRect != Rectangle . Empty || NeedsLayout ) ;
617
629
set
618
630
{
@@ -627,22 +639,8 @@ public bool NeedsDraw
627
639
}
628
640
}
629
641
630
- private bool _subViewNeedsDraw ;
631
-
632
642
/// <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 ; }
646
644
647
645
/// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
648
646
/// <remarks>
@@ -653,7 +651,7 @@ public void SetNeedsDraw ()
653
651
{
654
652
Rectangle viewport = Viewport ;
655
653
656
- if ( /* !Visible || */ ( _needsDrawRect != Rectangle . Empty && viewport . IsEmpty ) )
654
+ if ( ! Visible || ( _needsDrawRect != Rectangle . Empty && viewport . IsEmpty ) )
657
655
{
658
656
// This handles the case where the view has not been initialized yet
659
657
return ;
@@ -675,10 +673,11 @@ public void SetNeedsDraw ()
675
673
/// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
676
674
public void SetNeedsDraw ( Rectangle viewPortRelativeRegion )
677
675
{
678
- //if (!Visible)
679
- //{
680
- // return;
681
- //}
676
+ if ( ! Visible )
677
+ {
678
+ return ;
679
+ }
680
+
682
681
if ( _needsDrawRect . IsEmpty )
683
682
{
684
683
_needsDrawRect = viewPortRelativeRegion ;
@@ -692,10 +691,7 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
692
691
_needsDrawRect = new ( x , y , w , h ) ;
693
692
}
694
693
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.
699
695
700
696
if ( Border is { } && Border . Thickness != Thickness . Empty )
701
697
{
@@ -709,7 +705,7 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
709
705
710
706
SuperView ? . SetSubViewNeedsDraw ( ) ;
711
707
712
- if ( this is Adornment adornment /*and (Gui.Padding or Gui.Border)*/ )
708
+ if ( this is Adornment adornment )
713
709
{
714
710
adornment . Parent ? . SetSubViewNeedsDraw ( ) ;
715
711
}
@@ -729,11 +725,10 @@ public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
729
725
/// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
730
726
public void SetSubViewNeedsDraw ( )
731
727
{
732
- //if (!Visible)
733
- //{
734
- // SubViewNeedsDraw = false;
735
- // return;
736
- //}
728
+ if ( ! Visible )
729
+ {
730
+ return ;
731
+ }
737
732
738
733
SubViewNeedsDraw = true ;
739
734
0 commit comments