// Open GL/TK stuff
 public static void FitToScreen(FrameworkElement screen, int width, int height)
 {
     GraphicsCompanion.SetViewportDimensions((int)screen.Width, (int)screen.Height, width, height);
 }
        private static Mutex _mutex = null; // Used to detect other instances of the same application

        public MainWindow()
        {
            InitializeComponent();
            if (!_settings.LaunchMinimized)
            {
                // Position in last known location, unless negative, then center on screen.
                var wa = Screen.PrimaryScreen.WorkingArea;
                var b  = Screen.PrimaryScreen.Bounds;
                if (
                    _settings.WindowTop >= wa.Y &&
                    _settings.WindowLeft >= wa.X &&
                    _settings.WindowTop < (b.Height - Height) &&
                    _settings.WindowLeft < (b.Width - Width)
                    )
                {
                    Top  = _settings.WindowTop;
                    Left = _settings.WindowLeft;
                }
                else
                {
                    WindowStartupLocation = WindowStartupLocation.CenterScreen;
                }
            }

            // Prevent multiple instances
            _mutex = new Mutex(true, Properties.Resources.AppName, out bool createdNew);
            if (!createdNew)
            {
                System.Windows.MessageBox.Show(
                    System.Windows.Application.Current.MainWindow,
                    "This application is already running!",
                    Properties.Resources.AppName,
                    MessageBoxButton.OK,
                    MessageBoxImage.Information
                    );
                System.Windows.Application.Current.Shutdown();
            }

            // Tray icon
            var icon = Properties.Resources.Icon.Clone() as System.Drawing.Icon;

            _notifyIcon             = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.MouseClick += NotifyIcon_Click;

            _notifyIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(new System.ComponentModel.Container());
            _notifyIcon.ContextMenuStrip.SuspendLayout();
            var openMenuItem = new System.Windows.Forms.ToolStripMenuItem("Open");

            openMenuItem.Click += (sender, args) => NotifyIcon_Click(sender, new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            var exitMenuItem = new System.Windows.Forms.ToolStripMenuItem("Exit");

            exitMenuItem.Click += (sender, args) => System.Windows.Application.Current.Shutdown();
            _notifyIcon.ContextMenuStrip.Items.Add(openMenuItem);
            _notifyIcon.ContextMenuStrip.Items.Add(exitMenuItem);
            _notifyIcon.ContextMenuStrip.Name = "NotifyIconContextMenu";
            _notifyIcon.ContextMenuStrip.ResumeLayout(false);

            _notifyIcon.Text    = $"Click to show the {Properties.Resources.AppName} window";
            _notifyIcon.Icon    = icon;
            _notifyIcon.Visible = true;

            Title = Properties.Resources.AppName;

            LoadSettings();
#if DEBUG
            Label_Version.Content = $"{Properties.Resources.Version}d";
#else
            Label_Version.Content = Properties.Resources.Version;
#endif
            // Controller
            _controller = new MainController((status, state) => {
                Dispatcher.Invoke(() =>
                {
                    switch (status)
                    {
                    case SuperServer.ServerStatus.Connected:
                        label_ServerStatus.Background = Brushes.OliveDrab;
                        label_ServerStatus.Content    = "Online";
                        break;

                    case SuperServer.ServerStatus.Disconnected:
                        label_ServerStatus.Background = Brushes.Tomato;
                        label_ServerStatus.Content    = "Offline";
                        break;

                    case SuperServer.ServerStatus.Error:
                        label_ServerStatus.Background = Brushes.Gray;
                        label_ServerStatus.Content    = "Error";
                        break;
                    }
                });
            },
                                             (status) => {
                Dispatcher.Invoke(() => {
                    if (status)
                    {
                        label_OpenVRStatus.Background = Brushes.OliveDrab;
                        label_OpenVRStatus.Content    = "Connected";
                    }
                    else
                    {
                        label_OpenVRStatus.Background = Brushes.Tomato;
                        label_OpenVRStatus.Content    = "Disconnected";
                        if (_settings.ExitWithSteam)
                        {
                            _controller.Shutdown();
                            if (_notifyIcon != null)
                            {
                                _notifyIcon.Dispose();
                            }
                            System.Windows.Application.Current.Shutdown();
                        }
                    }
                });
            }
                                             );

            GraphicsCompanion.StartOpenTK(this);
            _controller.SetPort(_settings.Port);

            if (_settings.LaunchMinimized)
            {
                Loaded += (sender, args) =>
                {
                    var wa = Screen.PrimaryScreen.WorkingArea;
                    var b  = Screen.PrimaryScreen.Bounds;
                    if ( // If we have a valid stored location, use it
                        _settings.WindowTop >= wa.Y &&
                        _settings.WindowLeft >= wa.X &&
                        _settings.WindowTop < (b.Height - Height) &&
                        _settings.WindowLeft < (b.Width - Width)
                        )
                    {
                        Top  = _settings.WindowTop;
                        Left = _settings.WindowLeft;
                    }
                    else     // Otherwise center on screen
                    {
                        Top  = wa.Y + (wa.Height / 2 - Height / 2);
                        Left = wa.X + (wa.Width / 2 - Width / 2);
                    }

                    WindowState   = WindowState.Minimized;
                    ShowInTaskbar = !_settings.Tray;
                };
            }
        }