mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 00:46:12 +02:00
- Exception handling for plugin events
- Revert: Execute `OnValueChanged` event in task
This commit is contained in:
@@ -489,8 +489,15 @@ namespace DesktopMagic
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
button.Click += (_s, _e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
eButton.Click();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayException(ex.Message);
|
||||
}
|
||||
};
|
||||
eButton.OnValueChanged += () =>
|
||||
{
|
||||
@@ -512,8 +519,15 @@ namespace DesktopMagic
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
checkBox.Click += (_s, _e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayException(ex.Message);
|
||||
}
|
||||
};
|
||||
eCheckBox.OnValueChanged += () =>
|
||||
{
|
||||
@@ -535,8 +549,15 @@ namespace DesktopMagic
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
textBox.TextChanged += (_s, _e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
eTextBox.Value = textBox.Text;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayException(ex.Message);
|
||||
}
|
||||
};
|
||||
eTextBox.OnValueChanged += () =>
|
||||
{
|
||||
@@ -558,8 +579,15 @@ namespace DesktopMagic
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
integerUpDown.ValueChanged += (_s, _e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayException(ex.Message);
|
||||
}
|
||||
};
|
||||
eIntegerUpDown.OnValueChanged += () =>
|
||||
{
|
||||
@@ -583,8 +611,15 @@ namespace DesktopMagic
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
slider.ValueChanged += (_s, _e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
eSlider.Value = slider.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayException(ex.Message);
|
||||
}
|
||||
};
|
||||
eSlider.OnValueChanged += () =>
|
||||
{
|
||||
@@ -600,6 +635,16 @@ namespace DesktopMagic
|
||||
}
|
||||
}
|
||||
|
||||
private void DisplayException(string message)
|
||||
{
|
||||
Logger.Log(message, "PluginInput");
|
||||
_ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
int index = WindowNames.IndexOf(((Tuple<string, int>)optionsComboBox.SelectedItem).Item1.ToString());
|
||||
|
||||
PluginWindow window = Windows[index] as PluginWindow;
|
||||
window?.Exit();
|
||||
}
|
||||
|
||||
#endregion options
|
||||
|
||||
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
|
||||
|
||||
@@ -224,6 +224,8 @@ namespace DesktopMagic
|
||||
private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!stop)
|
||||
{
|
||||
Bitmap result = pluginClassInstance.Main();
|
||||
|
||||
@@ -242,8 +244,10 @@ namespace DesktopMagic
|
||||
image.Source = BitmapToImageSource(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
stop = true;
|
||||
MainWindow.Logger.Log(ex.ToString(), "Plugin");
|
||||
_ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Exit();
|
||||
@@ -272,8 +276,9 @@ namespace DesktopMagic
|
||||
return bitmapSource;
|
||||
}
|
||||
|
||||
private void Exit()
|
||||
public void Exit()
|
||||
{
|
||||
stop = true;
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
OnExit?.Invoke();
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
using DesktopMagicPluginAPI.Inputs;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace DesktopMagicPlugin.Test
|
||||
{
|
||||
@@ -13,22 +14,35 @@ namespace DesktopMagicPlugin.Test
|
||||
[Element("Gif path:")]
|
||||
private TextBox input = new TextBox("");
|
||||
|
||||
[Element]
|
||||
private Label info = new Label("");
|
||||
|
||||
private List<Bitmap> bitmaps = new List<Bitmap>();
|
||||
|
||||
private int frameCount = -1;
|
||||
|
||||
private const string SaveFilePath = "gifPath.txt";
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
input.OnValueChanged += Input_OnValueChanged;
|
||||
if (File.Exists(SaveFilePath))
|
||||
{
|
||||
input.Value = File.ReadAllText(SaveFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
private void Input_OnValueChanged()
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
info.Value = "Loading...";
|
||||
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
|
||||
@@ -39,9 +53,19 @@ namespace DesktopMagicPlugin.Test
|
||||
|
||||
bitmaps.Add(new Bitmap(gif));
|
||||
}
|
||||
File.WriteAllText(SaveFilePath, input.Value);
|
||||
info.Value = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.Value = "File not found!";
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
catch (Exception ex)
|
||||
{
|
||||
info.Value = $"Error: {ex.Message}";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override Bitmap Main()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DesktopMagicPluginAPI.Inputs
|
||||
{
|
||||
@@ -45,7 +44,7 @@ namespace DesktopMagicPluginAPI.Inputs
|
||||
/// </summary>
|
||||
public void Click()
|
||||
{
|
||||
_ = Task.Run(() => OnClick?.Invoke());
|
||||
OnClick?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DesktopMagicPluginAPI.Inputs
|
||||
{
|
||||
@@ -18,7 +17,7 @@ namespace DesktopMagicPluginAPI.Inputs
|
||||
/// </summary>
|
||||
protected void ValueChanged()
|
||||
{
|
||||
_ = Task.Run(() => OnValueChanged?.Invoke());
|
||||
OnValueChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user