From a13babb780ad13480239dc5962d5017b8f83b9b8 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 5 Mar 2025 17:39:22 +0100 Subject: [PATCH] Add autostart option --- .../DataContexts/MainWindowDataContext.cs | 31 ++++++- src/InvLock/InvLock.csproj | 2 + src/InvLock/MainWindow.xaml.cs | 1 + src/InvLock/Utilities/StartupManager.cs | 81 +++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/InvLock/Utilities/StartupManager.cs diff --git a/src/InvLock/DataContexts/MainWindowDataContext.cs b/src/InvLock/DataContexts/MainWindowDataContext.cs index 2de8097..a8aecb4 100644 --- a/src/InvLock/DataContexts/MainWindowDataContext.cs +++ b/src/InvLock/DataContexts/MainWindowDataContext.cs @@ -1,13 +1,19 @@ using CommunityToolkit.Mvvm.Input; +using InvLock.Utilities; + using SharpHook.Native; +using System.ComponentModel; using System.Reflection; +using System.Runtime.CompilerServices; namespace InvLock.DataContexts; -internal partial class MainWindowDataContext(Func lockWindowAccessor) +internal partial class MainWindowDataContext(Func lockWindowAccessor) : INotifyPropertyChanged { + public event PropertyChangedEventHandler? PropertyChanged; + #if DEBUG public string Title => $"{App.AppName} - Dev {AppVersion}"; #else @@ -20,6 +26,24 @@ internal partial class MainWindowDataContext(Func lockWindowAccesso public Settings Settings { get; } = Settings.Load(); + public bool IsAutoStartEnabled + { + get => StartupManager.IsAutoStartEnabled(); + set + { + if (value) + { + _ = StartupManager.EnableAutoStart(); + } + else + { + _ = StartupManager.DisableAutoStart(); + } + + OnPropertyChanged(); + } + } + [RelayCommand] public async Task RecordLockShortcut() { @@ -55,4 +79,9 @@ internal partial class MainWindowDataContext(Func lockWindowAccesso Settings.UnlockShortcut = unlockShortcut; } } + + protected void OnPropertyChanged([CallerMemberName] string? name = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + } } \ No newline at end of file diff --git a/src/InvLock/InvLock.csproj b/src/InvLock/InvLock.csproj index 37ee1ae..63ded88 100644 --- a/src/InvLock/InvLock.csproj +++ b/src/InvLock/InvLock.csproj @@ -17,9 +17,11 @@ + + diff --git a/src/InvLock/MainWindow.xaml.cs b/src/InvLock/MainWindow.xaml.cs index 926f921..a3c4b76 100644 --- a/src/InvLock/MainWindow.xaml.cs +++ b/src/InvLock/MainWindow.xaml.cs @@ -71,6 +71,7 @@ public partial class MainWindow : FluentWindow e.Cancel = true; ShowInTaskbar = false; + WindowState = WindowState.Minimized; Visibility = Visibility.Collapsed; } } diff --git a/src/InvLock/Utilities/StartupManager.cs b/src/InvLock/Utilities/StartupManager.cs new file mode 100644 index 0000000..0d99ca0 --- /dev/null +++ b/src/InvLock/Utilities/StartupManager.cs @@ -0,0 +1,81 @@ +using IWshRuntimeLibrary; + +using System.IO; +using System.Reflection; +using System.Windows; + +namespace InvLock.Utilities; + +public enum AutoStartStatus +{ + Success = 0, + InvalidParameters = 1, + AlreadyEnabled = 2, + AlreadyDisabled = 3, + UnexpectedError = 4 +} + +public static class StartupManager +{ + private static readonly string appPath = Path.ChangeExtension(Assembly.GetEntryAssembly()?.Location ?? throw new InvalidOperationException("Failed to get the application path."), ".exe"); + private static readonly string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + private static readonly string shortcutPath = Path.Combine(startupFolder, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); + + public static bool IsAutoStartEnabled() + { + return System.IO.File.Exists(shortcutPath); + } + + public static AutoStartStatus EnableAutoStart() + { + if (IsAutoStartEnabled()) + { + return AutoStartStatus.AlreadyEnabled; + } + + try + { + WshShell shell = new WshShell(); + IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); + shortcut.TargetPath = appPath; + shortcut.WorkingDirectory = Path.GetDirectoryName(appPath); + shortcut.Save(); + return AutoStartStatus.Success; + } + catch + { + return AutoStartStatus.UnexpectedError; + } + } + + public static AutoStartStatus DisableAutoStart() + { + if (!IsAutoStartEnabled()) + { + return AutoStartStatus.AlreadyDisabled; + } + + try + { + System.IO.File.Delete(shortcutPath); + return AutoStartStatus.Success; + } + catch + { + return AutoStartStatus.UnexpectedError; + } + } + + public static void ToggleAutoStart() + { + AutoStartStatus status = IsAutoStartEnabled() ? DisableAutoStart() : EnableAutoStart(); + + _ = status switch + { + AutoStartStatus.Success => MessageBox.Show("Auto-start setting changed successfully."), + AutoStartStatus.AlreadyEnabled => MessageBox.Show("Auto-start is already enabled."), + AutoStartStatus.AlreadyDisabled => MessageBox.Show("Auto-start is already disabled."), + _ => MessageBox.Show("Failed to change auto-start setting."), + }; + } +} \ No newline at end of file