From 949106e470879b72a0bf90f7f2705728806c264f Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 14 Apr 2023 11:55:53 +0200 Subject: [PATCH] Plugins - Added help pages --- src/Artemis.Core/Plugins/PluginInfo.cs | 3 + .../Plugins/PluginInfoHelpPage.cs | 26 ++++++++ .../Services/PluginManagementService.cs | 3 + .../Help/MarkdownPluginHelpPageView.axaml | 14 +---- .../Help/MarkdownPluginHelpPageView.axaml.cs | 3 +- .../Help/MarkdownPluginHelpPageViewModel.cs | 39 +++++++++--- .../Plugins/Help/PluginHelpWindowView.axaml | 6 +- .../Help/PluginHelpWindowView.axaml.cs | 3 +- .../Screens/Plugins/PluginView.axaml | 2 +- .../Screens/Plugins/PluginViewModel.cs | 3 - .../Settings/Updating/ReleaseView.axaml | 15 +---- src/Artemis.UI/Styles/Markdown.axaml | 60 +++++++++++++++++++ 12 files changed, 134 insertions(+), 43 deletions(-) create mode 100644 src/Artemis.Core/Plugins/PluginInfoHelpPage.cs create mode 100644 src/Artemis.UI/Styles/Markdown.axaml diff --git a/src/Artemis.Core/Plugins/PluginInfo.cs b/src/Artemis.Core/Plugins/PluginInfo.cs index 6421749f8..c571518c7 100644 --- a/src/Artemis.Core/Plugins/PluginInfo.cs +++ b/src/Artemis.Core/Plugins/PluginInfo.cs @@ -188,6 +188,9 @@ public class PluginInfo : CorePropertyChanged, IPrerequisitesSubject } } + [JsonProperty] + internal List HelpPages { get; set; } = new(); + /// /// Gets a boolean indicating whether this plugin is compatible with the current operating system and API version /// diff --git a/src/Artemis.Core/Plugins/PluginInfoHelpPage.cs b/src/Artemis.Core/Plugins/PluginInfoHelpPage.cs new file mode 100644 index 000000000..8f63c1623 --- /dev/null +++ b/src/Artemis.Core/Plugins/PluginInfoHelpPage.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; + +namespace Artemis.Core; + +/// +/// Represents basic info about a plugin and contains a reference to the instance of said plugin +/// +internal class PluginInfoHelpPage +{ + [JsonConstructor] + public PluginInfoHelpPage(string title, string markdownFile, string id) + { + Title = title; + MarkdownFile = markdownFile; + Id = id; + } + + [JsonProperty(Required = Required.Always)] + public string Title { get; } + + [JsonProperty(Required = Required.Always)] + public string MarkdownFile { get; } + + [JsonProperty(Required = Required.Always)] + public string Id { get; } +} \ No newline at end of file diff --git a/src/Artemis.Core/Services/PluginManagementService.cs b/src/Artemis.Core/Services/PluginManagementService.cs index c03aec7bb..e8a7317d3 100644 --- a/src/Artemis.Core/Services/PluginManagementService.cs +++ b/src/Artemis.Core/Services/PluginManagementService.cs @@ -350,6 +350,9 @@ internal class PluginManagementService : IPluginManagementService // Load the entity and fall back on creating a new one Plugin plugin = new(pluginInfo, directory, _pluginRepository.GetPluginByGuid(pluginInfo.Guid)); + foreach (PluginInfoHelpPage pluginInfoHelpPage in pluginInfo.HelpPages) + plugin.HelpPages.Add(new MarkdownPluginHelpPage(plugin, pluginInfoHelpPage.Title, pluginInfoHelpPage.Id, pluginInfoHelpPage.MarkdownFile)); + OnPluginLoading(new PluginEventArgs(plugin)); // Locate the main assembly entry diff --git a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml index f090b7f6f..e6441425b 100644 --- a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml +++ b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml @@ -3,25 +3,13 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia" - xmlns:plugins="clr-namespace:Artemis.UI.Screens.Plugins" - xmlns:ctxt="clr-namespace:ColorTextBlock.Avalonia;assembly=ColorTextBlock.Avalonia" xmlns:help="clr-namespace:Artemis.UI.Screens.Plugins.Help" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Artemis.UI.Screens.Plugins.Help.MarkdownPluginHelpPageView" x:DataType="help:MarkdownPluginHelpPageViewModel"> - - - + diff --git a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml.cs b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml.cs index dde34b97d..a151841c2 100644 --- a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml.cs +++ b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageView.axaml.cs @@ -1,9 +1,10 @@ using Avalonia.Controls; using Avalonia.Markup.Xaml; +using Avalonia.ReactiveUI; namespace Artemis.UI.Screens.Plugins.Help; -public partial class MarkdownPluginHelpPageView : UserControl +public partial class MarkdownPluginHelpPageView : ReactiveUserControl { public MarkdownPluginHelpPageView() { diff --git a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageViewModel.cs b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageViewModel.cs index 9f647b405..69dee5d11 100644 --- a/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageViewModel.cs +++ b/src/Artemis.UI/Screens/Plugins/Help/MarkdownPluginHelpPageViewModel.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Reactive.Disposables; using System.Threading.Tasks; @@ -9,13 +10,23 @@ namespace Artemis.UI.Screens.Plugins.Help; public class MarkdownPluginHelpPageViewModel : ActivatableViewModelBase, IPluginHelpPage { - private string? _markdownText; private readonly MarkdownPluginHelpPage _helpPage; + private string? _markdownText; public MarkdownPluginHelpPageViewModel(MarkdownPluginHelpPage helpPage) { _helpPage = helpPage; - this.WhenActivated(d => Load().DisposeWith(d)); + this.WhenActivated(d => + { + FileSystemWatcher watcher = new(); + watcher.Path = Path.GetDirectoryName(_helpPage.MarkdownFile) ?? throw new InvalidOperationException($"Path \"{_helpPage.MarkdownFile}\" does not contain a directory"); + watcher.Filter = Path.GetFileName(_helpPage.MarkdownFile); + watcher.EnableRaisingEvents = true; + watcher.Changed += WatcherOnChanged; + watcher.DisposeWith(d); + + LoadMarkdown().DisposeWith(d); + }); } public string? MarkdownText @@ -24,6 +35,25 @@ public class MarkdownPluginHelpPageViewModel : ActivatableViewModelBase, IPlugin set => RaiseAndSetIfChanged(ref _markdownText, value); } + private async void WatcherOnChanged(object sender, FileSystemEventArgs e) + { + await LoadMarkdown(); + } + + private async Task LoadMarkdown() + { + try + { + await using FileStream stream = new(_helpPage.MarkdownFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using StreamReader reader = new(stream); + MarkdownText = await reader.ReadToEndAsync(); + } + catch (Exception e) + { + MarkdownText = e.Message; + } + } + /// public Plugin Plugin => _helpPage.Plugin; @@ -32,9 +62,4 @@ public class MarkdownPluginHelpPageViewModel : ActivatableViewModelBase, IPlugin /// public string Id => _helpPage.Id; - - private async Task Load() - { - MarkdownText ??= await File.ReadAllTextAsync(_helpPage.MarkdownFile); - } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml b/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml index 49b9551ac..0935be744 100644 --- a/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml +++ b/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml @@ -10,7 +10,7 @@ x:DataType="help:PluginHelpWindowViewModel" Icon="/Assets/Images/Logo/application.ico" Title="{CompiledBinding DisplayName}" - Width="800" + Width="1200" Height="800" WindowStartupLocation="CenterOwner"> @@ -18,7 +18,7 @@ - + @@ -28,4 +28,4 @@ - + \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml.cs b/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml.cs index 0ccc2a611..c821716bb 100644 --- a/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml.cs +++ b/src/Artemis.UI/Screens/Plugins/Help/PluginHelpWindowView.axaml.cs @@ -1,10 +1,11 @@ +using Artemis.UI.Shared; using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; namespace Artemis.UI.Screens.Plugins.Help; -public partial class PluginHelpWindowView : Window +public partial class PluginHelpWindowView : ReactiveAppWindow { public PluginHelpWindowView() { diff --git a/src/Artemis.UI/Screens/Plugins/PluginView.axaml b/src/Artemis.UI/Screens/Plugins/PluginView.axaml index cae9c18ff..605a44a59 100644 --- a/src/Artemis.UI/Screens/Plugins/PluginView.axaml +++ b/src/Artemis.UI/Screens/Plugins/PluginView.axaml @@ -95,7 +95,7 @@ IsVisible="{CompiledBinding HasHelpPages}" Command="{CompiledBinding OpenHelp}" ToolTip.Tip="View help pages"> - + - - - + diff --git a/src/Artemis.UI/Styles/Markdown.axaml b/src/Artemis.UI/Styles/Markdown.axaml new file mode 100644 index 000000000..5374e71b0 --- /dev/null +++ b/src/Artemis.UI/Styles/Markdown.axaml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + \ No newline at end of file