1616using Microsoft . Extensions . DependencyInjection ;
1717using Microsoft . Extensions . Hosting ;
1818using Microsoft . Extensions . Logging ;
19+ using Microsoft . UI . Dispatching ;
1920using Microsoft . UI . Xaml ;
2021using Microsoft . Win32 ;
2122using Microsoft . Windows . AppLifecycle ;
2627
2728namespace Coder . Desktop . App ;
2829
29- public partial class App : Application
30+ public partial class App : Application , IDispatcherQueueManager
3031{
3132 private const string MutagenControllerConfigSection = "MutagenController" ;
3233 private const string UpdaterConfigSection = "Updater" ;
@@ -50,13 +51,11 @@ public partial class App : Application
5051 private readonly ILogger < App > _logger ;
5152 private readonly IUriHandler _uriHandler ;
5253 private readonly IUserNotifier _userNotifier ;
53-
54- private bool _handleWindowClosed = true ;
55-
5654 private readonly ISettingsManager < CoderConnectSettings > _settingsManager ;
57-
5855 private readonly IHostApplicationLifetime _appLifetime ;
5956
57+ private bool _handleWindowClosed = true ;
58+
6059 public App ( )
6160 {
6261 var builder = Host . CreateApplicationBuilder ( ) ;
@@ -91,7 +90,7 @@ public App()
9190 services . AddSingleton < ICoderApiClientFactory , CoderApiClientFactory > ( ) ;
9291 services . AddSingleton < IAgentApiClientFactory , AgentApiClientFactory > ( ) ;
9392
94- services . AddSingleton < IDispatcherQueueManager , AppDispatcherQueueManager > ( ) ;
93+ services . AddSingleton < IDispatcherQueueManager > ( _ => this ) ;
9594 services . AddSingleton < ICredentialBackend > ( _ =>
9695 new WindowsCredentialBackend ( WindowsCredentialBackend . CoderCredentialsTargetName ) ) ;
9796 services . AddSingleton < ICredentialManager , CredentialManager > ( ) ;
@@ -323,4 +322,17 @@ private static void AddDefaultConfig(IConfigurationBuilder builder)
323322#endif
324323 } ) ;
325324 }
325+
326+ public void RunInUiThread ( DispatcherQueueHandler action )
327+ {
328+ var dispatcherQueue = TrayWindow ? . DispatcherQueue ;
329+ if ( dispatcherQueue is null )
330+ throw new InvalidOperationException ( "DispatcherQueue is not available" ) ;
331+ if ( dispatcherQueue . HasThreadAccess )
332+ {
333+ action ( ) ;
334+ return ;
335+ }
336+ dispatcherQueue . TryEnqueue ( action ) ;
337+ }
326338}
0 commit comments