Refresh file on change from background

This commit is contained in:
Matt Parker
2025-07-31 18:48:45 +10:00
parent 1dca2d19fd
commit 37a5bfc42b
4 changed files with 58 additions and 6 deletions

View File

@@ -1,4 +1,8 @@
<MudText> @using SharpIDE.Photino.Services
@inject RefreshOpenFileService RefreshOpenFileService
<MudText>
@Path.GetFileName(FilePath) @Path.GetFileName(FilePath)
@if (_unsavedEdits) @if (_unsavedEdits)
{ {
@@ -10,15 +14,20 @@
height: calc(100vh - 100px); height: calc(100vh - 100px);
} }
</style> </style>
<StandaloneCodeEditor Id="my-editor-id" ConstructionOptions="@EditorConstructionOptions" OnDidChangeModelContent="evnt => _unsavedEdits = true" OnDidBlurEditorText="@SaveFileToDisk" /> @{
}
<StandaloneCodeEditor @ref="_codeEditorRef" Id="my-editor-id" ConstructionOptions="@EditorConstructionOptions" OnDidChangeModelContent="IfDirtyMarkDirty" OnDidBlurEditorText="@SaveFileToDisk" />
@code { @code {
[Parameter, EditorRequired] [Parameter, EditorRequired]
public string FilePath { get; set; } = null!; public string FilePath { get; set; } = null!;
private string? _fileContent; private string? _fileContent;
private string? _unsavedFileContent;
private bool _unsavedEdits = false; private bool _unsavedEdits = false;
StandaloneCodeEditor _codeEditorRef = null!;
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{ {
return new StandaloneEditorConstructionOptions return new StandaloneEditorConstructionOptions
@@ -33,6 +42,20 @@
} }
protected override async Task OnInitializedAsync() 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); var fileInfo = new FileInfo(FilePath);
if (!fileInfo.Exists) if (!fileInfo.Exists)
@@ -53,7 +76,20 @@
ArgumentNullException.ThrowIfNull(editor, nameof(editor)); ArgumentNullException.ThrowIfNull(editor, nameof(editor));
var editorTextValue = await editor.GetValue(); var editorTextValue = await editor.GetValue();
await File.WriteAllTextAsync(FilePath, editorTextValue); await File.WriteAllTextAsync(FilePath, editorTextValue);
_fileContent = editorTextValue;
_unsavedEdits = false; _unsavedEdits = false;
} }
private async Task IfDirtyMarkDirty()
{
// Probably quite non-performant
if (await _codeEditorRef.GetValue() != _fileContent)
{
_unsavedEdits = true;
}
else
{
_unsavedEdits = false;
}
}
} }

View File

@@ -58,7 +58,6 @@
private void LoadSolution(string solutionPath) private void LoadSolution(string solutionPath)
{ {
return;
var solutionFile = GetNodesInSolution.ParseSolutionFileFromPath(solutionPath); var solutionFile = GetNodesInSolution.ParseSolutionFileFromPath(solutionPath);
ArgumentNullException.ThrowIfNull(solutionFile); ArgumentNullException.ThrowIfNull(solutionFile);
_solutionFile = solutionFile; _solutionFile = solutionFile;

View File

@@ -1,7 +1,8 @@
using Microsoft.Build.Locator; using Microsoft.Build.Locator;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using MudBlazor.Services; using MudBlazor.Services;
using Photino.Blazor; using Photino.Blazor;
using SharpIDE.Photino.Services;
namespace SharpIDE.Photino; namespace SharpIDE.Photino;
@@ -12,8 +13,9 @@ public class Program
{ {
var appBuilder = PhotinoBlazorAppBuilder.CreateDefault(args); var appBuilder = PhotinoBlazorAppBuilder.CreateDefault(args);
appBuilder.Services.AddLogging(); appBuilder.Services.AddLogging();//
appBuilder.Services.AddMudServices(); appBuilder.Services.AddMudServices();
appBuilder.Services.AddSingleton<RefreshOpenFileService>();
appBuilder.RootComponents.Add<App>("app"); appBuilder.RootComponents.Add<App>("app");
@@ -27,6 +29,12 @@ public class Program
//.SetIconFile("favicon.ico") //.SetIconFile("favicon.ico")
.SetTitle("SharpIDE.Photino"); .SetTitle("SharpIDE.Photino");
app.MainWindow.WindowFocusInHandler += (sender, _) =>
{
var refreshOpenFileService = app.Services.GetRequiredService<RefreshOpenFileService>();
refreshOpenFileService.InvokeRefreshOpenFile();
};
AppDomain.CurrentDomain.UnhandledException += (sender, error) => AppDomain.CurrentDomain.UnhandledException += (sender, error) =>
{ {
app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString()); app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString());
@@ -38,4 +46,3 @@ public class Program
app.Run(); app.Run();
} }
} }

View File

@@ -0,0 +1,10 @@
namespace SharpIDE.Photino.Services;
public class RefreshOpenFileService
{
public event Func<Task>? RefreshOpenFile;
public void InvokeRefreshOpenFile()
{
RefreshOpenFile?.Invoke();
}
}