diff --git a/src/SharpIDE.Photino/Components/CodeViewer.razor b/src/SharpIDE.Photino/Components/CodeViewer.razor index 2df6ac9..de214ab 100644 --- a/src/SharpIDE.Photino/Components/CodeViewer.razor +++ b/src/SharpIDE.Photino/Components/CodeViewer.razor @@ -1,4 +1,8 @@ - +@using SharpIDE.Photino.Services + +@inject RefreshOpenFileService RefreshOpenFileService + + @Path.GetFileName(FilePath) @if (_unsavedEdits) { @@ -10,15 +14,20 @@ height: calc(100vh - 100px); } - +@{ +} + @code { [Parameter, EditorRequired] public string FilePath { get; set; } = null!; private string? _fileContent; + private string? _unsavedFileContent; private bool _unsavedEdits = false; + StandaloneCodeEditor _codeEditorRef = null!; + private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) { return new StandaloneEditorConstructionOptions @@ -33,6 +42,20 @@ } protected override async Task OnInitializedAsync() + { + await ReadFile(); + RefreshOpenFileService.RefreshOpenFile += async () => + { + Console.WriteLine("RefreshOpenFileService.RefreshOpenFile called"); + await ReadFile(); + var model = await _codeEditorRef.GetModel(); + ArgumentNullException.ThrowIfNull(model); + await model.SetValue(_fileContent); + StateHasChanged(); + }; + } + + private async Task ReadFile() { var fileInfo = new FileInfo(FilePath); if (!fileInfo.Exists) @@ -53,7 +76,20 @@ ArgumentNullException.ThrowIfNull(editor, nameof(editor)); var editorTextValue = await editor.GetValue(); await File.WriteAllTextAsync(FilePath, editorTextValue); + _fileContent = editorTextValue; _unsavedEdits = false; } + private async Task IfDirtyMarkDirty() + { + // Probably quite non-performant + if (await _codeEditorRef.GetValue() != _fileContent) + { + _unsavedEdits = true; + } + else + { + _unsavedEdits = false; + } + } } diff --git a/src/SharpIDE.Photino/Components/SolutionExplorer.razor b/src/SharpIDE.Photino/Components/SolutionExplorer.razor index eae95bf..be350e5 100644 --- a/src/SharpIDE.Photino/Components/SolutionExplorer.razor +++ b/src/SharpIDE.Photino/Components/SolutionExplorer.razor @@ -58,7 +58,6 @@ private void LoadSolution(string solutionPath) { - return; var solutionFile = GetNodesInSolution.ParseSolutionFileFromPath(solutionPath); ArgumentNullException.ThrowIfNull(solutionFile); _solutionFile = solutionFile; diff --git a/src/SharpIDE.Photino/Program.cs b/src/SharpIDE.Photino/Program.cs index 1f7ef40..aa5e88f 100644 --- a/src/SharpIDE.Photino/Program.cs +++ b/src/SharpIDE.Photino/Program.cs @@ -1,7 +1,8 @@ -using Microsoft.Build.Locator; +using Microsoft.Build.Locator; using Microsoft.Extensions.DependencyInjection; using MudBlazor.Services; using Photino.Blazor; +using SharpIDE.Photino.Services; namespace SharpIDE.Photino; @@ -12,8 +13,9 @@ public class Program { var appBuilder = PhotinoBlazorAppBuilder.CreateDefault(args); - appBuilder.Services.AddLogging(); + appBuilder.Services.AddLogging();// appBuilder.Services.AddMudServices(); + appBuilder.Services.AddSingleton(); appBuilder.RootComponents.Add("app"); @@ -27,6 +29,12 @@ public class Program //.SetIconFile("favicon.ico") .SetTitle("SharpIDE.Photino"); + app.MainWindow.WindowFocusInHandler += (sender, _) => + { + var refreshOpenFileService = app.Services.GetRequiredService(); + refreshOpenFileService.InvokeRefreshOpenFile(); + }; + AppDomain.CurrentDomain.UnhandledException += (sender, error) => { app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString()); @@ -38,4 +46,3 @@ public class Program app.Run(); } } - diff --git a/src/SharpIDE.Photino/Services/RefreshOpenFileService.cs b/src/SharpIDE.Photino/Services/RefreshOpenFileService.cs new file mode 100644 index 0000000..041673c --- /dev/null +++ b/src/SharpIDE.Photino/Services/RefreshOpenFileService.cs @@ -0,0 +1,10 @@ +namespace SharpIDE.Photino.Services; + +public class RefreshOpenFileService +{ + public event Func? RefreshOpenFile; + public void InvokeRefreshOpenFile() + { + RefreshOpenFile?.Invoke(); + } +}