From 928390110ef13fc08f79c0e7e5d04c197da18023 Mon Sep 17 00:00:00 2001 From: Henrik Lau Eriksson Date: Sun, 4 Aug 2024 16:35:07 +0200 Subject: [PATCH] update --- .../GEmojiSharp.PowerToysRun.csproj | 2 +- .../GEmojiSharpSettings.cs | 31 +++++ src/GEmojiSharp.PowerToysRun/Main.cs | 112 ++++++++++++++++-- .../MainTests.cs | 42 +++---- 4 files changed, 156 insertions(+), 31 deletions(-) create mode 100644 src/GEmojiSharp.PowerToysRun/GEmojiSharpSettings.cs diff --git a/src/GEmojiSharp.PowerToysRun/GEmojiSharp.PowerToysRun.csproj b/src/GEmojiSharp.PowerToysRun/GEmojiSharp.PowerToysRun.csproj index f69dd23..060f9c0 100644 --- a/src/GEmojiSharp.PowerToysRun/GEmojiSharp.PowerToysRun.csproj +++ b/src/GEmojiSharp.PowerToysRun/GEmojiSharp.PowerToysRun.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/GEmojiSharp.PowerToysRun/GEmojiSharpSettings.cs b/src/GEmojiSharp.PowerToysRun/GEmojiSharpSettings.cs new file mode 100644 index 0000000..3ab2b2f --- /dev/null +++ b/src/GEmojiSharp.PowerToysRun/GEmojiSharpSettings.cs @@ -0,0 +1,31 @@ +using Community.PowerToys.Run.Plugin.Update; +using Microsoft.PowerToys.Settings.UI.Library; + +namespace GEmojiSharp.PowerToysRun +{ + /// + /// Plugin settings. + /// + public class GEmojiSharpSettings + { + /// + /// Initializes a new instance of the class. + /// + public GEmojiSharpSettings() + { + Update = new PluginUpdateSettings + { + ResultScore = 100, + }; + } + + /// + /// Plugin update settings. + /// + public PluginUpdateSettings Update { get; set; } + + internal IEnumerable GetAdditionalOptions() => Update.GetAdditionalOptions(); + + internal void SetAdditionalOptions(IEnumerable additionalOptions) => Update.SetAdditionalOptions(additionalOptions); + } +} diff --git a/src/GEmojiSharp.PowerToysRun/Main.cs b/src/GEmojiSharp.PowerToysRun/Main.cs index dcd95cc..21c2fb2 100644 --- a/src/GEmojiSharp.PowerToysRun/Main.cs +++ b/src/GEmojiSharp.PowerToysRun/Main.cs @@ -1,16 +1,44 @@ using System.Text.RegularExpressions; using System.Windows; +using System.Windows.Controls; using System.Windows.Input; +using Community.PowerToys.Run.Plugin.Update; using ManagedCommon; +using Microsoft.PowerToys.Settings.UI.Library; +using Wox.Infrastructure.Storage; using Wox.Plugin; +using Wox.Plugin.Logger; namespace GEmojiSharp.PowerToysRun { /// /// Main class of this plugin that implement all used interfaces. /// - public class Main : IPlugin, IContextMenu, IDisposable + public class Main : IPlugin, IContextMenu, ISettingProvider, ISavable, IDisposable { + /// + /// Initializes a new instance of the class. + /// + public Main() + { + Storage = new PluginJsonStorage(); + Settings = Storage.Load(); + Updater = new PluginUpdateHandler(Settings.Update); + Updater.UpdateInstalling += OnUpdateInstalling; + Updater.UpdateInstalled += OnUpdateInstalled; + Updater.UpdateSkipped += OnUpdateSkipped; + } + + internal Main(GEmojiSharpSettings settings) + { + Storage = new PluginJsonStorage(); + Settings = settings; + Updater = new PluginUpdateHandler(Settings.Update); + Updater.UpdateInstalling += OnUpdateInstalling; + Updater.UpdateInstalled += OnUpdateInstalled; + Updater.UpdateSkipped += OnUpdateSkipped; + } + /// /// ID of the plugin. /// @@ -26,6 +54,17 @@ public class Main : IPlugin, IContextMenu, IDisposable /// public string Description => "GitHub Emoji PowerToys Run plugin"; + /// + /// Additional options for the plugin. + /// + public IEnumerable AdditionalOptions => Settings.GetAdditionalOptions(); + + private PluginJsonStorage Storage { get; } + + private GEmojiSharpSettings Settings { get; } + + private PluginUpdateHandler Updater { get; } + private PluginInitContext? Context { get; set; } private string? IconPath { get; set; } @@ -39,27 +78,34 @@ public class Main : IPlugin, IContextMenu, IDisposable /// A filtered list, can be empty when nothing was found. public List Query(Query query) { + var results = new List(); + + if (Updater.IsUpdateAvailable()) + { + results.AddRange(Updater.GetResults()); + } + if (query?.Search is null) { - return new List(0); + return results; } var value = query.Search; if (string.IsNullOrEmpty(value)) { - return Emoji.All.Select(GetResult).ToList(); + results.AddRange(Emoji.All.Select(GetResult)); + return results; } var emojis = (GEmoji[])Emoji.Find(value); if (emojis.Length != 0) { - return emojis.Select(GetResult).ToList(); + results.AddRange(emojis.Select(GetResult)); + return results; } - var results = new List(); - if (HasAlias(value)) { var result = Emoji.Emojify(value); @@ -127,6 +173,8 @@ public void Init(PluginInitContext context) Context = context ?? throw new ArgumentNullException(nameof(context)); Context.API.ThemeChanged += OnThemeChanged; UpdateIconPath(Context.API.GetCurrentTheme()); + + Updater.Init(Context); } /// @@ -136,6 +184,12 @@ public void Init(PluginInitContext context) /// A list context menu entries. public List LoadContextMenus(Result selectedResult) { + var results = Updater.GetContextMenuResults(selectedResult); + if (results.Count != 0) + { + return results; + } + if (selectedResult?.ContextData is GEmoji emoji) { var raw = new ContextMenuResult @@ -216,9 +270,33 @@ public List LoadContextMenus(Result selectedResult) ]; } - return new List(0); + return []; + } + + /// + /// Creates setting panel. + /// + /// The control. + /// method is not implemented. + public Control CreateSettingPanel() => throw new NotImplementedException(); + + /// + /// Updates settings. + /// + /// The plugin settings. + public void UpdateSettings(PowerLauncherPluginSettings settings) + { + ArgumentNullException.ThrowIfNull(settings); + + Settings.SetAdditionalOptions(settings.AdditionalOptions); + Save(); } + /// + /// Saves settings. + /// + public void Save() => Storage.Save(); + /// public void Dispose() { @@ -242,6 +320,8 @@ protected virtual void Dispose(bool disposing) Context.API.ThemeChanged -= OnThemeChanged; } + Updater.Dispose(); + Disposed = true; } @@ -254,6 +334,24 @@ private static bool CopyToClipboard(string value) private void UpdateIconPath(Theme theme) => IconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? "Images/gemojisharp.light.png" : "Images/gemojisharp.dark.png"; private void OnThemeChanged(Theme currentTheme, Theme newTheme) => UpdateIconPath(newTheme); + + private void OnUpdateInstalling(object? sender, PluginUpdateEventArgs e) + { + Log.Info("UpdateInstalling: " + e.Version, GetType()); + } + + private void OnUpdateInstalled(object? sender, PluginUpdateEventArgs e) + { + Log.Info("UpdateInstalled: " + e.Version, GetType()); + Context!.API.ShowNotification($"{Name} {e.Version}", "Update installed"); + } + + private void OnUpdateSkipped(object? sender, PluginUpdateEventArgs e) + { + Log.Info("UpdateSkipped: " + e.Version, GetType()); + Save(); + Context?.API.ChangeQuery(Context.CurrentPluginMetadata.ActionKeyword, true); + } } internal record EmojifiedString(string Value); diff --git a/tests/GEmojiSharp.PowerToysRun.Tests/MainTests.cs b/tests/GEmojiSharp.PowerToysRun.Tests/MainTests.cs index 76df3ba..2602e80 100644 --- a/tests/GEmojiSharp.PowerToysRun.Tests/MainTests.cs +++ b/tests/GEmojiSharp.PowerToysRun.Tests/MainTests.cs @@ -7,53 +7,53 @@ namespace GEmojiSharp.Tests.PowerToysRun { public class MainTests { + private Main _subject = null!; + + [SetUp] + public void SetUp() + { + _subject = new Main(new GEmojiSharpSettings()); + } + [Test] public void Query_emojis() { - var subject = new Main(); - - subject.Query(null!).Should().BeEmpty(); + _subject.Query(null!).Should().BeEmpty(); - subject.Query(new Query("")).Should().NotBeEmpty(); + _subject.Query(new Query("")).Should().NotBeEmpty(); - subject.Query(new Query("globe showing")).Should() + _subject.Query(new Query("globe showing")).Should() .Contain(x => x.Title == "🌍") .And.Contain(x => x.Title == "🌎") .And.Contain(x => x.Title == "🌏"); - subject.Query(new Query("tada")).Should().ContainSingle(x => x.Title == "🎉"); + _subject.Query(new Query("tada")).Should().ContainSingle(x => x.Title == "🎉"); } [Test] public void Query_Emojify() { - var subject = new Main(); - - subject.Query(new Query("Hello, :earth_africa:")).Should().ContainSingle(x => x.Title == "Hello, 🌍"); + _subject.Query(new Query("Hello, :earth_africa:")).Should().ContainSingle(x => x.Title == "Hello, 🌍"); } [Test] public void Query_Demojify() { - var subject = new Main(); - - subject.Query(new Query("Hello, 🌍")).Should().ContainSingle(x => x.Title == "Hello, :earth_africa:"); + _subject.Query(new Query("Hello, 🌍")).Should().ContainSingle(x => x.Title == "Hello, :earth_africa:"); } [Test] public void LoadContextMenus_GEmoji() { - var subject = new Main(); - - subject.LoadContextMenus(new Result()).Should().BeEmpty(); + _subject.LoadContextMenus(new Result()).Should().BeEmpty(); var result = new Result { ContextData = Emoji.Get("tada") }; - subject.LoadContextMenus(result).Should() + _subject.LoadContextMenus(result).Should() .Contain(x => x.Title == "Copy raw emoji (Enter)") .And.Contain(x => x.Title == "Copy emoji aliases (Ctrl+C)"); result = new Result { ContextData = Emoji.Get("wave") }; - subject.LoadContextMenus(result).Should() + _subject.LoadContextMenus(result).Should() .Contain(x => x.Title == "Copy raw emoji (Enter)") .And.Contain(x => x.Title == "Copy emoji aliases (Ctrl+C)") .And.Contain(x => x.Title == "Copy raw emoji skin tone variants (Ctrl+Enter)"); @@ -62,19 +62,15 @@ public void LoadContextMenus_GEmoji() [Test] public void LoadContextMenus_EmojifiedString() { - var subject = new Main(); var result = new Result { ContextData = new EmojifiedString("Hello, 🌍") }; - - subject.LoadContextMenus(result).Should().Contain(x => x.Title == "Copy emojified text (Enter)"); + _subject.LoadContextMenus(result).Should().Contain(x => x.Title == "Copy emojified text (Enter)"); } [Test] public void LoadContextMenus_DemojifiedString() { - var subject = new Main(); var result = new Result { ContextData = new DemojifiedString("Hello, :earth_africa:") }; - - subject.LoadContextMenus(result).Should().Contain(x => x.Title == "Copy demojified text (Enter)"); + _subject.LoadContextMenus(result).Should().Contain(x => x.Title == "Copy demojified text (Enter)"); } } }