From e8de6fe9a5d0fbb35ff5b4265835d9a0128c0ebe Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:54:29 +0100 Subject: [PATCH] Rename Plugin & API namespaces and implement saving/loading of setting --- src/DesktopMagic.sln | 8 +-- .../BuiltInWindowElements/DatePlugin.cs | 11 ++-- .../MusicVisualizerPlugin.cs | 24 ++++----- .../BuiltInWindowElements/TimePlugin.cs | 11 ++-- src/DesktopMagic/DesktopMagic.csproj | 2 +- .../Helpers/SettingElementGenerator.cs | 22 ++++---- src/DesktopMagic/MainWindow.xaml | 2 +- src/DesktopMagic/MainWindow.xaml.cs | 30 ++++++----- src/DesktopMagic/Plugins/InputElement.cs | 28 ---------- src/DesktopMagic/Plugins/PluginData.cs | 5 +- src/DesktopMagic/Plugins/PluginWindow.xaml.cs | 22 +++++--- src/DesktopMagic/Plugins/SettingElement.cs | 51 +++++++++++++++++++ src/DesktopMagic/Plugins/Theme.cs | 5 +- .../Resources/StringResources.de.xaml | 1 + .../Resources/StringResources.en.xaml | 1 + src/DesktopMagic/Settings/PluginSettings.cs | 4 +- ....csproj => DesktopMagic.PluginTest.csproj} | 2 +- src/DesktopMagicPlugin.Test/PluginScript.cs | 13 +++-- ...uginAPI.csproj => DesktopMagic.Api.csproj} | 1 + .../Drawing/FontComparer.cs | 2 +- .../Drawing/GraphicsExtentions.cs | 2 +- .../Drawing/RenderQuality.cs | 2 +- src/DesktopMagicPluginAPI/IPluginData.cs | 2 +- src/DesktopMagicPluginAPI/ITheme.cs | 2 +- src/DesktopMagicPluginAPI/Plugin.cs | 8 +-- src/DesktopMagicPluginAPI/Settings/Button.cs | 2 +- .../Settings/CheckBox.cs | 13 ++++- .../Settings/ComboBox.cs | 28 ++++++++-- .../Settings/IntegerUpDown.cs | 13 ++++- src/DesktopMagicPluginAPI/Settings/Label.cs | 4 +- .../Settings/MouseButton.cs | 2 +- src/DesktopMagicPluginAPI/Settings/Setting.cs | 15 +++++- .../Settings/SettingAttribute.cs | 22 +++++--- src/DesktopMagicPluginAPI/Settings/Slider.cs | 13 ++++- src/DesktopMagicPluginAPI/Settings/TextBox.cs | 12 ++++- 35 files changed, 247 insertions(+), 138 deletions(-) delete mode 100644 src/DesktopMagic/Plugins/InputElement.cs create mode 100644 src/DesktopMagic/Plugins/SettingElement.cs rename src/DesktopMagicPlugin.Test/{DesktopMagicPlugin.Test.csproj => DesktopMagic.PluginTest.csproj} (93%) rename src/DesktopMagicPluginAPI/{DesktopMagicPluginAPI.csproj => DesktopMagic.Api.csproj} (97%) diff --git a/src/DesktopMagic.sln b/src/DesktopMagic.sln index 9bd026d..aa5c869 100644 --- a/src/DesktopMagic.sln +++ b/src/DesktopMagic.sln @@ -1,13 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31205.134 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34309.116 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic", "DesktopMagic\DesktopMagic.csproj", "{B56B4CCC-CF92-4C05-9DEA-57666D81889A}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagicPluginAPI", "DesktopMagicPluginAPI\DesktopMagicPluginAPI.csproj", "{9D193A10-3E8E-447F-89F1-4B316C2EE37D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.Api", "DesktopMagicPluginAPI\DesktopMagic.Api.csproj", "{9D193A10-3E8E-447F-89F1-4B316C2EE37D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagicPlugin.Test", "DesktopMagicPlugin.Test\DesktopMagicPlugin.Test.csproj", "{EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.PluginTest", "DesktopMagicPlugin.Test\DesktopMagic.PluginTest.csproj", "{EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.Installer", "DesktopMagic.Installer\DesktopMagic.Installer.csproj", "{A06BD685-7F92-4799-846B-3EC38345108E}" EndProject diff --git a/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs index b0df29b..00190d0 100644 --- a/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs @@ -1,6 +1,5 @@ -using DesktopMagicPluginAPI; -using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Settings; +using DesktopMagic.Api; +using DesktopMagic.Api.Settings; using System; using System.Drawing; @@ -10,16 +9,16 @@ namespace DesktopMagic.BuiltInWindowElements; internal class DatePlugin : Plugin { - [Setting("Short date")] + [Setting("short-date", "Short date")] private readonly CheckBox shortDatecheckBox = new CheckBox(true); private DateTime oldDateTime = DateTime.MinValue; private Color oldColor = Color.White; - private string oldFont; + private string oldFont = string.Empty; private bool oldShortDatecheckBoxValue; public override int UpdateInterval => 1000; - public override Bitmap Main() + public override Bitmap? Main() { if (oldDateTime.Date == DateTime.Now.Date && oldColor == Application.Theme.PrimaryColor && oldFont == Application.Theme.Font && oldShortDatecheckBoxValue == shortDatecheckBox.Value) { diff --git a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs index 217f047..12eb17c 100644 --- a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs @@ -1,8 +1,6 @@ -using DesktopMagic.Helpers; - -using DesktopMagicPluginAPI; -using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Settings; +using DesktopMagic.Api; +using DesktopMagic.Api.Settings; +using DesktopMagic.Helpers; using NAudio.Wave; @@ -22,22 +20,22 @@ internal class MusicVisualizerPlugin : Plugin private readonly Bitmap output = new Bitmap(880, 300); - [Setting("Mirror")] + [Setting("mirror", "Mirror")] private readonly CheckBox mirrorMode = new CheckBox(false); - [Setting("Line")] + [Setting("line", "Line")] private readonly CheckBox lineMode = new CheckBox(false); - [Setting("Spectrum Mode")] + [Setting("spectrum-mode", "Spectrum Mode")] private readonly ComboBox spectrumMode = new ComboBox("Bottom", "Middle", "Top"); - [Setting("Amplification")] + [Setting("amplification", "Amplification")] private readonly IntegerUpDown amplifierLevel = new IntegerUpDown(-50, 50, 0); - [Setting("Line thickness")] + [Setting("line-thickness", "Line thickness")] private readonly IntegerUpDown lineThickness = new IntegerUpDown(1, 10, 1); - private WasapiLoopbackCapture waveIn; + private WasapiLoopbackCapture waveIn = null!; private volatile bool calculate = true; private List lastFft = []; public override int UpdateInterval => 0; @@ -64,7 +62,7 @@ internal class MusicVisualizerPlugin : Plugin waveIn?.StopRecording(); } - private void OnDataAvailable(object sender, WaveInEventArgs e) + private void OnDataAvailable(object? sender, WaveInEventArgs e) { if (calculate) { @@ -80,7 +78,7 @@ internal class MusicVisualizerPlugin : Plugin } } - private void FftCalculated(object sender, FftEventArgs e) + private void FftCalculated(object? sender, FftEventArgs e) { if (!calculate) { diff --git a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs index 2bf6e75..f6c77fe 100644 --- a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs @@ -1,6 +1,5 @@ -using DesktopMagicPluginAPI; -using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Settings; +using DesktopMagic.Api; +using DesktopMagic.Api.Settings; using System; using System.Drawing; @@ -10,13 +9,13 @@ namespace DesktopMagic.BuiltInWindowElements; internal class TimePlugin : Plugin { - [Setting("Display Seconds")] + [Setting("display-seconds", "Display Seconds")] private readonly CheckBox displaySecondsCheckBox = new CheckBox(true); - public override int UpdateInterval => 1000; - private string? oldFont = null; private float maxWidth = 0; + public override int UpdateInterval => 1000; + public override Bitmap Main() { string time = displaySecondsCheckBox.Value ? DateTime.Now.ToLongTimeString() : DateTime.Now.ToShortTimeString(); diff --git a/src/DesktopMagic/DesktopMagic.csproj b/src/DesktopMagic/DesktopMagic.csproj index b6087ce..20a7f0b 100644 --- a/src/DesktopMagic/DesktopMagic.csproj +++ b/src/DesktopMagic/DesktopMagic.csproj @@ -50,7 +50,7 @@ - + diff --git a/src/DesktopMagic/Helpers/SettingElementGenerator.cs b/src/DesktopMagic/Helpers/SettingElementGenerator.cs index 31591b4..0e85262 100644 --- a/src/DesktopMagic/Helpers/SettingElementGenerator.cs +++ b/src/DesktopMagic/Helpers/SettingElementGenerator.cs @@ -10,11 +10,11 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) { private readonly ComboBox optionsComboBox = optionsComboBox; - public void Generate(InputElement settingElement, DockPanel dockPanel, TextBlock textBlock) + public void Generate(SettingElement settingElement, DockPanel dockPanel, TextBlock textBlock) { dockPanel.UpdateLayout(); textBlock.UpdateLayout(); - if (settingElement.Input is DesktopMagicPluginAPI.Inputs.Label eLabel) + if (settingElement.Input is DesktopMagic.Api.Settings.Label eLabel) { textBlock.Text = eLabel.Value; textBlock.Margin = new Thickness(0, 5, 3, 0); @@ -33,7 +33,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) }); }; } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.Button eButton) + else if (settingElement.Input is DesktopMagic.Api.Settings.Button eButton) { Button button = new() { @@ -66,7 +66,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) _ = dockPanel.Children.Add(button); } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.CheckBox eCheckBox) + else if (settingElement.Input is DesktopMagic.Api.Settings.CheckBox eCheckBox) { CheckBox checkBox = new() { @@ -96,7 +96,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) _ = dockPanel.Children.Add(checkBox); } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.TextBox eTextBox) + else if (settingElement.Input is DesktopMagic.Api.Settings.TextBox eTextBox) { TextBox textBox = new() { @@ -125,7 +125,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) }; _ = dockPanel.Children.Add(textBox); } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.IntegerUpDown eIntegerUpDown) + else if (settingElement.Input is DesktopMagic.Api.Settings.IntegerUpDown eIntegerUpDown) { Xceed.Wpf.Toolkit.IntegerUpDown integerUpDown = new() { @@ -155,7 +155,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) }; _ = dockPanel.Children.Add(integerUpDown); } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.Slider eSlider) + else if (settingElement.Input is DesktopMagic.Api.Settings.Slider eSlider) { Slider slider = new() { @@ -188,7 +188,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) _ = dockPanel.Children.Add(slider); } - else if (settingElement.Input is DesktopMagicPluginAPI.Settings.ComboBox eComboBox) + else if (settingElement.Input is DesktopMagic.Api.Settings.ComboBox eComboBox) { ComboBox comboBox = new() { @@ -196,14 +196,14 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) IsEditable = false, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, - SelectedIndex = 0 + SelectedItem = eComboBox.Value }; comboBox.SelectionChanged += (_s, _e) => { try { - eComboBox.Value = comboBox.SelectedItem.ToString(); + eComboBox.Value = comboBox.SelectedItem.ToString() ?? string.Empty; } catch (Exception ex) { @@ -224,7 +224,7 @@ internal class SettingElementGenerator(ComboBox optionsComboBox) { App.Logger.Log(message, "PluginInput"); _ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - int index = MainWindow.WindowNames.IndexOf(optionsComboBox.SelectedItem.ToString()); + int index = MainWindow.WindowNames.IndexOf(optionsComboBox.SelectedItem.ToString() ?? string.Empty); PluginWindow window = MainWindow.Windows[index]; window?.Exit(); diff --git a/src/DesktopMagic/MainWindow.xaml b/src/DesktopMagic/MainWindow.xaml index 8e5212e..f8aa17a 100644 --- a/src/DesktopMagic/MainWindow.xaml +++ b/src/DesktopMagic/MainWindow.xaml @@ -22,7 +22,7 @@ Loaded="Window_Loaded"> - + diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index cee5e30..1d8c20a 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -4,17 +4,12 @@ using DesktopMagic.Helpers; using DesktopMagic.Plugins; using DesktopMagic.Settings; -using Microsoft.Win32; - using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Diagnostics; -using System.Globalization; using System.IO; using System.Linq; using System.Reflection; -using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -29,17 +24,18 @@ namespace DesktopMagic private readonly System.Windows.Forms.NotifyIcon notifyIcon = new(); private readonly MainWindowDataContext mainWindowDataContext = new(); - private bool loaded = false; - private bool blockWindowsClosing = true; - public static List Windows { get; } = []; - public static List WindowNames { get; } = []; - private Dictionary builtInPlugins = new() + private readonly Dictionary builtInPlugins = new() { {"Music Visualizer", typeof(MusicVisualizerPlugin)}, {"Time", typeof(TimePlugin)}, {"Date", typeof(DatePlugin)} }; + + private bool loaded = false; + private bool blockWindowsClosing = true; + public static List Windows { get; } = []; + public static List WindowNames { get; } = []; internal static bool EditMode { get; set; } = false; private DesktopMagicSettings Settings @@ -184,7 +180,7 @@ namespace DesktopMagic if (builtInPlugins.TryGetValue(pluginName, out Type? pluginType)) { - window = new PluginWindow((DesktopMagicPluginAPI.Plugin)Activator.CreateInstance(pluginType)!, pluginName, pluginSettings) + window = new PluginWindow((DesktopMagic.Api.Plugin)Activator.CreateInstance(pluginType)!, pluginName, pluginSettings) { Title = pluginName }; @@ -337,7 +333,7 @@ namespace DesktopMagic SettingElementGenerator settingElementGenerator = new SettingElementGenerator(optionsComboBox); - foreach (InputElement settingElement in pluginSettings.Settings) + foreach (SettingElement settingElement in pluginSettings.Settings) { DockPanel dockPanel = new() { @@ -466,8 +462,14 @@ namespace DesktopMagic InputDialog inputDialog = new((string)FindResource("enterLayoutName")); if (inputDialog.ShowDialog() == true) { - Settings.Layouts.Add(new Layout(inputDialog.ResponseText)); - Settings.CurrentLayoutName = inputDialog.ResponseText; + if (Settings.Layouts.Any(l => l.Name.Trim() == inputDialog.ResponseText.Trim())) + { + _ = MessageBox.Show((string)FindResource("layoutAlreadyExists")); + return; + } + + Settings.Layouts.Add(new Layout(inputDialog.ResponseText.Trim())); + Settings.CurrentLayoutName = inputDialog.ResponseText.Trim(); SaveSettings(); } } diff --git a/src/DesktopMagic/Plugins/InputElement.cs b/src/DesktopMagic/Plugins/InputElement.cs deleted file mode 100644 index 03dd61c..0000000 --- a/src/DesktopMagic/Plugins/InputElement.cs +++ /dev/null @@ -1,28 +0,0 @@ -using DesktopMagicPluginAPI.Settings; - -using System.Text.Json.Serialization; - -namespace DesktopMagic.Plugins; - -public class InputElement -{ - public Setting Input { get; } - public string Name { get; } - public int OrderIndex { get; } - -#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. - - public InputElement(Setting input, string name, int orderIndex) - { - Input = input; - Name = name; - OrderIndex = orderIndex; - } - - [JsonConstructor] - private InputElement() - { - } - -#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. -} \ No newline at end of file diff --git a/src/DesktopMagic/Plugins/PluginData.cs b/src/DesktopMagic/Plugins/PluginData.cs index 5ca68a0..7299733 100644 --- a/src/DesktopMagic/Plugins/PluginData.cs +++ b/src/DesktopMagic/Plugins/PluginData.cs @@ -1,6 +1,5 @@ -using DesktopMagic.Settings; - -using DesktopMagicPluginAPI; +using DesktopMagic.Api; +using DesktopMagic.Settings; using System.Drawing; diff --git a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs index eca9502..516901d 100644 --- a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs @@ -1,12 +1,10 @@ -using DesktopMagic.Helpers; +using DesktopMagic.Api; +using DesktopMagic.Api.Drawing; +using DesktopMagic.Api.Settings; +using DesktopMagic.Helpers; using DesktopMagic.Plugins; using DesktopMagic.Settings; -using DesktopMagicPluginAPI; -using DesktopMagicPluginAPI.Drawing; -using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Settings; - using System; using System.Collections.Generic; using System.Drawing; @@ -254,7 +252,7 @@ public partial class PluginWindow : Window FieldInfo[] props = instance.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetField); #pragma warning restore S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields - List settingElements = []; + List settingElements = []; foreach (FieldInfo prop in props) { if (prop.GetValue(instance) is Setting element) @@ -264,7 +262,15 @@ public partial class PluginWindow : Window { if (attribute is SettingAttribute elementAttribute) { - settingElements.Add(new InputElement(element, elementAttribute.Name, elementAttribute.OrderIndex)); + SettingElement settingElement = new SettingElement(element, elementAttribute.Id, elementAttribute.Name, elementAttribute.OrderIndex); + + if (settings.Settings.Exists(e => e.Id == elementAttribute.Id)) + { + SettingElement settingsSettingElement = settings.Settings.First(e => e.Id == elementAttribute.Id); + settingElement.JsonValue = settingsSettingElement.JsonValue; + } + + settingElements.Add(settingElement); break; } } diff --git a/src/DesktopMagic/Plugins/SettingElement.cs b/src/DesktopMagic/Plugins/SettingElement.cs new file mode 100644 index 0000000..074f4e6 --- /dev/null +++ b/src/DesktopMagic/Plugins/SettingElement.cs @@ -0,0 +1,51 @@ +using DesktopMagic.Api.Settings; + +using System.Text.Json.Serialization; + +namespace DesktopMagic.Plugins; + +public class SettingElement +{ + private string jsonValue = string.Empty; + + [JsonIgnore] + public Setting Input { get; set; } + + public string Name { get; set; } + public int OrderIndex { get; set; } + + public string Id { get; set; } + + public string JsonValue + { + get => Input?.GetJsonValue() ?? jsonValue; + set + { + if (Input is null) + { + jsonValue = value; + } + else + { + Input.SetJsonValue(value); + } + } + } + + public SettingElement(Setting input, string id, string name, int orderIndex) + { + Input = input; + Id = id; + Name = name; + OrderIndex = orderIndex; + } + +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + + [JsonConstructor] + private SettingElement() + { + } + +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +} \ No newline at end of file diff --git a/src/DesktopMagic/Plugins/Theme.cs b/src/DesktopMagic/Plugins/Theme.cs index bf2a6af..2ed05d5 100644 --- a/src/DesktopMagic/Plugins/Theme.cs +++ b/src/DesktopMagic/Plugins/Theme.cs @@ -1,6 +1,5 @@ -using DesktopMagic.Helpers; - -using DesktopMagicPluginAPI; +using DesktopMagic.Api; +using DesktopMagic.Helpers; using System.ComponentModel; using System.Runtime.CompilerServices; diff --git a/src/DesktopMagic/Resources/StringResources.de.xaml b/src/DesktopMagic/Resources/StringResources.de.xaml index 4d38a99..e9ae0ba 100644 --- a/src/DesktopMagic/Resources/StringResources.de.xaml +++ b/src/DesktopMagic/Resources/StringResources.de.xaml @@ -25,6 +25,7 @@ Ok Abbrechen Layoutnamen eingeben: + Layout exestiert bereits! Unten Mitte diff --git a/src/DesktopMagic/Resources/StringResources.en.xaml b/src/DesktopMagic/Resources/StringResources.en.xaml index 8935929..c14a488 100644 --- a/src/DesktopMagic/Resources/StringResources.en.xaml +++ b/src/DesktopMagic/Resources/StringResources.en.xaml @@ -25,6 +25,7 @@ Ok Cancel Enter layout name + Layout already exists! Bottom Middle diff --git a/src/DesktopMagic/Settings/PluginSettings.cs b/src/DesktopMagic/Settings/PluginSettings.cs index 4672184..c800316 100644 --- a/src/DesktopMagic/Settings/PluginSettings.cs +++ b/src/DesktopMagic/Settings/PluginSettings.cs @@ -13,7 +13,7 @@ public class PluginSettings : INotifyPropertyChanged public event PropertyChangedEventHandler? PropertyChanged; private readonly Theme? theme; - private List settings = []; + private List settings = []; private bool enabled = false; private Point position = new Point(100, 100); private Point size = new Point(300, 300); @@ -21,7 +21,7 @@ public class PluginSettings : INotifyPropertyChanged [JsonIgnore] public Theme Theme => theme is null ? MainWindowDataContext.GetSettings().CurrentLayout.Theme : theme; - public List Settings + public List Settings { get => settings; set diff --git a/src/DesktopMagicPlugin.Test/DesktopMagicPlugin.Test.csproj b/src/DesktopMagicPlugin.Test/DesktopMagic.PluginTest.csproj similarity index 93% rename from src/DesktopMagicPlugin.Test/DesktopMagicPlugin.Test.csproj rename to src/DesktopMagicPlugin.Test/DesktopMagic.PluginTest.csproj index 045de12..1f7adff 100644 --- a/src/DesktopMagicPlugin.Test/DesktopMagicPlugin.Test.csproj +++ b/src/DesktopMagicPlugin.Test/DesktopMagic.PluginTest.csproj @@ -10,6 +10,6 @@ - + \ No newline at end of file diff --git a/src/DesktopMagicPlugin.Test/PluginScript.cs b/src/DesktopMagicPlugin.Test/PluginScript.cs index 00c3d8f..e51e1d2 100644 --- a/src/DesktopMagicPlugin.Test/PluginScript.cs +++ b/src/DesktopMagicPlugin.Test/PluginScript.cs @@ -1,6 +1,5 @@ -using DesktopMagicPluginAPI; -using DesktopMagicPluginAPI.Inputs; -using DesktopMagicPluginAPI.Settings; +using DesktopMagic.Api; +using DesktopMagic.Api.Settings; using System; using System.Collections.Generic; @@ -9,16 +8,16 @@ using System.Drawing.Imaging; using System.IO; using System.Threading.Tasks; -namespace DesktopMagicPlugin.Test; +namespace DesktopMagic.PluginTest; public class GifPlugin : Plugin { private const string SaveFilePath = "gifPath.txt"; - [Setting("Gif path:")] + [Setting("gif-path", "Gif path:")] private readonly TextBox input = new TextBox(""); - [Setting] + [Setting("info")] private readonly Label info = new Label(""); private readonly List bitmaps = []; @@ -64,7 +63,7 @@ public class GifPlugin : Plugin PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus - UpdateInterval = (item.Value[0] + (item.Value[1] * 256)) * 10; //FrameDelay in ms + UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms bitmaps.Clear(); for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) { diff --git a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.csproj b/src/DesktopMagicPluginAPI/DesktopMagic.Api.csproj similarity index 97% rename from src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.csproj rename to src/DesktopMagicPluginAPI/DesktopMagic.Api.csproj index fd6b028..26d4941 100644 --- a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.csproj +++ b/src/DesktopMagicPluginAPI/DesktopMagic.Api.csproj @@ -11,6 +11,7 @@ 0.0.0.5 0.0.0.5 + enable diff --git a/src/DesktopMagicPluginAPI/Drawing/FontComparer.cs b/src/DesktopMagicPluginAPI/Drawing/FontComparer.cs index df72204..e39cd45 100644 --- a/src/DesktopMagicPluginAPI/Drawing/FontComparer.cs +++ b/src/DesktopMagicPluginAPI/Drawing/FontComparer.cs @@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis; using System.Drawing; -namespace DesktopMagicPluginAPI.Drawing; +namespace DesktopMagic.Api.Drawing; internal class FontComparer : IEqualityComparer { diff --git a/src/DesktopMagicPluginAPI/Drawing/GraphicsExtentions.cs b/src/DesktopMagicPluginAPI/Drawing/GraphicsExtentions.cs index 74f9f59..dab29a7 100644 --- a/src/DesktopMagicPluginAPI/Drawing/GraphicsExtentions.cs +++ b/src/DesktopMagicPluginAPI/Drawing/GraphicsExtentions.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Drawing; -namespace DesktopMagicPluginAPI.Drawing; +namespace DesktopMagic.Api.Drawing; /// /// Extensions for the class. diff --git a/src/DesktopMagicPluginAPI/Drawing/RenderQuality.cs b/src/DesktopMagicPluginAPI/Drawing/RenderQuality.cs index 08d7fe9..b6c42f8 100644 --- a/src/DesktopMagicPluginAPI/Drawing/RenderQuality.cs +++ b/src/DesktopMagicPluginAPI/Drawing/RenderQuality.cs @@ -1,4 +1,4 @@ -namespace DesktopMagicPluginAPI.Drawing; +namespace DesktopMagic.Api.Drawing; /// /// Specifies which render quality is used to display the bitmap images. diff --git a/src/DesktopMagicPluginAPI/IPluginData.cs b/src/DesktopMagicPluginAPI/IPluginData.cs index 0285932..3f0aaca 100644 --- a/src/DesktopMagicPluginAPI/IPluginData.cs +++ b/src/DesktopMagicPluginAPI/IPluginData.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace DesktopMagicPluginAPI; +namespace DesktopMagic.Api; /// /// Defines properties and methods that provide information about the main application. diff --git a/src/DesktopMagicPluginAPI/ITheme.cs b/src/DesktopMagicPluginAPI/ITheme.cs index e952a5f..8184102 100644 --- a/src/DesktopMagicPluginAPI/ITheme.cs +++ b/src/DesktopMagicPluginAPI/ITheme.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace DesktopMagicPluginAPI; +namespace DesktopMagic.Api; /// /// The theme settings of the main application. diff --git a/src/DesktopMagicPluginAPI/Plugin.cs b/src/DesktopMagicPluginAPI/Plugin.cs index 1db3caf..d0f5326 100644 --- a/src/DesktopMagicPluginAPI/Plugin.cs +++ b/src/DesktopMagicPluginAPI/Plugin.cs @@ -1,10 +1,10 @@ -using DesktopMagicPluginAPI.Drawing; -using DesktopMagicPluginAPI.Inputs; +using DesktopMagic.Api.Drawing; +using DesktopMagic.Api.Settings; using System; using System.Drawing; -namespace DesktopMagicPluginAPI; +namespace DesktopMagic.Api; /// /// The plugin class. @@ -50,7 +50,7 @@ public abstract class Plugin /// Occurs when the elapses. /// /// - public abstract Bitmap Main(); + public abstract Bitmap? Main(); /// /// Occurs when the window is clicked by the mouse. diff --git a/src/DesktopMagicPluginAPI/Settings/Button.cs b/src/DesktopMagicPluginAPI/Settings/Button.cs index a60a92f..f0393be 100644 --- a/src/DesktopMagicPluginAPI/Settings/Button.cs +++ b/src/DesktopMagicPluginAPI/Settings/Button.cs @@ -1,6 +1,6 @@ using System; -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a button control. diff --git a/src/DesktopMagicPluginAPI/Settings/CheckBox.cs b/src/DesktopMagicPluginAPI/Settings/CheckBox.cs index a1253c0..2031d9c 100644 --- a/src/DesktopMagicPluginAPI/Settings/CheckBox.cs +++ b/src/DesktopMagicPluginAPI/Settings/CheckBox.cs @@ -1,4 +1,4 @@ -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a check box control. @@ -31,4 +31,15 @@ public class CheckBox : Setting { Value = value; } + + internal override string GetJsonValue() + { + return Value.ToString(); + } + + internal override void SetJsonValue(string value) + { + _ = bool.TryParse(value, out bool result); + Value = result; + } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/ComboBox.cs b/src/DesktopMagicPluginAPI/Settings/ComboBox.cs index 368895f..caeb7ec 100644 --- a/src/DesktopMagicPluginAPI/Settings/ComboBox.cs +++ b/src/DesktopMagicPluginAPI/Settings/ComboBox.cs @@ -1,8 +1,7 @@ -using DesktopMagicPluginAPI.Inputs; +using System.Collections.ObjectModel; +using System.Text.Json; -using System.Collections.ObjectModel; - -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a selection control with a drop-down list. @@ -44,4 +43,25 @@ public class ComboBox : Setting Items.Add(item); } } + + internal override string GetJsonValue() + { + return JsonSerializer.Serialize(new { Value, Items }); + } + + internal override void SetJsonValue(string value) + { + if (string.IsNullOrEmpty(value)) + { + return; + } + + JsonElement json = JsonSerializer.Deserialize(value); + Items.Clear(); + foreach (JsonElement item in json.GetProperty(nameof(Items)).EnumerateArray()) + { + Items.Add(item.GetString() ?? string.Empty); + } + Value = json.GetProperty(nameof(Value)).GetString() ?? string.Empty; + } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs b/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs index 23f3ef5..1361d45 100644 --- a/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs +++ b/src/DesktopMagicPluginAPI/Settings/IntegerUpDown.cs @@ -1,6 +1,6 @@ using System; -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a up-down control. @@ -61,4 +61,15 @@ public class IntegerUpDown : Setting Maximum = max; Value = value; } + + internal override string GetJsonValue() + { + return Value.ToString(); + } + + internal override void SetJsonValue(string value) + { + _ = int.TryParse(value, out int result); + Value = result; + } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/Label.cs b/src/DesktopMagicPluginAPI/Settings/Label.cs index 762af3e..179ce98 100644 --- a/src/DesktopMagicPluginAPI/Settings/Label.cs +++ b/src/DesktopMagicPluginAPI/Settings/Label.cs @@ -1,6 +1,4 @@ -using DesktopMagicPluginAPI.Settings; - -namespace DesktopMagicPluginAPI.Inputs; +namespace DesktopMagic.Api.Settings; /// /// Represents a label control. diff --git a/src/DesktopMagicPluginAPI/Settings/MouseButton.cs b/src/DesktopMagicPluginAPI/Settings/MouseButton.cs index c66748a..0a5bc71 100644 --- a/src/DesktopMagicPluginAPI/Settings/MouseButton.cs +++ b/src/DesktopMagicPluginAPI/Settings/MouseButton.cs @@ -1,4 +1,4 @@ -namespace DesktopMagicPluginAPI.Inputs; +namespace DesktopMagic.Api.Settings; /// /// Mouse Buttons diff --git a/src/DesktopMagicPluginAPI/Settings/Setting.cs b/src/DesktopMagicPluginAPI/Settings/Setting.cs index 0d15b9b..60ce809 100644 --- a/src/DesktopMagicPluginAPI/Settings/Setting.cs +++ b/src/DesktopMagicPluginAPI/Settings/Setting.cs @@ -1,6 +1,9 @@ using System; +using System.Runtime.CompilerServices; -namespace DesktopMagicPluginAPI.Settings; +[assembly: InternalsVisibleTo("DesktopMagic")] + +namespace DesktopMagic.Api.Settings; /// /// The element base class. @@ -10,7 +13,15 @@ public abstract class Setting /// /// Occurs when the value has been changed. /// - public event Action OnValueChanged; + public event Action? OnValueChanged; + + internal virtual string GetJsonValue() + { + return string.Empty; + } + + internal virtual void SetJsonValue(string value) + { } /// /// Triggers the event. diff --git a/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs b/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs index f9697c1..1c55775 100644 --- a/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs +++ b/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs @@ -1,6 +1,6 @@ using System; -namespace DesktopMagicPluginAPI.Inputs; +namespace DesktopMagic.Api.Settings; /// /// Marks a Property as element. @@ -11,20 +11,27 @@ public class SettingAttribute : Attribute /// /// The name of the element. /// - public string Name { get; } + public string Name { get; set; } /// /// The order index of the element. /// - public int OrderIndex { get; } + public int OrderIndex { get; set; } + + /// + /// The id of the element. + /// + public string Id { get; } /// /// Marks a Property as element with the provided and . /// + /// The id of the element. /// The name of the element. /// The order index of the element. - public SettingAttribute(string name, int orderIndex = 0) + public SettingAttribute(string id, string name, int orderIndex = 0) { + Id = id; Name = name; OrderIndex = orderIndex; } @@ -32,14 +39,17 @@ public class SettingAttribute : Attribute /// /// Marks a Property as element with the provided . /// + /// The id of the element. /// The order index of the element. - public SettingAttribute(int orderIndex) + public SettingAttribute(string id, int orderIndex) { + Id = id; OrderIndex = orderIndex; } /// - public SettingAttribute() + public SettingAttribute(string id) { + Id = id; } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/Slider.cs b/src/DesktopMagicPluginAPI/Settings/Slider.cs index 04b25da..813bd44 100644 --- a/src/DesktopMagicPluginAPI/Settings/Slider.cs +++ b/src/DesktopMagicPluginAPI/Settings/Slider.cs @@ -1,6 +1,6 @@ using System; -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a slider control. @@ -60,4 +60,15 @@ public sealed class Slider : Setting Maximum = max; Value = value; } + + internal override string GetJsonValue() + { + return Value.ToString(); + } + + internal override void SetJsonValue(string value) + { + _ = double.TryParse(value, out double result); + Value = result; + } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/TextBox.cs b/src/DesktopMagicPluginAPI/Settings/TextBox.cs index f6b73bb..a4bea95 100644 --- a/src/DesktopMagicPluginAPI/Settings/TextBox.cs +++ b/src/DesktopMagicPluginAPI/Settings/TextBox.cs @@ -1,4 +1,4 @@ -namespace DesktopMagicPluginAPI.Settings; +namespace DesktopMagic.Api.Settings; /// /// Represents a text box control. @@ -31,4 +31,14 @@ public class TextBox : Setting { Value = value; } + + internal override string GetJsonValue() + { + return Value; + } + + internal override void SetJsonValue(string value) + { + Value = value; + } } \ No newline at end of file