1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 21:38:38 +00:00

UI - Fix hotkeybox keys being mangled up

This commit is contained in:
Robert 2023-04-08 18:56:42 +02:00
parent 2fcc6d7862
commit 0107bfdd24
4 changed files with 45 additions and 16 deletions

View File

@ -8,6 +8,8 @@ using Avalonia.Data;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using DryIoc;
using FluentAvalonia.Core; using FluentAvalonia.Core;
using Humanizer; using Humanizer;
using Material.Icons; using Material.Icons;
@ -19,6 +21,7 @@ namespace Artemis.UI.Shared;
/// </summary> /// </summary>
public class HotkeyBox : UserControl public class HotkeyBox : UserControl
{ {
private readonly IInputService _inputService;
private readonly TextBox _displayTextBox; private readonly TextBox _displayTextBox;
/// <summary> /// <summary>
@ -26,39 +29,50 @@ public class HotkeyBox : UserControl
/// </summary> /// </summary>
public HotkeyBox() public HotkeyBox()
{ {
InitializeComponent(); _inputService = UI.Locator.Resolve<IInputService>();
InitializeComponent();
_displayTextBox = this.Find<TextBox>("DisplayTextBox"); _displayTextBox = this.Find<TextBox>("DisplayTextBox");
_displayTextBox.KeyDown += DisplayTextBoxOnKeyDown;
_displayTextBox.KeyUp += DisplayTextBoxOnKeyUp;
UpdateDisplayTextBox(); UpdateDisplayTextBox();
} }
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
_inputService.KeyboardKeyDown += InputServiceOnKeyboardKeyDown;
_inputService.KeyboardKeyUp += InputServiceOnKeyboardKeyUp;
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
_inputService.KeyboardKeyDown -= InputServiceOnKeyboardKeyDown;
_inputService.KeyboardKeyUp -= InputServiceOnKeyboardKeyUp;
}
private static void HotkeyChanging(IAvaloniaObject sender, bool before) private static void HotkeyChanging(IAvaloniaObject sender, bool before)
{ {
((HotkeyBox) sender).UpdateDisplayTextBox(); ((HotkeyBox) sender).UpdateDisplayTextBox();
} }
private void DisplayTextBoxOnKeyDown(object? sender, KeyEventArgs e) private void InputServiceOnKeyboardKeyDown(object? sender, ArtemisKeyboardKeyEventArgs e)
{ {
if (e.Key >= Key.LeftShift && e.Key <= Key.RightAlt) if (e.Key >= KeyboardKey.LeftShift && e.Key <= KeyboardKey.RightAlt)
return; return;
Hotkey ??= new Hotkey(); Hotkey ??= new Hotkey();
Hotkey.Key = (KeyboardKey?) e.Key; Hotkey.Key = e.Key;
Hotkey.Modifiers = (KeyboardModifierKey?) e.KeyModifiers; Hotkey.Modifiers = e.Modifiers;
UpdateDisplayTextBox();
HotkeyChanged?.Invoke(this, EventArgs.Empty);
e.Handled = true; Dispatcher.UIThread.Post(() =>
{
UpdateDisplayTextBox();
HotkeyChanged?.Invoke(this, EventArgs.Empty);
});
} }
private void DisplayTextBoxOnKeyUp(object? sender, KeyEventArgs e) private void InputServiceOnKeyboardKeyUp(object? sender, ArtemisKeyboardKeyEventArgs e)
{ {
if (e.KeyModifiers == KeyModifiers.None) if (e.Modifiers == KeyboardModifierKey.None)
FocusManager.Instance?.Focus(null); Dispatcher.UIThread.Post(() => FocusManager.Instance?.Focus(null));
e.Handled = true;
} }
private void UpdateDisplayTextBox() private void UpdateDisplayTextBox()

View File

@ -17,5 +17,7 @@ public static class ContainerExtensions
{ {
Assembly artemisShared = typeof(IArtemisSharedUIService).GetAssembly(); Assembly artemisShared = typeof(IArtemisSharedUIService).GetAssembly();
container.RegisterMany(new[] { artemisShared }, type => type.IsAssignableTo<IArtemisSharedUIService>(), Reuse.Singleton); container.RegisterMany(new[] { artemisShared }, type => type.IsAssignableTo<IArtemisSharedUIService>(), Reuse.Singleton);
UI.Locator = container;
} }
} }

View File

@ -0,0 +1,8 @@
using DryIoc;
namespace Artemis.UI.Shared;
internal static class UI
{
public static IContainer Locator { get; set; } = null!;
}

View File

@ -5,7 +5,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.Storage.Entities.General;
using Artemis.Storage.Repositories; using Artemis.Storage.Repositories;
using Artemis.UI.Exceptions; using Artemis.UI.Exceptions;
using Artemis.UI.Shared.Services.MainWindow; using Artemis.UI.Shared.Services.MainWindow;
@ -110,6 +109,9 @@ public class UpdateService : IUpdateService
private async void HandleAutoUpdateEvent(object? sender, EventArgs e) private async void HandleAutoUpdateEvent(object? sender, EventArgs e)
{ {
if (Constants.CurrentVersion == "local")
return;
// The event can trigger from multiple sources with a timer acting as a fallback, only actual perform an action once per max 59 minutes // The event can trigger from multiple sources with a timer acting as a fallback, only actual perform an action once per max 59 minutes
if (DateTime.UtcNow - _lastAutoUpdateCheck < TimeSpan.FromMinutes(59)) if (DateTime.UtcNow - _lastAutoUpdateCheck < TimeSpan.FromMinutes(59))
return; return;
@ -204,6 +206,9 @@ public class UpdateService : IUpdateService
/// <inheritdoc /> /// <inheritdoc />
public bool Initialize() public bool Initialize()
{ {
if (Constants.CurrentVersion == "local")
return false;
string? channelArgument = Constants.StartupArguments.FirstOrDefault(a => a.StartsWith("--channel=")); string? channelArgument = Constants.StartupArguments.FirstOrDefault(a => a.StartsWith("--channel="));
if (channelArgument != null) if (channelArgument != null)
Channel = channelArgument.Split("=")[1]; Channel = channelArgument.Split("=")[1];