Add plugin methods for changed themes, settings showing message boxes and logging

This commit is contained in:
Stone_Red
2025-12-25 01:22:39 +01:00
parent 4778f5673f
commit 43c77e6a36
7 changed files with 115 additions and 49 deletions
+9 -12
View File
@@ -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;
}
}
+15 -15
View File
@@ -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;
}
}
+24 -9
View File
@@ -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);
}
}
@@ -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;
}
+15 -10
View File
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.Drawing;
namespace DesktopMagic.Api;
@@ -8,14 +7,6 @@ namespace DesktopMagic.Api;
/// </summary>
public interface IPluginData
{
/// <summary>
/// Occurs when the application's theme has changed.
/// </summary>
/// <remarks>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.</remarks>
event EventHandler? ThemeChanged;
/// <summary>
/// Gets the current theme setting of the main application.
/// </summary>
@@ -45,4 +36,18 @@ public interface IPluginData
/// Updates the plugin window.
/// </summary>
void UpdateWindow();
/// <summary>
/// Logs a message to the application log.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="level">The log level (Info, Warning, Error).</param>
void Log(string message, LogLevel level = LogLevel.Info);
/// <summary>
/// Shows a message box to the user.
/// </summary>
/// <param name="message">The message to display.</param>
/// <param name="title">The title of the message box.</param>
void ShowMessage(string message, string title);
}
+22
View File
@@ -0,0 +1,22 @@
namespace DesktopMagic.Api;
/// <summary>
/// Specifies the severity level of a log message.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Informational message for general information.
/// </summary>
Info,
/// <summary>
/// Warning message indicating a potential issue.
/// </summary>
Warning,
/// <summary>
/// Error message indicating a failure or critical issue.
/// </summary>
Error
}
+21 -1
View File
@@ -7,8 +7,14 @@ using System.Drawing;
namespace DesktopMagic.Api;
/// <summary>
/// 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.
/// </summary>
/// <remarks>Derive from this class to implement custom plugin functionality. The class defines lifecycle methods
/// such as <see cref="Start"/>, <see cref="Stop"/>, and <see cref="Main"/> 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.</remarks>
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)
{
}
/// <summary>
/// Occurs when the application's theme has changed.
/// </summary>
public virtual void OnThemeChanged()
{
}
/// <summary>
/// Invoked when the settings have changed to allow derived classes to respond to configuration updates.
/// </summary>
public virtual void OnSettingsChanged()
{
}
}