Skip to content

Commit 0758833

Browse files
committed
Add window rendering optimizations
Renderer.cs: - Add ClearDestinationOnWindowMove option check - Allows disabling background clear during window moves - Reduces flicker for performance-critical scenarios VisibleRegions.cs: - Add CalculateVisibleRegions() for arbitrary rectangles - Used for desktop clears to avoid overwriting visible windows - Reuses pooled buffers to avoid allocations - Subtracts overlapping window areas from visible regions
1 parent 1f17765 commit 0758833

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

SharpConsoleUI/Renderer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ public void RenderWindow(Window window)
341341
}
342342

343343
// Fill the background only for the visible regions
344-
foreach (var region in visibleRegions)
344+
// Can be disabled via ClearDestinationOnWindowMove option to reduce flicker during moves
345+
if (_consoleWindowSystem.Options.ClearDestinationOnWindowMove)
345346
{
346-
FillRect(region.Left, region.Top, region.Width, region.Height, ' ', window.BackgroundColor, null);
347+
foreach (var region in visibleRegions)
348+
{
349+
FillRect(region.Left, region.Top, region.Width, region.Height, ' ', window.BackgroundColor, null);
350+
}
347351
}
348352

349353
// Draw window borders - these might be partially hidden but the drawing functions

SharpConsoleUI/VisibleRegions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ public List<Rectangle> CalculateVisibleRegions(Window window, List<Window> overl
6868
return new List<Rectangle>(current);
6969
}
7070

71+
/// <summary>
72+
/// Calculates visible regions of an arbitrary rectangle by subtracting overlapping windows.
73+
/// Used for desktop clears to avoid overwriting visible windows.
74+
/// </summary>
75+
/// <param name="area">The rectangle area to calculate visibility for.</param>
76+
/// <param name="overlappingWindows">Windows that overlap with the area.</param>
77+
/// <returns>List of rectangles representing visible portions (not covered by windows).</returns>
78+
public List<Rectangle> CalculateVisibleRegions(Rectangle area, List<Window> overlappingWindows)
79+
{
80+
// Use pooled buffers to avoid allocations
81+
_regionsBuffer1.Clear();
82+
_regionsBuffer1.Add(area); // Start with full area
83+
84+
var current = _regionsBuffer1;
85+
var next = _regionsBuffer2;
86+
87+
// For each overlapping window, subtract its area from the visible regions
88+
foreach (var other in overlappingWindows)
89+
{
90+
var overlappingRect = new Rectangle(
91+
other.Left,
92+
other.Top,
93+
other.Width,
94+
other.Height);
95+
96+
next.Clear();
97+
SubtractRectangle(current, overlappingRect, next);
98+
99+
// Swap buffers for next iteration
100+
var temp = current;
101+
current = next;
102+
next = temp;
103+
}
104+
105+
// Return copy for caller (they expect ownership)
106+
return new List<Rectangle>(current);
107+
}
108+
71109
private bool DoRectanglesIntersect(Rectangle r1, Rectangle r2)
72110
{
73111
return r1.Left < r2.Left + r2.Width &&

0 commit comments

Comments
 (0)