Simplify find_next_enabled

This commit is contained in:
Darren Burns
2024-10-24 14:21:32 +01:00
parent 0bb81368e9
commit 952ee22edc
2 changed files with 7 additions and 13 deletions

View File

@@ -16,4 +16,4 @@ def timer(subject: str = "time") -> Generator[None, None, None]:
yield
elapsed = perf_counter() - start
elapsed_ms = elapsed * 1000
log(f"{subject} elapsed {elapsed_ms:.2f}ms")
log(f"{subject} elapsed {elapsed_ms:.4f}ms")

View File

@@ -8,12 +8,13 @@ to implement wrapping.
from __future__ import annotations
from functools import partial
from itertools import count
from typing import Literal, Protocol, Sequence
from typing_extensions import TypeAlias
from textual._loop import loop_from_index
class Disableable(Protocol):
"""Non-widgets that have an enabled/disabled status."""
@@ -136,17 +137,10 @@ def find_next_enabled(
)
return None
start = anchor + direction if not with_anchor else anchor
key_function = partial(
get_directed_distance,
start=start,
direction=direction,
wrap_at=len(candidates),
)
enabled_candidates = [
index for index, candidate in enumerate(candidates) if not candidate.disabled
]
return min(enabled_candidates, key=key_function, default=anchor)
for index, candidate in loop_from_index(candidates, anchor, direction, wrap=True):
if not candidate.disabled:
return index
return None
def find_next_enabled_no_wrap(