store last selected sln file

This commit is contained in:
Matt Parker
2025-07-31 19:47:33 +10:00
parent dea219ea90
commit 620d65b166
3 changed files with 65 additions and 3 deletions

View File

@@ -1,12 +1,16 @@
@using Ardalis.GuardClauses
@using global::Photino.Blazor
@using SharpIDE.Photino.Models
@inject PhotinoBlazorApp PhotinoBlazorApp
@inject AppState AppState
<MudStack Class="mx-4">
<MudTextField T="string" Label="Solution File" Value="@_solutionFilePath" ReadOnly="true" />
<MudButton OnClick="@PickSolution">Pick Solution</MudButton>
<MudButton OnClick="@Proceed" Disabled="@(string.IsNullOrWhiteSpace(_solutionFilePath))">Open</MudButton>
<MudStack Row="true">
<MudTextField T="string" Label="Solution File" Value="@_solutionFilePath" ReadOnly="true" />
<MudButton OnClick="@PickSolution">Pick Solution</MudButton>
</MudStack>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@Proceed" Disabled="@(string.IsNullOrWhiteSpace(_solutionFilePath))">Open</MudButton>
</MudStack>
@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);
}
}

View File

@@ -0,0 +1,6 @@
namespace SharpIDE.Photino.Models;
public class AppState
{
public required string SolutionFilePath { get; set; }
}

View File

@@ -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<RefreshOpenFileService>();
appBuilder.Services.AddSingleton<AppState>();
appBuilder.RootComponents.Add<App>("app");
@@ -35,6 +38,21 @@ public class Program
refreshOpenFileService.InvokeRefreshOpenFile();
};
var configFilePath = GetConfigFilePath();
using var scope = app.Services.CreateScope();
var appState = scope.ServiceProvider.GetRequiredService<AppState>();
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<AppState>(stream);
if (deserializedAppState is not null)
{
appState.SolutionFilePath = deserializedAppState.SolutionFilePath;
}
}
}