Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with tabbed content underline #2274

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from time import monotonic
from typing import TYPE_CHECKING, ClassVar

import rich.repr
Expand All @@ -22,6 +23,10 @@
from typing_extensions import Self


THROTTLE_DELTA = 0.005
"""Don't call `watch_active` more than once every THROTTLE_DELTA seconds."""


class Underline(Widget):
"""The animated underline beneath tabs."""

Expand Down Expand Up @@ -175,6 +180,9 @@ class Tabs(Widget, can_focus=True):
| right | Move to the next tab. |
"""

_last_update: float = -1
"""Time of last update to throttle updates."""

class TabActivated(Message):
"""Sent when a new tab is activated."""

Expand Down Expand Up @@ -399,7 +407,16 @@ def compose(self) -> ComposeResult:
yield Underline()

def watch_active(self, previously_active: str, active: str) -> None:
"""Handle a change to the active tab."""
"""Handle a change to the active tab.

This function is throttled to only run once every THROTTLE_DELTA seconds.
"""

now = monotonic()
if now < self._last_update + THROTTLE_DELTA:
return
self._last_update = now

if active:
active_tab = self.query_one(f"#tabs-list > #{active}", Tab)
self.query("#tabs-list > Tab.-active").remove_class("-active")
Expand All @@ -420,7 +437,7 @@ def _highlight_active(self, animate: bool = True) -> None:
"""
underline = self.query_one(Underline)
try:
active_tab = self.query_one(f"#tabs-list > Tab.-active")
active_tab = self.query_one("#tabs-list > Tab.-active")
except NoMatches:
underline.highlight_start = 0
underline.highlight_end = 0
Expand Down Expand Up @@ -504,5 +521,6 @@ def _move_tab(self, direction: int) -> None:
return
tab_count = len(tabs)
new_tab_index = (tabs.index(active_tab) + direction) % tab_count
print(tabs[new_tab_index].id or "")
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
self.active = tabs[new_tab_index].id or ""
self._scroll_active_tab()