Code refactoring

This commit is contained in:
Jaex 2024-03-29 02:29:07 +03:00
parent 77d4f7a039
commit a92bafb725
4 changed files with 53 additions and 47 deletions

View File

@ -111,6 +111,7 @@
this.Controls.Add(this.flpMenu);
this.Controls.Add(this.txtException);
this.Name = "ErrorForm";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.TopMost = true;
this.Shown += new System.EventHandler(this.ErrorForm_Shown);
this.flpMenu.ResumeLayout(false);

View File

@ -822,14 +822,14 @@ namespace ShareX
LanguageHelper.ChangeLanguage(Program.Settings.Language);
Program.MainForm.UpdateControls();
await Program.MainForm.UpdateControls();
DebugHelper.WriteLine($"Import completed: {importPath}");
}
}
}
private void btnResetSettings_Click(object sender, EventArgs e)
private async void btnResetSettings_Click(object sender, EventArgs e)
{
if (MessageBox.Show(Resources.ApplicationSettingsForm_btnResetSettings_Click_WouldYouLikeToResetShareXSettings, "ShareX - " + Resources.Confirmation,
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
@ -841,7 +841,7 @@ namespace ShareX
LanguageHelper.ChangeLanguage(Program.Settings.Language);
Program.MainForm.UpdateControls();
await Program.MainForm.UpdateControls();
DebugHelper.WriteLine("Settings reset.");
}

View File

@ -58,7 +58,8 @@ namespace ShareX
RunPuushTasks();
NativeMethods.UseImmersiveDarkMode(Handle, ShareXResources.IsDarkTheme);
UpdateControls();
await UpdateControls();
DebugHelper.WriteLine("Startup time: {0} ms", Program.StartTimer.ElapsedMilliseconds);
@ -197,7 +198,7 @@ namespace ShareX
HandleCreated += MainForm_HandleCreated;
}
public void UpdateControls()
public async Task UpdateControls()
{
IsReady = false;
@ -260,7 +261,7 @@ namespace ShareX
AfterTaskSettingsJobs();
AfterApplicationSettingsJobs();
InitHotkeys();
await InitHotkeys();
IsReady = true;
}
@ -317,39 +318,35 @@ namespace ShareX
}
}
private void InitHotkeys()
private async Task InitHotkeys()
{
Task.Run(() =>
await Task.Run(() => SettingManager.WaitHotkeysConfig());
if (Program.HotkeyManager == null)
{
SettingManager.WaitHotkeysConfig();
}).ContinueInCurrentContext(() =>
Program.HotkeyManager = new HotkeyManager(this);
Program.HotkeyManager.HotkeyTrigger += HandleHotkeys;
}
Program.HotkeyManager.UpdateHotkeys(Program.HotkeysConfig.Hotkeys, !Program.IgnoreHotkeyWarning);
DebugHelper.WriteLine("HotkeyManager started.");
if (Program.WatchFolderManager == null)
{
if (Program.HotkeyManager == null)
{
Program.HotkeyManager = new HotkeyManager(this);
Program.HotkeyManager.HotkeyTrigger += HandleHotkeys;
}
Program.WatchFolderManager = new WatchFolderManager();
}
Program.HotkeyManager.UpdateHotkeys(Program.HotkeysConfig.Hotkeys, !Program.IgnoreHotkeyWarning);
Program.WatchFolderManager.UpdateWatchFolders();
DebugHelper.WriteLine("HotkeyManager started.");
DebugHelper.WriteLine("WatchFolderManager started.");
if (Program.WatchFolderManager == null)
{
Program.WatchFolderManager = new WatchFolderManager();
}
UpdateWorkflowsMenu();
Program.WatchFolderManager.UpdateWatchFolders();
DebugHelper.WriteLine("WatchFolderManager started.");
UpdateWorkflowsMenu();
if (pHotkeys.Visible)
{
pHotkeys.Focus();
}
});
if (pHotkeys.Visible)
{
pHotkeys.Focus();
}
}
private async void HandleHotkeys(HotkeySettings hotkeySetting)

View File

@ -47,8 +47,29 @@ namespace ShareX
public HotkeyManager(HotkeyForm form)
{
hotkeyForm = form;
hotkeyForm.HotkeyPress += hotkeyForm_HotkeyPress;
hotkeyForm.FormClosed += (sender, e) => hotkeyForm.InvokeSafe(() => UnregisterAllHotkeys(false));
hotkeyForm.HotkeyPress += HotkeyForm_HotkeyPress;
hotkeyForm.FormClosed += HotkeyForm_FormClosed;
}
private void HotkeyForm_HotkeyPress(ushort id, Keys key, Modifiers modifier)
{
if (!IgnoreHotkeys && (!Program.Settings.DisableHotkeysOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
{
HotkeySettings hotkeySetting = Hotkeys.Find(x => x.HotkeyInfo.ID == id);
if (hotkeySetting != null)
{
OnHotkeyTrigger(hotkeySetting);
}
}
}
private void HotkeyForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (hotkeyForm != null && !hotkeyForm.IsDisposed)
{
UnregisterAllHotkeys(false);
}
}
public void UpdateHotkeys(List<HotkeySettings> hotkeys, bool showFailedHotkeys)
@ -68,19 +89,6 @@ namespace ShareX
}
}
private void hotkeyForm_HotkeyPress(ushort id, Keys key, Modifiers modifier)
{
if (!IgnoreHotkeys && (!Program.Settings.DisableHotkeysOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
{
HotkeySettings hotkeySetting = Hotkeys.Find(x => x.HotkeyInfo.ID == id);
if (hotkeySetting != null)
{
OnHotkeyTrigger(hotkeySetting);
}
}
}
protected void OnHotkeyTrigger(HotkeySettings hotkeySetting)
{
HotkeyTrigger?.Invoke(hotkeySetting);