1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 07:10:10 +00:00
msfs-popout-panel-manager/WpfApp/ApplicationWindow.xaml.cs

106 lines
4.4 KiB
C#
Raw Normal View History

2022-01-27 13:40:04 +00:00
using Hardcodet.Wpf.TaskbarNotification;
using MahApps.Metro.Controls;
using MSFSPopoutPanelManager.Shared;
2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.WindowsAgent;
2022-01-27 13:40:04 +00:00
using MSFSPopoutPanelManager.WpfApp.ViewModel;
2022-07-23 19:23:32 +00:00
using Prism.Commands;
using System.Threading.Tasks;
2022-01-27 13:40:04 +00:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
namespace MSFSPopoutPanelManager.WpfApp
{
public partial class ApplicationWindow : MetroWindow
{
private ApplicationViewModel _viewModel;
// This command has to be here since it doesn't work in view model, window StateChanged never gets fire
2022-07-23 19:23:32 +00:00
public DelegateCommand RestoreWindowCommand => new DelegateCommand(() => { this.WindowState = WindowState.Normal; }, () => { return true; });
2022-01-27 13:40:04 +00:00
public ApplicationWindow()
{
InitializeComponent();
_viewModel = new ApplicationViewModel();
_viewModel.ShowContextMenuBalloonTip += HandleShowContextMenuBalloonTip;
this.DataContext = _viewModel;
UserControlPanelSelection userControlPanelSelection = new UserControlPanelSelection(_viewModel.PanelSelectionViewModel);
UserControlPanelConfiguration userControlPanelConfiguration = new UserControlPanelConfiguration(_viewModel.PanelConfigurationViewModel);
Binding panelSelectionVisibilityBinding = new Binding()
{
Source = this.DataContext,
Path = new PropertyPath("IsShownPanelSelectionScreen"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new BoolToVisibilityConverter()
};
BindingOperations.SetBinding(userControlPanelSelection, UserControl.VisibilityProperty, panelSelectionVisibilityBinding);
Binding panelConfigurationVisibilityBinding = new Binding()
{
Source = this.DataContext,
Path = new PropertyPath("IsShownPanelConfigurationScreen"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new BoolToVisibilityConverter()
};
BindingOperations.SetBinding(userControlPanelConfiguration, UserControl.VisibilityProperty, panelConfigurationVisibilityBinding);
panelSteps.Children.Add(userControlPanelSelection);
panelSteps.Children.Add(userControlPanelConfiguration);
notifyIcon.DoubleClickCommand = RestoreWindowCommand;
2022-07-23 19:23:32 +00:00
WindowActionManager.OnPopOutManagerAlwaysOnTopChanged += (sender, e) => { this.Topmost = e; };
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
private void HandleShowContextMenuBalloonTip(object sender, StatusMessageEventArg e)
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
Application.Current.Dispatcher.Invoke(() =>
2022-01-27 13:40:04 +00:00
{
2022-07-23 19:23:32 +00:00
notifyIcon.ShowBalloonTip("MSFS Pop Out Panel Manager Error", e.Message, BalloonIcon.Error);
});
2022-01-27 13:40:04 +00:00
}
private void Window_StateChanged(object sender, System.EventArgs e)
{
switch (this.WindowState)
{
case WindowState.Maximized:
this.ShowInTaskbar = true;
break;
case WindowState.Minimized:
2022-07-23 19:23:32 +00:00
if (_viewModel.AppSettingData.AppSetting.MinimizeToTray)
2022-01-27 13:40:04 +00:00
{
notifyIcon.Visibility = Visibility.Visible;
this.ShowInTaskbar = false;
}
break;
case WindowState.Normal:
notifyIcon.Visibility = Visibility.Hidden;
this.ShowInTaskbar = true;
// Fix always on top status once app is minimize and then restore
2022-07-23 19:23:32 +00:00
if (_viewModel.AppSettingData.AppSetting.AlwaysOnTop)
WindowActionManager.ApplyAlwaysOnTop(_viewModel.ApplicationHandle, PanelType.PopOutManager, true);
2022-01-27 13:40:04 +00:00
break;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
2022-07-23 19:23:32 +00:00
_viewModel.ApplicationHandle = new WindowInteropHelper(Window.GetWindow(this)).Handle;
_viewModel.ApplicationWindow = Application.Current.MainWindow;
2022-01-27 13:40:04 +00:00
_viewModel.Initialize();
}
2022-07-23 19:23:32 +00:00
private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
2022-04-18 03:38:33 +00:00
{
2022-07-23 19:23:32 +00:00
e.Cancel = true;
await _viewModel.WindowClosing();
2022-04-18 03:38:33 +00:00
}
2022-01-27 13:40:04 +00:00
}
}