From 4651c8012c64ecdaf8c18c3446801c6dd0e06ae1 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:47:47 +1000 Subject: [PATCH] add appstate --- .../Features/IdeSettings/AppState.cs | 19 +++++++++ .../Features/IdeSettings/AppState.cs.uid | 1 + .../Features/IdeSettings/AppStateLoader.cs | 39 +++++++++++++++++++ .../IdeSettings/AppStateLoader.cs.uid | 1 + src/SharpIDE.Godot/IdeWindow.cs | 21 +++++----- src/SharpIDE.Godot/Singletons.cs | 2 + 6 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/SharpIDE.Godot/Features/IdeSettings/AppState.cs create mode 100644 src/SharpIDE.Godot/Features/IdeSettings/AppState.cs.uid create mode 100644 src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs create mode 100644 src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs.uid diff --git a/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs b/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs new file mode 100644 index 0000000..105e7f8 --- /dev/null +++ b/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs @@ -0,0 +1,19 @@ +namespace SharpIDE.Godot.Features.IdeSettings; + +public class AppState +{ + public string? LastOpenSolutionFilePath { get; set; } + public IdeSettings IdeSettings { get; set; } = new IdeSettings(); + public List PreviouslyOpenedSolutions { get; set; } = []; +} + +public class IdeSettings +{ + public bool AutoOpenLastSolution { get; set; } +} + +public class PreviouslyOpenedSln +{ + public required string Name { get; set; } + public required string FilePath { get; set; } +} \ No newline at end of file diff --git a/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs.uid b/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs.uid new file mode 100644 index 0000000..d7ba844 --- /dev/null +++ b/src/SharpIDE.Godot/Features/IdeSettings/AppState.cs.uid @@ -0,0 +1 @@ +uid://ccsfv10f1lhxd diff --git a/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs b/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs new file mode 100644 index 0000000..a9ea1f5 --- /dev/null +++ b/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs @@ -0,0 +1,39 @@ +using System.Text.Json; +using Ardalis.GuardClauses; + +namespace SharpIDE.Godot.Features.IdeSettings; + +public static class AppStateLoader +{ + private static string GetConfigFilePath() + { + var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + var configFolder = Path.Combine(folder, "SharpIDE"); + Directory.CreateDirectory(configFolder); + var configFilePath = Path.Combine(configFolder, "sharpIde.json"); + return configFilePath; + } + + public static AppState LoadAppStateFromConfigFile() + { + var configFilePath = GetConfigFilePath(); + if (File.Exists(configFilePath) is false) + { + File.WriteAllText(configFilePath, "{}"); + } + + using var stream = File.OpenRead(configFilePath); + var deserializedAppState = JsonSerializer.Deserialize(stream); + Guard.Against.Null(deserializedAppState, nameof(deserializedAppState)); + + return deserializedAppState; + } + + public static void SaveAppStateToConfigFile(AppState appState) + { + var configFilePath = GetConfigFilePath(); + using var stream = File.Create(configFilePath); + JsonSerializer.Serialize(stream, appState); + stream.Flush(); + } +} \ No newline at end of file diff --git a/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs.uid b/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs.uid new file mode 100644 index 0000000..085bfe6 --- /dev/null +++ b/src/SharpIDE.Godot/Features/IdeSettings/AppStateLoader.cs.uid @@ -0,0 +1 @@ +uid://dnu6x5m37dapi diff --git a/src/SharpIDE.Godot/IdeWindow.cs b/src/SharpIDE.Godot/IdeWindow.cs index ae40f64..50a923d 100644 --- a/src/SharpIDE.Godot/IdeWindow.cs +++ b/src/SharpIDE.Godot/IdeWindow.cs @@ -2,6 +2,7 @@ using Godot; using Microsoft.Build.Locator; using Microsoft.Extensions.Hosting; using SharpIDE.Application.Features.Events; +using SharpIDE.Godot.Features.IdeSettings; using SharpIDE.Godot.Features.SlnPicker; namespace SharpIDE.Godot; @@ -25,19 +26,21 @@ public partial class IdeWindow : Control ResourceLoader.LoadThreadedRequest(IdeRootScenePath); MSBuildLocator.RegisterDefaults(); GodotServiceDefaults.AddServiceDefaults(); + Singletons.AppState = AppStateLoader.LoadAppStateFromConfigFile(); //GetWindow().SetMinSize(new Vector2I(1152, 648)); Callable.From(() => PickSolution(true)).CallDeferred(); } - // public override void _ExitTree() - // { - // GodotGlobalEvents.Instance = null!; - // GlobalEvents.Instance = null!; - // GC.Collect(); - // GC.WaitForPendingFinalizers(); - // GC.Collect(); - // PrintOrphanNodes(); - // } + public override void _ExitTree() + { + AppStateLoader.SaveAppStateToConfigFile(Singletons.AppState); + // GodotGlobalEvents.Instance = null!; + // GlobalEvents.Instance = null!; + // GC.Collect(); + // GC.WaitForPendingFinalizers(); + // GC.Collect(); + // PrintOrphanNodes(); + } public void PickSolution(bool fullscreen = false) { diff --git a/src/SharpIDE.Godot/Singletons.cs b/src/SharpIDE.Godot/Singletons.cs index 7308259..e130ff1 100644 --- a/src/SharpIDE.Godot/Singletons.cs +++ b/src/SharpIDE.Godot/Singletons.cs @@ -1,5 +1,6 @@ using SharpIDE.Application.Features.Build; using SharpIDE.Application.Features.Run; +using SharpIDE.Godot.Features.IdeSettings; namespace SharpIDE.Godot; @@ -7,4 +8,5 @@ public static class Singletons { public static RunService RunService { get; } = new RunService(); public static BuildService BuildService { get; } = new BuildService(); + public static AppState AppState { get; set; } = null!; } \ No newline at end of file