wijjit.layout.dirty.DirtyRegionManager

class wijjit.layout.dirty.DirtyRegionManager[source]

Manages dirty regions for optimized screen rendering.

This class tracks rectangular regions of the screen that need to be redrawn and automatically merges overlapping or adjacent regions to minimize rendering overhead.

The manager limits the number of tracked regions to avoid O(n^2) worst-case performance in the merging algorithm. When the region count exceeds MAX_REGIONS, the manager switches to full-screen dirty mode for efficiency.

Variables:
  • _regions (list[DirtyRegion]) – List of dirty regions

  • _full_screen_dirty (bool) – Whether the entire screen is marked as dirty

  • _screen_width (int or None) – Screen width (set when marking full screen dirty)

  • _screen_height (int or None) – Screen height (set when marking full screen dirty)

  • Attributes (Class)

  • ----------------

  • MAX_REGIONS (int) – Maximum number of regions to track before switching to full-screen dirty mode. Default is 64, which provides good balance between region tracking precision and merge algorithm performance.

Examples

>>> manager = DirtyRegionManager()
>>> manager.mark_dirty(0, 0, 10, 5)
>>> manager.mark_dirty(5, 0, 10, 5)  # Overlaps, will be merged
>>> regions = manager.get_merged_regions()
>>> len(regions)
1
>>> regions[0]
DirtyRegion(x=0, y=0, width=15, height=5)
__init__()[source]

Initialize the dirty region manager.

Return type:

None

Methods

__init__()

Initialize the dirty region manager.

clear()

Clear all dirty regions.

get_merged_regions()

Get all dirty regions as merged rectangles.

is_dirty()

Check if there are any dirty regions.

is_full_screen_dirty()

Check if the full screen is marked as dirty.

mark_dirty(x, y, width, height)

Mark a rectangular region as dirty.

mark_dirty_bounds(bounds)

Mark a region as dirty using a Bounds object.

mark_full_screen(width, height)

Mark the entire screen as dirty.

Attributes

MAX_REGIONS

MAX_REGIONS: int = 64
mark_dirty(x, y, width, height)[source]

Mark a rectangular region as dirty.

The region will be merged with any overlapping or adjacent regions automatically to optimize rendering.

Parameters:
  • x (int) – Left edge column position

  • y (int) – Top edge row position

  • width (int) – Width in columns

  • height (int) – Height in rows

Return type:

None

Notes

If the full screen is already marked as dirty, this method has no effect since the entire screen will be redrawn anyway.

mark_dirty_bounds(bounds)[source]

Mark a region as dirty using a Bounds object.

Parameters:

bounds (Bounds) – The bounds to mark as dirty

Return type:

None

mark_full_screen(width, height)[source]

Mark the entire screen as dirty.

This clears all individual dirty regions and marks the whole screen for redraw. Use this for major layout changes or when the cost of tracking individual regions exceeds the benefit.

Parameters:
  • width (int) – Screen width in columns

  • height (int) – Screen height in rows

Return type:

None

get_merged_regions()[source]

Get all dirty regions as merged rectangles.

Returns dirty regions with overlapping/adjacent regions merged to minimize the number of screen updates.

Returns:

List of dirty regions as (x, y, width, height) tuples

Return type:

list[tuple[int, int, int, int]]

Notes

If the full screen is marked as dirty, returns a single region covering the entire screen.

is_full_screen_dirty()[source]

Check if the full screen is marked as dirty.

Returns:

True if full screen needs redraw

Return type:

bool

is_dirty()[source]

Check if there are any dirty regions.

Returns:

True if there are dirty regions or full screen is dirty

Return type:

bool

clear()[source]

Clear all dirty regions.

Call this after rendering is complete to reset the dirty state.

Return type:

None

__repr__()[source]

String representation of the dirty region manager.

Returns:

String representation showing dirty state

Return type:

str