mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-01 04:30:49 +12:00
Refactor clipboard upload form codes
This commit is contained in:
parent
0651a33c00
commit
3d4e1c952a
|
@ -36,14 +36,6 @@ public enum EDataType
|
||||||
URL
|
URL
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum EClipboardContentType
|
|
||||||
{
|
|
||||||
Default,
|
|
||||||
Image,
|
|
||||||
Text,
|
|
||||||
Files
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum PNGBitDepth // Localized
|
public enum PNGBitDepth // Localized
|
||||||
{
|
{
|
||||||
Default,
|
Default,
|
||||||
|
|
4
ShareX/Forms/ClipboardUploadForm.Designer.cs
generated
4
ShareX/Forms/ClipboardUploadForm.Designer.cs
generated
|
@ -51,6 +51,7 @@ private void InitializeComponent()
|
||||||
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
||||||
this.btnCancel.Name = "btnCancel";
|
this.btnCancel.Name = "btnCancel";
|
||||||
this.btnCancel.UseVisualStyleBackColor = true;
|
this.btnCancel.UseVisualStyleBackColor = true;
|
||||||
|
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
|
||||||
//
|
//
|
||||||
// txtClipboard
|
// txtClipboard
|
||||||
//
|
//
|
||||||
|
@ -89,6 +90,7 @@ private void InitializeComponent()
|
||||||
this.btnUpload.DialogResult = System.Windows.Forms.DialogResult.OK;
|
this.btnUpload.DialogResult = System.Windows.Forms.DialogResult.OK;
|
||||||
this.btnUpload.Name = "btnUpload";
|
this.btnUpload.Name = "btnUpload";
|
||||||
this.btnUpload.UseVisualStyleBackColor = true;
|
this.btnUpload.UseVisualStyleBackColor = true;
|
||||||
|
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
|
||||||
//
|
//
|
||||||
// ClipboardUploadForm
|
// ClipboardUploadForm
|
||||||
//
|
//
|
||||||
|
@ -106,7 +108,7 @@ private void InitializeComponent()
|
||||||
this.Controls.Add(this.lbClipboard);
|
this.Controls.Add(this.lbClipboard);
|
||||||
this.Name = "ClipboardUploadForm";
|
this.Name = "ClipboardUploadForm";
|
||||||
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
|
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
|
||||||
this.Load += new System.EventHandler(this.ClipboardContentViewer_Load);
|
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ClipboardUploadForm_FormClosed);
|
||||||
this.Shown += new System.EventHandler(this.ClipboardContentViewer_Shown);
|
this.Shown += new System.EventHandler(this.ClipboardContentViewer_Shown);
|
||||||
this.ResumeLayout(false);
|
this.ResumeLayout(false);
|
||||||
this.PerformLayout();
|
this.PerformLayout();
|
||||||
|
|
|
@ -35,13 +35,16 @@ public partial class ClipboardUploadForm : Form
|
||||||
{
|
{
|
||||||
public bool IsClipboardContentValid { get; private set; }
|
public bool IsClipboardContentValid { get; private set; }
|
||||||
public bool DontShowThisWindow { get; private set; }
|
public bool DontShowThisWindow { get; private set; }
|
||||||
public EClipboardContentType ClipboardContentType { get; private set; }
|
|
||||||
public object ClipboardContent { get; private set; }
|
public object ClipboardContent { get; private set; }
|
||||||
|
public bool KeepClipboardContent { get; private set; }
|
||||||
|
|
||||||
public ClipboardUploadForm(bool showCheckBox = false)
|
private TaskSettings taskSettings;
|
||||||
|
|
||||||
|
public ClipboardUploadForm(TaskSettings taskSettings, bool showCheckBox = false)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
ShareXResources.ApplyTheme(this);
|
ShareXResources.ApplyTheme(this);
|
||||||
|
this.taskSettings = taskSettings;
|
||||||
|
|
||||||
if (ShareXResources.UseCustomTheme)
|
if (ShareXResources.UseCustomTheme)
|
||||||
{
|
{
|
||||||
|
@ -49,14 +52,12 @@ public ClipboardUploadForm(bool showCheckBox = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
cbDontShowThisWindow.Visible = showCheckBox;
|
cbDontShowThisWindow.Visible = showCheckBox;
|
||||||
|
|
||||||
|
IsClipboardContentValid = CheckClipboardContent();
|
||||||
|
btnUpload.Enabled = IsClipboardContentValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClipboardContentViewer_Load(object sender, EventArgs e)
|
private bool CheckClipboardContent()
|
||||||
{
|
|
||||||
IsClipboardContentValid = CheckClipboardContents();
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool CheckClipboardContents()
|
|
||||||
{
|
{
|
||||||
pbClipboard.Visible = txtClipboard.Visible = lbClipboard.Visible = false;
|
pbClipboard.Visible = txtClipboard.Visible = lbClipboard.Visible = false;
|
||||||
|
|
||||||
|
@ -66,7 +67,6 @@ private bool CheckClipboardContents()
|
||||||
{
|
{
|
||||||
if (bmp != null)
|
if (bmp != null)
|
||||||
{
|
{
|
||||||
ClipboardContentType = EClipboardContentType.Image;
|
|
||||||
ClipboardContent = bmp.Clone();
|
ClipboardContent = bmp.Clone();
|
||||||
pbClipboard.LoadImage(bmp);
|
pbClipboard.LoadImage(bmp);
|
||||||
pbClipboard.Visible = true;
|
pbClipboard.Visible = true;
|
||||||
|
@ -81,7 +81,6 @@ private bool CheckClipboardContents()
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(text))
|
if (!string.IsNullOrEmpty(text))
|
||||||
{
|
{
|
||||||
ClipboardContentType = EClipboardContentType.Text;
|
|
||||||
ClipboardContent = text;
|
ClipboardContent = text;
|
||||||
txtClipboard.Text = text;
|
txtClipboard.Text = text;
|
||||||
txtClipboard.Visible = true;
|
txtClipboard.Visible = true;
|
||||||
|
@ -95,7 +94,6 @@ private bool CheckClipboardContents()
|
||||||
|
|
||||||
if (files != null && files.Length > 0)
|
if (files != null && files.Length > 0)
|
||||||
{
|
{
|
||||||
ClipboardContentType = EClipboardContentType.Files;
|
|
||||||
ClipboardContent = files;
|
ClipboardContent = files;
|
||||||
lbClipboard.Items.AddRange(files);
|
lbClipboard.Items.AddRange(files);
|
||||||
lbClipboard.Visible = true;
|
lbClipboard.Visible = true;
|
||||||
|
@ -108,14 +106,53 @@ private bool CheckClipboardContents()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ClipboardUpload()
|
||||||
|
{
|
||||||
|
if (IsClipboardContentValid)
|
||||||
|
{
|
||||||
|
switch (ClipboardContent)
|
||||||
|
{
|
||||||
|
case Bitmap bmp:
|
||||||
|
KeepClipboardContent = true;
|
||||||
|
UploadManager.ProcessImageUpload(bmp, taskSettings);
|
||||||
|
break;
|
||||||
|
case string text:
|
||||||
|
UploadManager.ProcessTextUpload(text, taskSettings);
|
||||||
|
break;
|
||||||
|
case string[] files:
|
||||||
|
UploadManager.ProcessFilesUpload(files, taskSettings);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void ClipboardContentViewer_Shown(object sender, EventArgs e)
|
private void ClipboardContentViewer_Shown(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
this.ForceActivate();
|
this.ForceActivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ClipboardUploadForm_FormClosed(object sender, FormClosedEventArgs e)
|
||||||
|
{
|
||||||
|
if (!KeepClipboardContent && ClipboardContent is Bitmap bmp)
|
||||||
|
{
|
||||||
|
bmp.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void cbDontShowThisWindow_CheckedChanged(object sender, EventArgs e)
|
private void cbDontShowThisWindow_CheckedChanged(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
DontShowThisWindow = cbDontShowThisWindow.Checked;
|
DontShowThisWindow = cbDontShowThisWindow.Checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void btnUpload_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
ClipboardUpload();
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btnCancel_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -142,7 +142,7 @@ public static void UploadFolder(TaskSettings taskSettings = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessImageUpload(Bitmap bmp, TaskSettings taskSettings)
|
public static void ProcessImageUpload(Bitmap bmp, TaskSettings taskSettings)
|
||||||
{
|
{
|
||||||
if (bmp != null)
|
if (bmp != null)
|
||||||
{
|
{
|
||||||
|
@ -155,7 +155,7 @@ private static void ProcessImageUpload(Bitmap bmp, TaskSettings taskSettings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessTextUpload(string text, TaskSettings taskSettings)
|
public static void ProcessTextUpload(string text, TaskSettings taskSettings)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(text))
|
if (!string.IsNullOrEmpty(text))
|
||||||
{
|
{
|
||||||
|
@ -193,7 +193,7 @@ private static void ProcessTextUpload(string text, TaskSettings taskSettings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessFilesUpload(string[] files, TaskSettings taskSettings)
|
public static void ProcessFilesUpload(string[] files, TaskSettings taskSettings)
|
||||||
{
|
{
|
||||||
if (files != null && files.Length > 0)
|
if (files != null && files.Length > 0)
|
||||||
{
|
{
|
||||||
|
@ -225,58 +225,13 @@ public static void ClipboardUpload(TaskSettings taskSettings = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ClipboardUploadCached(ClipboardUploadForm form, TaskSettings taskSettings = null)
|
|
||||||
{
|
|
||||||
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
|
||||||
|
|
||||||
if (form.ClipboardContentType == EClipboardContentType.Image)
|
|
||||||
{
|
|
||||||
Bitmap bmp = (Bitmap)form.ClipboardContent;
|
|
||||||
|
|
||||||
ProcessImageUpload(bmp, taskSettings);
|
|
||||||
}
|
|
||||||
else if (form.ClipboardContentType == EClipboardContentType.Text)
|
|
||||||
{
|
|
||||||
string text = (string)form.ClipboardContent;
|
|
||||||
|
|
||||||
ProcessTextUpload(text, taskSettings);
|
|
||||||
}
|
|
||||||
else if (form.ClipboardContentType == EClipboardContentType.Files)
|
|
||||||
{
|
|
||||||
string[] files = (string[])form.ClipboardContent;
|
|
||||||
|
|
||||||
ProcessFilesUpload(files, taskSettings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ProcessClipboardUploadDialog(ClipboardUploadForm form, TaskSettings taskSettings = null)
|
|
||||||
{
|
|
||||||
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
|
||||||
|
|
||||||
if (form.ShowDialog() == DialogResult.OK && form.IsClipboardContentValid)
|
|
||||||
{
|
|
||||||
if (form.ClipboardContentType != EClipboardContentType.Default)
|
|
||||||
{
|
|
||||||
ClipboardUploadCached(form, taskSettings);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ClipboardUpload(taskSettings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (form.ClipboardContentType == EClipboardContentType.Image)
|
|
||||||
{
|
|
||||||
((Bitmap)form.ClipboardContent).Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ClipboardUploadWithContentViewer(TaskSettings taskSettings = null)
|
public static void ClipboardUploadWithContentViewer(TaskSettings taskSettings = null)
|
||||||
{
|
{
|
||||||
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
||||||
|
|
||||||
using (ClipboardUploadForm clipboardUploadForm = new ClipboardUploadForm())
|
using (ClipboardUploadForm clipboardUploadForm = new ClipboardUploadForm(taskSettings))
|
||||||
{
|
{
|
||||||
ProcessClipboardUploadDialog(clipboardUploadForm, taskSettings);
|
clipboardUploadForm.ShowDialog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,10 +241,9 @@ public static void ClipboardUploadMainWindow(TaskSettings taskSettings = null)
|
||||||
|
|
||||||
if (Program.Settings.ShowClipboardContentViewer)
|
if (Program.Settings.ShowClipboardContentViewer)
|
||||||
{
|
{
|
||||||
using (ClipboardUploadForm clipboardUploadForm = new ClipboardUploadForm(true))
|
using (ClipboardUploadForm clipboardUploadForm = new ClipboardUploadForm(taskSettings, true))
|
||||||
{
|
{
|
||||||
ProcessClipboardUploadDialog(clipboardUploadForm, taskSettings);
|
clipboardUploadForm.ShowDialog();
|
||||||
|
|
||||||
Program.Settings.ShowClipboardContentViewer = !clipboardUploadForm.DontShowThisWindow;
|
Program.Settings.ShowClipboardContentViewer = !clipboardUploadForm.DontShowThisWindow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue