From d518f9602b63dde22e309dc4d3ed44e85ab45343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Peyronnet?= Date: Sat, 6 Jan 2024 10:22:49 +0100 Subject: [PATCH] Added the possibility to launch the app on start (#39) --- PermaTop/Classes/Global.cs | 12 + PermaTop/Classes/Settings.cs | 2 + PermaTop/Pages/SettingsPage.xaml | 606 ++++++++++++++++++++-------- PermaTop/Pages/SettingsPage.xaml.cs | 14 +- 4 files changed, 473 insertions(+), 161 deletions(-) diff --git a/PermaTop/Classes/Global.cs b/PermaTop/Classes/Global.cs index 31efc20..2f993b4 100644 --- a/PermaTop/Classes/Global.cs +++ b/PermaTop/Classes/Global.cs @@ -343,6 +343,18 @@ public static void ChangeLanguage() } } + public static void SetLaunchAtStart(bool launchAtStart) + { + var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); + if (launchAtStart) + { + key?.SetValue("PermaTop", Environment.ProcessPath ?? $@"{AppContext.BaseDirectory}\PermaTop.exe"); + return; + } + + key?.DeleteValue("PermaTop", false); + } + [DllImport("user32.dll")] internal static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex); diff --git a/PermaTop/Classes/Settings.cs b/PermaTop/Classes/Settings.cs index 8e3fb7b..06ef469 100644 --- a/PermaTop/Classes/Settings.cs +++ b/PermaTop/Classes/Settings.cs @@ -31,11 +31,13 @@ public class Settings public Languages Language { get; set; } public bool IsFirstRun { get; set; } public bool CheckUpdateOnStart { get; set; } + public bool? LaunchAtStart { get; set; } public Settings() { Theme = Themes.System; Language = Languages.Default; IsFirstRun = true; CheckUpdateOnStart = true; + LaunchAtStart = false; } } diff --git a/PermaTop/Pages/SettingsPage.xaml b/PermaTop/Pages/SettingsPage.xaml index 77bc82f..48ce371 100644 --- a/PermaTop/Pages/SettingsPage.xaml +++ b/PermaTop/Pages/SettingsPage.xaml @@ -1,171 +1,457 @@ - + - - - - - - + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/PermaTop/Pages/SettingsPage.xaml.cs b/PermaTop/Pages/SettingsPage.xaml.cs index bcf3f20..fe8ef3c 100644 --- a/PermaTop/Pages/SettingsPage.xaml.cs +++ b/PermaTop/Pages/SettingsPage.xaml.cs @@ -52,6 +52,9 @@ public SettingsPage() private async void InitUI() { + // Upgrade settings + Global.Settings.LaunchAtStart ??= false; + // About section VersionTxt.Text = Global.Version; // Update the current version label @@ -74,6 +77,7 @@ private async void InitUI() // Checkboxes UpdateOnStartChk.IsChecked = Global.Settings.CheckUpdateOnStart; + LaunchOnStartChk.IsChecked = Global.Settings.LaunchAtStart ?? false; if (!Global.Settings.CheckUpdateOnStart) return; try @@ -263,5 +267,13 @@ private void UpdateOnStartChk_Checked(object sender, RoutedEventArgs e) Global.Settings.CheckUpdateOnStart = UpdateOnStartChk.IsChecked ?? true; XmlSerializerManager.SaveToXml(Global.Settings, Global.SettingsPath); } - } + + private void LaunchOnStartChk_Checked(object sender, RoutedEventArgs e) + { + Global.Settings.LaunchAtStart = LaunchOnStartChk.IsChecked; + XmlSerializerManager.SaveToXml(Global.Settings, Global.SettingsPath); + + Global.SetLaunchAtStart(Global.Settings.LaunchAtStart ?? false); + } + } }