Files @ 70b1de28b2a2
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/App.axaml.cs

Jason Maltzen
Re-enable the ability to save debug screenshots based on a setting value to help debug reaction capturing. Update the README to correctly reflect the debug.screenshot setting name, location of the settings file, and removal of the old debug menu.
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using DesertPaintCodex.Services;
using DesertPaintCodex.Views;

namespace DesertPaintCodex
{
    internal class App : Application
    {
        private WelcomeView? _welcomeView;
        private MainWindow? _mainWindow;
        
        public override void Initialize()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override void OnFrameworkInitializationCompleted()
        { 
            ShowWelcomeView();
            base.OnFrameworkInitializationCompleted();
        }

        public void ReturnToWelcome()
        {
            ShowWelcomeView();
            CloseMainWindow();
        }
        
        public void RefreshMainWindow()
        {
            if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return;
            
            desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            CloseMainWindow();
            ShowMainWindow();
        }

        private void ShowWelcomeView()
        {
            if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return;
            
            desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            _welcomeView         = new WelcomeView();
            _welcomeView.Closed += OnWelcomeViewClosed;
            _welcomeView.Show();
        }

        private void ShowMainWindow()
        {
            if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return;
            
            desktop.ShutdownMode = ShutdownMode.OnMainWindowClose;
            _mainWindow          = new MainWindow();
            _mainWindow.Closing += _mainWindow.OnMainWindowClosing;
            desktop.MainWindow   = _mainWindow;
            desktop.MainWindow.Show();
        }

        private void Shutdown()
        {
            if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return;
            desktop.Shutdown();
        }

        private void CloseMainWindow()
        {
            if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return;
            if (_mainWindow != null)
            {
                desktop.MainWindow.Closing -= _mainWindow.OnMainWindowClosing;
            }
            desktop.MainWindow.Close();
            _mainWindow = null;
            desktop.MainWindow = null;
        }

        private void OnWelcomeViewClosed(object? obj, EventArgs args)
        {
            if (_welcomeView != null) _welcomeView.Closed -= OnWelcomeViewClosed;
            _welcomeView = null;

            if (ProfileManager.CurrentProfile == null)
            {
                Shutdown();
            }
            else
            {
                ShowMainWindow();
            }
        }
    }
}