From d169223912c0e79a5c328889f08a925a57de93f9 Mon Sep 17 00:00:00 2001 From: hawkeye Date: Tue, 1 Aug 2023 23:39:03 -0400 Subject: [PATCH] Update timing code --- MainApp/App.xaml.cs | 102 +++++++++++------------ Orchestration/PanelPopOutOrchestrator.cs | 6 +- Orchestration/PanelSourceOrchestrator.cs | 8 +- WindowsAgent/InputEmulationManager.cs | 3 + 4 files changed, 62 insertions(+), 57 deletions(-) diff --git a/MainApp/App.xaml.cs b/MainApp/App.xaml.cs index e306e4b..8301a97 100644 --- a/MainApp/App.xaml.cs +++ b/MainApp/App.xaml.cs @@ -5,6 +5,9 @@ using MSFSPopoutPanelManager.Orchestration; using MSFSPopoutPanelManager.Shared; using MSFSPopoutPanelManager.WindowsAgent; using System; +using System.Diagnostics; +using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -26,58 +29,7 @@ namespace MSFSPopoutPanelManager.MainApp DpiAwareness.Enable(); // Must run this first - Initialize(); - - // Setup dependency injections - AppHost = Host.CreateDefaultBuilder() - .ConfigureServices((hostContext, services) => - { - services.AddSingleton(); - - services.AddSingleton(); - services.AddSingleton(s => new OrchestratorUIHelper(s.GetRequiredService())); - - services.AddSingleton(s => new ApplicationViewModel(s.GetRequiredService())); - services.AddSingleton(s => new HelpViewModel(s.GetRequiredService())); - services.AddSingleton(s => new ProfileCardListViewModel(s.GetRequiredService())); - services.AddSingleton(s => new ProfileCardViewModel(s.GetRequiredService())); - services.AddSingleton(s => new TrayIconViewModel(s.GetRequiredService())); - - services.AddTransient(s => new AddProfileViewModel(s.GetRequiredService())); - services.AddTransient(s => new PopOutPanelListViewModel(s.GetRequiredService())); - services.AddTransient(s => new PopOutPanelCardViewModel(s.GetRequiredService())); - services.AddTransient(s => new PanelConfigFieldViewModel(s.GetRequiredService())); - services.AddTransient(s => new PanelCoorOverlayViewModel(s.GetRequiredService())); - - services.AddTransient(s => new MessageWindowViewModel(s.GetRequiredService())); - services.AddTransient(s => new HudBarViewModel(s.GetRequiredService())); - - }).Build(); - - await AppHost!.StartAsync(); - - // Startup window (must come after DPI setup above) - MainWindow = AppHost.Services.GetRequiredService(); - MainWindow.Show(); - - base.OnStartup(e); - - // Setup orchestration UI handler - var orchestrationUIHelper = App.AppHost.Services.GetRequiredService(); - - // Setup message window dialog - var messageWindow = new MessageWindow(); - messageWindow.Show(); - } - - private void Initialize() - { - const string appName = "MSFS PopOut Panel Manager"; - bool createdNew; - - var mutex = new Mutex(true, appName, out createdNew); - - if (!createdNew) + if(IsRunning()) { //app is already running! Exiting the application Application.Current.Shutdown(); @@ -88,9 +40,55 @@ namespace MSFSPopoutPanelManager.MainApp Dispatcher.UnhandledException += HandleDispatcherException; TaskScheduler.UnobservedTaskException += HandleTaskSchedulerUnobservedTaskException; AppDomain.CurrentDomain.UnhandledException += HandledDomainException; + + // Setup dependency injections + AppHost = Host.CreateDefaultBuilder() + .ConfigureServices((hostContext, services) => + { + services.AddSingleton(); + + services.AddSingleton(); + services.AddSingleton(s => new OrchestratorUIHelper(s.GetRequiredService())); + + services.AddSingleton(s => new ApplicationViewModel(s.GetRequiredService())); + services.AddSingleton(s => new HelpViewModel(s.GetRequiredService())); + services.AddSingleton(s => new ProfileCardListViewModel(s.GetRequiredService())); + services.AddSingleton(s => new ProfileCardViewModel(s.GetRequiredService())); + services.AddSingleton(s => new TrayIconViewModel(s.GetRequiredService())); + + services.AddTransient(s => new AddProfileViewModel(s.GetRequiredService())); + services.AddTransient(s => new PopOutPanelListViewModel(s.GetRequiredService())); + services.AddTransient(s => new PopOutPanelCardViewModel(s.GetRequiredService())); + services.AddTransient(s => new PanelConfigFieldViewModel(s.GetRequiredService())); + services.AddTransient(s => new PanelCoorOverlayViewModel(s.GetRequiredService())); + + services.AddTransient(s => new MessageWindowViewModel(s.GetRequiredService())); + services.AddTransient(s => new HudBarViewModel(s.GetRequiredService())); + + }).Build(); + + await AppHost!.StartAsync(); + + // Startup window (must come after DPI setup above) + MainWindow = AppHost.Services.GetRequiredService(); + MainWindow.Show(); + + // Setup orchestration UI handler + var orchestrationUIHelper = App.AppHost.Services.GetRequiredService(); + + // Setup message window dialog + var messageWindow = new MessageWindow(); + messageWindow.Show(); + + base.OnStartup(e); } } + private bool IsRunning() + { + return Process.GetProcesses().Count(p => p.ProcessName.Contains(Assembly.GetEntryAssembly().GetName().Name)) > 1; + } + private void HandleTaskSchedulerUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) { FileLogger.WriteException(e.Exception.Message, e.Exception); diff --git a/Orchestration/PanelPopOutOrchestrator.cs b/Orchestration/PanelPopOutOrchestrator.cs index 063a384..7f52f9b 100644 --- a/Orchestration/PanelPopOutOrchestrator.cs +++ b/Orchestration/PanelPopOutOrchestrator.cs @@ -186,6 +186,7 @@ namespace MSFSPopoutPanelManager.Orchestration // Remember current game's zoom level to be recall after pop out _prePopOutCockpitZoomLevel = _flightSimData.CockpitCameraZoom; InputEmulationManager.LoadCustomView(AppSetting.PopOutSetting.AutoPanning.KeyBinding); + Thread.Sleep(1000); FlightSimOrchestrator.SetCockpitCameraZoomLevel(50); StatusMessageWriter.WriteOkStatusMessage(); } @@ -482,12 +483,13 @@ namespace MSFSPopoutPanelManager.Orchestration switch (AppSetting.PopOutSetting.AfterPopOutCameraView.CameraView) { case AfterPopOutCameraViewType.CockpitCenterView: - FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePopOutCockpitZoomLevel); InputEmulationManager.CenterView(); + Thread.Sleep(500); + FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePopOutCockpitZoomLevel); break; case AfterPopOutCameraViewType.CustomCameraView: - FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePopOutCockpitZoomLevel); InputEmulationManager.LoadCustomView(AppSetting.PopOutSetting.AfterPopOutCameraView.KeyBinding); + FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePopOutCockpitZoomLevel); break; } } diff --git a/Orchestration/PanelSourceOrchestrator.cs b/Orchestration/PanelSourceOrchestrator.cs index b34c289..b79645a 100644 --- a/Orchestration/PanelSourceOrchestrator.cs +++ b/Orchestration/PanelSourceOrchestrator.cs @@ -100,20 +100,22 @@ namespace MSFSPopoutPanelManager.Orchestration // Recenter game or return to after pop out camera view if (!AppSetting.PopOutSetting.AfterPopOutCameraView.IsEnabled) { - FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); InputEmulationManager.CenterView(); + Thread.Sleep(500); + FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); } else { switch (AppSetting.PopOutSetting.AfterPopOutCameraView.CameraView) { case AfterPopOutCameraViewType.CockpitCenterView: - FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); InputEmulationManager.CenterView(); + Thread.Sleep(500); + FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); break; case AfterPopOutCameraViewType.CustomCameraView: - FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); InputEmulationManager.LoadCustomView(AppSetting.PopOutSetting.AfterPopOutCameraView.KeyBinding); + FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel); break; } } diff --git a/WindowsAgent/InputEmulationManager.cs b/WindowsAgent/InputEmulationManager.cs index ed89e3e..a62f444 100644 --- a/WindowsAgent/InputEmulationManager.cs +++ b/WindowsAgent/InputEmulationManager.cs @@ -141,6 +141,9 @@ namespace MSFSPopoutPanelManager.WindowsAgent PInvoke.keybd_event(Convert.ToByte(customViewKey), 0, KEYEVENTF_KEYUP, 0); PInvoke.keybd_event(Convert.ToByte(VK_LMENU), 0, KEYEVENTF_KEYUP, 0); Thread.Sleep(200); + + // Small delay to let load custom view finish + Thread.Sleep(500); } public static void ToggleFullScreenPanel(IntPtr hwnd)