Code refactoring

This commit is contained in:
Jaex 2023-06-04 01:07:30 +03:00
parent 9b008ac399
commit 5e7dbd23fa
28 changed files with 48 additions and 63 deletions

View file

@ -345,8 +345,8 @@ public static void SupportSelectAll(this TextBox tb)
if (e.KeyData == (Keys.Control | Keys.A)) if (e.KeyData == (Keys.Control | Keys.A))
{ {
tb.SelectAll(); tb.SelectAll();
e.SuppressKeyPress = true; e.SuppressKeyPress = true;
e.Handled = true;
} }
}; };
} }
@ -928,6 +928,14 @@ public static void CloseOnEscape(this Form form)
{ {
form.KeyPreview = true; form.KeyPreview = true;
form.KeyDown += (sender, e) =>
{
if (e.KeyCode == Keys.Escape)
{
e.SuppressKeyPress = true;
}
};
form.KeyUp += (sender, e) => form.KeyUp += (sender, e) =>
{ {
if (e.KeyCode == Keys.Escape) if (e.KeyCode == Keys.Escape)

View file

@ -37,8 +37,7 @@ public partial class ClipboardViewerForm : Form
public ClipboardViewerForm() public ClipboardViewerForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
} }
private void ClipboardViewerForm_Load(object sender, EventArgs e) private void ClipboardViewerForm_Load(object sender, EventArgs e)

View file

@ -46,8 +46,7 @@ public partial class ColorPickerForm : Form
public ColorPickerForm(Color currentColor, bool isScreenColorPickerMode = false, bool checkClipboard = true, ColorPickerOptions options = null) public ColorPickerForm(Color currentColor, bool isScreenColorPickerMode = false, bool checkClipboard = true, ColorPickerOptions options = null)
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
clipboardStatusHider = new ControlHider(btnClipboardStatus, 2000); clipboardStatusHider = new ControlHider(btnClipboardStatus, 2000);
IsScreenColorPickerMode = isScreenColorPickerMode; IsScreenColorPickerMode = isScreenColorPickerMode;

View file

@ -35,8 +35,7 @@ public partial class DNSChangerForm : Form
public DNSChangerForm() public DNSChangerForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
AddDNS(Resources.DNSChangerForm_DNSChangerForm_Manual); AddDNS(Resources.DNSChangerForm_DNSChangerForm_Manual);
AddDNS("Google Public DNS", "8.8.8.8", "8.8.4.4"); // https://developers.google.com/speed/public-dns/ AddDNS("Google Public DNS", "8.8.8.8", "8.8.4.4"); // https://developers.google.com/speed/public-dns/

View file

@ -40,8 +40,7 @@ public partial class HashCheckForm : Form
public HashCheckForm() public HashCheckForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
UpdateCompareControls(); UpdateCompareControls();
cbHashType.Items.AddRange(Helpers.GetEnumDescriptions<HashType>()); cbHashType.Items.AddRange(Helpers.GetEnumDescriptions<HashType>());

View file

@ -36,8 +36,7 @@ public class InputBox : Form
private InputBox(string title, string inputText = null, string okText = null, string cancelText = null) private InputBox(string title, string inputText = null, string okText = null, string cancelText = null)
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
InputText = inputText; InputText = inputText;

View file

@ -35,8 +35,7 @@ public partial class MonitorTestForm : Form
public MonitorTestForm() public MonitorTestForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
Rectangle screenBounds = CaptureHelpers.GetScreenBounds(); Rectangle screenBounds = CaptureHelpers.GetScreenBounds();
Location = screenBounds.Location; Location = screenBounds.Location;

View file

@ -36,8 +36,7 @@ private OutputBox(string text, string title, bool scrollToEnd = false)
{ {
InitializeComponent(); InitializeComponent();
rtbText.AddContextMenu(); rtbText.AddContextMenu();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
Text = "ShareX - " + title; Text = "ShareX - " + title;
rtbText.Text = text; rtbText.Text = text;

View file

@ -52,7 +52,6 @@ public ScreenTearingTestForm()
Bounds = screenRectangle; Bounds = screenRectangle;
Cursor = Cursors.Hand; Cursor = Cursors.Hand;
FormBorderStyle = FormBorderStyle.None; FormBorderStyle = FormBorderStyle.None;
Icon = ShareXResources.Icon;
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Text = "ShareX - Screen tearing test"; Text = "ShareX - Screen tearing test";
ShowInTaskbar = false; ShowInTaskbar = false;
@ -60,7 +59,7 @@ public ScreenTearingTestForm()
ResumeLayout(false); ResumeLayout(false);
this.CloseOnEscape(); ShareXResources.ApplyTheme(this, true);
animationTime = Stopwatch.StartNew(); animationTime = Stopwatch.StartNew();
} }

View file

@ -123,8 +123,13 @@ public static Bitmap Logo
public static ShareXTheme Theme { get; set; } = ShareXTheme.DarkTheme; public static ShareXTheme Theme { get; set; } = ShareXTheme.DarkTheme;
public static void ApplyTheme(Form form, bool setIcon = true) public static void ApplyTheme(Form form, bool closeOnEscape = false, bool setIcon = true)
{ {
if (closeOnEscape)
{
form.CloseOnEscape();
}
if (setIcon) if (setIcon)
{ {
form.Icon = Icon; form.Icon = Icon;

View file

@ -89,9 +89,7 @@ public HistoryForm(string historyPath, HistorySettings settings, Action<string>
tstbSearch.Text = Settings.SearchText; tstbSearch.Text = Settings.SearchText;
} }
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
if (Settings.RememberWindowState) if (Settings.RememberWindowState)
{ {
@ -412,11 +410,11 @@ private async void HistoryForm_KeyDown(object sender, KeyEventArgs e)
switch (e.KeyData) switch (e.KeyData)
{ {
case Keys.F5: case Keys.F5:
e.Handled = true; e.SuppressKeyPress = true;
await RefreshHistoryItems(); await RefreshHistoryItems();
break; break;
case Keys.Control | Keys.F5 when HelpersOptions.DevMode: case Keys.Control | Keys.F5 when HelpersOptions.DevMode:
e.Handled = true; e.SuppressKeyPress = true;
await RefreshHistoryItems(true); await RefreshHistoryItems(true);
break; break;
} }
@ -521,7 +519,7 @@ private void lvHistory_MouseDoubleClick(object sender, MouseEventArgs e)
private void lvHistory_KeyDown(object sender, KeyEventArgs e) private void lvHistory_KeyDown(object sender, KeyEventArgs e)
{ {
e.Handled = e.SuppressKeyPress = him.HandleKeyInput(e); e.SuppressKeyPress = him.HandleKeyInput(e);
} }
private void lvHistory_ItemDrag(object sender, ItemDragEventArgs e) private void lvHistory_ItemDrag(object sender, ItemDragEventArgs e)

View file

@ -74,9 +74,7 @@ public ImageHistoryForm(string historyPath, ImageHistorySettings settings, Actio
tstbSearch.Text = Settings.SearchText; tstbSearch.Text = Settings.SearchText;
} }
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
if (Settings.RememberWindowState) if (Settings.RememberWindowState)
{ {
@ -187,11 +185,11 @@ private void ImageHistoryForm_KeyDown(object sender, KeyEventArgs e)
{ {
case Keys.F5: case Keys.F5:
RefreshHistoryItems(); RefreshHistoryItems();
e.Handled = true; e.SuppressKeyPress = true;
break; break;
case Keys.Control | Keys.F5 when HelpersOptions.DevMode: case Keys.Control | Keys.F5 when HelpersOptions.DevMode:
RefreshHistoryItems(true); RefreshHistoryItems(true);
e.Handled = true; e.SuppressKeyPress = true;
break; break;
} }
} }
@ -231,7 +229,6 @@ private void tstbSearch_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Enter) if (e.KeyCode == Keys.Enter)
{ {
RefreshHistoryItems(); RefreshHistoryItems();
e.Handled = true;
e.SuppressKeyPress = true; e.SuppressKeyPress = true;
} }
} }
@ -254,7 +251,7 @@ private void tsbSettings_Click(object sender, EventArgs e)
private void ilvImages_KeyDown(object sender, KeyEventArgs e) private void ilvImages_KeyDown(object sender, KeyEventArgs e)
{ {
e.Handled = e.SuppressKeyPress = him.HandleKeyInput(e); e.SuppressKeyPress = him.HandleKeyInput(e);
} }
#endregion Form events #endregion Form events

View file

@ -43,8 +43,7 @@ public partial class DirectoryIndexerForm : Form
public DirectoryIndexerForm(IndexerSettings settings) public DirectoryIndexerForm(IndexerSettings settings)
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
Settings = settings; Settings = settings;
pgSettings.SelectedObject = Settings; pgSettings.SelectedObject = Settings;

View file

@ -55,8 +55,7 @@ private ImageBeautifierForm(ImageBeautifierOptions options = null)
} }
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
title = Text; title = Text;
LoadOptions(); LoadOptions();

View file

@ -44,8 +44,7 @@ public ImageCombinerForm(ImageCombinerOptions options)
Options = options; Options = options;
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
if (Options.Orientation == Orientation.Horizontal) if (Options.Orientation == Orientation.Horizontal)
{ {

View file

@ -42,8 +42,7 @@ public partial class ImageSplitterForm : Form
public ImageSplitterForm() public ImageSplitterForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
UpdateButtonStates(); UpdateButtonStates();
} }

View file

@ -37,8 +37,7 @@ public partial class ImageThumbnailerForm : Form
public ImageThumbnailerForm() public ImageThumbnailerForm()
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
} }
private void UpdateEnabled() private void UpdateEnabled()

View file

@ -46,8 +46,7 @@ public VideoConverterForm(string ffmpegFilePath, VideoConverterOptions options)
Options = options; Options = options;
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
UpdateOptions(); UpdateOptions();

View file

@ -46,8 +46,7 @@ public VideoThumbnailerForm(string ffmpegPath, VideoThumbnailOptions options)
Options = options; Options = options;
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
txtMediaPath.Text = Options.LastVideoPath ?? ""; txtMediaPath.Text = Options.LastVideoPath ?? "";
pgOptions.SelectedObject = Options; pgOptions.SelectedObject = Options;

View file

@ -149,7 +149,6 @@ private void tstbSearch_KeyDown(object sender, KeyEventArgs e)
{ {
if (e.KeyCode == Keys.Enter) if (e.KeyCode == Keys.Enter)
{ {
e.Handled = true;
e.SuppressKeyPress = true; e.SuppressKeyPress = true;
} }
} }
@ -158,7 +157,6 @@ private void tstbSearch_KeyUp(object sender, KeyEventArgs e)
{ {
if (e.KeyCode == Keys.Enter) if (e.KeyCode == Keys.Enter)
{ {
e.Handled = true;
e.SuppressKeyPress = true; e.SuppressKeyPress = true;
if (ilvStickers.Items.Count > 0) if (ilvStickers.Items.Count > 0)

View file

@ -1162,7 +1162,7 @@ internal void CreateToolbar()
tsMain.PerformLayout(); tsMain.PerformLayout();
menuForm.ResumeLayout(false); menuForm.ResumeLayout(false);
ShareXResources.ApplyTheme(menuForm, false); ShareXResources.ApplyTheme(menuForm, false, false);
menuForm.Show(Form); menuForm.Show(Form);
@ -1190,14 +1190,14 @@ private void MenuForm_KeyDown(object sender, KeyEventArgs e)
form_KeyDown(sender, e); form_KeyDown(sender, e);
Form.RegionCaptureForm_KeyDown(sender, e); Form.RegionCaptureForm_KeyDown(sender, e);
e.Handled = true; e.SuppressKeyPress = true;
} }
private void MenuForm_KeyUp(object sender, KeyEventArgs e) private void MenuForm_KeyUp(object sender, KeyEventArgs e)
{ {
form_KeyUp(sender, e); form_KeyUp(sender, e);
e.Handled = true; e.SuppressKeyPress = true;
} }
private void MenuForm_LocationChanged(object sender, EventArgs e) private void MenuForm_LocationChanged(object sender, EventArgs e)

View file

@ -36,8 +36,7 @@ public partial class TextUploadForm : Form
public TextUploadForm(string content = null) public TextUploadForm(string content = null)
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
if (string.IsNullOrEmpty(content) && ClipboardHelpers.ContainsText()) if (string.IsNullOrEmpty(content) && ClipboardHelpers.ContainsText())
{ {

View file

@ -40,8 +40,7 @@ public InspectWindowForm()
{ {
InitializeComponent(); InitializeComponent();
rtbInfo.AddContextMenu(); rtbInfo.AddContextMenu();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
SelectHandle(true); SelectHandle(true);
} }

View file

@ -1465,7 +1465,7 @@ private void lvUploads_KeyDown(object sender, KeyEventArgs e)
break; break;
} }
e.Handled = e.SuppressKeyPress = true; e.SuppressKeyPress = true;
} }
private void pbPreview_MouseDown(object sender, MouseEventArgs e) private void pbPreview_MouseDown(object sender, MouseEventArgs e)

View file

@ -61,8 +61,7 @@ public QRCodeForm(string text = null)
{ {
InitializeComponent(); InitializeComponent();
rtbDecodeResult.AddContextMenu(); rtbDecodeResult.AddContextMenu();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
if (!string.IsNullOrEmpty(text)) if (!string.IsNullOrEmpty(text))
{ {

View file

@ -40,8 +40,7 @@ public partial class BorderlessWindowForm : Form
public BorderlessWindowForm(BorderlessWindowSettings settings) public BorderlessWindowForm(BorderlessWindowSettings settings)
{ {
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
Settings = settings; Settings = settings;
} }

View file

@ -48,8 +48,7 @@ public OCRForm(Bitmap bmp, OCROptions options)
Options = options; Options = options;
InitializeComponent(); InitializeComponent();
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
this.CloseOnEscape();
OCRLanguage[] languages = OCRHelper.AvailableLanguages.OrderBy(x => x.DisplayName).ToArray(); OCRLanguage[] languages = OCRHelper.AvailableLanguages.OrderBy(x => x.DisplayName).ToArray();

View file

@ -145,10 +145,9 @@ private PinToScreenForm(PinToScreenOptions options)
InitializeComponent(); InitializeComponent();
// TODO: Add options form // TODO: Add options form
tsbOptions.Visible = false; tsbOptions.Visible = false;
ShareXResources.ApplyTheme(this); ShareXResources.ApplyTheme(this, true);
TopMost = Options.TopMost; TopMost = Options.TopMost;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
this.CloseOnEscape();
tsMain.Cursor = Cursors.Arrow; tsMain.Cursor = Cursors.Arrow;
openHandCursor = Helpers.CreateCursor(Resources.openhand); openHandCursor = Helpers.CreateCursor(Resources.openhand);