Files
HexManiacAdvance/Gen3Hex/View/WindowsFileSystem.cs
Benjamin Popp b65f3eadfb adding cut/copy/paste/delete
adding 'help' to menu, with links to GitHub and credit to Solarized.
2018-11-11 15:57:30 -06:00

47 lines
1.8 KiB
C#

using HavenSoft.Gen3Hex.Model;
using Microsoft.Win32;
using System.IO;
using System.Linq;
using System.Windows;
namespace HavenSoft.Gen3Hex.View {
public class WindowsFileSystem : IFileSystem {
public string CopyText {
get => Clipboard.ContainsText() ? Clipboard.GetText() : string.Empty;
set => Clipboard.SetText(value);
}
public LoadedFile OpenFile(params string[] extensionOptions) {
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);
}
public string RequestNewName(string currentName, params string[] extensionOptions) {
var dialog = new SaveFileDialog { FileName = currentName, Filter = CreateFilterFromOptions(extensionOptions) };
var result = dialog.ShowDialog();
if (result != true) return null;
return dialog.FileName;
}
public bool Save(LoadedFile file) {
File.WriteAllBytes(file.Name, file.Contents);
return true;
}
public bool? TrySavePrompt(LoadedFile file) {
var result = MessageBox.Show($"Would you like to save {file.Name}?", Application.Current.MainWindow.Title, MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Cancel) return null;
if (result == MessageBoxResult.No) return false;
return Save(file);
}
private static string CreateFilterFromOptions(string[] extensionOptions) {
return string.Join(",", extensionOptions.Select(option => $"*.{option}"));
}
}
}