Reference-Count ViewModels for save check

only ask to save when the last viewport for a particular model closes
This commit is contained in:
haven1433
2023-12-08 11:28:24 -06:00
parent 289d3f7e00
commit 95da02a604
4 changed files with 10 additions and 1 deletions

View File

@@ -32,6 +32,8 @@ namespace HavenSoft.HexManiac.Core.Models
}
}
public int ReferenceCount { get; set; }
public bool SpartanMode { get; set; }
public delegate System.Collections.Generic.IEnumerable<T> AllDelegate_<T>() where T : Runs.IFormattedRun;

View File

@@ -20,6 +20,7 @@ namespace HavenSoft.HexManiac.Core.Models {
byte[] RawData { get; }
ModelCacheScope CurrentCacheScope { get; }
int ReferenceCount { get; set; }
bool SpartanMode { get; set; }
bool HasChanged(int index);
int ChangeCount { get; }
@@ -148,6 +149,8 @@ namespace HavenSoft.HexManiac.Core.Models {
}
}
public int ReferenceCount { get; set; } = 0; // used for tracking how many ViewModels currently use this Model
public BaseModel(byte[] data) {
RawData = data;
var code = data.GetGameCode();

View File

@@ -22,6 +22,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels {
ScrollRegion.DeferHeader();
Parent = viewPort;
Width = Parent.Width;
viewPort.Model.ReferenceCount -= 1; // don't reference count child viewports
}
public void RefreshHeaders() => ScrollRegion.UpdateDeferedHeader();

View File

@@ -676,7 +676,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels {
}
private void CloseExecuted(IFileSystem fileSystem) {
if (!history.IsSaved && ownsHistory) {
if (!history.IsSaved && Model.ReferenceCount == 1) {
var metadata = Model.ExportMetadata(RefTable, Singletons.MetadataInfo);
var result = fileSystem.TrySavePrompt(new LoadedFile(FileName, Model.RawData));
if (result == null) return;
@@ -685,6 +685,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels {
history.TagAsSaved();
}
}
Model.ReferenceCount -= 1;
Closed.Raise(this);
}
@@ -1041,6 +1043,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels {
}
PythonTool = pythonTool;
ownsHistory = changeHistory == null;
model.ReferenceCount += 1;
history = changeHistory ?? new ChangeHistory<ModelDelta>(RevertChanges);
history.PropertyChanged += HistoryPropertyChanged;