From 620d65b1667e55e64c9c4c319324492151cd9760 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Thu, 31 Jul 2025 19:47:33 +1000 Subject: [PATCH] store last selected sln file --- .../Components/SolutionPickerDialog.razor | 16 +++++-- src/SharpIDE.Photino/Models/AppState.cs | 6 +++ src/SharpIDE.Photino/Program.cs | 46 +++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/SharpIDE.Photino/Models/AppState.cs diff --git a/src/SharpIDE.Photino/Components/SolutionPickerDialog.razor b/src/SharpIDE.Photino/Components/SolutionPickerDialog.razor index 200f93e..543e5c8 100644 --- a/src/SharpIDE.Photino/Components/SolutionPickerDialog.razor +++ b/src/SharpIDE.Photino/Components/SolutionPickerDialog.razor @@ -1,12 +1,16 @@ @using Ardalis.GuardClauses @using global::Photino.Blazor +@using SharpIDE.Photino.Models @inject PhotinoBlazorApp PhotinoBlazorApp +@inject AppState AppState - - Pick Solution - Open + + + Pick Solution + + Open @code { @@ -15,6 +19,11 @@ private string? _solutionFilePath; + protected override void OnInitialized() + { + _solutionFilePath = AppState.SolutionFilePath; + } + private async Task PickSolution() { var files = await PhotinoBlazorApp.MainWindow.ShowOpenFileAsync("Choose Solution File", filters: [("Solution File", [".sln"])]); @@ -25,6 +34,7 @@ private void Proceed() { Guard.Against.NullOrWhiteSpace(_solutionFilePath); + AppState.SolutionFilePath = _solutionFilePath; MudDialog.Close(_solutionFilePath); } } diff --git a/src/SharpIDE.Photino/Models/AppState.cs b/src/SharpIDE.Photino/Models/AppState.cs new file mode 100644 index 0000000..dc44979 --- /dev/null +++ b/src/SharpIDE.Photino/Models/AppState.cs @@ -0,0 +1,6 @@ +namespace SharpIDE.Photino.Models; + +public class AppState +{ + public required string SolutionFilePath { get; set; } +} diff --git a/src/SharpIDE.Photino/Program.cs b/src/SharpIDE.Photino/Program.cs index aa5e88f..cb37ea0 100644 --- a/src/SharpIDE.Photino/Program.cs +++ b/src/SharpIDE.Photino/Program.cs @@ -1,7 +1,9 @@ +using System.Text.Json; using Microsoft.Build.Locator; using Microsoft.Extensions.DependencyInjection; using MudBlazor.Services; using Photino.Blazor; +using SharpIDE.Photino.Models; using SharpIDE.Photino.Services; namespace SharpIDE.Photino; @@ -16,6 +18,7 @@ public class Program appBuilder.Services.AddLogging();// appBuilder.Services.AddMudServices(); appBuilder.Services.AddSingleton(); + appBuilder.Services.AddSingleton(); appBuilder.RootComponents.Add("app"); @@ -35,6 +38,21 @@ public class Program refreshOpenFileService.InvokeRefreshOpenFile(); }; + var configFilePath = GetConfigFilePath(); + + using var scope = app.Services.CreateScope(); + var appState = scope.ServiceProvider.GetRequiredService(); + + LoadAppStateFromConfigFile(appState, configFilePath); + + app.MainWindow.RegisterWindowClosingHandler((sender, eventArgs) => + { + using var stream = File.Create(configFilePath); + JsonSerializer.Serialize(stream, appState); + stream.Flush(); + return false; + }); + AppDomain.CurrentDomain.UnhandledException += (sender, error) => { app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString()); @@ -45,4 +63,32 @@ public class Program app.Run(); } + + private static string GetConfigFilePath() + { + var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + var configFolder = Path.Combine(folder, "SharpIDE.PhotinoConfig"); + Directory.CreateDirectory(configFolder); + var configFilePath = Path.Combine(configFolder, "config.json"); + return configFilePath; + } + + private static void LoadAppStateFromConfigFile(AppState appState, string configFilePath) + { + if (File.Exists(configFilePath) is false) + { + File.WriteAllText(configFilePath, string.Empty); + } + + using var stream = File.OpenRead(configFilePath); + if (stream.Length is 0) + { + return; + } + var deserializedAppState = JsonSerializer.Deserialize(stream); + if (deserializedAppState is not null) + { + appState.SolutionFilePath = deserializedAppState.SolutionFilePath; + } + } }