Skip to content

Commit

Permalink
Cleanup options and make shortcut-delay configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
haefele committed Oct 16, 2023
1 parent 5583e5f commit 7b28502
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
o.AppShutdownHeader = "Beenden";
});
services.AddOptions<OpenAIOptions>()
.Bind(context.Configuration.GetSection("OpenAI"))
.BindConfiguration("OpenAI")
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<ApplicationOptions>()
Expand All @@ -117,17 +117,20 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
o.GitHubUsername = "haefele";
o.GitHubRepository = "ChatPrisma";
o.GitHubReleaseAssetName = "App.zip";
o.CheckForUpdatesInBackground = true;
o.MinutesBetweenUpdateChecks = 30;
})
.Bind(context.Configuration.GetSection("Updater"))
.BindConfiguration("Updater")
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<KeyboardOptions>()
services.AddOptions<HotkeyOptions>()
.Configure(o =>
{
o.Key = "Y";
o.KeyModifiers = "Ctrl+Shift+Alt";
o.HotkeyDelayInMilliseconds = 200;
})
.Bind(context.Configuration.GetSection("Keyboard"))
.BindConfiguration("Hotkey")
.ValidateDataAnnotations()
.ValidateOnStart();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace ChatPrisma.Options
{
public class KeyboardOptions
public class HotkeyOptions
{
[Required]
public string Key { get; set; } = default!;
[Required]
public string KeyModifiers { get; set; } = default!;
public int HotkeyDelayInMilliseconds { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/ChatPrisma/Options/UpdaterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class UpdaterOptions
[Required]
public string GitHubReleaseAssetName { get; set; } = default!;

public bool CheckForUpdatesInBackground { get; set; } = true;
public int MinutesBetweenUpdateChecks { get; set; } = 10;
public bool CheckForUpdatesInBackground { get; set; }
public int MinutesBetweenUpdateChecks { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ChatPrisma.Services.KeyboardHooks;

public class GlobalKeyInterceptorKeyboardHooks(ILogger<GlobalKeyInterceptorKeyboardHooks> logger, IOptions<KeyboardOptions> keyboardOptions) : IKeyboardHooks, IDisposable
public class GlobalKeyInterceptorKeyboardHooks(ILogger<GlobalKeyInterceptorKeyboardHooks> logger, IOptions<HotkeyOptions> keyboardOptions) : IKeyboardHooks, IDisposable
{
public event EventHandler? CombinationPressed;

Expand All @@ -21,7 +21,7 @@ public Task StartAsync()
};

this._interceptor = new KeyInterceptor(shortcuts);
this._interceptor.ShortcutPressed += OnShortcutPressed;
this._interceptor.ShortcutPressed += this.OnShortcutPressed;

logger.LogInformation("Started listening for keyboard shortcuts.");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System.Windows.Forms;
using System.Windows.Forms;
using ChatPrisma.Options;
using Microsoft.Extensions.Options;

namespace ChatPrisma.Services.TextExtractor;

public class ClipboardTextExtractor : ITextExtractor
public class ClipboardTextExtractor(IOptionsMonitor<HotkeyOptions> hotkeyOptions) : ITextExtractor
{
public async Task<string?> GetCurrentTextAsync()
{
// Give the user a bit of time to release the keyboard keys,
// otherwise CTRL+C will not work
await Task.Delay(TimeSpan.FromMilliseconds(200));
await Task.Delay(TimeSpan.FromMilliseconds(hotkeyOptions.CurrentValue.HotkeyDelayInMilliseconds));

// Need to clear the clipboard, or otherwise we might get some previously copied text
Clipboard.Clear();
Expand Down
5 changes: 3 additions & 2 deletions src/ChatPrisma/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
},
"Updater": {
"CheckForUpdatesInBackground": true,
"MinutesBetweenUpdateChecks": 10
"MinutesBetweenUpdateChecks": 30
},
"Keyboard": {
"Hotkey": {
"Key": "Y",
"KeyModifiers": "Ctrl+Shift+Alt",
"HotkeyDelayInMilliseconds": 200,
},
"NLog": {
"throwConfigExceptions": true,
Expand Down

0 comments on commit 7b28502

Please sign in to comment.