From 7bd58913f16b377533c0231f871b762fdb8ef29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Mon, 16 Oct 2023 00:17:45 +0200 Subject: [PATCH] Add auto-start functionality --- src/ChatPrisma/App.xaml.cs | 1 + .../HostedServices/PrismaHostedService.cs | 4 +- .../Services/AutoStart/IAutoStartService.cs | 7 ++++ .../AutoStart/RegistryAutoStartService.cs | 42 +++++++++++++++++++ .../Services/Dialogs/DialogService.cs | 22 ++++++---- .../Services/Dialogs/IDialogService.cs | 6 +-- .../Services/Dialogs/IInitialize.cs | 6 +++ .../Views/Settings/SettingsView.xaml | 40 +++++++++++++++--- .../Views/Settings/SettingsViewModel.cs | 26 +++++++++++- src/ChatPrisma/WindowsTray.cs | 18 ++++---- 10 files changed, 143 insertions(+), 29 deletions(-) create mode 100644 src/ChatPrisma/Services/AutoStart/IAutoStartService.cs create mode 100644 src/ChatPrisma/Services/AutoStart/RegistryAutoStartService.cs create mode 100644 src/ChatPrisma/Services/Dialogs/IInitialize.cs diff --git a/src/ChatPrisma/App.xaml.cs b/src/ChatPrisma/App.xaml.cs index 65d172b..b78e123 100644 --- a/src/ChatPrisma/App.xaml.cs +++ b/src/ChatPrisma/App.xaml.cs @@ -139,6 +139,7 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho }); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // Hosted Services services.AddHostedService(); diff --git a/src/ChatPrisma/HostedServices/PrismaHostedService.cs b/src/ChatPrisma/HostedServices/PrismaHostedService.cs index e4c6960..52f7328 100644 --- a/src/ChatPrisma/HostedServices/PrismaHostedService.cs +++ b/src/ChatPrisma/HostedServices/PrismaHostedService.cs @@ -1,4 +1,4 @@ -using ChatPrisma.Services.Dialogs; +using ChatPrisma.Services.Dialogs; using ChatPrisma.Services.KeyboardHooks; using ChatPrisma.Services.TextExtractor; using ChatPrisma.Services.ViewModels; @@ -36,7 +36,7 @@ private async void KeyboardHooksOnCombinationPressed(object? sender, EventArgs e try { var textEnhancementViewModel = viewModelFactory.CreateTextEnhancementViewModel(text); - dialogService.ShowDialog(textEnhancementViewModel); + await dialogService.ShowDialog(textEnhancementViewModel); } finally { diff --git a/src/ChatPrisma/Services/AutoStart/IAutoStartService.cs b/src/ChatPrisma/Services/AutoStart/IAutoStartService.cs new file mode 100644 index 0000000..a47e5c2 --- /dev/null +++ b/src/ChatPrisma/Services/AutoStart/IAutoStartService.cs @@ -0,0 +1,7 @@ +namespace ChatPrisma.Services.AutoStart; + +public interface IAutoStartService +{ + Task IsInAutoStart(); + Task SetAutoStart(bool enabled); +} diff --git a/src/ChatPrisma/Services/AutoStart/RegistryAutoStartService.cs b/src/ChatPrisma/Services/AutoStart/RegistryAutoStartService.cs new file mode 100644 index 0000000..a047f3f --- /dev/null +++ b/src/ChatPrisma/Services/AutoStart/RegistryAutoStartService.cs @@ -0,0 +1,42 @@ +using ChatPrisma.Options; +using Microsoft.Extensions.Options; +using Microsoft.Win32; + +namespace ChatPrisma.Services.AutoStart; + +public class RegistryAutoStartService(IOptions applicationOptions) : IAutoStartService +{ + public async Task IsInAutoStart() + { + await Task.CompletedTask; + + var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", writable: false); + var result = key?.GetValue(applicationOptions.Value.ApplicationName) is not null; + + // If auto-start is enabled, ensure that we have the correct application path in the registry + // We do that by just enabling auto-start again + if (result is true) + { + await this.SetAutoStart(true); + } + + return result; + } + + public async Task SetAutoStart(bool enabled) + { + await Task.CompletedTask; + + var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", writable: true) ?? + Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); + + if (enabled) + { + key.SetValue(applicationOptions.Value.ApplicationName, $"\"{Environment.ProcessPath}\""); + } + else + { + key.DeleteValue(applicationOptions.Value.ApplicationName, throwOnMissingValue: false); + } + } +} diff --git a/src/ChatPrisma/Services/Dialogs/DialogService.cs b/src/ChatPrisma/Services/Dialogs/DialogService.cs index bb91ef4..63d298b 100644 --- a/src/ChatPrisma/Services/Dialogs/DialogService.cs +++ b/src/ChatPrisma/Services/Dialogs/DialogService.cs @@ -1,4 +1,4 @@ -using System.Windows; +using System.Windows; using System.Windows.Data; using ChatPrisma.Options; using ChatPrisma.Themes; @@ -9,12 +9,9 @@ namespace ChatPrisma.Services.Dialogs; public class DialogService(IServiceProvider serviceProvider, IOptions applicationOptions) : IDialogService { - public bool? ShowDialog(object viewModel) + public async Task ShowDialog(object viewModel) { - var viewType = this.TryResolveViewType(viewModel); - - if (viewType is null) - throw new PrismaException(); + var viewType = this.ResolveViewType(viewModel); var view = (FrameworkElement)ActivatorUtilities.CreateInstance(serviceProvider, viewType); this.InvokeInitializeComponents(view); @@ -49,13 +46,22 @@ public class DialogService(IServiceProvider serviceProvider, IOptions ShowDialog(object viewModel); +} diff --git a/src/ChatPrisma/Services/Dialogs/IInitialize.cs b/src/ChatPrisma/Services/Dialogs/IInitialize.cs new file mode 100644 index 0000000..8a448b4 --- /dev/null +++ b/src/ChatPrisma/Services/Dialogs/IInitialize.cs @@ -0,0 +1,6 @@ +namespace ChatPrisma.Services.Dialogs; + +public interface IInitialize +{ + Task InitializeAsync(); +} diff --git a/src/ChatPrisma/Views/Settings/SettingsView.xaml b/src/ChatPrisma/Views/Settings/SettingsView.xaml index 58b82ba..8b2a18f 100644 --- a/src/ChatPrisma/Views/Settings/SettingsView.xaml +++ b/src/ChatPrisma/Views/Settings/SettingsView.xaml @@ -1,13 +1,41 @@ - - - + + d:DataContext="{d:DesignInstance local:SettingsViewModel}" + + themes:Attached.WindowTitle="Einstellungen" + + Width="300"> + + + + + + + + + + + +