1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-29 09:10:09 +00:00
msfs-popout-panel-manager/Orchestration/PanelConfigurationOrchestrator.cs

295 lines
13 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.UserDataAgent;
using MSFSPopoutPanelManager.WindowsAgent;
2022-01-27 13:40:04 +00:00
using System;
using System.Drawing;
using System.Linq;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Orchestration
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
public class PanelConfigurationOrchestrator : ObservableObject
2022-01-27 13:40:04 +00:00
{
private static PInvoke.WinEventProc _winEvent; // keep this as static to prevent garbage collect or the app will crash
2022-07-23 19:23:32 +00:00
private static IntPtr _winEventHook;
private Rectangle _lastWindowRectangle;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
public PanelConfigurationOrchestrator()
2022-01-27 13:40:04 +00:00
{
_winEvent = new PInvoke.WinEventProc(EventCallback);
AllowEdit = true;
}
2022-07-23 19:23:32 +00:00
internal ProfileData ProfileData { get; set; }
internal AppSettingData AppSettingData { get; set; }
private Profile ActiveProfile { get { return ProfileData == null ? null : ProfileData.ActiveProfile; } }
public bool AllowEdit { get; set; }
public void StartConfiguration()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
HookWinEvent();
2022-07-25 12:40:21 +00:00
TouchEventManager.ActiveProfile = ProfileData.ActiveProfile;
TouchEventManager.AppSetting = AppSettingData.AppSetting;
if (ActiveProfile.PanelConfigs.Any(p => p.TouchEnabled) && !TouchEventManager.IsHooked)
{
TouchEventManager.Hook();
}
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
public void EndConfiguration()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
UnhookWinEvent();
2022-07-25 12:40:21 +00:00
TouchEventManager.UnHook();
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
public void LockStatusUpdated()
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
ActiveProfile.IsLocked = !ActiveProfile.IsLocked;
ProfileData.WriteProfiles();
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
public void PanelConfigPropertyUpdated(int panelIndex, PanelConfigPropertyName configPropertyName)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
if (panelIndex == -1 || !AllowEdit || ActiveProfile.IsLocked)
2022-01-27 13:40:04 +00:00
return;
2022-07-23 19:23:32 +00:00
var panelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(p => p.PanelIndex == panelIndex);
2022-01-27 13:40:04 +00:00
if (panelConfig != null)
{
2022-07-23 19:23:32 +00:00
if (configPropertyName == PanelConfigPropertyName.FullScreen)
2022-01-27 13:40:04 +00:00
{
2022-04-18 19:52:39 +00:00
InputEmulationManager.ToggleFullScreenPanel(panelConfig.PanelHandle);
panelConfig.HideTitlebar = false;
panelConfig.AlwaysOnTop = false;
}
2022-07-23 19:23:32 +00:00
else if (configPropertyName == PanelConfigPropertyName.PanelName)
2022-04-18 19:52:39 +00:00
{
var name = panelConfig.PanelName;
2022-07-23 19:23:32 +00:00
if (panelConfig.PanelType == PanelType.CustomPopout && name.IndexOf("(Custom)") == -1)
{
2022-04-18 19:52:39 +00:00
name = name + " (Custom)";
2022-07-23 19:23:32 +00:00
PInvoke.SetWindowText(panelConfig.PanelHandle, name);
}
2022-04-18 19:52:39 +00:00
}
2022-06-30 23:53:08 +00:00
else if (!panelConfig.FullScreen)
2022-04-18 19:52:39 +00:00
{
2022-07-23 19:23:32 +00:00
switch (configPropertyName)
2022-04-18 19:52:39 +00:00
{
case PanelConfigPropertyName.Left:
case PanelConfigPropertyName.Top:
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.Width:
case PanelConfigPropertyName.Height:
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-04-18 19:52:39 +00:00
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.AlwaysOnTop:
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyAlwaysOnTop(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.AlwaysOnTop, new Rectangle(panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height));
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.HideTitlebar:
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, panelConfig.HideTitlebar);
2022-04-18 19:52:39 +00:00
break;
2022-07-25 12:40:21 +00:00
case PanelConfigPropertyName.TouchEnabled:
if (ActiveProfile.PanelConfigs.Any(p => p.TouchEnabled) && !TouchEventManager.IsHooked)
TouchEventManager.Hook();
else if (ActiveProfile.PanelConfigs.All(p => !p.TouchEnabled) && TouchEventManager.IsHooked)
TouchEventManager.UnHook();
2022-07-30 02:29:20 +00:00
if (!panelConfig.TouchEnabled)
panelConfig.DisableGameRefocus = false;
2022-07-25 12:40:21 +00:00
break;
2022-04-18 19:52:39 +00:00
}
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
ProfileData.WriteProfiles();
2022-01-27 13:40:04 +00:00
}
}
2022-07-23 19:23:32 +00:00
public void PanelConfigIncreaseDecrease(int panelIndex, PanelConfigPropertyName configPropertyName, int changeAmount)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
if (panelIndex == -1 || !AllowEdit || ActiveProfile.IsLocked || ActiveProfile.PanelConfigs == null || ActiveProfile.PanelConfigs.Count == 0)
2022-01-27 13:40:04 +00:00
return;
2022-07-23 19:23:32 +00:00
var panelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(p => p.PanelIndex == panelIndex);
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
if (panelConfig != null)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
// Should not apply any other settings if panel is full screen mode
2022-04-18 19:52:39 +00:00
if (panelConfig.FullScreen)
return;
2022-01-27 13:40:04 +00:00
int orignalLeft = panelConfig.Left;
2022-07-23 19:23:32 +00:00
switch (configPropertyName)
2022-01-27 13:40:04 +00:00
{
case PanelConfigPropertyName.Left:
panelConfig.Left += changeAmount;
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-01-27 13:40:04 +00:00
break;
case PanelConfigPropertyName.Top:
panelConfig.Top += changeAmount;
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-01-27 13:40:04 +00:00
break;
case PanelConfigPropertyName.Width:
panelConfig.Width += changeAmount;
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-04-18 03:38:33 +00:00
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
2022-04-18 03:38:33 +00:00
2022-01-27 13:40:04 +00:00
break;
case PanelConfigPropertyName.Height:
panelConfig.Height += changeAmount;
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-04-18 03:38:33 +00:00
2022-07-23 19:23:32 +00:00
WindowActionManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
2022-04-18 03:38:33 +00:00
2022-01-27 13:40:04 +00:00
break;
default:
return;
}
2022-07-23 19:23:32 +00:00
ProfileData.WriteProfiles();
2022-01-27 13:40:04 +00:00
}
}
2022-07-23 19:23:32 +00:00
private void HookWinEvent()
{
// Setup panel config event hooks
2022-07-25 12:40:21 +00:00
_winEventHook = PInvoke.SetWinEventHook(PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND, PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, _winEvent, 0, 0, PInvokeConstant.WINEVENT_OUTOFCONTEXT);
2022-07-23 19:23:32 +00:00
}
private void UnhookWinEvent()
{
// Unhook all Win API events
PInvoke.UnhookWinEvent(_winEventHook);
}
private void EventCallback(IntPtr hWinEventHook, uint iEvent, IntPtr hwnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime)
2022-01-27 13:40:04 +00:00
{
2022-06-30 23:53:08 +00:00
switch (iEvent)
2022-01-27 13:40:04 +00:00
{
2022-06-13 03:49:56 +00:00
case PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE:
case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND:
case PInvokeConstant.EVENT_SYSTEM_CAPTURESTART:
case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND:
// check by priority to speed up comparing of escaping constraints
2022-07-23 19:23:32 +00:00
if (hwnd == IntPtr.Zero
2022-06-13 03:49:56 +00:00
|| idObject != 0
|| hWinEventHook != _winEventHook
|| !AllowEdit
2022-07-23 19:23:32 +00:00
|| ActiveProfile == null
|| ActiveProfile.PanelConfigs == null
|| ActiveProfile.PanelConfigs.Count == 0)
2022-06-13 03:49:56 +00:00
{
return;
}
2022-07-23 19:23:32 +00:00
HandleEventCallback(hwnd, iEvent);
2022-06-13 03:49:56 +00:00
break;
default:
break;
2022-01-27 13:40:04 +00:00
}
2022-06-13 03:49:56 +00:00
}
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
private void HandleEventCallback(IntPtr hwnd, uint iEvent)
2022-06-13 03:49:56 +00:00
{
2022-07-23 19:23:32 +00:00
var panelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(panel => panel.PanelHandle == hwnd);
2022-06-13 03:49:56 +00:00
if (panelConfig == null)
return;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
// Should not apply any other settings if panel is full screen mode
if (panelConfig.FullScreen)
return;
if (panelConfig.IsLockable && ActiveProfile.IsLocked)
2022-06-13 03:49:56 +00:00
{
switch (iEvent)
2022-01-27 13:40:04 +00:00
{
2022-06-13 03:49:56 +00:00
case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND:
// Move window back to original location
2022-01-27 13:40:04 +00:00
PInvoke.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height, false);
2022-06-13 03:49:56 +00:00
break;
case PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE:
2022-01-27 13:40:04 +00:00
// Detect if window is maximized, if so, save settings
WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
wp.length = System.Runtime.InteropServices.Marshal.SizeOf(wp);
2022-07-23 19:23:32 +00:00
PInvoke.GetWindowPlacement(hwnd, ref wp);
2022-01-27 13:40:04 +00:00
if (wp.showCmd == PInvokeConstant.SW_SHOWMAXIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWMINIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWNORMAL)
{
2022-07-23 19:23:32 +00:00
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_RESTORE);
2022-01-27 13:40:04 +00:00
}
2022-06-13 03:49:56 +00:00
break;
}
}
else
2022-01-27 13:40:04 +00:00
{
switch (iEvent)
{
case PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE:
Rectangle winRectangle;
PInvoke.GetWindowRect(panelConfig.PanelHandle, out winRectangle);
if (_lastWindowRectangle == winRectangle) // ignore duplicate callback messages
return;
_lastWindowRectangle = winRectangle;
Rectangle clientRectangle;
PInvoke.GetClientRect(panelConfig.PanelHandle, out clientRectangle);
panelConfig.Left = winRectangle.Left;
panelConfig.Top = winRectangle.Top;
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
{
panelConfig.Width = clientRectangle.Width;
panelConfig.Height = clientRectangle.Height;
}
else
{
panelConfig.Width = clientRectangle.Width + 16;
panelConfig.Height = clientRectangle.Height + 39;
}
2022-01-27 13:40:04 +00:00
// Detect if window is maximized, if so, save settings
WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
wp.length = System.Runtime.InteropServices.Marshal.SizeOf(wp);
2022-07-23 19:23:32 +00:00
PInvoke.GetWindowPlacement(hwnd, ref wp);
2022-01-27 13:40:04 +00:00
if (wp.showCmd == PInvokeConstant.SW_SHOWMAXIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWMINIMIZED)
{
2022-07-23 19:23:32 +00:00
ProfileData.WriteProfiles();
2022-01-27 13:40:04 +00:00
}
break;
case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND:
2022-07-23 19:23:32 +00:00
ProfileData.WriteProfiles();
break;
2022-06-13 03:49:56 +00:00
}
}
}
2022-01-27 13:40:04 +00:00
}
}