Skip to content

Commit a13babb

Browse files
Add autostart option
1 parent ccb4699 commit a13babb

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

src/InvLock/DataContexts/MainWindowDataContext.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
using CommunityToolkit.Mvvm.Input;
22

3+
using InvLock.Utilities;
4+
35
using SharpHook.Native;
46

7+
using System.ComponentModel;
58
using System.Reflection;
9+
using System.Runtime.CompilerServices;
610

711
namespace InvLock.DataContexts;
812

9-
internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccessor)
13+
internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccessor) : INotifyPropertyChanged
1014
{
15+
public event PropertyChangedEventHandler? PropertyChanged;
16+
1117
#if DEBUG
1218
public string Title => $"{App.AppName} - Dev {AppVersion}";
1319
#else
@@ -20,6 +26,24 @@ internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccesso
2026

2127
public Settings Settings { get; } = Settings.Load();
2228

29+
public bool IsAutoStartEnabled
30+
{
31+
get => StartupManager.IsAutoStartEnabled();
32+
set
33+
{
34+
if (value)
35+
{
36+
_ = StartupManager.EnableAutoStart();
37+
}
38+
else
39+
{
40+
_ = StartupManager.DisableAutoStart();
41+
}
42+
43+
OnPropertyChanged();
44+
}
45+
}
46+
2347
[RelayCommand]
2448
public async Task RecordLockShortcut()
2549
{
@@ -55,4 +79,9 @@ public async Task RecordUnlockShortcut()
5579
Settings.UnlockShortcut = unlockShortcut;
5680
}
5781
}
82+
83+
protected void OnPropertyChanged([CallerMemberName] string? name = null)
84+
{
85+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
86+
}
5887
}

src/InvLock/InvLock.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
<ItemGroup>
1818
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
1919
<PackageReference Include="CuteUtils" Version="1.0.0" />
20+
<PackageReference Include="Interop.IWshRuntimeLibrary" Version="1.0.1" />
2021
<PackageReference Include="SharpHook" Version="5.3.8" />
2122
<PackageReference Include="WPF-UI" Version="4.0.0" />
2223
<PackageReference Include="WPF-UI.Tray" Version="4.0.0" />
24+
<PackageReference Include="WpfScreenHelper" Version="2.1.1" />
2325
</ItemGroup>
2426

2527
<ItemGroup>

src/InvLock/MainWindow.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void FluentWindow_Closing(object sender, System.ComponentModel.CancelEve
7171
e.Cancel = true;
7272

7373
ShowInTaskbar = false;
74+
WindowState = WindowState.Minimized;
7475
Visibility = Visibility.Collapsed;
7576
}
7677
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using IWshRuntimeLibrary;
2+
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Windows;
6+
7+
namespace InvLock.Utilities;
8+
9+
public enum AutoStartStatus
10+
{
11+
Success = 0,
12+
InvalidParameters = 1,
13+
AlreadyEnabled = 2,
14+
AlreadyDisabled = 3,
15+
UnexpectedError = 4
16+
}
17+
18+
public static class StartupManager
19+
{
20+
private static readonly string appPath = Path.ChangeExtension(Assembly.GetEntryAssembly()?.Location ?? throw new InvalidOperationException("Failed to get the application path."), ".exe");
21+
private static readonly string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
22+
private static readonly string shortcutPath = Path.Combine(startupFolder, Path.GetFileNameWithoutExtension(appPath) + ".lnk");
23+
24+
public static bool IsAutoStartEnabled()
25+
{
26+
return System.IO.File.Exists(shortcutPath);
27+
}
28+
29+
public static AutoStartStatus EnableAutoStart()
30+
{
31+
if (IsAutoStartEnabled())
32+
{
33+
return AutoStartStatus.AlreadyEnabled;
34+
}
35+
36+
try
37+
{
38+
WshShell shell = new WshShell();
39+
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
40+
shortcut.TargetPath = appPath;
41+
shortcut.WorkingDirectory = Path.GetDirectoryName(appPath);
42+
shortcut.Save();
43+
return AutoStartStatus.Success;
44+
}
45+
catch
46+
{
47+
return AutoStartStatus.UnexpectedError;
48+
}
49+
}
50+
51+
public static AutoStartStatus DisableAutoStart()
52+
{
53+
if (!IsAutoStartEnabled())
54+
{
55+
return AutoStartStatus.AlreadyDisabled;
56+
}
57+
58+
try
59+
{
60+
System.IO.File.Delete(shortcutPath);
61+
return AutoStartStatus.Success;
62+
}
63+
catch
64+
{
65+
return AutoStartStatus.UnexpectedError;
66+
}
67+
}
68+
69+
public static void ToggleAutoStart()
70+
{
71+
AutoStartStatus status = IsAutoStartEnabled() ? DisableAutoStart() : EnableAutoStart();
72+
73+
_ = status switch
74+
{
75+
AutoStartStatus.Success => MessageBox.Show("Auto-start setting changed successfully."),
76+
AutoStartStatus.AlreadyEnabled => MessageBox.Show("Auto-start is already enabled."),
77+
AutoStartStatus.AlreadyDisabled => MessageBox.Show("Auto-start is already disabled."),
78+
_ => MessageBox.Show("Failed to change auto-start setting."),
79+
};
80+
}
81+
}

0 commit comments

Comments
 (0)