59 lines
2.2 KiB
C#
59 lines
2.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using Eto.Drawing;
|
|
using Eto.Forms;
|
|
using UpToolEto.Forms;
|
|
using UpToolLib;
|
|
using UpToolLib.DataStructures;
|
|
|
|
namespace UpToolEto
|
|
{
|
|
public class UTLibFunctions : IExternalFunctionality
|
|
{
|
|
private readonly Application _application;
|
|
private const int ImgSize = 32;
|
|
public Tuple<bool, byte[]> Download(Uri link)
|
|
{
|
|
DownloadDialog dlg = new(link, _application, this);
|
|
_application.AsyncInvoke(() => dlg.ShowModal());
|
|
dlg.StartDownload();
|
|
Log("Downloading " + link);
|
|
while (dlg.CurrentState == DownloadDialog.State.Downloading) Thread.Sleep(20);
|
|
Log("Download complete");
|
|
return new Tuple<bool, byte[]>(dlg.CurrentState == DownloadDialog.State.Success, dlg.Download);
|
|
}
|
|
|
|
public string FetchImageB64(Uri link)
|
|
{
|
|
using WebClient client = new();
|
|
using Stream s = client.OpenRead(link);
|
|
using Bitmap source = new(s);
|
|
using Icon bmp = source.WithSize(ImgSize, ImgSize);
|
|
using Bitmap bitmapResized = new(bmp);
|
|
using MemoryStream ms = new();
|
|
bitmapResized.Save(ms, ImageFormat.Png);
|
|
return Convert.ToBase64String(ms.ToArray());
|
|
}
|
|
|
|
public bool YesNoDialog(string text, bool defaultVal) =>
|
|
_application.Invoke(() => MessageBox.Show(text, MessageBoxButtons.YesNo,
|
|
defaultButton: defaultVal ? MessageBoxDefaultButton.Yes : MessageBoxDefaultButton.No)) ==
|
|
DialogResult.Yes;
|
|
|
|
public void OkDialog(string text) => _application.Invoke(() => MessageBox.Show(text));
|
|
|
|
public object GetDefaultIcon() => Bitmap.FromResource("UpToolLib.C_64.ico", typeof(UpToolLibMain)).WithSize(ImgSize, ImgSize);
|
|
|
|
public object ImageFromB64(string b64) => new Bitmap(Convert.FromBase64String(b64)).WithSize(ImgSize, ImgSize);
|
|
|
|
public void Log(string text)
|
|
{
|
|
Console.WriteLine(text);
|
|
//TODO implement visual logging
|
|
}
|
|
|
|
public UTLibFunctions(Application application) => _application = application;
|
|
}
|
|
} |