@@ -113,6 +113,10 @@ private async Task ResizeTask()
113113
114114 private Terminal _terminal ;
115115 private BufferedReader _reader ;
116+ private ShellProfile _profile ;
117+
118+ public TerminalView ( ShellProfile profile ) : this ( )
119+ => _profile = profile ;
116120
117121 public TerminalView ( )
118122 {
@@ -121,6 +125,9 @@ public TerminalView()
121125
122126 private async void WebViewControl_Loaded ( object sender , Microsoft . UI . Xaml . RoutedEventArgs e )
123127 {
128+ if ( WebViewControl . Source is not null )
129+ return ;
130+
124131 var envOptions = new CoreWebView2EnvironmentOptions ( )
125132 {
126133 // TODO: switch to "ScrollBarStyle" when available
@@ -143,8 +150,7 @@ private async void WebViewControl_Loaded(object sender, Microsoft.UI.Xaml.Routed
143150 var provider = new DefaultValueProvider ( ) ;
144151 var options = provider . GetDefaultTerminalOptions ( ) ;
145152 var keyBindings = provider . GetCommandKeyBindings ( ) ;
146- var profile = _mainPageModel . TerminalSelectedProfile ;
147- var theme = provider . GetPreInstalledThemes ( ) . First ( x => x . Id == profile . TerminalThemeId ) ;
153+ var theme = provider . GetPreInstalledThemes ( ) . First ( x => x . Id == _profile . TerminalThemeId ) ;
148154
149155 WebViewControl . CoreWebView2 . Profile . PreferredColorScheme = ( ActualTheme == Microsoft . UI . Xaml . ElementTheme . Dark ) ? CoreWebView2PreferredColorScheme . Dark : CoreWebView2PreferredColorScheme . Light ;
150156
@@ -167,7 +173,7 @@ private async void WebViewControl_Loaded(object sender, Microsoft.UI.Xaml.Routed
167173 }
168174 }
169175
170- StartShellProcess ( size , profile ) ;
176+ StartShellProcess ( size , _profile ) ;
171177
172178 lock ( _resizeLock )
173179 {
@@ -329,8 +335,6 @@ private async Task<string> ExecuteScriptAsync(string script)
329335
330336 public void Dispose ( )
331337 {
332- _mainPageModel . GetTerminalFolder = null ;
333- _mainPageModel . SetTerminalFolder = null ;
334338 WebViewControl . Close ( ) ;
335339 _outputBlockedBuffer ? . Dispose ( ) ;
336340 _reader ? . Dispose ( ) ;
@@ -339,6 +343,34 @@ public void Dispose()
339343
340344 public void Paste ( string text ) => OnPaste ? . Invoke ( this , text ) ;
341345
346+ public async Task < string ? > GetTerminalFolder ( )
347+ {
348+ var tcs = new TaskCompletionSource < string > ( ) ;
349+ EventHandler < object > getResponse = ( s , e ) =>
350+ {
351+ var pwd = Encoding . UTF8 . GetString ( ( byte [ ] ) e ) ;
352+ var match = Regex . Match ( pwd , @"[a-zA-Z]:\\(((?![<>:""\r/\\|?*]).)+((?<![ .])\\)?)*" ) ;
353+ if ( match . Success )
354+ tcs . TrySetResult ( match . Value ) ;
355+ } ;
356+ OnOutput += getResponse ;
357+ if ( _profile . Location . Contains ( "wsl.exe" ) )
358+ _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "wslpath -w \" $(pwd)\" \r ") ) ;
359+ else
360+ _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd .\r ") ) ;
361+ var pwd = await tcs . Task . WithTimeoutAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
362+ OnOutput -= getResponse ;
363+ return pwd ;
364+ }
365+
366+ public void SetTerminalFolder ( string folder )
367+ {
368+ if ( _profile . Location . Contains ( "wsl.exe" ) )
369+ _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd \" $(wslpath \" { folder } \" )\" \r ") ) ;
370+ else
371+ _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd \" { folder } \" \r ") ) ;
372+ }
373+
342374 private void StartShellProcess ( TerminalSize size , ShellProfile profile )
343375 {
344376 var ShellExecutableName = Path . GetFileNameWithoutExtension ( profile . Location ) ;
@@ -352,38 +384,12 @@ private void StartShellProcess(TerminalSize size, ShellProfile profile)
352384 _terminal . OutputReady += ( s , e ) =>
353385 {
354386 _reader = new BufferedReader ( _terminal . ConsoleOutStream , OutputReceivedCallback , true ) ;
355- _mainPageModel . GetTerminalFolder = async ( ) =>
356- {
357- var tcs = new TaskCompletionSource < string > ( ) ;
358- EventHandler < object > getResponse = ( s , e ) =>
359- {
360- var pwd = Encoding . UTF8 . GetString ( ( byte [ ] ) e ) ;
361- var match = Regex . Match ( pwd , @"[a-zA-Z]:\\(((?![<>:""\r/\\|?*]).)+((?<![ .])\\)?)*" ) ;
362- if ( match . Success )
363- tcs . TrySetResult ( match . Value ) ;
364- } ;
365- OnOutput += getResponse ;
366- if ( profile . Location . Contains ( "wsl.exe" ) )
367- _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "wslpath -w \" $(pwd)\" \r ") ) ;
368- else
369- _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd .\r ") ) ;
370- var pwd = await tcs . Task . WithTimeoutAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
371- OnOutput -= getResponse ;
372- return pwd ;
373- } ;
374- _mainPageModel . SetTerminalFolder = ( folder ) =>
375- {
376- if ( profile . Location . Contains ( "wsl.exe" ) )
377- _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd \" $(wslpath \" { folder } \" )\" \r ") ) ;
378- else
379- _terminal . WriteToPseudoConsole ( Encoding . UTF8 . GetBytes ( $ "cd \" { folder } \" \r ") ) ;
380- } ;
381387 } ;
382388 _terminal . Exited += ( s , e ) =>
383389 {
384390 DispatcherQueue . EnqueueAsync ( ( ) =>
385391 {
386- _mainPageModel . IsTerminalViewOpen = false ;
392+ _mainPageModel . TerminalCloseCommand . Execute ( Tag ) ;
387393 } ) ;
388394 } ;
389395
@@ -424,7 +430,6 @@ Task<string> GetTextAsync()
424430
425431 private void TerminalView_Unloaded ( object sender , Microsoft . UI . Xaml . RoutedEventArgs e )
426432 {
427- Dispose ( ) ;
428433 }
429434
430435 private async void TerminalView_ActualThemeChanged ( Microsoft . UI . Xaml . FrameworkElement sender , object args )
@@ -434,8 +439,7 @@ private async void TerminalView_ActualThemeChanged(Microsoft.UI.Xaml.FrameworkEl
434439
435440 var serializerSettings = new JsonSerializerOptions ( ) ;
436441 serializerSettings . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
437- var profile = _mainPageModel . TerminalSelectedProfile ;
438- var theme = new DefaultValueProvider ( ) . GetPreInstalledThemes ( ) . First ( x => x . Id == profile . TerminalThemeId ) ;
442+ var theme = new DefaultValueProvider ( ) . GetPreInstalledThemes ( ) . First ( x => x . Id == _profile . TerminalThemeId ) ;
439443
440444 WebViewControl . CoreWebView2 . Profile . PreferredColorScheme = ( ActualTheme == Microsoft . UI . Xaml . ElementTheme . Dark ) ? CoreWebView2PreferredColorScheme . Dark : CoreWebView2PreferredColorScheme . Light ;
441445
0 commit comments