Add ThemeChanged event to IPluginData

This commit is contained in:
Stone_Red
2025-12-24 23:58:53 +01:00
parent c90be28f38
commit e3d6eb4c8c
5 changed files with 49 additions and 16 deletions
+13 -7
View File
@@ -13,22 +13,28 @@ internal class DatePlugin : Plugin
private readonly CheckBox shortDateCheckBox = new CheckBox(true);
private DateTime oldDateTime = DateTime.MinValue;
private Color oldColor = Color.White;
private string oldFont = string.Empty;
private bool oldShortDateCheckBoxValue;
private bool themeChanged = false;
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 && oldColor == Application.Theme.PrimaryColor && oldFont == Application.Theme.Font && oldShortDateCheckBoxValue == shortDateCheckBox.Value)
if (oldDateTime.Date == DateTime.Now.Date && !themeChanged)
{
return null;
}
oldDateTime = DateTime.Now;
oldColor = Application.Theme.PrimaryColor;
oldFont = Application.Theme.Font;
oldShortDateCheckBoxValue = shortDateCheckBox.Value;
string date = shortDateCheckBox.Value ? DateTime.Now.ToShortDateString() : DateTime.Now.ToLongDateString();
+15 -7
View File
@@ -13,24 +13,32 @@ internal class TimePlugin : Plugin
private readonly CheckBox displaySecondsCheckBox = new CheckBox(true);
private string oldTime = string.Empty;
private Color oldColor = Color.White;
private string oldFont = string.Empty;
private bool oldDisplaySecondsCheckBoxValue;
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();
if (oldTime == time && oldColor == Application.Theme.PrimaryColor && oldFont == Application.Theme.Font && oldDisplaySecondsCheckBoxValue == displaySecondsCheckBox.Value)
if (oldTime == time && !themeChanged)
{
return null;
}
oldTime = time;
oldColor = Application.Theme.PrimaryColor;
oldFont = Application.Theme.Font;
oldDisplaySecondsCheckBoxValue = displaySecondsCheckBox.Value;
Font font = new Font(Application.Theme.Font, 200);
+8
View File
@@ -1,6 +1,7 @@
using DesktopMagic.Api;
using DesktopMagic.Settings;
using System;
using System.Drawing;
namespace DesktopMagic.Plugins;
@@ -9,6 +10,8 @@ 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);
@@ -23,4 +26,9 @@ internal class PluginData(PluginWindow window, PluginSettings pluginSettings) :
{
window.UpdatePluginWindow();
}
internal void NotifyThemeChanged()
{
ThemeChanged?.Invoke(this, EventArgs.Empty);
}
}
@@ -104,7 +104,6 @@ public partial class PluginWindow : Window, IPluginWindow
public void UpdatePluginWindow()
{
Dispatcher.Invoke(ThemeChanged);
UpdateTimer_Elapsed(updateTimer, null);
}
@@ -209,6 +208,8 @@ public partial class PluginWindow : Window, IPluginWindow
viewBox.Margin = new Thickness(settings.Theme.Margin);
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
(pluginClassInstance?.Application as PluginData)?.NotifyThemeChanged();
}
private void ExecuteSource()
@@ -267,6 +268,7 @@ public partial class PluginWindow : Window, IPluginWindow
pluginClassInstance.Start();
UpdatePluginWindow();
Dispatcher.Invoke(ThemeChanged);
if (pluginClassInstance.UpdateInterval > 0)
{
+10 -1
View File
@@ -1,4 +1,5 @@
using System.Drawing;
using System;
using System.Drawing;
namespace DesktopMagic.Api;
@@ -7,6 +8,14 @@ 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>