From 336de2ca42596f909435983924461637728ccf36 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 16 Nov 2023 22:34:23 +0100 Subject: [PATCH] Fix built in plugins --- .../CpuUsageWindow.xaml.cs | 221 +++++++++--------- .../BuiltInWindowElements/DatePlugin.cs | 2 +- .../MusicVisualizerPlugin.cs | 54 +++-- .../BuiltInWindowElements/TimePlugin.cs | 2 +- src/DesktopMagic/DesktopMagic.csproj | 13 ++ src/DesktopMagic/MainWindow.xaml | 40 ++-- src/DesktopMagic/MainWindow.xaml.cs | 168 +++---------- .../Inputs/IntegerUpDown.cs | 8 - src/DesktopMagicPluginAPI/Inputs/Slider.cs | 8 - 9 files changed, 202 insertions(+), 314 deletions(-) diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs index 904840f..5d0e1aa 100644 --- a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs +++ b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs @@ -10,138 +10,137 @@ using System.Windows.Interop; using System.Windows.Media; using System.Windows.Threading; -namespace DesktopMagic +namespace DesktopMagic; + +public partial class CpuUsageWindow : Window { - public partial class CpuUsageWindow : Window + private readonly RegistryKey key; + private readonly PerformanceCounter cpuCounter = new PerformanceCounter(); + + public CpuUsageWindow() { - private readonly RegistryKey key; - private readonly PerformanceCounter cpuCounter = new PerformanceCounter(); + InitializeComponent(); - public CpuUsageWindow() + Window w = new() { - InitializeComponent(); + Top = -100, + Left = -100, + Width = 0, + Height = 0, - Window w = new() - { - Top = -100, - Left = -100, - Width = 0, - Height = 0, + WindowStyle = WindowStyle.ToolWindow, + ShowInTaskbar = false + }; + w.Show(); + Owner = w; + w.Hide(); - WindowStyle = WindowStyle.ToolWindow, - ShowInTaskbar = false - }; - w.Show(); - Owner = w; - w.Hide(); + cpuCounter.CategoryName = "Processor"; + cpuCounter.CounterName = "% Processor Time"; + cpuCounter.InstanceName = "_Total"; - 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) + Timer t = new Timer { - base.OnSourceInitialized(e); + Interval = 100 + }; + t.Elapsed += UpdateTimer_Elapsed; + t.Start(); - //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) + Timer valueTimer = new Timer { - 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; - } + Interval = 1000 + }; + valueTimer.Elapsed += ValueTimer_Elapsed; - 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; + valueTimer.Start(); - ClculateWidth(); - }); - } + 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; + } - private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e) + 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(() => { - string cpuUsage = GetCpuUsage(); - Dispatcher.Invoke(() => + if (MainWindow.EditMode) { - valueTextBlock.Text = cpuUsage; - }); - } - - private void ClculateWidth() - { - string template = "CPU: ###%"; - double lenght = 0; - for (int i = 0; i < 9; i++) + panel.Visibility = Visibility.Visible; + tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10; + WindowPos.SetIsLocked(this, false); + ResizeMode = ResizeMode.CanResize; + } + else { - lenght = Math.Max(StringUtilities.MeasureString(template.Replace('#', i.ToString()[0]), textBlock).Width, lenght); + panel.Visibility = Visibility.Collapsed; + tileBar.CaptionHeight = 0; + WindowPos.SetIsLocked(this, true); + ResizeMode = ResizeMode.NoResize; } - dockPanel.Width = lenght; - } - private string GetCpuUsage() - { - return $"{(int)cpuCounter.NextValue()}%"; - } + 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; - private void Window_LocationChanged(object sender, EventArgs e) - { - key.SetValue("CpuUsageWindowTop", Top); - key.SetValue("CpuUsageWindowLeft", Left); - } + ClculateWidth(); + }); + } - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) + private void ValueTimer_Elapsed(object sender, ElapsedEventArgs e) + { + string cpuUsage = GetCpuUsage(); + Dispatcher.Invoke(() => { - key.SetValue("CpuUsageWindowHeight", Height); - key.SetValue("CpuUsageWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; + 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/DatePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs index 460921d..b47681a 100644 --- a/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs @@ -9,7 +9,7 @@ namespace DesktopMagic.BuiltInWindowElements; internal class DatePlugin : Plugin { - [Element("Short date:")] + [Element("Short date")] private readonly CheckBox shortDatecheckBox = new CheckBox(true); private DateTime oldDateTime = DateTime.MinValue; diff --git a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs index 95eea84..68704ad 100644 --- a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs @@ -1,6 +1,7 @@ using DesktopMagic.Helpers; using DesktopMagicPluginAPI; +using DesktopMagicPluginAPI.Inputs; using NAudio.Wave; @@ -19,6 +20,22 @@ internal class MusicVisualizerPlugin : Plugin private readonly SampleAggregator sampleAggregator = new SampleAggregator(fftLength); private readonly Bitmap output = new Bitmap(880, 300); + + [Element("Mirror")] + private readonly CheckBox mirrorMode = new CheckBox(false); + + [Element("Line")] + private readonly CheckBox lineMode = new CheckBox(false); + + [Element("Spectrum Mode")] + private readonly ComboBox spectrumMode = new ComboBox("Bottom", "Middle", "Top"); + + [Element("Amplification")] + private readonly IntegerUpDown amplifierLevel = new IntegerUpDown(-50, 50, 0); + + [Element("Line thickness")] + private readonly IntegerUpDown lineThickness = new IntegerUpDown(1, 10, 1); + private WasapiLoopbackCapture waveIn; private volatile bool calculate = true; private List lastFft = []; @@ -78,7 +95,7 @@ internal class MusicVisualizerPlugin : Plugin v = 20; } - int multiplier = 100 - (MainWindow.AmplifierLevel * 2); + int multiplier = 100 - (amplifierLevel.Value * 2); multiplier = multiplier == 0 ? 1 : multiplier; fft.Add(Math.Abs(e.Result[i].Y * v / multiplier)); } @@ -165,12 +182,12 @@ internal class MusicVisualizerPlugin : Plugin { int offset = 0; - if (!MainWindow.MirrorMode && MainWindow.SpectrumMode != 1) + if (!mirrorMode.Value && spectrumMode.Value != "Middle") { scaledFft.Insert(0, 0); } - if (!MainWindow.LineMode) + if (!lineMode.Value) { offset = 1; } @@ -183,17 +200,18 @@ internal class MusicVisualizerPlugin : Plugin int fftIndex = 0; bool fftIndexReverse = false; - if (MainWindow.MirrorMode || MainWindow.SpectrumMode == 1) + if (mirrorMode.Value || spectrumMode.Value == "Middle") { fftIndex = scaledFft.Count - 1; } + for (int pointIndex = 0; pointIndex < scaledFft.Count; pointIndex += 1) { int value = (int)Math.Max(scaledFft[fftIndex] * 50000, 0); - switch (MainWindow.SpectrumMode) + switch (spectrumMode.Value) { - case 1: + case "Middle": if (!fftIndexReverse) { @@ -202,7 +220,7 @@ internal class MusicVisualizerPlugin : Plugin } break; - case 2: + case "Top": points[pointIndex + 1] = new PointF(2 * pointIndex, value); break; @@ -211,7 +229,7 @@ internal class MusicVisualizerPlugin : Plugin break; } - if (MainWindow.MirrorMode || MainWindow.SpectrumMode == 1) + if (mirrorMode.Value || spectrumMode.Value == "Middle") { if (!fftIndexReverse) { @@ -231,20 +249,18 @@ internal class MusicVisualizerPlugin : Plugin SetPoints(points, output.Width, output.Height, offset); - Brush brush = MainWindow.MusicVisualzerColor.HasValue - ? new SolidBrush(MainWindow.MusicVisualzerColor.Value) - : new SolidBrush(MainWindow.Theme.PrimaryColor); + Brush brush = new SolidBrush(Application.Theme.PrimaryColor); - if (MainWindow.LineMode) + if (lineMode.Value) { - if (MainWindow.SpectrumMode == 1) + if (spectrumMode.Value == "Middle") { - gr.DrawLines(new Pen(brush), points.Take(points.Length / 2).ToArray()); - gr.DrawLines(new Pen(brush), points.Skip(points.Length / 2).ToArray()); + gr.DrawLines(new Pen(brush, lineThickness.Value), points.Take(points.Length / 2).ToArray()); + gr.DrawLines(new Pen(brush, lineThickness.Value), points.Skip(points.Length / 2).ToArray()); } else { - gr.DrawLines(new Pen(brush), points); + gr.DrawLines(new Pen(brush, lineThickness.Value), points); } } else @@ -263,9 +279,9 @@ internal class MusicVisualizerPlugin : Plugin private void SetPoints(PointF[] points, int width, int height, int offset) { - switch (MainWindow.SpectrumMode) + switch (spectrumMode.Value) { - case 1: + case "Middle": points[0] = new PointF(width, height / 2); points[^1] = new PointF(0, height / 2); @@ -273,7 +289,7 @@ internal class MusicVisualizerPlugin : Plugin points[(points.Length / 2) - 1] = new PointF(0, height / 2); break; - case 2: + case "Top": points[0] = new PointF(0, 0 - offset); points[^1] = new PointF(points[^2].X, 0 - offset); break; diff --git a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs index 07d375d..06530f9 100644 --- a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs @@ -9,7 +9,7 @@ namespace DesktopMagic.BuiltInWindowElements; internal class TimePlugin : Plugin { - [Element("Display Seconds:")] + [Element("Display Seconds")] private readonly CheckBox displaySecondscheckBox = new CheckBox(true); public override int UpdateInterval => 1000; diff --git a/src/DesktopMagic/DesktopMagic.csproj b/src/DesktopMagic/DesktopMagic.csproj index 1ac5379..dd6de0e 100644 --- a/src/DesktopMagic/DesktopMagic.csproj +++ b/src/DesktopMagic/DesktopMagic.csproj @@ -19,12 +19,25 @@ DEBUG;TRACE + + + + + + + + + + + + + diff --git a/src/DesktopMagic/MainWindow.xaml b/src/DesktopMagic/MainWindow.xaml index 1f0ff02..9c25e2a 100644 --- a/src/DesktopMagic/MainWindow.xaml +++ b/src/DesktopMagic/MainWindow.xaml @@ -31,14 +31,14 @@ - + - + @@ -46,24 +46,10 @@ - + - - - -