From 43c77e6a3622d1256c08bc196f90a6c7f2467be2 Mon Sep 17 00:00:00 2001
From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com>
Date: Thu, 25 Dec 2025 01:22:39 +0100
Subject: [PATCH] Add plugin methods for changed themes, settings showing
message boxes and logging
---
src/DesktopMagic/BuiltInPlugins/DatePlugin.cs | 21 +++++-------
src/DesktopMagic/BuiltInPlugins/TimePlugin.cs | 30 ++++++++---------
src/DesktopMagic/Plugins/PluginData.cs | 33 ++++++++++++++-----
src/DesktopMagic/Plugins/PluginWindow.xaml.cs | 9 ++++-
src/DesktopMagicPluginAPI/IPluginData.cs | 25 ++++++++------
src/DesktopMagicPluginAPI/LogLevel.cs | 22 +++++++++++++
src/DesktopMagicPluginAPI/Plugin.cs | 24 ++++++++++++--
7 files changed, 115 insertions(+), 49 deletions(-)
create mode 100644 src/DesktopMagicPluginAPI/LogLevel.cs
diff --git a/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs b/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs
index f622fd3..cefe11e 100644
--- a/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs
+++ b/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs
@@ -17,16 +17,6 @@ internal class DatePlugin : Plugin
public override int UpdateInterval => 1000;
- public override void Start()
- {
- Application.ThemeChanged += (_, _) =>
- {
- themeChanged = true;
- Application.UpdateWindow();
- themeChanged = false;
- };
- }
-
public override Bitmap? Main()
{
if (oldDateTime.Date == DateTime.Now.Date && !themeChanged)
@@ -35,10 +25,11 @@ internal class DatePlugin : Plugin
}
oldDateTime = DateTime.Now;
+ themeChanged = false;
string date = shortDateCheckBox.Value ? DateTime.Now.ToShortDateString() : DateTime.Now.ToLongDateString();
- Font font = new Font(Application.Theme.Font, 200);
+ using Font font = new Font(Application.Theme.Font, 200);
Bitmap bmp = new Bitmap(1, 1);
bmp.SetResolution(100, 100);
@@ -51,10 +42,16 @@ internal class DatePlugin : Plugin
bmp.SetResolution(100, 100);
using Graphics gr = Graphics.FromImage(bmp);
+ using SolidBrush brush = new SolidBrush(Application.Theme.PrimaryColor);
gr.TextRenderingHint = TextRenderingHint.AntiAlias;
- gr.DrawString(date, font, new SolidBrush(Application.Theme.PrimaryColor), 0, 0);
+ gr.DrawString(date, font, brush, 0, 0);
return bmp;
}
+
+ public override void OnThemeChanged()
+ {
+ themeChanged = true;
+ }
}
\ No newline at end of file
diff --git a/src/DesktopMagic/BuiltInPlugins/TimePlugin.cs b/src/DesktopMagic/BuiltInPlugins/TimePlugin.cs
index 8497124..c8030be 100644
--- a/src/DesktopMagic/BuiltInPlugins/TimePlugin.cs
+++ b/src/DesktopMagic/BuiltInPlugins/TimePlugin.cs
@@ -14,21 +14,9 @@ internal class TimePlugin : Plugin
private string oldTime = string.Empty;
private bool themeChanged;
+
public override int UpdateInterval => 1000;
- public override void Start()
- {
- Application.ThemeChanged += (_, _) => ThemeChanged();
- displaySecondsCheckBox.OnValueChanged += ThemeChanged;
-
- void ThemeChanged()
- {
- themeChanged = true;
- Application.UpdateWindow();
- themeChanged = false;
- }
- }
-
public override Bitmap? Main()
{
string time = displaySecondsCheckBox.Value ? DateTime.Now.ToLongTimeString() : DateTime.Now.ToShortTimeString();
@@ -39,8 +27,9 @@ internal class TimePlugin : Plugin
}
oldTime = time;
+ themeChanged = false;
- Font font = new Font(Application.Theme.Font, 200);
+ using Font font = new Font(Application.Theme.Font, 200);
Bitmap bmp = new Bitmap(1, 1);
bmp.SetResolution(100, 100);
@@ -53,10 +42,21 @@ internal class TimePlugin : Plugin
bmp.SetResolution(100, 100);
using Graphics gr = Graphics.FromImage(bmp);
+ using SolidBrush brush = new SolidBrush(Application.Theme.PrimaryColor);
gr.TextRenderingHint = TextRenderingHint.AntiAlias;
- gr.DrawString(time, font, new SolidBrush(Application.Theme.PrimaryColor), 0, 0);
+ gr.DrawString(time, font, brush, 0, 0);
return bmp;
}
+
+ public override void OnThemeChanged()
+ {
+ themeChanged = true;
+ }
+
+ public override void OnSettingsChanged()
+ {
+ themeChanged = true;
+ }
}
\ No newline at end of file
diff --git a/src/DesktopMagic/Plugins/PluginData.cs b/src/DesktopMagic/Plugins/PluginData.cs
index 5ba54ef..bf1a00a 100644
--- a/src/DesktopMagic/Plugins/PluginData.cs
+++ b/src/DesktopMagic/Plugins/PluginData.cs
@@ -1,7 +1,8 @@
-using DesktopMagic.Api;
+using CuteUtils.Logging;
+
+using DesktopMagic.Api;
using DesktopMagic.Settings;
-using System;
using System.Drawing;
namespace DesktopMagic.Plugins;
@@ -10,8 +11,6 @@ internal class PluginData(PluginWindow window, PluginSettings pluginSettings) :
{
private readonly PluginWindow window = window;
- public event EventHandler? ThemeChanged;
-
public ITheme Theme => pluginSettings.Theme;
public Size WindowSize => new Size((int)window.ActualWidth, (int)window.ActualHeight);
@@ -22,13 +21,29 @@ internal class PluginData(PluginWindow window, PluginSettings pluginSettings) :
public string PluginPath => window.PluginFolderPath;
+ public void Log(string message, LogLevel level = LogLevel.Info)
+ {
+ LogSeverity severity = level switch
+ {
+ LogLevel.Info => LogSeverity.Info,
+ LogLevel.Warning => LogSeverity.Warn,
+ LogLevel.Error => LogSeverity.Error,
+ _ => LogSeverity.Info,
+ };
+
+ App.Logger.Log($"\"{PluginName}\" - Stopping plugin", "Plugin", severity);
+ }
+
+ public void ShowMessage(string message, string title)
+ {
+ window.Dispatcher.Invoke(() =>
+ {
+ _ = System.Windows.MessageBox.Show(window, message, title);
+ });
+ }
+
public void UpdateWindow()
{
window.UpdatePluginWindow();
}
-
- internal void NotifyThemeChanged()
- {
- ThemeChanged?.Invoke(this, EventArgs.Empty);
- }
}
\ No newline at end of file
diff --git a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs
index 8e478e5..300fc85 100644
--- a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs
+++ b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs
@@ -209,7 +209,8 @@ public partial class PluginWindow : Window, IPluginWindow
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
- (pluginClassInstance?.Application as PluginData)?.NotifyThemeChanged();
+ pluginClassInstance?.OnThemeChanged();
+ pluginClassInstance?.Application.UpdateWindow();
}
private void ExecuteSource()
@@ -376,6 +377,12 @@ public partial class PluginWindow : Window, IPluginWindow
settingElement.JsonValue = settingsSettingElement.JsonValue;
}
+ element.OnValueChanged += () =>
+ {
+ pluginClassInstance?.OnSettingsChanged();
+ pluginClassInstance?.Application.UpdateWindow();
+ };
+
settingElements.Add(settingElement);
break;
}
diff --git a/src/DesktopMagicPluginAPI/IPluginData.cs b/src/DesktopMagicPluginAPI/IPluginData.cs
index 5a42904..e9ba593 100644
--- a/src/DesktopMagicPluginAPI/IPluginData.cs
+++ b/src/DesktopMagicPluginAPI/IPluginData.cs
@@ -1,5 +1,4 @@
-using System;
-using System.Drawing;
+using System.Drawing;
namespace DesktopMagic.Api;
@@ -8,14 +7,6 @@ namespace DesktopMagic.Api;
///
public interface IPluginData
{
- ///
- /// Occurs when the application's theme has changed.
- ///
- /// Subscribe to this event to be notified when the theme changes, allowing UI elements or
- /// components to update their appearance accordingly. The event is raised after the theme change has been
- /// applied.
- event EventHandler? ThemeChanged;
-
///
/// Gets the current theme setting of the main application.
///
@@ -45,4 +36,18 @@ public interface IPluginData
/// Updates the plugin window.
///
void UpdateWindow();
+
+ ///
+ /// Logs a message to the application log.
+ ///
+ /// The message to log.
+ /// The log level (Info, Warning, Error).
+ void Log(string message, LogLevel level = LogLevel.Info);
+
+ ///
+ /// Shows a message box to the user.
+ ///
+ /// The message to display.
+ /// The title of the message box.
+ void ShowMessage(string message, string title);
}
\ No newline at end of file
diff --git a/src/DesktopMagicPluginAPI/LogLevel.cs b/src/DesktopMagicPluginAPI/LogLevel.cs
new file mode 100644
index 0000000..f2d92dd
--- /dev/null
+++ b/src/DesktopMagicPluginAPI/LogLevel.cs
@@ -0,0 +1,22 @@
+namespace DesktopMagic.Api;
+
+///
+/// Specifies the severity level of a log message.
+///
+public enum LogLevel
+{
+ ///
+ /// Informational message for general information.
+ ///
+ Info,
+
+ ///
+ /// Warning message indicating a potential issue.
+ ///
+ Warning,
+
+ ///
+ /// Error message indicating a failure or critical issue.
+ ///
+ Error
+}
\ No newline at end of file
diff --git a/src/DesktopMagicPluginAPI/Plugin.cs b/src/DesktopMagicPluginAPI/Plugin.cs
index 911db6c..5677365 100644
--- a/src/DesktopMagicPluginAPI/Plugin.cs
+++ b/src/DesktopMagicPluginAPI/Plugin.cs
@@ -7,8 +7,14 @@ using System.Drawing;
namespace DesktopMagic.Api;
///
-/// The plugin class.
+/// Provides an abstract base class for creating plugins that can be integrated into the application, supporting periodic updates,
+/// rendering, and user interaction.
///
+/// Derive from this class to implement custom plugin functionality. The class defines lifecycle methods
+/// such as , , and for activation, deactivation, and periodic
+/// execution. It also provides event handlers for mouse and theme interactions, as well as access to application data
+/// and rendering configuration. Implementations should override relevant methods to respond to user input, update
+/// intervals, and configuration changes as needed.
public abstract class Plugin
{
[Setting("desktopmagic-horizontal-alignment", "Horizontal Alignment", -999)]
@@ -86,4 +92,18 @@ public abstract class Plugin
public virtual void OnMouseWheel(Point position, int delta)
{
}
-}
\ No newline at end of file
+
+ ///
+ /// Occurs when the application's theme has changed.
+ ///
+ public virtual void OnThemeChanged()
+ {
+ }
+
+ ///
+ /// Invoked when the settings have changed to allow derived classes to respond to configuration updates.
+ ///
+ public virtual void OnSettingsChanged()
+ {
+ }
+}