11from __future__ import annotations
22
3- import asyncio
43from dataclasses import dataclass
54from typing import ClassVar
65
@@ -520,27 +519,20 @@ def remove_tab(self, tab_or_id: Tab | str | None) -> AwaitComplete:
520519 except NoMatches :
521520 return AwaitComplete ()
522521
523- removing_active_tab = remove_tab .has_class ("-active" )
524- next_tab = self ._next_active
525- remove_await = remove_tab .remove ()
526-
527- highlight_updated = asyncio .Event ()
522+ if remove_tab .has_class ("-active" ):
523+ next_tab = self ._next_active
524+ else :
525+ next_tab = None
528526
529527 async def do_remove () -> None :
530528 """Perform the remove after refresh so the underline bar gets new positions."""
531- await remove_await
532- if next_tab is None or (removing_active_tab and next_tab .id is None ):
533- self .active = ""
534- elif removing_active_tab :
529+ await remove_tab .remove ()
530+ if next_tab is not None :
535531 self .active = next_tab .id or ""
536- next_tab .add_class ("-active" )
537-
538- highlight_updated .set ()
539-
540- async def wait_for_highlight_update () -> None :
541- await highlight_updated .wait ()
532+ if not self .query ("#tabs-list > Tab" ):
533+ self .active = ""
542534
543- return AwaitComplete (do_remove (), wait_for_highlight_update () )
535+ return AwaitComplete (do_remove ())
544536
545537 def validate_active (self , active : str ) -> str :
546538 """Check id assigned to active attribute is a valid tab."""
@@ -584,7 +576,9 @@ def watch_active(self, previously_active: str, active: str) -> None:
584576 except NoMatches :
585577 return
586578 active_tab .add_class ("-active" )
579+
587580 self ._highlight_active (animate = previously_active != "" )
581+
588582 self ._scroll_active_tab ()
589583 self .post_message (self .TabActivated (self , active_tab ))
590584 else :
@@ -604,29 +598,30 @@ def _highlight_active(
604598 """
605599 underline = self .query_one (Underline )
606600 try :
607- active_tab = self .query_one (f "#tabs-list > Tab.-active" )
601+ _active_tab = self .query_one ("#tabs-list > Tab.-active" )
608602 except NoMatches :
609603 underline .show_highlight = False
610604 underline .highlight_start = 0
611605 underline .highlight_end = 0
612606 else :
613607 underline .show_highlight = True
614- tab_region = active_tab .virtual_region .shrink (active_tab .styles .gutter )
615- start , end = tab_region .column_span
616- # This is a basic animation, so we only disable it if we want no animations.
617- if animate and self .app .animation_level != "none" :
618608
619- def animate_underline () -> None :
620- """Animate the underline."""
621- try :
622- active_tab = self .query_one (f"#tabs-list > Tab.-active" )
623- except NoMatches :
624- pass
625- else :
626- tab_region = active_tab .virtual_region .shrink (
627- active_tab .styles .gutter
628- )
629- start , end = tab_region .column_span
609+ def move_underline (animate : bool ) -> None :
610+ """Move the tab underline.
611+
612+ Args:
613+ animate: animate the underline to its new position.
614+ """
615+ try :
616+ active_tab = self .query_one ("#tabs-list > Tab.-active" )
617+ except NoMatches :
618+ pass
619+ else :
620+ tab_region = active_tab .virtual_region .shrink (
621+ active_tab .styles .gutter
622+ )
623+ start , end = tab_region .column_span
624+ if animate :
630625 underline .animate (
631626 "highlight_start" ,
632627 start ,
@@ -639,11 +634,17 @@ def animate_underline() -> None:
639634 duration = 0.3 ,
640635 level = "basic" ,
641636 )
637+ else :
638+ underline .highlight_start = start
639+ underline .highlight_end = end
642640
643- self .set_timer (0.02 , lambda : self .call_after_refresh (animate_underline ))
641+ if animate and self .app .animation_level != "none" :
642+ self .set_timer (
643+ 0.02 ,
644+ lambda : self .call_after_refresh (move_underline , True ),
645+ )
644646 else :
645- underline .highlight_start = start
646- underline .highlight_end = end
647+ self .call_after_refresh (move_underline , False )
647648
648649 async def _on_tab_clicked (self , event : Tab .Clicked ) -> None :
649650 """Activate a tab that was clicked."""
0 commit comments