mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Simplify find_next_enabled
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user