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:
@@ -490,7 +490,14 @@ namespace DesktopMagic
|
|||||||
};
|
};
|
||||||
button.Click += (_s, _e) =>
|
button.Click += (_s, _e) =>
|
||||||
{
|
{
|
||||||
eButton.Click();
|
try
|
||||||
|
{
|
||||||
|
eButton.Click();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayException(ex.Message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
eButton.OnValueChanged += () =>
|
eButton.OnValueChanged += () =>
|
||||||
{
|
{
|
||||||
@@ -513,7 +520,14 @@ namespace DesktopMagic
|
|||||||
};
|
};
|
||||||
checkBox.Click += (_s, _e) =>
|
checkBox.Click += (_s, _e) =>
|
||||||
{
|
{
|
||||||
eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault();
|
try
|
||||||
|
{
|
||||||
|
eCheckBox.Value = checkBox.IsChecked.GetValueOrDefault();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayException(ex.Message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
eCheckBox.OnValueChanged += () =>
|
eCheckBox.OnValueChanged += () =>
|
||||||
{
|
{
|
||||||
@@ -536,7 +550,14 @@ namespace DesktopMagic
|
|||||||
};
|
};
|
||||||
textBox.TextChanged += (_s, _e) =>
|
textBox.TextChanged += (_s, _e) =>
|
||||||
{
|
{
|
||||||
eTextBox.Value = textBox.Text;
|
try
|
||||||
|
{
|
||||||
|
eTextBox.Value = textBox.Text;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayException(ex.Message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
eTextBox.OnValueChanged += () =>
|
eTextBox.OnValueChanged += () =>
|
||||||
{
|
{
|
||||||
@@ -559,7 +580,14 @@ namespace DesktopMagic
|
|||||||
};
|
};
|
||||||
integerUpDown.ValueChanged += (_s, _e) =>
|
integerUpDown.ValueChanged += (_s, _e) =>
|
||||||
{
|
{
|
||||||
eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault();
|
try
|
||||||
|
{
|
||||||
|
eIntegerUpDown.Value = integerUpDown.Value.GetValueOrDefault();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayException(ex.Message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
eIntegerUpDown.OnValueChanged += () =>
|
eIntegerUpDown.OnValueChanged += () =>
|
||||||
{
|
{
|
||||||
@@ -584,7 +612,14 @@ namespace DesktopMagic
|
|||||||
};
|
};
|
||||||
slider.ValueChanged += (_s, _e) =>
|
slider.ValueChanged += (_s, _e) =>
|
||||||
{
|
{
|
||||||
eSlider.Value = slider.Value;
|
try
|
||||||
|
{
|
||||||
|
eSlider.Value = slider.Value;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayException(ex.Message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
eSlider.OnValueChanged += () =>
|
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
|
#endregion options
|
||||||
|
|
||||||
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
|
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
|
||||||
|
|||||||
@@ -225,25 +225,29 @@ namespace DesktopMagic
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bitmap result = pluginClassInstance.Main();
|
if (!stop)
|
||||||
|
{
|
||||||
|
Bitmap result = pluginClassInstance.Main();
|
||||||
|
|
||||||
if (pluginClassInstance.UpdateInterval > 0)
|
if (pluginClassInstance.UpdateInterval > 0)
|
||||||
{
|
{
|
||||||
valueTimer.Interval = pluginClassInstance.UpdateInterval;
|
valueTimer.Interval = pluginClassInstance.UpdateInterval;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
valueTimer.Stop();
|
valueTimer.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Update Image
|
//Update Image
|
||||||
Dispatcher.Invoke(() =>
|
Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
image.Source = BitmapToImageSource(result);
|
image.Source = BitmapToImageSource(result);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
stop = true;
|
||||||
MainWindow.Logger.Log(ex.ToString(), "Plugin");
|
MainWindow.Logger.Log(ex.ToString(), "Plugin");
|
||||||
_ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
_ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
Exit();
|
Exit();
|
||||||
@@ -272,8 +276,9 @@ namespace DesktopMagic
|
|||||||
return bitmapSource;
|
return bitmapSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Exit()
|
public void Exit()
|
||||||
{
|
{
|
||||||
|
stop = true;
|
||||||
Dispatcher.Invoke(() =>
|
Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
OnExit?.Invoke();
|
OnExit?.Invoke();
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
using DesktopMagicPluginAPI.Inputs;
|
using DesktopMagicPluginAPI.Inputs;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using System.Drawing.Drawing2D;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace DesktopMagicPlugin.Test
|
namespace DesktopMagicPlugin.Test
|
||||||
{
|
{
|
||||||
@@ -13,35 +14,58 @@ namespace DesktopMagicPlugin.Test
|
|||||||
[Element("Gif path:")]
|
[Element("Gif path:")]
|
||||||
private TextBox input = new TextBox("");
|
private TextBox input = new TextBox("");
|
||||||
|
|
||||||
|
[Element]
|
||||||
|
private Label info = new Label("");
|
||||||
|
|
||||||
private List<Bitmap> bitmaps = new List<Bitmap>();
|
private List<Bitmap> bitmaps = new List<Bitmap>();
|
||||||
|
|
||||||
private int frameCount = -1;
|
private int frameCount = -1;
|
||||||
|
|
||||||
|
private const string SaveFilePath = "gifPath.txt";
|
||||||
|
|
||||||
public override void Start()
|
public override void Start()
|
||||||
{
|
{
|
||||||
input.OnValueChanged += Input_OnValueChanged;
|
input.OnValueChanged += Input_OnValueChanged;
|
||||||
|
if (File.Exists(SaveFilePath))
|
||||||
|
{
|
||||||
|
input.Value = File.ReadAllText(SaveFilePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Input_OnValueChanged()
|
private void Input_OnValueChanged()
|
||||||
{
|
{
|
||||||
try
|
_ = Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (File.Exists(input.Value))
|
try
|
||||||
{
|
{
|
||||||
Image gif = Image.FromFile(input.Value);
|
info.Value = "Loading...";
|
||||||
PropertyItem item = gif.GetPropertyItem(0x5100); // FrameDelay in libgdiplus
|
if (File.Exists(input.Value))
|
||||||
|
|
||||||
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);
|
Image gif = Image.FromFile(input.Value);
|
||||||
|
|
||||||
bitmaps.Add(new Bitmap(gif));
|
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));
|
||||||
|
}
|
||||||
|
File.WriteAllText(SaveFilePath, input.Value);
|
||||||
|
info.Value = string.Empty;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
info.Value = "File not found!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
catch { }
|
{
|
||||||
|
info.Value = $"Error: {ex.Message}";
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Bitmap Main()
|
public override Bitmap Main()
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DesktopMagicPluginAPI.Inputs
|
namespace DesktopMagicPluginAPI.Inputs
|
||||||
{
|
{
|
||||||
@@ -45,7 +44,7 @@ namespace DesktopMagicPluginAPI.Inputs
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Click()
|
public void Click()
|
||||||
{
|
{
|
||||||
_ = Task.Run(() => OnClick?.Invoke());
|
OnClick?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DesktopMagicPluginAPI.Inputs
|
namespace DesktopMagicPluginAPI.Inputs
|
||||||
{
|
{
|
||||||
@@ -18,7 +17,7 @@ namespace DesktopMagicPluginAPI.Inputs
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected void ValueChanged()
|
protected void ValueChanged()
|
||||||
{
|
{
|
||||||
_ = Task.Run(() => OnValueChanged?.Invoke());
|
OnValueChanged?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user