diff --git a/ShareX/Forms/ApplicationSettingsForm.cs b/ShareX/Forms/ApplicationSettingsForm.cs index bc85374c4..36932a515 100644 --- a/ShareX/Forms/ApplicationSettingsForm.cs +++ b/ShareX/Forms/ApplicationSettingsForm.cs @@ -291,7 +291,7 @@ private void UpdateStartWithWindows() 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 @@ private void cbStartWithWindows_CheckedChanged(object sender, EventArgs e) { try { - StartupManagerSingletonProvider.CurrentStartupManager.State = cbStartWithWindows.Checked ? StartupState.Enabled : StartupState.Disabled; + StartupManager.State = cbStartWithWindows.Checked ? StartupState.Enabled : StartupState.Disabled; UpdateStartWithWindows(); } catch (Exception ex) diff --git a/ShareX/Forms/FirstTimeConfigForm.cs b/ShareX/Forms/FirstTimeConfigForm.cs index 045d2518f..f0654fb74 100644 --- a/ShareX/Forms/FirstTimeConfigForm.cs +++ b/ShareX/Forms/FirstTimeConfigForm.cs @@ -38,7 +38,7 @@ public FirstTimeConfigForm() 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 @@ private void cbRunStartup_CheckedChanged(object sender, EventArgs e) { if (loaded) { - StartupManagerSingletonProvider.CurrentStartupManager.State = cbRunStartup.Checked ? StartupState.Enabled : StartupState.Disabled; + StartupManager.State = cbRunStartup.Checked ? StartupState.Enabled : StartupState.Disabled; } } diff --git a/ShareX/IntegrationHelpers.cs b/ShareX/IntegrationHelpers.cs index 2677b286d..b8243d5a7 100644 --- a/ShareX/IntegrationHelpers.cs +++ b/ShareX/IntegrationHelpers.cs @@ -457,7 +457,7 @@ public static void SteamShowInApp(bool showInApp) public static void Uninstall() { - StartupManagerSingletonProvider.CurrentStartupManager.State = StartupState.Disabled; + StartupManager.State = StartupState.Disabled; CreateShellContextMenuButton(false); CreateEditShellContextMenuButton(false); CreateCustomUploaderExtension(false); diff --git a/ShareX/StartupManagers/GenericStartupManager.cs b/ShareX/StartupManager.cs similarity index 66% rename from ShareX/StartupManagers/GenericStartupManager.cs rename to ShareX/StartupManager.cs index fd3ffa9e3..0b5ab05ef 100644 --- a/ShareX/StartupManagers/GenericStartupManager.cs +++ b/ShareX/StartupManager.cs @@ -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 @@ public StartupState State { 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 @@ public StartupState State { throw new NotSupportedException(); } +#endif } } } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/ShareX/StartupManagers/CentennialStartupManager.cs b/ShareX/StartupManagers/CentennialStartupManager.cs deleted file mode 100644 index 998475291..000000000 --- a/ShareX/StartupManagers/CentennialStartupManager.cs +++ /dev/null @@ -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 . -*/ - -#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 \ No newline at end of file diff --git a/ShareX/StartupManagers/DesktopStartupManager.cs b/ShareX/StartupManagers/DesktopStartupManager.cs deleted file mode 100644 index 222504f21..000000000 --- a/ShareX/StartupManagers/DesktopStartupManager.cs +++ /dev/null @@ -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 . -*/ - -#endregion License Information (GPL v3) - -#if !MicrosoftStore - -using System.Windows.Forms; - -namespace ShareX -{ - public class DesktopStartupManager : GenericStartupManager - { - public override string StartupTargetPath => Application.ExecutablePath; - } -} - -#endif \ No newline at end of file diff --git a/ShareX/StartupManagers/IStartupManager.cs b/ShareX/StartupManagers/IStartupManager.cs deleted file mode 100644 index 228b1902f..000000000 --- a/ShareX/StartupManagers/IStartupManager.cs +++ /dev/null @@ -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 . -*/ - -#endregion License Information (GPL v3) - -namespace ShareX -{ - public interface IStartupManager - { - StartupState State { get; set; } - } -} \ No newline at end of file diff --git a/ShareX/StartupManagers/StartupManagerSingletonProvider.cs b/ShareX/StartupManagers/StartupManagerSingletonProvider.cs deleted file mode 100644 index d7accdacf..000000000 --- a/ShareX/StartupManagers/StartupManagerSingletonProvider.cs +++ /dev/null @@ -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 . -*/ - -#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 - } - } -} \ No newline at end of file diff --git a/ShareX/StartupManagers/SteamStartupManager.cs b/ShareX/StartupManagers/SteamStartupManager.cs deleted file mode 100644 index e2f202db6..000000000 --- a/ShareX/StartupManagers/SteamStartupManager.cs +++ /dev/null @@ -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 . -*/ - -#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 \ No newline at end of file