mirror of
https://github.com/haven1433/HexManiacAdvance.git
synced 2026-10-05 06:36:35 -05:00
adding ability for IFileSystem to tell me when something changes on disk.
first pass at implementing the windows version of the new interface methods IViewPort now has a public method to call whenever you want to notify it that its underlying file has been changed. Added 3 tests showing how the Editor and ViewPort should behave with this new functionality. - Editor should hook up ViewPorts whenever a new tab is opened - Editor should clear out ViewPorts whenever a tab is closed - ViewPort should reload when the file is changed and it is not. Note that the model isn't implemented yet. This is just some failing tests.
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
using HavenSoft.Gen3Hex.Model;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace HavenSoft.Gen3Hex.View {
|
||||
public class WindowsFileSystem : IFileSystem {
|
||||
private readonly Dictionary<string, List<FileSystemWatcher>> watchers = new Dictionary<string, List<FileSystemWatcher>>();
|
||||
|
||||
public string CopyText {
|
||||
get => Clipboard.ContainsText() ? Clipboard.GetText() : string.Empty;
|
||||
set => Clipboard.SetText(value);
|
||||
@@ -15,9 +19,31 @@ namespace HavenSoft.Gen3Hex.View {
|
||||
var dialog = new OpenFileDialog { Filter = CreateFilterFromOptions(extensionOptions) };
|
||||
var result = dialog.ShowDialog();
|
||||
if (result != true) return null;
|
||||
if (!File.Exists(dialog.FileName)) return null;
|
||||
var data = File.ReadAllBytes(dialog.FileName);
|
||||
return new LoadedFile(dialog.FileName, data);
|
||||
return LoadFile(dialog.FileName);
|
||||
}
|
||||
|
||||
public LoadedFile LoadFile(string fileName) {
|
||||
if (!File.Exists(fileName)) return null;
|
||||
var data = File.ReadAllBytes(fileName);
|
||||
return new LoadedFile(fileName, data);
|
||||
}
|
||||
|
||||
public void AddListenerToFile(string fileName, Action<IFileSystem> listener) {
|
||||
var watcher = new FileSystemWatcher(Path.GetDirectoryName(fileName)) {
|
||||
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName,
|
||||
};
|
||||
watcher.Changed += (sender, e) => {
|
||||
if (e.FullPath.EndsWith(fileName)) listener(this);
|
||||
};
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
if (!watchers.ContainsKey(fileName)) watchers[fileName] = new List<FileSystemWatcher>();
|
||||
watchers[fileName].Add(watcher);
|
||||
}
|
||||
|
||||
public void RemoveAllListenersForFile(string fileName) {
|
||||
if (!watchers.ContainsKey(fileName)) return;
|
||||
watchers[fileName].Clear();
|
||||
}
|
||||
|
||||
public string RequestNewName(string currentName, params string[] extensionOptions) {
|
||||
|
||||
Reference in New Issue
Block a user