Files @ 0fc41569b1c7
Branch filter:

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

Jason Maltzen
Update to 1.11.1
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();
            }
        }
    }
}