1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 13:50:14 +00:00
msfs-popout-panel-manager/MainApp/ViewModel/OrchestratorUIHelper.cs

130 lines
5.1 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;
namespace MSFSPopoutPanelManager.MainApp.ViewModel
{
public class OrchestratorUIHelper : BaseViewModel
{
private bool _minimizeForPopOut;
public OrchestratorUIHelper(MainOrchestrator orchestrator) : base(orchestrator)
{
Orchestrator.PanelSource.OnOverlayShowed += HandleShowOverlay;
Orchestrator.PanelSource.OnOverlayRemoved += HandleRemoveOverlay;
Orchestrator.PanelPopOut.OnPopOutStarted += HandleOnPopOutStarted;
Orchestrator.PanelPopOut.OnPopOutCompleted += HandleOnPopOutCompleted;
Orchestrator.PanelPopOut.OnHudBarOpened += HandleOnHudBarOpened;
}
private void HandleShowOverlay(object? sender, PanelConfig panelConfig)
{
if (panelConfig.PanelType != PanelType.CustomPopout)
return;
Application.Current.Dispatcher.Invoke(() =>
{
PanelCoorOverlay overlay = new PanelCoorOverlay(panelConfig.Id);
overlay.IsEditingPanelLocation = true;
overlay.WindowStartupLocation = WindowStartupLocation.Manual;
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(); }
overlay.WindowLocationChanged += (sender, e) =>
{
if (Orchestrator.ProfileData.ActiveProfile != null)
{
var panelSource = Orchestrator.ProfileData.ActiveProfile.PanelConfigs.FirstOrDefault(p => p.Id == panelConfig.Id);
if (panelSource != null)
{
panelSource.PanelSource.X = e.X;
panelSource.PanelSource.Y = e.Y;
}
}
};
});
}
private void HandleRemoveOverlay(object? sender, PanelConfig panelConfig)
{
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();
break;
}
}
}
});
}
private void HandleOnHudBarOpened(object? sender, PanelConfig panelConfig)
{
Application.Current.Dispatcher.Invoke(async () =>
{
HudBar hudBar = new HudBar(panelConfig.Id);
hudBar.Show();
Task.Run(async () =>
{
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 HandleOnPopOutStarted(object? sender, EventArgs e)
{
if (!Orchestrator.AppSettingData.ApplicationSetting.PopOutSetting.MinimizeDuringPopOut)
return;
Application.Current.Dispatcher.Invoke(() =>
{
// Temporary minimize the app for pop out process
_minimizeForPopOut = Orchestrator.ApplicationWindow.WindowState != WindowState.Minimized;
if (_minimizeForPopOut)
WindowActionManager.MinimizeWindow(Orchestrator.ApplicationHandle);
});
}
private void HandleOnPopOutCompleted(object? sender, EventArgs arg)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (Orchestrator.AppSettingData.ApplicationSetting.PopOutSetting.MinimizeAfterPopOut)
{
WindowActionManager.MinimizeWindow(Orchestrator.ApplicationHandle);
}
else if (_minimizeForPopOut)
{
Orchestrator.ApplicationWindow.Show();
WindowActionManager.BringWindowToForeground(Orchestrator.ApplicationHandle);
}
else if (!Orchestrator.AppSettingData.ApplicationSetting.GeneralSetting.AlwaysOnTop)
{
WindowActionManager.BringWindowToForeground(Orchestrator.ApplicationHandle);
}
});
_minimizeForPopOut = false;
}
}
}