Mk0.Software.ImageSorter/Mk0.Software.ImageSorter/Inpaint.cs
2024-08-18 19:29:34 +02:00

187 lines
6.5 KiB
C#

using CefSharp.WinForms;
using System;
using System.Windows.Forms;
using System.Collections.Specialized;
using CefSharp;
using System.IO;
namespace Mk0.Software.ImageSorter
{
public partial class Inpaint : Form, IKeyboardHandler
{
private readonly string url;
private readonly string file;
public ChromiumWebBrowser browser;
public Inpaint(string url, string file)
{
InitializeComponent();
this.url = url;
this.file = file;
InitializeChromium();
}
private void InitializeChromium()
{
#if ANYCPU
//Only required for PlatformTarget of AnyCPU
CefRuntime.SubscribeAnyCpuAssemblyResolver();
#endif
var settings = new CefSettings();
settings.LogSeverity = LogSeverity.Disable;
if (!Cef.IsInitialized)
{
var initialized = Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
if (!initialized)
{
MessageBox.Show("Cef.Initialized failed, check the log file for more details.");
}
}
browser = new ChromiumWebBrowser(url)
{
DownloadHandler = new DownloadHandler(),
MenuHandler = new MenuHandler(),
KeyboardHandler = this,
TabIndex = 0
};
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
private void Inpaint_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.lastWidthInpaint = Width;
Properties.Settings.Default.lastHeightInpaint = Height;
Properties.Settings.Default.lastTopInpaint = Top;
Properties.Settings.Default.lastLeftInpaint = Left;
Properties.Settings.Default.Save();
if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "inpaint-tmp", Path.GetFileNameWithoutExtension(file) + "_cleanup" + Path.GetExtension(file))))
{
DialogResult res = MessageBox.Show("Sie haben kein bearbeitetes Bild heruntergeladen. Klicken sie dazu auf den Downloadbutton am unteren Fensterrand.\n\nWollen sie die bearbeitete Datei doch noch herunterladen (Ja)?\n\nFalls sie ihre Änderungen verwerfen wollen, Klicken sie (Nein).", "Inpaint Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
switch (res)
{
case DialogResult.Yes:
{
e.Cancel = true;
break;
}
case DialogResult.No:
{
break;
}
}
}
}
private void Inpaint_Shown(object sender, EventArgs e)
{
if (Properties.Settings.Default.lastHeightInpaint >= MinimumSize.Height)
{
Height = Properties.Settings.Default.lastHeightInpaint;
}
if (Properties.Settings.Default.lastWidthInpaint >= MinimumSize.Width)
{
Width = Properties.Settings.Default.lastWidthInpaint;
}
if (Properties.Settings.Default.lastTopInpaint <= Screen.PrimaryScreen.Bounds.Height && Properties.Settings.Default.lastTopInpaint >= 0)
{
Top = Properties.Settings.Default.lastTopInpaint;
}
if (Properties.Settings.Default.lastLeftInpaint <= Screen.PrimaryScreen.Bounds.Width && Properties.Settings.Default.lastLeftInpaint >= 0)
{
Left = Properties.Settings.Default.lastLeftInpaint;
}
CopyPasteImage();
ToolTip t1 = new ToolTip
{
AutoPopDelay = 5000,
InitialDelay = 1000,
ReshowDelay = 500
};
t1.SetToolTip(buttonPaste, "Bild erneut in Inpaint laden/Zurücksetzen (F5)");
ToolTip t2 = new ToolTip
{
AutoPopDelay = 5000,
InitialDelay = 1000,
ReshowDelay = 500
};
t2.SetToolTip(buttonReload, "Inpaint neu laden (F2)");
}
private void CopyPasteImage()
{
System.Threading.Thread.Sleep(125);
StringCollection c = new StringCollection
{
file
};
Clipboard.SetFileDropList(c);
browser.Select();
browser.Focus();
SendKeys.Send("^{v}");
browser.Select();
browser.Focus();
SendKeys.Send("^{v}");
browser.Select();
browser.Focus();
SendKeys.Send("^{v}");
}
private void ButtonPaste_Click(object sender, EventArgs e)
{
Paste();
}
private void Paste()
{
CopyPasteImage();
}
private void ButtonReload_Click(object sender, EventArgs e)
{
Reload();
}
private void Reload()
{
browser.Reload();
CopyPasteImage();
}
public bool OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
{
isKeyboardShortcut = true;
return false;
}
public bool OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode))
{
var key = (Keys)windowsKeyCode;
switch (key)
{
case Keys.F2:
this.BeginInvoke(new Action(() => {
Reload();
}));
break;
case Keys.F5:
this.BeginInvoke(new Action(() => {
Paste();
}));
break;
case Keys.Escape:
this.BeginInvoke(new Action(() => {
this.Close();
}));
break;
}
}
return false;
}
}
}