fix get_widget_at

This commit is contained in:
Will McGugan
2022-10-01 15:15:51 +01:00
parent 02cb0313cb
commit 4c224fb795
2 changed files with 15 additions and 12 deletions

View File

@@ -201,7 +201,10 @@ class Compositor:
# Regions that require an update
self._dirty_regions: set[Region] = set()
self._layers_visible: dict[int, list[tuple[Widget, Region]]] | None = None
# Mapping of line numbers on to lists of widget and regions
self._layers_visible: dict[
int, list[tuple[Widget, Region, Region]]
] | None = None
@classmethod
def _regions_to_spans(
@@ -477,22 +480,22 @@ class Compositor:
return self._layers
@property
def layers_visible(self) -> dict[int, list[tuple[Widget, Region]]]:
def layers_visible(self) -> dict[int, list[tuple[Widget, Region, Region]]]:
"""Visible widgets and regions in layers order."""
if self._layers_visible is None:
layers_visible: dict[int, list[tuple[Widget, Region]]]
layers_visible: dict[int, list[tuple[Widget, Region, Region]]]
screen_region = self.size.region
_, screen_height = self.size
layers_visible = {y: [] for y in screen_region.line_range}
visible_intersection = screen_region.intersection
for widget, region, *_ in self:
(_x, y, _width, height) = region
for widget, cropped_region, region, *_ in self:
(_x, y, _width, height) = cropped_region
if y + height > 0 and y < screen_height:
for y in visible_intersection(region).line_range:
layers_visible[y].append((widget, region))
for y in visible_intersection(cropped_region).line_range:
layers_visible[y].append((widget, cropped_region, region))
self._layers_visible = layers_visible
return self._layers_visible
@@ -534,11 +537,10 @@ class Compositor:
Returns:
tuple[Widget, Region]: A tuple of the widget and its region.
"""
# TODO: Optimize with some line based lookup
contains = Region.contains
for widget, region in self.layers_visible.get(y, []):
if contains(region, x, y) and widget.visible:
for widget, cropped_region, region in self.layers_visible.get(y, []):
if contains(cropped_region, x, y) and widget.visible:
return widget, region
raise errors.NoWidget(f"No widget under screen coordinate ({x}, {y})")
@@ -553,8 +555,8 @@ class Compositor:
Iterable[tuple[Widget, Region]]: Sequence of (WIDGET, REGION) tuples.
"""
contains = Region.contains
for widget, region in self.layers_visible.get(y, []):
if contains(region, x, y) and widget.visible:
for widget, cropped_region, region in self.layers_visible.get(y, []):
if contains(cropped_region, x, y) and widget.visible:
yield widget, region
def get_style_at(self, x: int, y: int) -> Style:

View File

@@ -327,6 +327,7 @@ class TreeControl(Generic[NodeDataType], Static, can_focus=True):
else node.label
)
if node.id == self.hover_node:
1 / 0
label.stylize("underline")
label.apply_meta({"@click": f"click_label({node.id})", "tree_node": node.id})
return label