From 2fcc6d786241c4eb5d2c658da047cd1b50646eee Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 8 Apr 2023 13:13:58 +0200 Subject: [PATCH 1/3] Plugins - Added constructors to PluginException to provide a help document --- .../Exceptions/ArtemisPluginException.cs | 28 +++++++++++++++++-- .../Interfaces/IPluginManagementService.cs | 7 +++++ .../Services/PluginManagementService.cs | 14 +++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Artemis.Core/Exceptions/ArtemisPluginException.cs b/src/Artemis.Core/Exceptions/ArtemisPluginException.cs index 8e49b0e19..53f9fd30f 100644 --- a/src/Artemis.Core/Exceptions/ArtemisPluginException.cs +++ b/src/Artemis.Core/Exceptions/ArtemisPluginException.cs @@ -10,7 +10,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin) + internal ArtemisPluginException(Plugin plugin) { Plugin = plugin; } @@ -18,7 +18,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin, string message) : base(message) + internal ArtemisPluginException(Plugin plugin, string message) : base(message) { Plugin = plugin; } @@ -26,7 +26,7 @@ public class ArtemisPluginException : Exception /// /// Creates a new instance of the class /// - public ArtemisPluginException(Plugin plugin, string message, Exception inner) : base(message, inner) + internal ArtemisPluginException(Plugin plugin, string message, Exception inner) : base(message, inner) { Plugin = plugin; } @@ -44,9 +44,31 @@ public class ArtemisPluginException : Exception public ArtemisPluginException(string message, Exception inner) : base(message, inner) { } + + /// + /// Creates a new instance of the class + /// + public ArtemisPluginException(string message, string helpDocument) : base(message) + { + HelpDocument = helpDocument; + } + + /// + /// Creates a new instance of the class + /// + public ArtemisPluginException(string message, Exception inner, string helpDocument) : base(message, inner) + { + HelpDocument = helpDocument; + } /// /// Gets the plugin the error is related to /// public Plugin? Plugin { get; } + + /// + /// Gets or sets the help document related to this exception. + /// + public string? HelpDocument { get; } + } \ No newline at end of file diff --git a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs index 209158529..8e0faf564 100644 --- a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs +++ b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs @@ -127,6 +127,13 @@ public interface IPluginManagementService : IArtemisService, IDisposable /// If the current call stack contains a plugin, the plugin. Otherwise null Plugin? GetCallingPlugin(); + /// + /// Returns the plugin that threw the provided exception. + /// + /// + /// If the exception was thrown by a plugin, the plugin. Otherwise null + Plugin? GetPluginFromException(Exception exception); + /// /// Gets the plugin that defined the specified device /// diff --git a/src/Artemis.Core/Services/PluginManagementService.cs b/src/Artemis.Core/Services/PluginManagementService.cs index 06b9b6368..c03aec7bb 100644 --- a/src/Artemis.Core/Services/PluginManagementService.cs +++ b/src/Artemis.Core/Services/PluginManagementService.cs @@ -192,7 +192,19 @@ internal class PluginManagementService : IPluginManagementService public Plugin? GetCallingPlugin() { - StackTrace stackTrace = new(); // get call stack + return GetPluginFromStackTrace(new StackTrace()); + } + + public Plugin? GetPluginFromException(Exception exception) + { + if (exception is ArtemisPluginException pluginException && pluginException.Plugin != null) + return pluginException.Plugin; + + return GetPluginFromStackTrace(new StackTrace(exception)); + } + + private Plugin? GetPluginFromStackTrace(StackTrace stackTrace) + { StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames) foreach (StackFrame stackFrame in stackFrames) From 0107bfdd24b31f9202eed605574e83de9746239a Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 8 Apr 2023 18:56:42 +0200 Subject: [PATCH 2/3] UI - Fix hotkeybox keys being mangled up --- .../Controls/HotkeyBox.axaml.cs | 44 ++++++++++++------- .../DryIoc/ContainerExtensions.cs | 2 + src/Artemis.UI.Shared/Utilities.cs | 8 ++++ .../Services/Updating/UpdateService.cs | 7 ++- 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 src/Artemis.UI.Shared/Utilities.cs diff --git a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs index 0c50e69e4..9bfbe96b4 100644 --- a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs +++ b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs @@ -8,6 +8,8 @@ using Avalonia.Data; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.Threading; +using DryIoc; using FluentAvalonia.Core; using Humanizer; using Material.Icons; @@ -19,6 +21,7 @@ namespace Artemis.UI.Shared; /// public class HotkeyBox : UserControl { + private readonly IInputService _inputService; private readonly TextBox _displayTextBox; /// @@ -26,39 +29,50 @@ public class HotkeyBox : UserControl /// public HotkeyBox() { - InitializeComponent(); + _inputService = UI.Locator.Resolve(); + InitializeComponent(); _displayTextBox = this.Find("DisplayTextBox"); - _displayTextBox.KeyDown += DisplayTextBoxOnKeyDown; - _displayTextBox.KeyUp += DisplayTextBoxOnKeyUp; 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) { ((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; Hotkey ??= new Hotkey(); - Hotkey.Key = (KeyboardKey?) e.Key; - Hotkey.Modifiers = (KeyboardModifierKey?) e.KeyModifiers; - UpdateDisplayTextBox(); - HotkeyChanged?.Invoke(this, EventArgs.Empty); + Hotkey.Key = e.Key; + Hotkey.Modifiers = e.Modifiers; - 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) - FocusManager.Instance?.Focus(null); - - e.Handled = true; + if (e.Modifiers == KeyboardModifierKey.None) + Dispatcher.UIThread.Post(() => FocusManager.Instance?.Focus(null)); } private void UpdateDisplayTextBox() diff --git a/src/Artemis.UI.Shared/DryIoc/ContainerExtensions.cs b/src/Artemis.UI.Shared/DryIoc/ContainerExtensions.cs index 4c4711b02..1c86ecf84 100644 --- a/src/Artemis.UI.Shared/DryIoc/ContainerExtensions.cs +++ b/src/Artemis.UI.Shared/DryIoc/ContainerExtensions.cs @@ -17,5 +17,7 @@ public static class ContainerExtensions { Assembly artemisShared = typeof(IArtemisSharedUIService).GetAssembly(); container.RegisterMany(new[] { artemisShared }, type => type.IsAssignableTo(), Reuse.Singleton); + + UI.Locator = container; } } \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Utilities.cs b/src/Artemis.UI.Shared/Utilities.cs new file mode 100644 index 000000000..35abef7db --- /dev/null +++ b/src/Artemis.UI.Shared/Utilities.cs @@ -0,0 +1,8 @@ +using DryIoc; + +namespace Artemis.UI.Shared; + +internal static class UI +{ + public static IContainer Locator { get; set; } = null!; +} \ No newline at end of file diff --git a/src/Artemis.UI/Services/Updating/UpdateService.cs b/src/Artemis.UI/Services/Updating/UpdateService.cs index fee0b93b0..15e66985e 100644 --- a/src/Artemis.UI/Services/Updating/UpdateService.cs +++ b/src/Artemis.UI/Services/Updating/UpdateService.cs @@ -5,7 +5,6 @@ using System.Threading; using System.Threading.Tasks; using Artemis.Core; using Artemis.Core.Services; -using Artemis.Storage.Entities.General; using Artemis.Storage.Repositories; using Artemis.UI.Exceptions; using Artemis.UI.Shared.Services.MainWindow; @@ -110,6 +109,9 @@ public class UpdateService : IUpdateService 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 if (DateTime.UtcNow - _lastAutoUpdateCheck < TimeSpan.FromMinutes(59)) return; @@ -204,6 +206,9 @@ public class UpdateService : IUpdateService /// public bool Initialize() { + if (Constants.CurrentVersion == "local") + return false; + string? channelArgument = Constants.StartupArguments.FirstOrDefault(a => a.StartsWith("--channel=")); if (channelArgument != null) Channel = channelArgument.Split("=")[1]; From 1f0ec791cfe397b6ddbbcb7fa1dc4ca2f4ab8ef0 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Mon, 10 Apr 2023 10:36:24 +0100 Subject: [PATCH 3/3] Ui - Fixed every hotkey in the same tree receiving input --- src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs index 9bfbe96b4..dfa5bd38c 100644 --- a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs +++ b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs @@ -36,16 +36,20 @@ public class HotkeyBox : UserControl UpdateDisplayTextBox(); } - protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + protected override void OnGotFocus(GotFocusEventArgs e) { _inputService.KeyboardKeyDown += InputServiceOnKeyboardKeyDown; _inputService.KeyboardKeyUp += InputServiceOnKeyboardKeyUp; + + base.OnGotFocus(e); } - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + protected override void OnLostFocus(RoutedEventArgs e) { _inputService.KeyboardKeyDown -= InputServiceOnKeyboardKeyDown; _inputService.KeyboardKeyUp -= InputServiceOnKeyboardKeyUp; + + base.OnLostFocus(e); } private static void HotkeyChanging(IAvaloniaObject sender, bool before)