2020-01-10 04:27:59 +00:00
|
|
|
|
using DynamicData;
|
|
|
|
|
using DynamicData.Binding;
|
|
|
|
|
using Microsoft.WindowsAPICodePack.Dialogs;
|
|
|
|
|
using ReactiveUI;
|
2019-10-07 17:33:34 +00:00
|
|
|
|
using System;
|
2019-10-16 03:17:27 +00:00
|
|
|
|
using System.IO;
|
2020-01-14 04:29:23 +00:00
|
|
|
|
using System.Net.Http;
|
2020-01-10 04:27:59 +00:00
|
|
|
|
using System.Reactive.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;
|
2021-02-27 00:08:05 +00:00
|
|
|
|
using SharpDX.Text;
|
2019-10-16 03:17:27 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-09-18 21:37:32 +00:00
|
|
|
|
|
2020-01-14 04:29:23 +00:00
|
|
|
|
namespace Wabbajack
|
2019-09-18 21:37:32 +00:00
|
|
|
|
{
|
|
|
|
|
public static class UIUtils
|
|
|
|
|
{
|
2019-11-29 23:56:56 +00:00
|
|
|
|
public static BitmapImage BitmapImageFromResource(string name) => BitmapImageFromStream(System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Wabbajack;component/" + name)).Stream);
|
2019-10-25 04:26:29 +00:00
|
|
|
|
|
|
|
|
|
public static BitmapImage BitmapImageFromStream(Stream stream)
|
2019-09-27 04:07:54 +00:00
|
|
|
|
{
|
|
|
|
|
var img = new BitmapImage();
|
|
|
|
|
img.BeginInit();
|
2019-12-15 19:09:07 +00:00
|
|
|
|
img.CacheOption = BitmapCacheOption.OnLoad;
|
2019-10-25 04:26:29 +00:00
|
|
|
|
img.StreamSource = stream;
|
2019-09-27 04:07:54 +00:00
|
|
|
|
img.EndInit();
|
2019-12-15 19:09:07 +00:00
|
|
|
|
img.Freeze();
|
2019-09-27 04:07:54 +00:00
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
public static bool TryGetBitmapImageFromFile(AbsolutePath path, out BitmapImage bitmapImage)
|
2019-10-25 04:26:29 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-28 20:04:22 +00:00
|
|
|
|
if (!path.Exists)
|
2019-10-25 04:26:29 +00:00
|
|
|
|
{
|
|
|
|
|
bitmapImage = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-28 20:04:22 +00:00
|
|
|
|
bitmapImage = new BitmapImage(new Uri((string)path, UriKind.RelativeOrAbsolute));
|
2019-10-25 04:26:29 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
bitmapImage = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
public static AbsolutePath OpenFileDialog(string filter, string initialDirectory = null)
|
2019-09-27 04:07:54 +00:00
|
|
|
|
{
|
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
|
|
|
ofd.Filter = filter;
|
2019-11-06 03:22:38 +00:00
|
|
|
|
ofd.InitialDirectory = initialDirectory;
|
2019-09-27 04:07:54 +00:00
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
2020-03-28 20:04:22 +00:00
|
|
|
|
return (AbsolutePath)ofd.FileName;
|
|
|
|
|
return default;
|
2019-09-27 04:07:54 +00:00
|
|
|
|
}
|
2020-01-10 04:27:59 +00:00
|
|
|
|
|
|
|
|
|
public static IDisposable BindCpuStatus(IObservable<CPUStatus> status, ObservableCollectionExtended<CPUDisplayVM> list)
|
|
|
|
|
{
|
|
|
|
|
return status.ObserveOn(RxApp.TaskpoolScheduler)
|
|
|
|
|
.ToObservableChangeSet(x => x.ID)
|
|
|
|
|
.Batch(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler)
|
|
|
|
|
.EnsureUniqueChanges()
|
2020-02-08 04:35:08 +00:00
|
|
|
|
.ObserveOnGuiThread()
|
2020-01-10 04:27:59 +00:00
|
|
|
|
.TransformAndCache(
|
|
|
|
|
onAdded: (key, cpu) => new CPUDisplayVM(cpu),
|
|
|
|
|
onUpdated: (change, vm) => vm.AbsorbStatus(change.Current))
|
|
|
|
|
.AutoRefresh(x => x.IsWorking)
|
|
|
|
|
.AutoRefresh(x => x.StartTime)
|
|
|
|
|
.Filter(i => i.IsWorking && i.ID != WorkQueue.UnassignedCpuId)
|
|
|
|
|
.Sort(SortExpressionComparer<CPUDisplayVM>.Ascending(s => s.StartTime))
|
|
|
|
|
.Bind(list)
|
|
|
|
|
.Subscribe();
|
|
|
|
|
}
|
2020-01-14 04:29:23 +00:00
|
|
|
|
|
|
|
|
|
public static IObservable<BitmapImage> DownloadBitmapImage(this IObservable<string> obs, Action<Exception> exceptionHandler)
|
|
|
|
|
{
|
|
|
|
|
return obs
|
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
|
|
|
|
.SelectTask(async url =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-02-27 00:08:05 +00:00
|
|
|
|
var (found, mstream) = await FindCachedImage(url);
|
|
|
|
|
if (found) return mstream;
|
|
|
|
|
|
2020-01-14 04:29:23 +00:00
|
|
|
|
var ret = new MemoryStream();
|
|
|
|
|
using (var client = new HttpClient())
|
2021-02-27 00:08:05 +00:00
|
|
|
|
await using (var stream = await client.GetStreamAsync(url))
|
2020-01-14 04:29:23 +00:00
|
|
|
|
{
|
2021-02-27 00:08:05 +00:00
|
|
|
|
await stream.CopyToAsync(ret);
|
2020-01-14 04:29:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.Seek(0, SeekOrigin.Begin);
|
2021-02-27 00:08:05 +00:00
|
|
|
|
|
|
|
|
|
await WriteCachedImage(url, ret.ToArray());
|
2020-01-14 04:29:23 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exceptionHandler(ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Select(memStream =>
|
|
|
|
|
{
|
|
|
|
|
if (memStream == null) return default;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return BitmapImageFromStream(memStream);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exceptionHandler(ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
memStream.Dispose();
|
|
|
|
|
}
|
2021-02-27 00:08:05 +00:00
|
|
|
|
})
|
|
|
|
|
.ObserveOnGuiThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task WriteCachedImage(string url, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
var folder = Consts.LocalAppDataPath.Combine("ModListImages");
|
|
|
|
|
if (!folder.Exists) folder.CreateDirectory();
|
|
|
|
|
|
|
|
|
|
var path = folder.Combine(Encoding.UTF8.GetBytes(url).xxHash().ToHex());
|
|
|
|
|
await path.WriteAllBytesAsync(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<(bool Found, MemoryStream data)> FindCachedImage(string uri)
|
|
|
|
|
{
|
|
|
|
|
var folder = Consts.LocalAppDataPath.Combine("ModListImages");
|
|
|
|
|
if (!folder.Exists) folder.CreateDirectory();
|
|
|
|
|
|
|
|
|
|
var path = folder.Combine(Encoding.UTF8.GetBytes(uri).xxHash().ToHex());
|
|
|
|
|
return path.Exists ? (true, new MemoryStream(await path.ReadAllBytesAsync())) : (false, default);
|
2020-01-14 04:29:23 +00:00
|
|
|
|
}
|
2020-05-08 03:46:52 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Format bytes to a greater unit
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">number of bytes</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string FormatBytes(long bytes)
|
|
|
|
|
{
|
|
|
|
|
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
|
|
|
|
|
int i;
|
|
|
|
|
double dblSByte = bytes;
|
|
|
|
|
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
|
|
|
|
|
{
|
|
|
|
|
dblSByte = bytes / 1024.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
|
|
|
|
|
}
|
2019-09-18 21:37:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|