2019-10-07 17:33:34 +00:00
|
|
|
|
using Microsoft.WindowsAPICodePack.Dialogs;
|
|
|
|
|
using System;
|
2019-10-16 03:17:27 +00:00
|
|
|
|
using System.IO;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using System.Linq;
|
2019-09-18 21:37:32 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-09-27 04:07:54 +00:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2019-09-18 21:37:32 +00:00
|
|
|
|
using System.Windows.Threading;
|
2019-10-16 03:17:27 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-09-18 21:37:32 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib
|
2019-09-18 21:37:32 +00:00
|
|
|
|
{
|
|
|
|
|
public static class UIUtils
|
|
|
|
|
{
|
2019-10-07 17:33:34 +00:00
|
|
|
|
|
2019-09-18 21:37:32 +00:00
|
|
|
|
public static string ShowFolderSelectionDialog(string prompt)
|
|
|
|
|
{
|
2019-10-11 22:34:59 +00:00
|
|
|
|
if (System.Windows.Application.Current.Dispatcher.Thread != Thread.CurrentThread)
|
2019-09-18 21:37:32 +00:00
|
|
|
|
{
|
|
|
|
|
var task = new TaskCompletionSource<string>();
|
2019-10-11 22:34:59 +00:00
|
|
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
2019-09-18 21:37:32 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
task.SetResult(ShowFolderSelectionDialog(prompt));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
task.SetException(ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
task.Task.Wait();
|
|
|
|
|
if (task.Task.IsFaulted)
|
|
|
|
|
throw task.Task.Exception;
|
|
|
|
|
return task.Task.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dlg = new CommonOpenFileDialog();
|
|
|
|
|
dlg.Title = prompt;
|
|
|
|
|
dlg.IsFolderPicker = true;
|
|
|
|
|
dlg.InitialDirectory = Assembly.GetEntryAssembly().Location;
|
|
|
|
|
|
|
|
|
|
dlg.AddToMostRecentlyUsedList = false;
|
|
|
|
|
dlg.AllowNonFileSystemItems = false;
|
|
|
|
|
dlg.DefaultDirectory = Assembly.GetEntryAssembly().Location;
|
|
|
|
|
dlg.EnsureFileExists = true;
|
|
|
|
|
dlg.EnsurePathExists = true;
|
|
|
|
|
dlg.EnsureReadOnly = false;
|
|
|
|
|
dlg.EnsureValidNames = true;
|
|
|
|
|
dlg.Multiselect = false;
|
|
|
|
|
dlg.ShowPlacesList = true;
|
|
|
|
|
|
|
|
|
|
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
|
|
|
|
|
{
|
|
|
|
|
return dlg.FileName;
|
|
|
|
|
// Do something with selected folder string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 04:07:54 +00:00
|
|
|
|
public static BitmapImage BitmapImageFromResource(string name)
|
|
|
|
|
{
|
|
|
|
|
var img = new BitmapImage();
|
|
|
|
|
img.BeginInit();
|
2019-10-16 03:17:27 +00:00
|
|
|
|
img.StreamSource = Utils.GetResourceStream(name);
|
2019-09-27 04:07:54 +00:00
|
|
|
|
img.EndInit();
|
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string OpenFileDialog(string filter)
|
|
|
|
|
{
|
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
|
|
|
ofd.Filter = filter;
|
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
return ofd.FileName;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-09-18 21:37:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|