1313
1414from textual .app import App , ComposeResult
1515from textual .containers import Container , VerticalScroll
16- from textual .reactive import var
16+ from textual .reactive import reactive , var
1717from textual .widgets import DirectoryTree , Footer , Header , Static
1818
1919
@@ -27,6 +27,7 @@ class CodeBrowser(App):
2727 ]
2828
2929 show_tree = var (True )
30+ path : reactive [str | None ] = reactive (None )
3031
3132 def watch_show_tree (self , show_tree : bool ) -> None :
3233 """Called when show_tree is modified."""
@@ -45,27 +46,40 @@ def compose(self) -> ComposeResult:
4546 def on_mount (self ) -> None :
4647 self .query_one (DirectoryTree ).focus ()
4748
49+ def theme_change (_signal ) -> None :
50+ """Force the syntax to use a different theme."""
51+ self .watch_path (self .path )
52+
53+ self .theme_changed_signal .subscribe (self , theme_change )
54+
4855 def on_directory_tree_file_selected (
4956 self , event : DirectoryTree .FileSelected
5057 ) -> None :
5158 """Called when the user click a file in the directory tree."""
5259 event .stop ()
60+ self .path = str (event .path )
61+
62+ def watch_path (self , path : str | None ) -> None :
63+ """Called when path changes."""
5364 code_view = self .query_one ("#code" , Static )
65+ if path is None :
66+ code_view .update ("" )
67+ return
5468 try :
5569 syntax = Syntax .from_path (
56- str ( event . path ) ,
70+ path ,
5771 line_numbers = True ,
5872 word_wrap = False ,
5973 indent_guides = True ,
60- theme = "github-dark" ,
74+ theme = "github-dark" if self . current_theme . dark else "github-light" ,
6175 )
6276 except Exception :
6377 code_view .update (Traceback (theme = "github-dark" , width = None ))
6478 self .sub_title = "ERROR"
6579 else :
6680 code_view .update (syntax )
6781 self .query_one ("#code-view" ).scroll_home (animate = False )
68- self .sub_title = str ( event . path )
82+ self .sub_title = path
6983
7084 def action_toggle_files (self ) -> None :
7185 """Called in response to key binding."""
0 commit comments