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

192 lines
7.7 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Orchestration;
using MSFSPopoutPanelManager.WindowsAgent;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
2024-02-28 02:44:21 +00:00
using MSFSPopoutPanelManager.MainApp.AppWindow;
2023-07-12 22:41:31 +00:00
namespace MSFSPopoutPanelManager.MainApp.ViewModel
{
2024-02-28 02:44:21 +00:00
public class OrchestratorUiHelper : BaseViewModel
2023-07-12 22:41:31 +00:00
{
private bool _minimizeForPopOut;
2024-02-28 02:44:21 +00:00
public OrchestratorUiHelper(SharedStorage sharedStorage, PanelSourceOrchestrator panelSourceOrchestrator, PanelPopOutOrchestrator panelPopOutOrchestrator) : base(sharedStorage)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
panelSourceOrchestrator.OnOverlayShowed -= HandleShowOverlay;
panelSourceOrchestrator.OnOverlayShowed += HandleShowOverlay;
panelSourceOrchestrator.OnNonEditOverlayShowed -= HandleShowNonEditOverlay;
panelSourceOrchestrator.OnNonEditOverlayShowed += HandleShowNonEditOverlay;
panelSourceOrchestrator.OnOverlayRemoved -= HandleRemoveOverlay;
panelSourceOrchestrator.OnOverlayRemoved += HandleRemoveOverlay;
panelPopOutOrchestrator.OnPopOutStarted -= HandleOnPopOutStarted;
panelPopOutOrchestrator.OnPopOutStarted += HandleOnPopOutStarted;
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
panelPopOutOrchestrator.OnPopOutCompleted -= HandleOnPopOutCompleted;
panelPopOutOrchestrator.OnPopOutCompleted += HandleOnPopOutCompleted;
panelPopOutOrchestrator.OnHudBarOpened -= HandleOnHudBarOpened;
panelPopOutOrchestrator.OnHudBarOpened += HandleOnHudBarOpened;
panelPopOutOrchestrator.OnNumPadOpened -= HandleOnNumPadOpened;
panelPopOutOrchestrator.OnNumPadOpened += HandleOnNumPadOpened;
2024-09-17 14:42:07 +00:00
panelPopOutOrchestrator.OnSwitchWindowOpened -= HandleOnSwitchWindowOpened;
panelPopOutOrchestrator.OnSwitchWindowOpened += HandleOnSwitchWindowOpened;
2023-07-12 22:41:31 +00:00
}
2024-02-28 02:44:21 +00:00
private void HandleShowOverlay(object sender, PanelConfig panelConfig)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
ShowOverlay(panelConfig);
2023-08-17 06:03:17 +00:00
}
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
private void HandleShowNonEditOverlay(object sender, PanelConfig panelConfig)
2023-08-17 06:03:17 +00:00
{
ShowOverlay(panelConfig, true);
2023-07-12 22:41:31 +00:00
}
2024-02-28 02:44:21 +00:00
private void HandleRemoveOverlay(object sender, PanelConfig panelConfig)
2023-07-12 22:41:31 +00:00
{
Application.Current.Dispatcher.Invoke(() =>
{
for (int i = Application.Current.Windows.Count - 1; i >= 1; i--)
{
if (Application.Current.Windows[i] is PanelCoorOverlay)
{
var panel = Application.Current.Windows[i] as PanelCoorOverlay;
if (panel?.PanelId == panelConfig.Id)
{
panel.Close();
}
}
}
});
}
2024-02-28 02:44:21 +00:00
private void HandleOnHudBarOpened(object sender, PanelConfig panelConfig)
2023-07-12 22:41:31 +00:00
{
Application.Current.Dispatcher.Invoke(async () =>
{
2024-02-28 02:44:21 +00:00
var hudBar = new HudBar(panelConfig.Id);
2023-07-12 22:41:31 +00:00
hudBar.Show();
2024-02-28 02:44:21 +00:00
await Task.Run(() =>
{
Thread.Sleep(1000);
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
});
});
}
private void HandleOnNumPadOpened(object sender, PanelConfig panelConfig)
{
Application.Current.Dispatcher.Invoke(async () =>
{
2024-07-28 00:12:07 +00:00
var numPad = new NumPad(panelConfig.Id, panelConfig.Width, panelConfig.Height);
2024-02-28 02:44:21 +00:00
numPad.Show();
await Task.Run(() =>
2023-07-12 22:41:31 +00:00
{
Thread.Sleep(1000);
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2024-09-17 14:42:07 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
});
});
}
private void HandleOnSwitchWindowOpened(object sender, PanelConfig panelConfig)
{
Application.Current.Dispatcher.Invoke(async () =>
{
var switchWindow = new SwitchWindow(panelConfig.Id, panelConfig.Width, panelConfig.Height);
switchWindow.Show();
await Task.Run(() =>
{
Thread.Sleep(1000);
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2023-07-12 22:41:31 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
});
});
}
2024-02-28 02:44:21 +00:00
private void HandleOnPopOutStarted(object sender, EventArgs e)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
if (!AppSettingData.ApplicationSetting.PopOutSetting.MinimizeDuringPopOut)
2023-07-12 22:41:31 +00:00
return;
Application.Current.Dispatcher.Invoke(() =>
{
// Temporary minimize the app for pop out process
2024-02-28 02:44:21 +00:00
_minimizeForPopOut = ApplicationWindow.WindowState != WindowState.Minimized;
2023-07-12 22:41:31 +00:00
if (_minimizeForPopOut)
2024-02-28 02:44:21 +00:00
WindowActionManager.MinimizeWindow(ApplicationHandle);
2023-07-12 22:41:31 +00:00
});
}
2024-02-28 02:44:21 +00:00
private void HandleOnPopOutCompleted(object sender, EventArgs arg)
2023-07-12 22:41:31 +00:00
{
Application.Current.Dispatcher.Invoke(() =>
{
2024-02-28 02:44:21 +00:00
if (AppSettingData.ApplicationSetting.PopOutSetting.MinimizeAfterPopOut)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
WindowActionManager.MinimizeWindow(ApplicationHandle);
2023-07-12 22:41:31 +00:00
}
else if (_minimizeForPopOut)
{
2024-02-28 02:44:21 +00:00
ApplicationWindow.Show();
WindowActionManager.BringWindowToForeground(ApplicationHandle);
2023-07-12 22:41:31 +00:00
}
2024-02-28 02:44:21 +00:00
else if (!AppSettingData.ApplicationSetting.GeneralSetting.AlwaysOnTop)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
WindowActionManager.BringWindowToForeground(ApplicationHandle);
2023-07-12 22:41:31 +00:00
}
});
_minimizeForPopOut = false;
}
2023-08-17 06:03:17 +00:00
private void ShowOverlay(PanelConfig panelConfig, bool nonEdit = false)
{
if (panelConfig.PanelType != PanelType.CustomPopout)
return;
Application.Current.Dispatcher.Invoke(() =>
{
2023-08-18 05:03:56 +00:00
// Remove existing overlay if exist
HandleRemoveOverlay(this, panelConfig);
2024-02-28 02:44:21 +00:00
var overlay = new PanelCoorOverlay(panelConfig.Id, !nonEdit)
{
IsEditingPanelLocation = true,
WindowStartupLocation = WindowStartupLocation.Manual
};
2023-08-17 06:03:17 +00:00
overlay.SetWindowCoor(Convert.ToInt32(panelConfig.PanelSource.X), Convert.ToInt32(panelConfig.PanelSource.Y));
overlay.ShowInTaskbar = false;
// Fix MS.Win32.UnsafeNativeMethods.GetWindowText exception
try { overlay.Show(); } catch { overlay.Show(); }
2024-02-28 02:44:21 +00:00
overlay.OnWindowLocationChanged += (_, e) =>
2023-08-17 06:03:17 +00:00
{
2024-02-28 02:44:21 +00:00
var panelSource = ActiveProfile?.PanelConfigs.FirstOrDefault(p => p.Id == panelConfig.Id);
if (panelSource == null)
return;
panelSource.PanelSource.X = e.X;
panelSource.PanelSource.Y = e.Y;
2023-08-17 06:03:17 +00:00
};
});
}
2023-07-12 22:41:31 +00:00
}
}