mirror of
https://github.com/Stone-Red-Code/InvLock.git
synced 2026-09-04 00:56:10 +02:00
Add autostart option
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
|
||||||
|
using InvLock.Utilities;
|
||||||
|
|
||||||
using SharpHook.Native;
|
using SharpHook.Native;
|
||||||
|
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace InvLock.DataContexts;
|
namespace InvLock.DataContexts;
|
||||||
|
|
||||||
internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccessor)
|
internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccessor) : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
public string Title => $"{App.AppName} - Dev {AppVersion}";
|
public string Title => $"{App.AppName} - Dev {AppVersion}";
|
||||||
#else
|
#else
|
||||||
@@ -20,6 +26,24 @@ internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccesso
|
|||||||
|
|
||||||
public Settings Settings { get; } = Settings.Load();
|
public Settings Settings { get; } = Settings.Load();
|
||||||
|
|
||||||
|
public bool IsAutoStartEnabled
|
||||||
|
{
|
||||||
|
get => StartupManager.IsAutoStartEnabled();
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value)
|
||||||
|
{
|
||||||
|
_ = StartupManager.EnableAutoStart();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_ = StartupManager.DisableAutoStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
public async Task RecordLockShortcut()
|
public async Task RecordLockShortcut()
|
||||||
{
|
{
|
||||||
@@ -55,4 +79,9 @@ internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccesso
|
|||||||
Settings.UnlockShortcut = unlockShortcut;
|
Settings.UnlockShortcut = unlockShortcut;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void OnPropertyChanged([CallerMemberName] string? name = null)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,11 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||||
<PackageReference Include="CuteUtils" Version="1.0.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="SharpHook" Version="5.3.8" />
|
||||||
<PackageReference Include="WPF-UI" Version="4.0.0" />
|
<PackageReference Include="WPF-UI" Version="4.0.0" />
|
||||||
<PackageReference Include="WPF-UI.Tray" Version="4.0.0" />
|
<PackageReference Include="WPF-UI.Tray" Version="4.0.0" />
|
||||||
|
<PackageReference Include="WpfScreenHelper" Version="2.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ public partial class MainWindow : FluentWindow
|
|||||||
e.Cancel = true;
|
e.Cancel = true;
|
||||||
|
|
||||||
ShowInTaskbar = false;
|
ShowInTaskbar = false;
|
||||||
|
WindowState = WindowState.Minimized;
|
||||||
Visibility = Visibility.Collapsed;
|
Visibility = Visibility.Collapsed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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."),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user