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/Provider/PanelConfigurationManager.cs

316 lines
14 KiB
C#
Raw Normal View History

2022-01-27 13:40:04 +00:00
using MSFSPopoutPanelManager.Model;
using System;
using System.Drawing;
using System.Linq;
2022-06-13 03:49:56 +00:00
using System.Threading;
using System.Threading.Tasks;
2022-01-27 13:40:04 +00:00
namespace MSFSPopoutPanelManager.Provider
{
public class PanelConfigurationManager
{
private UserProfileManager _userProfileManager;
private IntPtr _winEventHook;
private static PInvoke.WinEventProc _winEvent; // keep this as static to prevent garbage collect or the app will crash
private Rectangle _lastWindowRectangle;
2022-06-13 03:49:56 +00:00
private uint _prevWinEvent = PInvokeConstant.EVENT_SYSTEM_CAPTUREEND;
private int _winEventClickLock = 0;
2022-01-27 13:40:04 +00:00
public UserProfile UserProfile { get; set; }
public bool AllowEdit { get; set; }
public PanelConfigurationManager(UserProfileManager userProfileManager)
{
_userProfileManager = userProfileManager;
_winEvent = new PInvoke.WinEventProc(EventCallback);
AllowEdit = true;
}
public void HookWinEvent()
{
2022-06-30 23:53:08 +00:00
// Setup panel config event hooks
2022-06-13 03:49:56 +00:00
_winEventHook = PInvoke.SetWinEventHook(PInvokeConstant.EVENT_SYSTEM_CAPTURESTART, PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE, DiagnosticManager.GetApplicationProcess().Handle, _winEvent, 0, 0, PInvokeConstant.WINEVENT_OUTOFCONTEXT);
2022-01-27 13:40:04 +00:00
}
public void UnhookWinEvent()
{
// Unhook all Win API events
PInvoke.UnhookWinEvent(_winEventHook);
}
public void LockPanelsUpdated()
{
UserProfile.IsLocked = !UserProfile.IsLocked;
_userProfileManager.WriteUserProfiles();
}
public void PanelConfigPropertyUpdated(PanelConfigItem panelConfigItem)
{
2022-05-31 15:45:35 +00:00
if (panelConfigItem == null || !AllowEdit || UserProfile.IsLocked)
2022-01-27 13:40:04 +00:00
return;
var panelConfig = UserProfile.PanelConfigs.ToList().Find(p => p.PanelIndex == panelConfigItem.PanelIndex);
if (panelConfig != null)
{
2022-04-18 19:52:39 +00:00
if (panelConfigItem.PanelConfigProperty == 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;
}
else if (panelConfigItem.PanelConfigProperty == PanelConfigPropertyName.PanelName)
{
var name = panelConfig.PanelName;
if (name.IndexOf("(Custom)") == -1)
name = name + " (Custom)";
PInvoke.SetWindowText(panelConfig.PanelHandle, name);
}
2022-06-30 23:53:08 +00:00
else if (!panelConfig.FullScreen)
2022-04-18 19:52:39 +00:00
{
switch (panelConfigItem.PanelConfigProperty)
{
case PanelConfigPropertyName.Left:
case PanelConfigPropertyName.Top:
2022-06-30 23:53:08 +00:00
WindowManager.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)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-06-30 23:53:08 +00:00
WindowManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
if (panelConfig.HideTitlebar)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
break;
case PanelConfigPropertyName.AlwaysOnTop:
2022-06-30 23:53:08 +00:00
WindowManager.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:
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, panelConfig.HideTitlebar);
break;
}
2022-01-27 13:40:04 +00:00
}
_userProfileManager.WriteUserProfiles();
}
}
public void PanelConfigIncreaseDecrease(PanelConfigItem panelConfigItem, int changeAmount)
{
2022-05-31 15:45:35 +00:00
if (panelConfigItem == null || !AllowEdit || UserProfile.IsLocked || UserProfile.PanelConfigs == null || UserProfile.PanelConfigs.Count == 0)
2022-01-27 13:40:04 +00:00
return;
var index = UserProfile.PanelConfigs.ToList().FindIndex(p => p.PanelIndex == panelConfigItem.PanelIndex);
if (index > -1)
{
var panelConfig = UserProfile.PanelConfigs[index];
2022-04-18 19:52:39 +00:00
// Cannot apply any other settings if panel is full screen mode
if (panelConfig.FullScreen)
return;
2022-01-27 13:40:04 +00:00
int orignalLeft = panelConfig.Left;
switch (panelConfigItem.PanelConfigProperty)
{
case PanelConfigPropertyName.Left:
panelConfig.Left += changeAmount;
2022-06-30 23:53:08 +00:00
WindowManager.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-06-30 23:53:08 +00:00
WindowManager.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)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-06-30 23:53:08 +00:00
WindowManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
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)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2022-06-30 23:53:08 +00:00
WindowManager.MoveWindowWithMsfsBugOverrirde(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 03:38:33 +00:00
if (panelConfig.HideTitlebar)
WindowManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
2022-01-27 13:40:04 +00:00
break;
default:
return;
}
_userProfileManager.WriteUserProfiles();
}
}
private void EventCallback(IntPtr hWinEventHook, uint iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime)
{
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
if (hWnd == IntPtr.Zero
|| idObject != 0
|| hWinEventHook != _winEventHook
|| !AllowEdit
|| UserProfile.PanelConfigs == null
|| UserProfile.PanelConfigs.Count == 0)
{
return;
}
HandleEventCallback(hWnd, iEvent);
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-06-13 03:49:56 +00:00
private void HandleEventCallback(IntPtr hWnd, uint iEvent)
{
var panelConfig = UserProfile.PanelConfigs.FirstOrDefault(panel => panel.PanelHandle == hWnd);
if (panelConfig == null)
return;
2022-01-27 13:40:04 +00:00
2022-06-13 03:49:56 +00:00
if (panelConfig.IsLockable && UserProfile.IsLocked)
{
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);
PInvoke.GetWindowPlacement(hWnd, ref wp);
if (wp.showCmd == PInvokeConstant.SW_SHOWMAXIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWMINIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWNORMAL)
{
PInvoke.ShowWindow(hWnd, PInvokeConstant.SW_RESTORE);
}
2022-06-13 03:49:56 +00:00
break;
case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND:
if (!panelConfig.TouchEnabled || _prevWinEvent == PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE)
break;
2022-01-27 13:40:04 +00:00
2022-06-13 03:49:56 +00:00
if (!panelConfig.HasTouchableEvent || _prevWinEvent == PInvokeConstant.EVENT_SYSTEM_CAPTUREEND)
break;
2022-01-27 13:40:04 +00:00
2022-06-13 03:49:56 +00:00
HandleTouchEvent(panelConfig);
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);
PInvoke.GetWindowPlacement(hWnd, ref wp);
if (wp.showCmd == PInvokeConstant.SW_SHOWMAXIMIZED || wp.showCmd == PInvokeConstant.SW_SHOWMINIMIZED)
{
_userProfileManager.WriteUserProfiles();
}
break;
case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND:
_userProfileManager.WriteUserProfiles();
break;
2022-06-13 03:49:56 +00:00
case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND:
if (!panelConfig.TouchEnabled || _prevWinEvent == PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE)
break;
if (!panelConfig.HasTouchableEvent || _prevWinEvent == PInvokeConstant.EVENT_SYSTEM_CAPTUREEND)
break;
HandleTouchEvent(panelConfig);
break;
}
}
_prevWinEvent = iEvent;
}
private void HandleTouchEvent(PanelConfig panelConfig)
{
Point point;
PInvoke.GetCursorPos(out point);
// Disable left clicking if user is touching the title bar area
if (point.Y - panelConfig.Top > (panelConfig.HideTitlebar ? 5 : 31))
{
var prevWinEventClickLock = ++_winEventClickLock;
UnhookWinEvent();
InputEmulationManager.LeftClickFast(point.X, point.Y);
HookWinEvent();
if (prevWinEventClickLock == _winEventClickLock)
{
Task.Run(() => RefocusMsfs(prevWinEventClickLock));
2022-01-27 13:40:04 +00:00
}
}
}
2022-06-13 03:49:56 +00:00
2022-06-30 23:53:08 +00:00
private void RefocusMsfs(int prevWinEventClickLock)
2022-06-13 03:49:56 +00:00
{
Thread.Sleep(1000);
if (prevWinEventClickLock == _winEventClickLock)
{
var simulatorProcess = DiagnosticManager.GetSimulatorProcess();
Rectangle rectangle;
PInvoke.GetWindowRect(simulatorProcess.Handle, out rectangle);
PInvoke.SetCursorPos(rectangle.X + 18, rectangle.Y + 80);
}
}
2022-01-27 13:40:04 +00:00
}
}