diff --git a/src/DesktopMagic.sln b/src/DesktopMagic.sln index aa5c869..3832c2c 100644 --- a/src/DesktopMagic.sln +++ b/src/DesktopMagic.sln @@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.Api", "Desktop EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.PluginTest", "DesktopMagicPlugin.Test\DesktopMagic.PluginTest.csproj", "{EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.Installer", "DesktopMagic.Installer\DesktopMagic.Installer.csproj", "{A06BD685-7F92-4799-846B-3EC38345108E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,10 +27,6 @@ Global {EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}.Debug|Any CPU.Build.0 = Debug|Any CPU {EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}.Release|Any CPU.ActiveCfg = Release|Any CPU {EF8407AB-28AE-4B5E-AC2E-D4C4A575FBC7}.Release|Any CPU.Build.0 = Release|Any CPU - {A06BD685-7F92-4799-846B-3EC38345108E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A06BD685-7F92-4799-846B-3EC38345108E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A06BD685-7F92-4799-846B-3EC38345108E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A06BD685-7F92-4799-846B-3EC38345108E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuMonitorPlugin.cs b/src/DesktopMagic/BuiltInWindowElements/CpuMonitorPlugin.cs new file mode 100644 index 0000000..537c99b --- /dev/null +++ b/src/DesktopMagic/BuiltInWindowElements/CpuMonitorPlugin.cs @@ -0,0 +1,60 @@ +using DesktopMagic.Api; +using DesktopMagic.Api.Settings; + +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Text; + +using SettingAttribute = DesktopMagic.Api.Settings.SettingAttribute; + +namespace DesktopMagic.BuiltInWindowElements; + +internal class CpuMonitorPlugin : Plugin +{ + private readonly PerformanceCounter cpuCounter = new PerformanceCounter("Processor Information", "% Processor Utility", "_Total"); + + [Setting("usage-circle", "Show pie")] + private readonly CheckBox usageCircleCheckBox = new CheckBox(false); + + public override int UpdateInterval => 1000; + + public override Bitmap Main() + { + float cpuUsage = cpuCounter.NextValue(); + + string cpuUsageString = $"{cpuUsage:000}%"; + + Font font = new Font(Application.Theme.Font, 200); + + Bitmap bmp = new Bitmap(1, 1); + bmp.SetResolution(100, 100); + using Graphics tmpGr = Graphics.FromImage(bmp); + tmpGr.TextRenderingHint = TextRenderingHint.AntiAlias; + + SizeF size = tmpGr.MeasureString(cpuUsageString, font); + + bmp = new Bitmap((int)size.Width, usageCircleCheckBox.Value ? (int)size.Width : (int)size.Height); + bmp.SetResolution(100, 100); + + using Graphics gr = Graphics.FromImage(bmp); + + gr.TextRenderingHint = TextRenderingHint.AntiAlias; + + if (usageCircleCheckBox.Value) + { + gr.FillPie(new SolidBrush(Application.Theme.SecondaryColor), 0, 0, size.Width, size.Width, 0, 360f / 100f * cpuUsage); + + gr.DrawArc(new Pen(Color.FromArgb(Application.Theme.PrimaryColor.A, Color.Green), 10), 5, 5, size.Width - 10, size.Width - 10, 0, 120); + gr.DrawArc(new Pen(Color.FromArgb(Application.Theme.PrimaryColor.A, Color.Orange), 10), 5, 5, size.Width - 10, size.Width - 10, 120, 120); + gr.DrawArc(new Pen(Color.FromArgb(Application.Theme.PrimaryColor.A, Color.Red), 10), 5, 5, size.Width - 10, size.Width - 10, 240, 120); + + gr.DrawString(cpuUsageString, font, new SolidBrush(Application.Theme.PrimaryColor), 0, (size.Width / 2) - (size.Height / 2)); + } + else + { + gr.DrawString(cpuUsageString, font, new SolidBrush(Application.Theme.PrimaryColor), 0, (size.Width / 2) - size.Height); + } + + return bmp; + } +} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml deleted file mode 100644 index a1b19b6..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs deleted file mode 100644 index 5d0e1aa..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs +++ /dev/null @@ -1,146 +0,0 @@ -using DesktopMagic.Helpers; - -using Microsoft.Win32; - -using System; -using System.Diagnostics; -using System.Timers; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; -using System.Windows.Threading; - -namespace DesktopMagic; - -public partial class CpuUsageWindow : Window -{ - private readonly RegistryKey key; - private readonly PerformanceCounter cpuCounter = new PerformanceCounter(); - - public CpuUsageWindow() - { - InitializeComponent(); - - Window w = new() - { - Top = -100, - Left = -100, - Width = 0, - Height = 0, - - WindowStyle = WindowStyle.ToolWindow, - ShowInTaskbar = false - }; - w.Show(); - Owner = w; - w.Hide(); - - cpuCounter.CategoryName = "Processor"; - cpuCounter.CounterName = "% Processor Time"; - cpuCounter.InstanceName = "_Total"; - - Timer t = new Timer - { - Interval = 100 - }; - t.Elapsed += UpdateTimer_Elapsed; - t.Start(); - - Timer valueTimer = new Timer - { - Interval = 1000 - }; - valueTimer.Elapsed += ValueTimer_Elapsed; - - valueTimer.Start(); - - key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - Top = double.Parse(key.GetValue("CpuUsageWindowTop", 100).ToString()); - Left = double.Parse(key.GetValue("CpuUsageWindowLeft", 100).ToString()); - Height = double.Parse(key.GetValue("CpuUsageWindowHeight", 200).ToString()); - Width = double.Parse(key.GetValue("CpuUsageWindowWidth", 500).ToString()); - IsEnabled = false; - } - - protected override void OnSourceInitialized(EventArgs e) - { - base.OnSourceInitialized(e); - - //Set the window style to noactivate. - WindowInteropHelper helper = new WindowInteropHelper(this); - _ = WindowPos.SetWindowLong(helper.Handle, WindowPos.GWL_EXSTYLE, - WindowPos.GetWindowLong(helper.Handle, WindowPos.GWL_EXSTYLE) | WindowPos.WS_EX_NOACTIVATE); - } - - private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e) - { - Dispatcher.Invoke(() => - { - if (MainWindow.EditMode) - { - panel.Visibility = Visibility.Visible; - tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10; - WindowPos.SetIsLocked(this, false); - ResizeMode = ResizeMode.CanResize; - } - else - { - panel.Visibility = Visibility.Collapsed; - tileBar.CaptionHeight = 0; - WindowPos.SetIsLocked(this, true); - ResizeMode = ResizeMode.NoResize; - } - - rectangleGeometry.Rect = new Rect(0, 0, border.ActualWidth, border.ActualHeight); - border.Background = MainWindow.Theme.BackgroundBrush; - border.CornerRadius = new CornerRadius(MainWindow.Theme.CornerRadius); - viewBox.Margin = new Thickness(MainWindow.Theme.Margin); - border.Width = viewBox.ActualWidth + (MainWindow.Theme.Margin * 2); - border.Height = viewBox.ActualHeight + (MainWindow.Theme.Margin * 2); - textBlock.FontFamily = new FontFamily(MainWindow.Theme.Font); - textBlock.Foreground = MainWindow.Theme.PrimaryBrush; - valueTextBlock.FontFamily = new FontFamily(MainWindow.Theme.Font); - valueTextBlock.Foreground = MainWindow.Theme.PrimaryBrush; - - ClculateWidth(); - }); - } - - private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e) - { - string cpuUsage = GetCpuUsage(); - Dispatcher.Invoke(() => - { - valueTextBlock.Text = cpuUsage; - }); - } - - private void ClculateWidth() - { - string template = "CPU: ###%"; - double lenght = 0; - for (int i = 0; i < 9; i++) - { - lenght = Math.Max(StringUtilities.MeasureString(template.Replace('#', i.ToString()[0]), textBlock).Width, lenght); - } - dockPanel.Width = lenght; - } - - private string GetCpuUsage() - { - return $"{(int)cpuCounter.NextValue()}%"; - } - - private void Window_LocationChanged(object sender, EventArgs e) - { - key.SetValue("CpuUsageWindowTop", Top); - key.SetValue("CpuUsageWindowLeft", Left); - } - - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) - { - key.SetValue("CpuUsageWindowHeight", Height); - key.SetValue("CpuUsageWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; - } -} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs index f6c77fe..b25d73c 100644 --- a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs @@ -12,8 +12,6 @@ internal class TimePlugin : Plugin [Setting("display-seconds", "Display Seconds")] private readonly CheckBox displaySecondsCheckBox = new CheckBox(true); - private string? oldFont = null; - private float maxWidth = 0; public override int UpdateInterval => 1000; public override Bitmap Main() @@ -29,18 +27,7 @@ internal class TimePlugin : Plugin SizeF size = tmpGr.MeasureString(time, font); - if (oldFont != Application.Theme.Font) - { - oldFont = Application.Theme.Font; - maxWidth = 0; - } - - if (size.Width > maxWidth) - { - maxWidth = size.Width; - } - - bmp = new Bitmap((int)maxWidth, (int)size.Height); + bmp = new Bitmap((int)size.Width, (int)size.Height); bmp.SetResolution(100, 100); using Graphics gr = Graphics.FromImage(bmp); diff --git a/src/DesktopMagic/DesktopMagic.csproj b/src/DesktopMagic/DesktopMagic.csproj index 3384e9d..02bf5e4 100644 --- a/src/DesktopMagic/DesktopMagic.csproj +++ b/src/DesktopMagic/DesktopMagic.csproj @@ -20,25 +20,12 @@ DEBUG;TRACE - - - - - - - - - - - - - diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index 1f3a345..755b32d 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -29,7 +29,8 @@ namespace DesktopMagic { {"Music Visualizer", typeof(MusicVisualizerPlugin)}, {"Time", typeof(TimePlugin)}, - {"Date", typeof(DatePlugin)} + {"Date", typeof(DatePlugin)}, + {"Cpu Usage", typeof(CpuMonitorPlugin)} }; private bool loaded = false; @@ -338,7 +339,8 @@ namespace DesktopMagic DockPanel dockPanel = new() { LastChildFill = true, - HorizontalAlignment = HorizontalAlignment.Stretch + HorizontalAlignment = HorizontalAlignment.Stretch, + Margin = new Thickness(0, 0, 0, 5) }; _ = optionsPanel.Children.Add(dockPanel); diff --git a/src/DesktopMagic/Plugins/PluginWindow.xaml b/src/DesktopMagic/Plugins/PluginWindow.xaml index d2cd1b9..770a642 100644 --- a/src/DesktopMagic/Plugins/PluginWindow.xaml +++ b/src/DesktopMagic/Plugins/PluginWindow.xaml @@ -17,12 +17,14 @@ - + - + - + + + diff --git a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs index 516901d..0864cb6 100644 --- a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs @@ -131,6 +131,8 @@ public partial class PluginWindow : Window if (MainWindow.EditMode) { panel.Visibility = Visibility.Visible; + imageBorder.BorderThickness = new Thickness(3); + image.Margin = new(-3); WindowPos.SetIsLocked(this, false); tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10; ResizeMode = ResizeMode.CanResize; @@ -138,6 +140,8 @@ public partial class PluginWindow : Window else { panel.Visibility = Visibility.Collapsed; + imageBorder.BorderThickness = new Thickness(0); + image.Margin = new Thickness(0); WindowPos.SetIsLocked(this, true); tileBar.CaptionHeight = 0; ResizeMode = ResizeMode.NoResize; @@ -224,7 +228,8 @@ public partial class PluginWindow : Window return; } - LoadOptions(instance); + LoadOptions(pluginClassInstance); + BindDefaultSettings(pluginClassInstance); valueTimer = new System.Timers.Timer { @@ -242,6 +247,40 @@ public partial class PluginWindow : Window } } + private void BindDefaultSettings(Plugin pluginClassInstance) + { + Dispatcher.Invoke(() => + { + SetHorizontalAlignment(); + SetVerticalAlignment(); + + pluginClassInstance.horizontalAlignment.OnValueChanged += SetHorizontalAlignment; + pluginClassInstance.verticalAlignment.OnValueChanged += SetVerticalAlignment; + }); + + void SetVerticalAlignment() + { + viewBox.VerticalAlignment = pluginClassInstance.verticalAlignment.Value switch + { + "Top" => VerticalAlignment.Top, + "Center" => VerticalAlignment.Center, + "Bottom" => VerticalAlignment.Bottom, + _ => VerticalAlignment.Center + }; + } + + void SetHorizontalAlignment() + { + viewBox.HorizontalAlignment = pluginClassInstance.horizontalAlignment.Value switch + { + "Left" => HorizontalAlignment.Left, + "Center" => HorizontalAlignment.Center, + "Right" => HorizontalAlignment.Right, + _ => HorizontalAlignment.Center + }; + } + } + private void LoadOptions(object instance) { App.Logger.Log($"\"{PluginName}\" - Loading plugin options", "Plugin"); @@ -294,7 +333,7 @@ public partial class PluginWindow : Window { if (IsRunning && pluginClassInstance is not null) { - Bitmap result = pluginClassInstance.Main(); + Bitmap? result = pluginClassInstance.Main(); if (pluginClassInstance.UpdateInterval > 0) { diff --git a/src/DesktopMagicPluginAPI/Plugin.cs b/src/DesktopMagicPluginAPI/Plugin.cs index d0f5326..c7b61b6 100644 --- a/src/DesktopMagicPluginAPI/Plugin.cs +++ b/src/DesktopMagicPluginAPI/Plugin.cs @@ -11,7 +11,13 @@ namespace DesktopMagic.Api; /// public abstract class Plugin { - private IPluginData application = null; + [Setting("horizontalAlignment", "Horizontal Alignment", -999)] + internal ComboBox horizontalAlignment = new ComboBox("Center", "Left", "Right"); + + [Setting("verticalAlignment", "Vertical Alignment", -998)] + internal ComboBox verticalAlignment = new ComboBox("Center", "Top", "Bottom"); + + private IPluginData application = null!; /// /// Informations about the main application. diff --git a/src/DesktopMagicPluginAPI/Settings/ComboBox.cs b/src/DesktopMagicPluginAPI/Settings/ComboBox.cs index caeb7ec..dd24916 100644 --- a/src/DesktopMagicPluginAPI/Settings/ComboBox.cs +++ b/src/DesktopMagicPluginAPI/Settings/ComboBox.cs @@ -1,5 +1,4 @@ using System.Collections.ObjectModel; -using System.Text.Json; namespace DesktopMagic.Api.Settings; @@ -8,7 +7,7 @@ namespace DesktopMagic.Api.Settings; /// public class ComboBox : Setting { - private string _value; + private string _value = string.Empty; /// /// Gets the collection used to generate the content of the . @@ -38,6 +37,8 @@ public class ComboBox : Setting /// public ComboBox(params string[] items) { + Value = items[0]; + foreach (string item in items) { Items.Add(item); @@ -46,7 +47,7 @@ public class ComboBox : Setting internal override string GetJsonValue() { - return JsonSerializer.Serialize(new { Value, Items }); + return Value; } internal override void SetJsonValue(string value) @@ -56,12 +57,6 @@ public class ComboBox : Setting return; } - JsonElement json = JsonSerializer.Deserialize(value); - Items.Clear(); - foreach (JsonElement item in json.GetProperty(nameof(Items)).EnumerateArray()) - { - Items.Add(item.GetString() ?? string.Empty); - } - Value = json.GetProperty(nameof(Value)).GetString() ?? string.Empty; + Value = value; } } \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs b/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs index 1c55775..070423f 100644 --- a/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs +++ b/src/DesktopMagicPluginAPI/Settings/SettingAttribute.cs @@ -5,13 +5,13 @@ namespace DesktopMagic.Api.Settings; /// /// Marks a Property as element. /// -[AttributeUsage(AttributeTargets.Field)] +[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public class SettingAttribute : Attribute { /// /// The name of the element. /// - public string Name { get; set; } + public string Name { get; set; } = string.Empty; /// /// The order index of the element.