From e00c873bd537fe8d2db8f98a9c1100bf3007c767 Mon Sep 17 00:00:00 2001 From: Stone-Red-Code <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 13 Sep 2021 19:49:05 +0200 Subject: [PATCH] - Remove unnecessary code - Execute `OnValueChanged` event in task --- src/DesktopMagic/MainWindow.xaml.cs | 20 -------- src/DesktopMagic/Plugin/PluginWindow.xaml.cs | 8 +--- src/DesktopMagicPlugin.Test/PluginScript.cs | 50 ++++++++++++++++---- src/DesktopMagicPluginAPI/Inputs/Button.cs | 5 +- src/DesktopMagicPluginAPI/Inputs/Element.cs | 3 +- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index d4e7457..4edf8f8 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -473,7 +473,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { label.Text = eLabel.Value; - Option_ValueChanged(); }); }; } @@ -498,7 +497,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { button.Content = eButton.Value; - Option_ValueChanged(); }); }; @@ -522,7 +520,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { checkBox.IsChecked = eCheckBox.Value; - Option_ValueChanged(); }); }; @@ -546,7 +543,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { textBox.Text = eTextBox.Value; - Option_ValueChanged(); }); }; _ = stackPanel.Children.Add(textBox); @@ -570,7 +566,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { integerUpDown.Value = eIntegerUpDown.Value; - Option_ValueChanged(); }); }; _ = stackPanel.Children.Add(integerUpDown); @@ -596,7 +591,6 @@ namespace DesktopMagic Dispatcher.Invoke(() => { slider.Value = eSlider.Value; - Option_ValueChanged(); }); }; @@ -606,20 +600,6 @@ namespace DesktopMagic } } - private void Option_ValueChanged() - { - //Plugin settings save currenty disabled. Not sure if I want to add it back in the future. - /* - string pluginName = ((Tuple)optionsComboBox.SelectedItem).Item1.ToString(); - - if (PluginsSettings.ContainsKey(pluginName)) - { - string jsonSettings = JsonSerializer.Serialize(PluginsSettings[pluginName]); - File.WriteAllText($"{applicationDataPath}\\Plugins\\{pluginName}\\{pluginName}.save", jsonSettings); - } - */ - } - #endregion options private void TextBlock_Loaded(object sender, RoutedEventArgs e) diff --git a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs index 25abc25..f7a110c 100644 --- a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs @@ -117,15 +117,9 @@ namespace DesktopMagic private void LoadPlugin() { - string PluginPath; - pluginFolderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{MainWindow.AppName}\\Plugins\\{pluginName}"; - if (File.Exists($"{pluginFolderPath}\\{pluginName}.dll")) - { - PluginPath = $"{pluginFolderPath}\\{pluginName}.dll"; - } - else + if (!File.Exists($"{pluginFolderPath}\\{pluginName}.dll")) { _ = MessageBox.Show("File does not exist!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Exit(); diff --git a/src/DesktopMagicPlugin.Test/PluginScript.cs b/src/DesktopMagicPlugin.Test/PluginScript.cs index 175a2db..a6f5f15 100644 --- a/src/DesktopMagicPlugin.Test/PluginScript.cs +++ b/src/DesktopMagicPlugin.Test/PluginScript.cs @@ -1,28 +1,60 @@ using DesktopMagicPluginAPI; using DesktopMagicPluginAPI.Inputs; using System.Drawing; -using System.Diagnostics; +using System.Drawing.Imaging; +using System.Drawing.Drawing2D; +using System.IO; +using System.Collections.Generic; namespace DesktopMagicPlugin.Test { public class GifPlugin : Plugin { [Element("Gif path:")] - public TextBox input = new TextBox(""); + private TextBox input = new TextBox(""); - public override int UpdateInterval { get; set; } = 100; + private List bitmaps = new List(); - public override void OnMouseWheel(Point position, int delta) + private int frameCount = -1; + + public override void Start() { - Debug.WriteLine(position + " | " + delta); + input.OnValueChanged += Input_OnValueChanged; + } + + private void Input_OnValueChanged() + { + try + { + if (File.Exists(input.Value)) + { + Image gif = Image.FromFile(input.Value); + PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus + + UpdateInterval = (item.Value[0] + item.Value[1] * 256) * 10; //FrameDelay in ms + bitmaps.Clear(); + for (int i = 0; i < gif.GetFrameCount(FrameDimension.Time); i++) + { + gif.SelectActiveFrame(FrameDimension.Time, i); + + bitmaps.Add(new Bitmap(gif)); + } + } + } + catch { } } public override Bitmap Main() { - Bitmap bmp = new Bitmap(100, 100); - using Graphics graphics = Graphics.FromImage(bmp); - graphics.Clear(Application.Color); - return bmp; + if (bitmaps.Count == 0) + return new Bitmap(1, 1); + + frameCount++; + + if (frameCount >= bitmaps.Count) + frameCount = 0; + + return bitmaps[frameCount]; } } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Inputs/Button.cs b/src/DesktopMagicPluginAPI/Inputs/Button.cs index 5ea1fc4..15c23fb 100644 --- a/src/DesktopMagicPluginAPI/Inputs/Button.cs +++ b/src/DesktopMagicPluginAPI/Inputs/Button.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace DesktopMagicPluginAPI.Inputs @@ -48,7 +45,7 @@ namespace DesktopMagicPluginAPI.Inputs /// public void Click() { - OnClick?.Invoke(); + _ = Task.Run(() => OnClick?.Invoke()); } } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Inputs/Element.cs b/src/DesktopMagicPluginAPI/Inputs/Element.cs index b23b4fa..810cfcf 100644 --- a/src/DesktopMagicPluginAPI/Inputs/Element.cs +++ b/src/DesktopMagicPluginAPI/Inputs/Element.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; namespace DesktopMagicPluginAPI.Inputs { @@ -17,7 +18,7 @@ namespace DesktopMagicPluginAPI.Inputs /// protected void ValueChanged() { - OnValueChanged?.Invoke(); + _ = Task.Run(() => OnValueChanged?.Invoke()); } } } \ No newline at end of file