Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more new features #10

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
o.ApplicationName = "Chat Prisma";
o.ApplicationVersion = Version.Parse(ThisAssembly.AssemblyVersion).ToString(3); // Remove the last 0 from the version number
o.CommitId = ThisAssembly.GitCommitId;
o.IsPublicVersion = ThisAssembly.IsPublicRelease;
o.ContactName = "Daniel Häfele";
o.ContactEmailAddress = "[email protected]";
})
Expand All @@ -120,6 +121,15 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
.Bind(context.Configuration.GetSection("Updater"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<KeyboardOptions>()
.Configure(o =>
{
o.Key = "Y";
o.KeyModifiers = "Ctrl+Shift+Alt";
})
.Bind(context.Configuration.GetSection("Keyboard"))
.ValidateDataAnnotations()
.ValidateOnStart();

// Services
services.AddSingleton<IKeyboardHooks, GlobalKeyInterceptorKeyboardHooks>();
Expand Down
1 change: 1 addition & 0 deletions src/ChatPrisma/Options/ApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ApplicationOptions
public string ApplicationVersion { get; set; } = default!;
[Required]
public string CommitId { get; set; } = default!;
public bool IsPublicVersion { get; set; } = false;
[Required]
public string ContactName { get; set; } = default!;
[Required]
Expand Down
12 changes: 12 additions & 0 deletions src/ChatPrisma/Options/KeyboardOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace ChatPrisma.Options
{
public class KeyboardOptions
{
[Required]
public string Key { get; set; } = default!;
[Required]
public string KeyModifiers { get; set; } = default!;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using ChatPrisma.Options;
using GlobalKeyInterceptor;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Shortcut = GlobalKeyInterceptor.Shortcut;

namespace ChatPrisma.Services.KeyboardHooks;

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

private KeyInterceptor? _interceptor;
public Task StartAsync()
{
var (key, keyModifiers) = ParseKey();

var shortcuts = new[]
{
new Shortcut(Key.Y, KeyModifier.Ctrl | KeyModifier.Shift | KeyModifier.Alt, "CTRL + SHIFT + ALT + Y"),
new Shortcut(key, keyModifiers, "Prisma Shortcut"),
};

this._interceptor = new KeyInterceptor(shortcuts);
Expand All @@ -37,6 +41,31 @@ private void OnShortcutPressed(object? sender, ShortcutPressedEventArgs e)
{
logger.LogTrace("Shortcut pressed: {Shortcut}", e.Shortcut.Name);
this.CombinationPressed?.Invoke(this, EventArgs.Empty);

e.IsHandled = true;
}

private (Key, KeyModifier) ParseKey()
{
try
{
var key = keyboardOptions.Value.Key;
var keyModifiers = keyboardOptions.Value.KeyModifiers;

var parsedKey = Enum.Parse<Key>(key, ignoreCase: true);

var parsedKeyModifiers = keyModifiers
.Split('+')
.Select(f => f.Trim())
.Select(f => Enum.Parse<KeyModifier>(f, ignoreCase: true))
.Aggregate((x, y) => x | y);

return (parsedKey, parsedKeyModifiers);
}
catch (Exception)
{
throw new PrismaException("Could not parse keyboard shortcut.");
}
}

public void Dispose()
Expand Down
1 change: 1 addition & 0 deletions src/ChatPrisma/Views/About/AboutView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</TextBlock>
</Run.ToolTip>
</Run>
<Run Foreground="Red" Text="{DXBinding 'IsPublicVersion ? `` : `Dev`', Mode=OneWay}" />
</TextBlock>
</StackPanel>
</StackPanel>
Expand Down
3 changes: 3 additions & 0 deletions src/ChatPrisma/Views/About/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public partial class AboutViewModel(IOptionsMonitor<ApplicationOptions> options)
[ObservableProperty]
private string _contactEmailAddress = options.CurrentValue.ContactEmailAddress;

[ObservableProperty]
private bool _isPublicVersion = options.CurrentValue.IsPublicVersion;

[ObservableProperty]
private ObservableCollection<ThirdPartyLibrary> _thirdPartyLibraries = new()
{
Expand Down
4 changes: 4 additions & 0 deletions src/ChatPrisma/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"CheckForUpdatesInBackground": true,
"MinutesBetweenUpdateChecks": 10
},
"Keyboard": {
"Key": "Y",
"KeyModifiers": "Ctrl+Shift+Alt",
},
"NLog": {
"throwConfigExceptions": true,
"targets": {
Expand Down