diff --git a/src/ChatPrisma/App.xaml.cs b/src/ChatPrisma/App.xaml.cs index ca42b37..4bf04b7 100644 --- a/src/ChatPrisma/App.xaml.cs +++ b/src/ChatPrisma/App.xaml.cs @@ -132,10 +132,19 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho o.Key = "Y"; o.KeyModifiers = "Ctrl+Shift+Alt"; o.HotkeyDelayInMilliseconds = 500; + o.ClipboardDelayInMilliseconds = 500; }) .BindConfiguration("Hotkey") .ValidateDataAnnotations() .ValidateOnStart(); + services.AddOptions() + .Configure(o => + { + o.TextSize = 12; + }) + .BindConfiguration("TextEnhancement") + .ValidateDataAnnotations() + .ValidateOnStart(); // Services services.AddSingleton(); diff --git a/src/ChatPrisma/HostedServices/PrismaHostedService.cs b/src/ChatPrisma/HostedServices/PrismaHostedService.cs index 52f7328..e3a4e09 100644 --- a/src/ChatPrisma/HostedServices/PrismaHostedService.cs +++ b/src/ChatPrisma/HostedServices/PrismaHostedService.cs @@ -20,27 +20,13 @@ public Task StopAsync(CancellationToken cancellationToken) return Task.CompletedTask; } - private bool _alreadyShowing; - private async void KeyboardHooksOnCombinationPressed(object? sender, EventArgs e) { - // Only allow one text to be enhanced at a time - if (this._alreadyShowing) - return; - var text = await textExtractor.GetCurrentTextAsync(); if (text is null) return; - this._alreadyShowing = true; - try - { - var textEnhancementViewModel = viewModelFactory.CreateTextEnhancementViewModel(text); - await dialogService.ShowDialog(textEnhancementViewModel); - } - finally - { - this._alreadyShowing = false; - } + var textEnhancementViewModel = viewModelFactory.CreateTextEnhancementViewModel(text); + await dialogService.ShowDialog(textEnhancementViewModel); } } diff --git a/src/ChatPrisma/Options/HotkeyOptions.cs b/src/ChatPrisma/Options/HotkeyOptions.cs index f77293a..f0c94e0 100644 --- a/src/ChatPrisma/Options/HotkeyOptions.cs +++ b/src/ChatPrisma/Options/HotkeyOptions.cs @@ -9,5 +9,6 @@ public class HotkeyOptions [Required] public string KeyModifiers { get; set; } = default!; public int HotkeyDelayInMilliseconds { get; set; } + public int ClipboardDelayInMilliseconds { get; set; } } } diff --git a/src/ChatPrisma/Options/TextEnhancementOptions.cs b/src/ChatPrisma/Options/TextEnhancementOptions.cs new file mode 100644 index 0000000..d57bade --- /dev/null +++ b/src/ChatPrisma/Options/TextEnhancementOptions.cs @@ -0,0 +1,6 @@ +namespace ChatPrisma.Options; + +public class TextEnhancementOptions +{ + public int TextSize { get; set; } +} diff --git a/src/ChatPrisma/Services/TextExtractor/ClipboardTextExtractor.cs b/src/ChatPrisma/Services/TextExtractor/ClipboardTextExtractor.cs index ff28bcc..002a85d 100644 --- a/src/ChatPrisma/Services/TextExtractor/ClipboardTextExtractor.cs +++ b/src/ChatPrisma/Services/TextExtractor/ClipboardTextExtractor.cs @@ -29,7 +29,8 @@ public class ClipboardTextExtractor(IOptionsMonitor hotkeyOptions Clipboard.Clear(); SendKeys.SendWait("^c"); - var selectedText = Clipboard.GetText(); + // It can take a while until the text is in the clipboard + var selectedText = await this.WaitUntilClipboardTextIsAvailable(); if (string.IsNullOrWhiteSpace(selectedText)) return null; @@ -72,4 +73,26 @@ private static bool AnyKeyPressed() return false; } + + private async Task WaitUntilClipboardTextIsAvailable() + { + var task = Task.Delay(TimeSpan.FromMilliseconds(hotkeyOptions.CurrentValue.ClipboardDelayInMilliseconds)); + var watch = Stopwatch.StartNew(); + + // Either wait until the task is completed or the user releases all keys + while (task.IsCompleted is false) + { + var dataObject = Clipboard.GetDataObject(); + if (dataObject?.GetData(DataFormats.Text) is string text) + { + logger.LogInformation("Early exit from WaitUntilClipboardIsFilled because we got some text from the clipboard (after {Time} ms)", watch.Elapsed.TotalMilliseconds); + return text; + } + + await Task.Delay(10); + } + + logger.LogInformation("Sadly no text available in clipboard (after {Time} ms)", watch.Elapsed.TotalMilliseconds); + return null; + } } diff --git a/src/ChatPrisma/Themes/ContextMenuStyles.xaml b/src/ChatPrisma/Themes/ContextMenuStyles.xaml index 4e2fbf4..ec648da 100644 --- a/src/ChatPrisma/Themes/ContextMenuStyles.xaml +++ b/src/ChatPrisma/Themes/ContextMenuStyles.xaml @@ -70,7 +70,7 @@ - + diff --git a/src/ChatPrisma/Themes/TextBoxStyles.xaml b/src/ChatPrisma/Themes/TextBoxStyles.xaml index 5d11983..9f71658 100644 --- a/src/ChatPrisma/Themes/TextBoxStyles.xaml +++ b/src/ChatPrisma/Themes/TextBoxStyles.xaml @@ -1,11 +1,33 @@ + xmlns:themes="clr-namespace:ChatPrisma.Themes" + xmlns:wpf="clr-namespace:FluentIcons.WPF;assembly=FluentIcons.WPF">