You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.2 KiB
83 lines
2.2 KiB
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MaterialDesignThemes.Wpf;
|
|
using Microsoft.Win32;
|
|
using Ookii.Dialogs.Wpf;
|
|
using Stylet;
|
|
|
|
namespace DiscordChatExporter.Gui.ViewModels.Framework;
|
|
|
|
public class DialogManager : IDisposable
|
|
{
|
|
private readonly IViewManager _viewManager;
|
|
private readonly SemaphoreSlim _dialogLock = new(1, 1);
|
|
|
|
public DialogManager(IViewManager viewManager)
|
|
{
|
|
_viewManager = viewManager;
|
|
}
|
|
|
|
public async ValueTask<T?> ShowDialogAsync<T>(DialogScreen<T> dialogScreen)
|
|
{
|
|
var view = _viewManager.CreateAndBindViewForModelIfNecessary(dialogScreen);
|
|
|
|
void OnDialogOpened(object? openSender, DialogOpenedEventArgs openArgs)
|
|
{
|
|
void OnScreenClosed(object? closeSender, EventArgs closeArgs)
|
|
{
|
|
try
|
|
{
|
|
openArgs.Session.Close();
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
// Race condition: dialog is already being closed
|
|
}
|
|
|
|
dialogScreen.Closed -= OnScreenClosed;
|
|
}
|
|
dialogScreen.Closed += OnScreenClosed;
|
|
}
|
|
|
|
await _dialogLock.WaitAsync();
|
|
try
|
|
{
|
|
await DialogHost.Show(view, OnDialogOpened);
|
|
return dialogScreen.DialogResult;
|
|
}
|
|
finally
|
|
{
|
|
_dialogLock.Release();
|
|
}
|
|
}
|
|
|
|
public string? PromptSaveFilePath(string filter = "All files|*.*", string defaultFilePath = "")
|
|
{
|
|
var dialog = new SaveFileDialog
|
|
{
|
|
Filter = filter,
|
|
AddExtension = true,
|
|
FileName = defaultFilePath,
|
|
DefaultExt = Path.GetExtension(defaultFilePath)
|
|
};
|
|
|
|
return dialog.ShowDialog() == true ? dialog.FileName : null;
|
|
}
|
|
|
|
public string? PromptDirectoryPath(string defaultDirPath = "")
|
|
{
|
|
var dialog = new VistaFolderBrowserDialog
|
|
{
|
|
SelectedPath = defaultDirPath
|
|
};
|
|
|
|
return dialog.ShowDialog() == true ? dialog.SelectedPath : null;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_dialogLock.Dispose();
|
|
}
|
|
} |