Add autostart option

This commit is contained in:
Stone_Red
2025-03-05 17:39:53 +01:00
parent ccb46993f5
commit a13babb780
4 changed files with 114 additions and 1 deletions
@@ -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<LockWindow?> lockWindowAccessor)
internal partial class MainWindowDataContext(Func<LockWindow?> 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<LockWindow?> 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<LockWindow?> lockWindowAccesso
Settings.UnlockShortcut = unlockShortcut;
}
}
protected void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
+2
View File
@@ -17,9 +17,11 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CuteUtils" Version="1.0.0" />
<PackageReference Include="Interop.IWshRuntimeLibrary" Version="1.0.1" />
<PackageReference Include="SharpHook" Version="5.3.8" />
<PackageReference Include="WPF-UI" Version="4.0.0" />
<PackageReference Include="WPF-UI.Tray" Version="4.0.0" />
<PackageReference Include="WpfScreenHelper" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
+1
View File
@@ -71,6 +71,7 @@ public partial class MainWindow : FluentWindow
e.Cancel = true;
ShowInTaskbar = false;
WindowState = WindowState.Minimized;
Visibility = Visibility.Collapsed;
}
}
+81
View File
@@ -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."),
};
}
}