optimize from_union add docstrings

This commit is contained in:
Will McGugan
2022-06-28 10:36:16 +01:00
parent 86fdc96ab3
commit cec49dbbc2
4 changed files with 34 additions and 13 deletions

View File

@@ -15,9 +15,9 @@
scrollbar-size-vertical: 2;
}
*:hover {
/* *:hover {
tint: red 30%;
}
} */
App > Screen {
layout: dock;

View File

@@ -186,6 +186,8 @@ class Border:
if has_bottom and lines:
lines.pop(-1)
# TODO: Divide is probably quite inefficient here,
# It could be much faster for the specific case of one off the start end end
divide = Segment.divide
if has_left and has_right:
for line in lines:

View File

@@ -8,6 +8,7 @@ from __future__ import annotations
import sys
from functools import lru_cache
from operator import itemgetter, attrgetter
from typing import Any, Collection, NamedTuple, Tuple, TypeVar, Union, cast
if sys.version_info >= (3, 10):
@@ -93,7 +94,10 @@ class Offset(NamedTuple):
"""
x1, y1 = self
x2, y2 = destination
return Offset(int(x1 + (x2 - x1) * factor), int((y1 + (y2 - y1) * factor)))
return Offset(
int(x1 + (x2 - x1) * factor),
int(y1 + (y2 - y1) * factor),
)
def get_distance_to(self, other: Offset) -> float:
"""Get the distance to another offset.
@@ -195,7 +199,14 @@ class Region(NamedTuple):
height: int = 0
@classmethod
def from_union(cls, regions: Collection[Region]) -> Region:
def from_union(
cls,
regions: Collection[Region],
_get_x=itemgetter(0),
_get_y=itemgetter(1),
_get_right=attrgetter("right"),
_get_bottom=attrgetter("bottom"),
) -> Region:
"""Create a Region from the union of other regions.
Args:
@@ -206,10 +217,10 @@ class Region(NamedTuple):
"""
if not regions:
raise ValueError("At least one region expected")
min_x = min([region.x for region in regions])
max_x = max([x + width for x, _y, width, _height in regions])
min_y = min([region.y for region in regions])
max_y = max([y + height for _x, y, _width, height in regions])
min_x = min(regions, key=_get_x).x
max_x = max(regions, key=_get_right).right
min_y = min(regions, key=_get_y).y
max_y = max(regions, key=_get_bottom).bottom
return cls(min_x, min_y, max_x - min_x, max_y - min_y)
@classmethod

View File

@@ -428,14 +428,17 @@ class Widget(DOMNode):
)
def _set_dirty(self, *regions: Region) -> None:
"""Set the Widget as 'dirty' (requiring re-render)."""
"""Set the Widget as 'dirty' (requiring re-paint).
# self._dirty_regions.clear()
# # TODO: Does this need to be content region?
# self._dirty_regions.append(self.size.region)
Regions should be specified as positional args. If no regions are added, then
the entire widget will be considered dirty.
Args:
*regions (Region): Regions which require a repaint.
"""
if regions:
content_offset = self.content_offset
self._dirty_regions.update(regions)
else:
self._dirty_regions.clear()
@@ -444,6 +447,11 @@ class Widget(DOMNode):
self._dirty_regions.add(self.size.region)
def get_dirty_regions(self) -> Collection[Region]:
"""Get regions which require a repaint.
Returns:
Collection[Region]: Regions to repaint.
"""
regions = self._dirty_regions.copy()
return regions