Add autostart checkbox

This commit is contained in:
Stone_Red
2025-02-25 19:20:02 +01:00
parent 376176460b
commit 695cb867ee
7 changed files with 122 additions and 43 deletions
-42
View File
@@ -1,42 +0,0 @@
param(
[string]$AppPath,
[switch]$Enable,
[switch]$Disable
)
# Validate input
if ([string]::IsNullOrWhiteSpace($AppPath) -or !(Test-Path $AppPath)) {
exit 5 # Invalid app path
}
$AppName = [System.IO.Path]::GetFileNameWithoutExtension($AppPath)
$StartupPath = [Environment]::GetFolderPath('Startup')
$ShortcutPath = Join-Path $StartupPath "$AppName.lnk"
try {
if ($Enable) {
if (Test-Path $ShortcutPath) {
exit 2 # Already enabled
}
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $AppPath
$Shortcut.Save()
exit 0 # Success
}
elseif ($Disable) {
if (!(Test-Path $ShortcutPath)) {
exit 3 # Already disabled
}
Remove-Item $ShortcutPath -Force
exit 0 # Success
}
else {
exit 1 # Invalid parameters
}
}
catch {
exit 4 # Unexpected error
}
@@ -1,4 +1,5 @@
using DesktopMagic.Settings;
using DesktopMagic.Helpers;
using DesktopMagic.Settings;
using System.ComponentModel;
using System.Runtime.CompilerServices;
@@ -32,6 +33,23 @@ internal class MainWindowDataContext : INotifyPropertyChanged
}
}
public bool IsAutoStartEnabled
{
get => StartupManager.IsAutoStartEnabled();
set
{
if (value)
{
_ = StartupManager.EnableAutoStart();
}
else
{
_ = StartupManager.DisableAutoStart();
}
OnPropertyChanged();
}
}
public static DesktopMagicSettings GetSettings()
{
return settings;
+18
View File
@@ -34,6 +34,18 @@
<None Remove="Resources\Images\modio-logo-bluedark.png" />
</ItemGroup>
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\Images\modio-cog-blue.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
@@ -62,4 +74,10 @@
</Resource>
<Resource Include="Resources\Images\modio-logo-bluedark.png" />
</ItemGroup>
<ItemGroup>
<None Update="AutoStart.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
@@ -0,0 +1,82 @@
using IWshRuntimeLibrary;
using System;
using System.IO;
using System.Reflection;
using System.Windows;
namespace DesktopMagic.Helpers;
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."),
};
}
}
+1
View File
@@ -161,6 +161,7 @@
</Button>
</StackPanel>
<StackPanel Grid.Column="2" Margin="{StaticResource DefaultMargin}" VerticalAlignment="Bottom">
<CheckBox x:Name="autoStartCheckBox" Content="{DynamicResource autoStart}" Foreground="Black" BorderBrush="#FF323232" Margin="0,0,0,5" Width="170" Height="20" Style="{StaticResource MaterialDesignDarkCheckBox}" IsChecked="{Binding IsAutoStartEnabled}" />
<Button x:Name="downloadPluginsButton" Margin="0,0,0,5" HorizontalAlignment="Right" Width="170" Click="PluginManagerButton_Click" FontWeight="Regular" Cursor="Hand">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ExtensionOutline" Margin="0 0 10 0" />
@@ -6,6 +6,7 @@
<system:String x:Key="lineMode">Linie</system:String>
<system:String x:Key="mirrorMode">Spiegeln</system:String>
<system:String x:Key="folder">Ordner</system:String>
<system:String x:Key="autoStart">Starten beim Hochfahren</system:String>
<system:String x:Key="unknown">Unbekannt</system:String>
<system:String x:Key="open">Öffnen</system:String>
<system:String x:Key="quit">Schließen</system:String>
@@ -6,6 +6,7 @@
<system:String x:Key="lineMode">Line</system:String>
<system:String x:Key="mirrorMode">Mirror</system:String>
<system:String x:Key="folder">Folder</system:String>
<system:String x:Key="autoStart">Launch on startup</system:String>
<system:String x:Key="unknown">Unknown</system:String>
<system:String x:Key="open">Open</system:String>
<system:String x:Key="quit">Quit</system:String>