1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 07:10:10 +00:00
msfs-popout-panel-manager/WpfApp/ViewModel/ApplicationViewModel.cs

189 lines
6.8 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.Orchestration;
2022-01-27 13:40:04 +00:00
using MSFSPopoutPanelManager.Shared;
2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.WindowsAgent;
using Prism.Commands;
using PropertyChanged;
2022-01-27 13:40:04 +00:00
using System;
2022-07-23 19:23:32 +00:00
using System.Threading.Tasks;
2022-01-27 13:40:04 +00:00
using System.Windows;
namespace MSFSPopoutPanelManager.WpfApp.ViewModel
{
2022-07-23 19:23:32 +00:00
[SuppressPropertyChangedWarnings]
2022-06-30 23:53:08 +00:00
public class ApplicationViewModel : ObservableObject
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
private MainOrchestrator _orchestrator;
private OrchestratorHelper _windowHelper;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public event EventHandler<StatusMessageEventArg> ShowContextMenuBalloonTip;
2022-01-27 13:40:04 +00:00
public PanelSelectionViewModel PanelSelectionViewModel { get; private set; }
public PanelConfigurationViewModel PanelConfigurationViewModel { get; private set; }
2022-06-30 23:53:08 +00:00
public PreferencesViewModel PreferencesViewModel { get; private set; }
2022-07-23 19:23:32 +00:00
public ProfileData ProfileData { get { return _orchestrator.ProfileData; } }
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public AppSettingData AppSettingData { get { return _orchestrator.AppSettingData; } }
public FlightSimData FlightSimData { get { return _orchestrator.FlightSimData; } }
public IntPtr ApplicationHandle
{
get { return _orchestrator.ApplicationHandle; }
set { _orchestrator.ApplicationHandle = value; }
}
public Window ApplicationWindow { set { _windowHelper.ApplicationWindow = value; } }
2022-01-27 13:40:04 +00:00
public string ApplicationVersion { get; private set; }
public WindowState InitialWindowState { get; private set; }
2022-07-23 19:23:32 +00:00
public string StatusMessage { get; set; }
public StatusMessageType StatusMessageType { get; set; }
2022-01-27 13:40:04 +00:00
public bool IsShownPanelConfigurationScreen { get; set; }
public bool IsShownPanelSelectionScreen { get; set; }
2022-07-23 19:23:32 +00:00
public DelegateCommand RestartCommand => new DelegateCommand(OnRestart, () => IsShownPanelConfigurationScreen);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand EditPreferencesCommand => new DelegateCommand(OnEditPreferences);
2022-06-30 23:53:08 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand ExitCommand => new DelegateCommand(async () => { await OnExit(); });
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand UserGuideCommand => new DelegateCommand(OnOpenUserGuide);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand DownloadLatestReleaseCommand => new DelegateCommand(OnDownloadLastest);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand<object> ChangeProfileCommand => new DelegateCommand<object>(OnChangeProfile);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public DelegateCommand StartPopOutCommand => new DelegateCommand(OnStartPopOut, () => ProfileData.ActiveProfile != null && ProfileData.ActiveProfile.PanelSourceCoordinates.Count > 0)
.ObservesProperty(() => ProfileData.ActiveProfile)
.ObservesProperty(() => ProfileData.ActiveProfile.PanelSourceCoordinates.Count);
public ApplicationViewModel()
{
_orchestrator = new MainOrchestrator();
_windowHelper = new OrchestratorHelper(_orchestrator);
PanelSelectionViewModel = new PanelSelectionViewModel(_orchestrator);
PanelConfigurationViewModel = new PanelConfigurationViewModel(_orchestrator);
PreferencesViewModel = new PreferencesViewModel(_orchestrator);
2022-01-27 13:40:04 +00:00
}
public void Initialize()
{
2022-07-23 19:23:32 +00:00
StatusMessageWriter.OnStatusMessage += HandleOnStatusMessage;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
_orchestrator.Initialize();
_orchestrator.AppSettingData.AlwaysOnTopChanged += (sender, e) => WindowActionManager.ApplyAlwaysOnTop(ApplicationHandle, PanelType.PopOutManager, e);
_orchestrator.FlightSim.OnSimulatorStarted += (sender, e) => ShowPanelSelection(true);
_orchestrator.FlightSim.OnSimulatorStopped += (sender, e) => OnRestart();
_orchestrator.FlightSim.OnFlightStopped += (sender, e) => OnRestart();
_orchestrator.PanelPopOut.OnPopOutStarted += (sender, e) => ShowPanelSelection(true);
_orchestrator.PanelPopOut.OnPopOutCompleted += (sender, e) => ShowPanelSelection(false);
2022-02-07 03:05:05 +00:00
2022-01-27 13:40:04 +00:00
// Set application version
2022-07-23 19:23:32 +00:00
ApplicationVersion = WindowProcessManager.GetApplicationVersion();
2022-01-27 13:40:04 +00:00
// Set window state
2022-07-23 19:23:32 +00:00
if (_orchestrator.AppSettingData.AppSetting.StartMinimized)
2022-01-27 13:40:04 +00:00
InitialWindowState = WindowState.Minimized;
// Set Always on Top
2022-07-23 19:23:32 +00:00
if (_orchestrator.AppSettingData.AppSetting.AlwaysOnTop)
WindowActionManager.ApplyAlwaysOnTop(ApplicationHandle, PanelType.PopOutManager, _orchestrator.AppSettingData.AppSetting.AlwaysOnTop);
2022-01-27 13:40:04 +00:00
ShowPanelSelection(true);
2022-06-30 23:53:08 +00:00
}
2022-07-23 19:23:32 +00:00
private void HandleOnStatusMessage(object sender, StatusMessageEventArg e)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
StatusMessage = e.Message;
StatusMessageType = e.StatusMessageType;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
if (StatusMessageType == StatusMessageType.Error)
ShowContextMenuBalloonTip?.Invoke(this, e);
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
public async Task WindowClosing()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
await _orchestrator.ApplicationClose();
2022-01-27 13:40:04 +00:00
2022-06-30 23:53:08 +00:00
if (Application.Current != null)
2022-07-23 19:23:32 +00:00
Environment.Exit(0);
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void ShowPanelSelection(bool show)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
if (show)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
IsShownPanelSelectionScreen = true;
IsShownPanelConfigurationScreen = false;
2022-01-27 13:40:04 +00:00
}
else
{
2022-07-23 19:23:32 +00:00
IsShownPanelSelectionScreen = false;
IsShownPanelConfigurationScreen = true;
2022-01-27 13:40:04 +00:00
}
}
2022-07-23 19:23:32 +00:00
#region Menu bar and context menu commands
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
private void OnRestart()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
// End panel configuration
_orchestrator.PanelConfiguration.EndConfiguration();
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
// Clear log
StatusMessageWriter.WriteMessage(string.Empty, StatusMessageType.Info, false);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
// Try to close all Custom Panel window
WindowActionManager.CloseAllPopOuts();
ShowPanelSelection(true);
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void OnEditPreferences()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
DialogHelper.PreferencesDialog(PreferencesViewModel);
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private async Task OnExit()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
await WindowClosing();
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void OnOpenUserGuide()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
_orchestrator.OnlineFeature.OpenUserGuide();
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void OnDownloadLastest()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
_orchestrator.OnlineFeature.OpenLatestDownload();
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void OnChangeProfile(object commandParameter)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
if (commandParameter == null)
return;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
var profileId = Convert.ToInt32(commandParameter);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
ProfileData.UpdateActiveProfile(profileId);
2022-01-27 13:40:04 +00:00
}
2022-02-07 03:05:05 +00:00
2022-07-23 19:23:32 +00:00
private void OnStartPopOut()
2022-02-07 03:05:05 +00:00
{
2022-07-23 19:23:32 +00:00
ShowPanelSelection(true);
PanelSelectionViewModel.StartPopOutCommand.Execute();
2022-04-18 03:38:33 +00:00
}
2022-07-23 19:23:32 +00:00
#endregion
2022-01-27 13:40:04 +00:00
}
2022-06-30 23:53:08 +00:00
}