Code refactoring

This commit is contained in:
Jaex 2024-04-11 20:42:10 +03:00
parent 8eb08c4a9a
commit cbad9a45e4
9 changed files with 48 additions and 224 deletions

View File

@ -291,7 +291,7 @@ namespace ShareX
try
{
StartupState state = StartupManagerSingletonProvider.CurrentStartupManager.State;
StartupState state = StartupManager.State;
cbStartWithWindows.Checked = state == StartupState.Enabled || state == StartupState.EnabledByPolicy;
if (state == StartupState.DisabledByUser)
@ -592,7 +592,7 @@ namespace ShareX
{
try
{
StartupManagerSingletonProvider.CurrentStartupManager.State = cbStartWithWindows.Checked ? StartupState.Enabled : StartupState.Disabled;
StartupManager.State = cbStartWithWindows.Checked ? StartupState.Enabled : StartupState.Disabled;
UpdateStartWithWindows();
}
catch (Exception ex)

View File

@ -38,7 +38,7 @@ namespace ShareX
InitializeComponent();
ShareXResources.ApplyTheme(this);
StartupState state = StartupManagerSingletonProvider.CurrentStartupManager.State;
StartupState state = StartupManager.State;
cbRunStartup.Checked = state == StartupState.Enabled || state == StartupState.EnabledByPolicy;
cbRunStartup.Enabled = state != StartupState.DisabledByUser && state != StartupState.DisabledByPolicy && state != StartupState.EnabledByPolicy;
@ -63,7 +63,7 @@ namespace ShareX
{
if (loaded)
{
StartupManagerSingletonProvider.CurrentStartupManager.State = cbRunStartup.Checked ? StartupState.Enabled : StartupState.Disabled;
StartupManager.State = cbRunStartup.Checked ? StartupState.Enabled : StartupState.Disabled;
}
}

View File

@ -457,7 +457,7 @@ namespace ShareX
public static void Uninstall()
{
StartupManagerSingletonProvider.CurrentStartupManager.State = StartupState.Disabled;
StartupManager.State = StartupState.Disabled;
CreateShellContextMenuButton(false);
CreateEditShellContextMenuButton(false);
CreateCustomUploaderExtension(false);

View File

@ -23,22 +23,43 @@
#endregion License Information (GPL v3)
#if !MicrosoftStore
using Microsoft.Win32;
using ShareX.HelpersLib;
using System;
using System.Windows.Forms;
#if MicrosoftStore
using Windows.ApplicationModel;
#endif
namespace ShareX
{
public abstract class GenericStartupManager : IStartupManager
public static class StartupManager
{
public abstract string StartupTargetPath { get; }
#if MicrosoftStore
private const int StartupTargetIndex = 0;
private static readonly StartupTask packageTask = StartupTask.GetForCurrentPackageAsync().GetAwaiter().GetResult()[StartupTargetIndex];
#endif
public StartupState State
public static string StartupTargetPath
{
get
{
#if STEAM
return FileHelpers.GetAbsolutePath("../ShareX_Launcher.exe");
#else
return Application.ExecutablePath;
#endif
}
}
public static StartupState State
{
get
{
#if MicrosoftStore
return (StartupState)packageTask.State;
#else
if (ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, "ShareX", StartupTargetPath))
{
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\StartupFolder",
@ -55,9 +76,24 @@ namespace ShareX
{
return StartupState.Disabled;
}
#endif
}
set
{
#if MicrosoftStore
if (value == StartupState.Enabled)
{
packageTask.RequestEnableAsync().GetAwaiter().GetResult();
}
else if (value == StartupState.Disabled)
{
packageTask.Disable();
}
else
{
throw new NotSupportedException();
}
#else
if (value == StartupState.Enabled || value == StartupState.Disabled)
{
ShortcutHelpers.SetShortcut(value == StartupState.Enabled, Environment.SpecialFolder.Startup, "ShareX", StartupTargetPath, "-silent");
@ -66,9 +102,8 @@ namespace ShareX
{
throw new NotSupportedException();
}
#endif
}
}
}
}
#endif
}

View File

@ -1,60 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
#if MicrosoftStore
using System;
using Windows.ApplicationModel;
namespace ShareX
{
public class CentennialStartupManager : IStartupManager
{
private const int StartupTargetIndex = 0;
private readonly StartupTask packageTask = StartupTask.GetForCurrentPackageAsync().GetAwaiter().GetResult()[StartupTargetIndex];
public StartupState State
{
get => (StartupState)packageTask.State;
set
{
if (value == StartupState.Enabled)
{
packageTask.RequestEnableAsync().GetAwaiter().GetResult();
}
else if (value == StartupState.Disabled)
{
packageTask.Disable();
}
else
{
throw new NotSupportedException();
}
}
}
}
}
#endif

View File

@ -1,38 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
#if !MicrosoftStore
using System.Windows.Forms;
namespace ShareX
{
public class DesktopStartupManager : GenericStartupManager
{
public override string StartupTargetPath => Application.ExecutablePath;
}
}
#endif

View File

@ -1,32 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
namespace ShareX
{
public interface IStartupManager
{
StartupState State { get; set; }
}
}

View File

@ -1,43 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
namespace ShareX
{
public static class StartupManagerSingletonProvider
{
public static IStartupManager CurrentStartupManager { get; private set; }
static StartupManagerSingletonProvider()
{
#if MicrosoftStore
CurrentStartupManager = new CentennialStartupManager();
#elif STEAM
CurrentStartupManager = new SteamStartupManager();
#else
CurrentStartupManager = new DesktopStartupManager();
#endif
}
}
}

View File

@ -1,38 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
#if STEAM
using ShareX.HelpersLib;
namespace ShareX
{
public class SteamStartupManager : GenericStartupManager
{
public override string StartupTargetPath => FileHelpers.GetAbsolutePath("../ShareX_Launcher.exe");
}
}
#endif