mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 00:46:12 +02:00
Add ThemeChanged event to IPluginData
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user