1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-28 08:40:09 +00:00
msfs-popout-panel-manager/MainApp/ViewModel/MessageWindowViewModel.cs

144 lines
5 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Orchestration;
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.WindowsAgent;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
2024-02-28 02:44:21 +00:00
using System.Windows.Media;
using Colors = System.Windows.Media.Colors;
2023-07-12 22:41:31 +00:00
namespace MSFSPopoutPanelManager.MainApp.ViewModel
{
public class MessageWindowViewModel : BaseViewModel
{
2023-11-05 02:10:03 +00:00
private const int WINDOW_WIDTH_POPOUT_MESSAGE = 400;
private const int WINDOW_HEIGHT_POPOUT_MESSAGE = 225;
private const int WINDOW_WIDTH_REGULAR_MESSAGE = 300;
private const int WINDOW_HEIGHT_REGULAR_MESSAGE = 75;
2023-07-12 22:41:31 +00:00
private bool _isVisible;
public IntPtr Handle { get; set; }
public event EventHandler<List<Run>> OnMessageUpdated;
public bool IsVisible
{
2024-02-28 02:44:21 +00:00
get => _isVisible;
2023-07-12 22:41:31 +00:00
set
{
if (!AppSettingData.ApplicationSetting.PopOutSetting.EnablePopOutMessages)
return;
2023-07-12 22:41:31 +00:00
_isVisible = value;
if (value)
{
CenterDialogToGameWindow();
}
}
}
2023-11-05 02:10:03 +00:00
public int WindowWidth { get; set; }
2023-07-12 22:41:31 +00:00
2023-11-05 02:10:03 +00:00
public int WindowHeight { get; set; }
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
public MessageWindowViewModel(SharedStorage sharedStorage, PanelSourceOrchestrator panelSourceOrchestrator, PanelPopOutOrchestrator panelPopOutOrchestrator) : base(sharedStorage)
2023-07-12 22:41:31 +00:00
{
IsVisible = false;
2024-02-28 02:44:21 +00:00
panelPopOutOrchestrator.OnPopOutStarted += (_, _) =>
2023-11-05 02:10:03 +00:00
{
IsVisible = true;
WindowWidth = WINDOW_WIDTH_POPOUT_MESSAGE;
WindowHeight = WINDOW_HEIGHT_POPOUT_MESSAGE;
};
2024-02-28 02:44:21 +00:00
panelPopOutOrchestrator.OnPopOutCompleted += (_, _) =>
2023-07-12 22:41:31 +00:00
{
Thread.Sleep(1000);
IsVisible = false;
2023-11-05 02:10:03 +00:00
WindowWidth = WINDOW_WIDTH_POPOUT_MESSAGE;
WindowHeight = WINDOW_HEIGHT_POPOUT_MESSAGE;
};
2024-02-28 02:44:21 +00:00
panelSourceOrchestrator.OnStatusMessageStarted += (_, _) =>
2023-11-05 02:10:03 +00:00
{
IsVisible = true;
WindowWidth = WINDOW_WIDTH_REGULAR_MESSAGE;
WindowHeight = WINDOW_HEIGHT_REGULAR_MESSAGE;
};
2024-02-28 02:44:21 +00:00
panelSourceOrchestrator.OnStatusMessageEnded += (_, _) =>
2023-11-05 02:10:03 +00:00
{
IsVisible = false;
WindowWidth = WINDOW_WIDTH_REGULAR_MESSAGE;
WindowHeight = WINDOW_HEIGHT_REGULAR_MESSAGE;
2023-07-12 22:41:31 +00:00
};
2024-02-28 02:44:21 +00:00
StatusMessageWriter.OnStatusMessage += (_, e) =>
2023-07-12 22:41:31 +00:00
{
Application.Current.Dispatcher.Invoke(() =>
{
if (AppSettingData.ApplicationSetting.PopOutSetting.EnablePopOutMessages)
{
WindowActionManager.ApplyAlwaysOnTop(Handle, PanelType.StatusMessageWindow, true);
2023-08-16 03:41:14 +00:00
OnMessageUpdated?.Invoke(this, FormatStatusMessages(e));
2023-07-12 22:41:31 +00:00
CenterDialogToGameWindow();
}
2023-07-12 22:41:31 +00:00
});
};
}
private void CenterDialogToGameWindow()
{
if (WindowProcessManager.SimulatorProcess == null)
return;
var simulatorRectangle = WindowActionManager.GetWindowRectangle(WindowProcessManager.SimulatorProcess.Handle);
2023-11-05 02:10:03 +00:00
var left = simulatorRectangle.Left + simulatorRectangle.Width / 2 - WindowWidth / 2;
var top = simulatorRectangle.Top + simulatorRectangle.Height / 2 - WindowHeight / 2;
WindowActionManager.MoveWindow(Handle, left, top, WindowWidth, WindowHeight);
2023-07-12 22:41:31 +00:00
WindowActionManager.ApplyAlwaysOnTop(Handle, PanelType.StatusMessageWindow, true);
}
2024-02-28 02:44:21 +00:00
private List<Run> FormatStatusMessages(List<StatusMessage> messages)
{
var runs = new List<Run>
{
Capacity = 0
};
foreach (var statusMessage in messages)
{
var run = new Run
{
Text = statusMessage.Message
};
switch (statusMessage.StatusMessageType)
{
case StatusMessageType.Success:
run.Foreground = new SolidColorBrush(Colors.LimeGreen);
break;
case StatusMessageType.Failure:
run.Foreground = new SolidColorBrush(Colors.IndianRed);
break;
case StatusMessageType.Executing:
run.Foreground = new SolidColorBrush(Colors.NavajoWhite);
break;
case StatusMessageType.Info:
break;
}
runs.Add(run);
if (statusMessage.NewLine)
runs.Add(new Run { Text = Environment.NewLine });
}
return runs;
}
2023-07-12 22:41:31 +00:00
}
}