1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 15:20:10 +00:00
msfs-popout-panel-manager/MainApp/AppWindow/AppMainWindow.xaml.cs
2024-02-27 21:44:21 -05:00

102 lines
3.8 KiB
C#

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interop;
using Microsoft.Extensions.DependencyInjection;
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.MainApp.AppUserControl;
using MSFSPopoutPanelManager.MainApp.ViewModel;
using MSFSPopoutPanelManager.WindowsAgent;
namespace MSFSPopoutPanelManager.MainApp.AppWindow
{
public partial class AppMainWindow
{
private readonly ApplicationViewModel _viewModel;
public AppMainWindow()
{
InitializeComponent();
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
return;
_viewModel = App.AppHost.Services.GetRequiredService<ApplicationViewModel>();
Loaded += AppWindow_Loaded;
Closing += AppWindow_Closing;
StateChanged += AppWindow_StateChanged;
WindowActionManager.OnPopOutManagerAlwaysOnTopChanged += (_, e) => { Topmost = e; };
MouseLeftButtonDown += (_, _) => DragMove();
}
private void AppWindow_Loaded(object sender, RoutedEventArgs e)
{
var window = Window.GetWindow(this);
if (window == null)
throw new ApplicationException("Cannot instantiate Pop Out Panel Manager.");
_viewModel.ApplicationHandle = new WindowInteropHelper(window).Handle;
_viewModel.ApplicationWindow = Application.Current.MainWindow;
_viewModel.Initialize();
DataContext = _viewModel;
// Try to fix always on to click through. This won't work for application's title bar since mouseEnter won't be triggered.
// This is super tricky to trick POPM process is MSFS process to avoid Windows OS focus stealing
MouseEnter += (_, _) =>WindowActionManager.SetWindowFocus(WindowProcessManager.GetApplicationProcess().Handle);
}
private void AppWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
_viewModel.WindowClosing();
}
private void AppWindow_StateChanged(object sender, EventArgs e)
{
switch (WindowState)
{
case WindowState.Maximized:
ShowInTaskbar = true;
break;
case WindowState.Minimized:
if (_viewModel.AppSettingData.ApplicationSetting.GeneralSetting.MinimizeToTray)
{
SystemTrayIcon.Tray.Visibility = Visibility.Visible;
ShowInTaskbar = false;
}
break;
case WindowState.Normal:
SystemTrayIcon.Tray.Visibility = Visibility.Hidden;
ShowInTaskbar = true;
// Fix always on top status once app is minimize and then restore
if (_viewModel.AppSettingData.ApplicationSetting.GeneralSetting.AlwaysOnTop)
WindowActionManager.ApplyAlwaysOnTop(_viewModel.ApplicationHandle, PanelType.PopOutManager, true);
break;
}
}
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
PanelDrawers.Children.Clear();
PanelDrawers.Children.Add(new PreferenceDrawer());
}
private void HelpButton_Click(object sender, RoutedEventArgs e)
{
PanelDrawers.Children.Clear();
PanelDrawers.Children.Add(new HelpDrawer());
}
private void BtnMinimize_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}