@@ -761,6 +761,9 @@ def __init__(
761761
762762 self ._hover_effects_timer : Timer | None = None
763763
764+ self ._css_update_count : int = 0
765+ """Incremented when CSS is invalidated."""
766+
764767 if self .ENABLE_COMMAND_PALETTE :
765768 for _key , binding in self ._bindings :
766769 if binding .action in {"command_palette" , "app.command_palette" }:
@@ -1243,17 +1246,24 @@ def _watch_theme(self, theme_name: str) -> None:
12431246 self .set_class (dark , "-dark-mode" , update = False )
12441247 self .set_class (not dark , "-light-mode" , update = False )
12451248 self ._refresh_truecolor_filter (self .ansi_theme )
1249+ self ._invalidate_css ()
12461250 self .call_next (self .refresh_css )
12471251 self .call_next (self .theme_changed_signal .publish , theme )
12481252
1253+ def _invalidate_css (self ) -> None :
1254+ """Invalidate CSS, so it will be refreshed."""
1255+ self ._css_update_count += 1
1256+
12491257 def watch_ansi_theme_dark (self , theme : TerminalTheme ) -> None :
12501258 if self .current_theme .dark :
12511259 self ._refresh_truecolor_filter (theme )
1260+ self ._invalidate_css ()
12521261 self .call_next (self .refresh_css )
12531262
12541263 def watch_ansi_theme_light (self , theme : TerminalTheme ) -> None :
12551264 if not self .current_theme .dark :
12561265 self ._refresh_truecolor_filter (theme )
1266+ self ._invalidate_css ()
12571267 self .call_next (self .refresh_css )
12581268
12591269 @property
@@ -2283,7 +2293,9 @@ def _init_mode(self, mode: str) -> AwaitMount:
22832293 screen , await_mount = self ._get_screen (new_screen )
22842294 stack .append (screen )
22852295 self ._load_screen_css (screen )
2286- self .refresh_css ()
2296+ if screen ._css_update_count != self ._css_update_count :
2297+ self .refresh_css ()
2298+
22872299 screen .post_message (events .ScreenResume ())
22882300 else :
22892301 # Mode is not defined
@@ -2328,7 +2340,8 @@ def switch_mode(self, mode: str) -> AwaitMount:
23282340 await_mount = AwaitMount (self .screen , [])
23292341
23302342 self ._current_mode = mode
2331- self .refresh_css ()
2343+ if self .screen ._css_update_count != self ._css_update_count :
2344+ self .refresh_css ()
23322345 self .screen ._screen_resized (self .size )
23332346 self .screen .post_message (events .ScreenResume ())
23342347
@@ -3445,6 +3458,7 @@ def refresh_css(self, animate: bool = True) -> None:
34453458 stylesheet .update (self .app , animate = animate )
34463459 try :
34473460 self .screen ._refresh_layout (self .size )
3461+ self .screen ._css_update_count = self ._css_update_count
34483462 except ScreenError :
34493463 pass
34503464 # The other screens in the stack will need to know about some style
@@ -3453,6 +3467,7 @@ def refresh_css(self, animate: bool = True) -> None:
34533467 for screen in self .screen_stack :
34543468 if screen != self .screen :
34553469 stylesheet .update (screen , animate = animate )
3470+ screen ._css_update_count = self ._css_update_count
34563471
34573472 def _display (self , screen : Screen , renderable : RenderableType | None ) -> None :
34583473 """Display a renderable within a sync.
0 commit comments