From 2f6662caea892606a49bb2c6cc526642a349b45e Mon Sep 17 00:00:00 2001 From: Stone-Red-Code <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:31:16 +0200 Subject: [PATCH] - Add `Margin` option - Add `Margin` property to `ITheme` - Add `Stop` method to the `Plugin` API - Add `DrawStringNoLeftPadding` and `MeasureStringNoLeftPadding` to `GraphicsExtentions` - Convert most inbuilt components to plugins. - Minor code improvements --- .../BuiltInWindowElements/CalendarWindow.xaml | 38 ----- .../CalendarWindow.xaml.cs | 154 ------------------ .../BuiltInWindowElements/CpuUsageWindow.xaml | 6 +- .../CpuUsageWindow.xaml.cs | 3 + .../BuiltInWindowElements/DatePlugin.cs | 50 ++++++ .../BuiltInWindowElements/DateWindow.xaml | 28 ---- .../BuiltInWindowElements/DateWindow.xaml.cs | 95 ----------- ...indow.xaml.cs => MusicVisualizerPlugin.cs} | 136 ++-------------- .../MusicVisualizerWindow.xaml | 28 ---- .../BuiltInWindowElements/TimePlugin.cs | 51 ++++++ .../BuiltInWindowElements/TimeWindow.xaml | 27 --- .../BuiltInWindowElements/TimeWindow.xaml.cs | 111 ------------- .../Helpers/SettingElementGenerator.cs | 2 +- src/DesktopMagic/MainWindow.xaml | 12 +- src/DesktopMagic/MainWindow.xaml.cs | 107 +++++++----- src/DesktopMagic/Plugins/PluginWindow.xaml | 4 +- src/DesktopMagic/Plugins/PluginWindow.xaml.cs | 77 +++++---- src/DesktopMagic/Plugins/Theme.cs | 1 + .../DesktopMagicPluginAPI.md | 87 +++++++++- .../DesktopMagicPluginAPI.xml | 50 +++++- .../Drawing/GraphicsExtentions.cs | 82 +++++++++- src/DesktopMagicPluginAPI/ITheme.cs | 5 + src/DesktopMagicPluginAPI/Plugin.cs | 7 + 23 files changed, 463 insertions(+), 698 deletions(-) delete mode 100644 src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml delete mode 100644 src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml.cs create mode 100644 src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs delete mode 100644 src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml delete mode 100644 src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml.cs rename src/DesktopMagic/BuiltInWindowElements/{MusicVisualizerWindow.xaml.cs => MusicVisualizerPlugin.cs} (62%) delete mode 100644 src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml create mode 100644 src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs delete mode 100644 src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml delete mode 100644 src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml.cs diff --git a/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml deleted file mode 100644 index 27c433c..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml.cs deleted file mode 100644 index 1c3c7af..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/CalendarWindow.xaml.cs +++ /dev/null @@ -1,154 +0,0 @@ -using Microsoft.Win32; - -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Timers; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; - -namespace DesktopMagic -{ - [Obsolete("Disabled due to lack of support (too much work)")] - public partial class CalendarWindow : Window - { - private RegistryKey key; - private string oldFont = ""; - private Brush oldColor; - private List upcomingEventNames = new List(); - private List upcomingEventTimes = new List(); - - public CalendarWindow() - { - InitializeComponent(); - - Window w = new Window(); - w.Top = -100; - w.Left = -100; - w.Width = 0; - w.Height = 0; - - w.WindowStyle = WindowStyle.ToolWindow; - w.ShowInTaskbar = false; - w.Show(); - Owner = w; - w.Hide(); - - Timer t = new Timer(); - t.Interval = 100; - t.Elapsed += UpdateTimer_Elapsed; - t.Start(); - - Timer valueTimer = new Timer(); - valueTimer.Interval = 600000; - valueTimer.Elapsed += ValueTimer_Elapsed; - ; - valueTimer.Start(); - - key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - Top = double.Parse(key.GetValue("CalendarWindowTop", 100).ToString()); - Left = double.Parse(key.GetValue("CalendarWindowLeft", 100).ToString()); - Height = double.Parse(key.GetValue("CalendarWindowHeight", 200).ToString()); - Width = double.Parse(key.GetValue("CalendarWindowWidth", 500).ToString()); - //this.IsEnabled = false; - - Task.Run(() => - { - (upcomingEventNames, upcomingEventTimes) = new CalendarManagment().GetEvents(); - Dispatcher.Invoke(() => - { - LoadEvents(); - }); - }); - } - - 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 ValueTimer_Elapsed(object sender, ElapsedEventArgs e) - { - Task.Run(() => - { - (upcomingEventNames, upcomingEventTimes) = new CalendarManagment().GetEvents(); - Dispatcher.Invoke(() => - { - LoadEvents(); - }); - }); - } - - private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e) - { - Dispatcher.Invoke(() => - { - if (MainWindow.EditMode) - { - panel.Visibility = Visibility.Visible; - WindowPos.SetIsLocked(this, false); - } - else - { - panel.Visibility = Visibility.Collapsed; - WindowPos.SetIsLocked(this, true); - } - if (MainWindow.Theme.Font != oldFont || MainWindow.Theme.PrimaryBrush != oldColor) - { - oldColor = MainWindow.Theme.PrimaryBrush; - oldFont = MainWindow.Theme.Font; - LoadEvents(); - } - listBox.SelectedIndex = -1; - }); - } - - private void LoadEvents() - { - listBox.Items.Clear(); - CalendarItems calendarItem = new CalendarItems(); - calendarItem.Eventname = "Termine: "; - calendarItem.Font = MainWindow.Theme.Font; - calendarItem.Color = MainWindow.Theme.PrimaryColor.ToString(); - listBox.Items.Add(calendarItem); - - for (int i = 0; i < upcomingEventNames.Count; i++) - { - calendarItem = new CalendarItems(); - calendarItem.Eventname = upcomingEventNames[i]; - calendarItem.DateTime = DateTime.Now.ToString("dd-MM-yyyy"); - calendarItem.Font = MainWindow.Theme.Font; - calendarItem.Color = MainWindow.Theme.PrimaryBrush.ToString(); - - if (DateTime.Now < DateTime.Today.AddMonths(12) || upcomingEventTimes[i] == "-") - { - listBox.Items.Add(calendarItem); - } - } - - while (listBox.Items.Count < 10) - { - listBox.Items.Add(""); - } - } - - private void Window_LocationChanged(object sender, EventArgs e) - { - key.SetValue("CalendarWindowTop", Top); - key.SetValue("CalendarWindowLeft", Left); - } - - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) - { - key.SetValue("CalendarWindowHeight", Height); - key.SetValue("CalendarWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; - } - } -} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml index 59cad89..a1b19b6 100644 --- a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml +++ b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml @@ -14,14 +14,14 @@ x:Name="window"> - + - - + + diff --git a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs index 1ba4cc1..db0dcce 100644 --- a/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs +++ b/src/DesktopMagic/BuiltInWindowElements/CpuUsageWindow.xaml.cs @@ -90,6 +90,9 @@ namespace DesktopMagic 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); diff --git a/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs new file mode 100644 index 0000000..06d6a57 --- /dev/null +++ b/src/DesktopMagic/BuiltInWindowElements/DatePlugin.cs @@ -0,0 +1,50 @@ +using DesktopMagicPluginAPI; + +using System; +using System.Drawing; +using System.Drawing.Text; + +namespace DesktopMagic.BuiltInWindowElements +{ + internal class DatePlugin : Plugin + { + public override int UpdateInterval => 1000; + + private DateTime oldDateTime = new DateTime(); + private Color oldColor = Color.White; + private string oldFont; + + public override Bitmap Main() + { + if (oldDateTime.Date == DateTime.Now.Date && oldColor == Application.Theme.PrimaryColor && oldFont == Application.Theme.Font) + { + return null; + } + + oldDateTime = DateTime.Now; + oldColor = Application.Theme.PrimaryColor; + oldFont = Application.Theme.Font; + + string date = DateTime.Now.ToLongDateString(); + + 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(date, font); + + bmp = new Bitmap((int)size.Width, (int)size.Height); + bmp.SetResolution(100, 100); + + using Graphics gr = Graphics.FromImage(bmp); + + gr.TextRenderingHint = TextRenderingHint.AntiAlias; + gr.DrawString(date, font, new SolidBrush(Application.Theme.PrimaryColor), 0, 0); + + return bmp; + } + } +} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml deleted file mode 100644 index 432daf9..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml.cs deleted file mode 100644 index 71fe0d9..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/DateWindow.xaml.cs +++ /dev/null @@ -1,95 +0,0 @@ -using Microsoft.Win32; - -using System; -using System.Timers; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; - -namespace DesktopMagic -{ - public partial class DateWindow : Window - { - private RegistryKey key; - - public DateWindow() - { - InitializeComponent(); - - Window w = new Window(); - w.Top = -100; - w.Left = -100; - w.Width = 0; - w.Height = 0; - - w.WindowStyle = WindowStyle.ToolWindow; - w.ShowInTaskbar = false; - w.Show(); - Owner = w; - w.Hide(); - - Timer t = new Timer(); - t.Interval = 100; - t.Elapsed += UpdateTimer_Elapsed; - t.Start(); - - key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - Top = double.Parse(key.GetValue("DateWindowTop", 100).ToString()); - Left = double.Parse(key.GetValue("DateWindowLeft", 100).ToString()); - Height = double.Parse(key.GetValue("DateWindowHeight", 200).ToString()); - Width = double.Parse(key.GetValue("DateWindowWidth", 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); - textBlock.FontFamily = new FontFamily(MainWindow.Theme.Font); - textBlock.Foreground = MainWindow.Theme.PrimaryBrush; - textBlock.Text = DateTime.Now.ToLongDateString(); - }); - } - - private void Window_LocationChanged(object sender, EventArgs e) - { - key.SetValue("DateWindowTop", Top); - key.SetValue("DateWindowLeft", Left); - } - - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) - { - key.SetValue("DateWindowHeight", Height); - key.SetValue("DateWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; - } - } -} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs similarity index 62% rename from src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml.cs rename to src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs index 37fea35..24216a0 100644 --- a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml.cs +++ b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs @@ -1,51 +1,24 @@ -using Microsoft.Win32; +using DesktopMagicPluginAPI; using NAudio.Wave; using System; using System.Collections.Generic; using System.Drawing; -using System.Drawing.Imaging; -using System.Globalization; using System.Linq; -using System.Runtime.InteropServices; -using System.Timers; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media.Imaging; -using System.Windows.Threading; -namespace DesktopMagic +namespace DesktopMagic.BuiltInWindowElements { - public partial class MusicVisualizerWindow : Window + internal class MusicVisualizerPlugin : Plugin { - [DllImport("user32.dll")] - private static extern void SetParent(IntPtr hWndChild, IntPtr hWndNewParent); - - private readonly RegistryKey key; - private readonly IWaveIn waveIn; + private IWaveIn waveIn; private const int fftLength = 1024; // NAudio fft wants powers of two! private readonly SampleAggregator sampleAggregator = new SampleAggregator(fftLength); private volatile bool calculate = true; + private Bitmap output = new Bitmap(880, 300); - public MusicVisualizerWindow() + public override void Start() { - InitializeComponent(); - - Window w = new() - { - Top = -100, - Left = -100, - Width = 0, - Height = 0, - WindowStyle = WindowStyle.ToolWindow, - ShowInTaskbar = false - }; - - w.Show(); - Owner = w; - w.Hide(); - sampleAggregator.FftCalculated += new EventHandler(FftCalculated); sampleAggregator.PerformFFT = true; @@ -53,58 +26,6 @@ namespace DesktopMagic waveIn.DataAvailable += OnDataAvailable; waveIn.StartRecording(); - - Timer t = new Timer(); - t.Interval = 100; - t.Elapsed += UpdateTimer_Elapsed; - t.Start(); - - key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - - Top = double.Parse(key.GetValue("MusicVisualizerWindowTop", 100).ToString(), CultureInfo.InvariantCulture); - - Left = double.Parse(key.GetValue("MusicVisualizerWindowLeft", 100).ToString(), CultureInfo.InvariantCulture); - - Height = double.Parse(key.GetValue("MusicVisualizerWindowHeight", 200).ToString(), CultureInfo.InvariantCulture); - - Width = double.Parse(key.GetValue("MusicVisualizerWindowWidth", 500).ToString(), CultureInfo.InvariantCulture); - - 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; - } - calculate = IsLoaded; - rectangleGeometry.Rect = new Rect(0, 0, border.ActualWidth, border.ActualHeight); - border.CornerRadius = new CornerRadius(MainWindow.Theme.CornerRadius); - border.Background = MainWindow.Theme.BackgroundBrush; - }); } private void OnDataAvailable(object sender, WaveInEventArgs e) @@ -226,7 +147,6 @@ namespace DesktopMagic lastFft = fft; try { - Bitmap bm = new Bitmap(880, 300); int offset = 0; if (!MainWindow.MirrorMode && MainWindow.SpectrumMode != 1) @@ -239,8 +159,9 @@ namespace DesktopMagic offset = 1; } - using (Graphics gr = Graphics.FromImage(bm)) + using (Graphics gr = Graphics.FromImage(output)) { + gr.Clear(Color.Transparent); PointF[] points = new PointF[scaledFft.Count + 2]; int fftIndex = 0; @@ -260,8 +181,8 @@ namespace DesktopMagic if (!fftIndexReverse) { - points[pointIndex + 1] = new PointF(bm.Width - (4 * pointIndex), (bm.Height / 2) + value); - points[(points.Length / 2) + pointIndex + 1] = new PointF(bm.Width - (4 * pointIndex), (bm.Height / 2) - value); + points[pointIndex + 1] = new PointF(output.Width - (4 * pointIndex), (output.Height / 2) + value); + points[(points.Length / 2) + pointIndex + 1] = new PointF(output.Width - (4 * pointIndex), (output.Height / 2) - value); } break; @@ -270,7 +191,7 @@ namespace DesktopMagic break; default: - points[pointIndex + 1] = new PointF(2 * pointIndex, bm.Height - value - 1 + offset); + points[pointIndex + 1] = new PointF(2 * pointIndex, output.Height - value - 1 + offset); break; } @@ -292,7 +213,7 @@ namespace DesktopMagic } } - SetPoints(points, bm.Width, bm.Height, offset); + SetPoints(points, output.Width, output.Height, offset); Brush brush = MainWindow.MusicVisualzerColor.HasValue ? new SolidBrush(MainWindow.MusicVisualzerColor.Value) @@ -316,10 +237,7 @@ namespace DesktopMagic } } - _ = Dispatcher.BeginInvoke((Action)delegate - { - image.Source = BitmapToImageSource(bm); - }); + Application.UpdateWindow(); } catch { } } @@ -348,33 +266,15 @@ namespace DesktopMagic } } - private BitmapSource BitmapToImageSource(Bitmap bitmap) + public override Bitmap Main() { - BitmapData bitmapData = bitmap.LockBits( - new Rectangle(0, 0, bitmap.Width, bitmap.Height), - ImageLockMode.ReadOnly, bitmap.PixelFormat); - - BitmapSource bitmapSource = BitmapSource.Create( - bitmapData.Width, bitmapData.Height, - bitmap.HorizontalResolution, bitmap.VerticalResolution, - System.Windows.Media.PixelFormats.Bgra32, null, - bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride); - - bitmap.UnlockBits(bitmapData); - return bitmapSource; + return output; } - private void Window_LocationChanged(object sender, EventArgs e) + public override void Stop() { - key.SetValue("MusicVisualizerWindowTop", Top); - key.SetValue("MusicVisualizerWindowLeft", Left); - } - - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) - { - key.SetValue("MusicVisualizerWindowHeight", Height); - key.SetValue("MusicVisualizerWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; + calculate = false; + waveIn?.StopRecording(); } } } \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml deleted file mode 100644 index bca74e6..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerWindow.xaml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs new file mode 100644 index 0000000..ae45b58 --- /dev/null +++ b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs @@ -0,0 +1,51 @@ +using DesktopMagicPluginAPI; +using DesktopMagicPluginAPI.Drawing; + +using System; +using System.Drawing; +using System.Drawing.Text; + +namespace DesktopMagic.BuiltInWindowElements +{ + internal class TimePlugin : Plugin + { + public override int UpdateInterval => 1000; + + public override Bitmap Main() + { + string time = DateTime.Now.ToLongTimeString(); + + Font font = new Font(Application.Theme.Font, 200); + + Bitmap bmp = new Bitmap(1, 1); + using Graphics tmpGr = Graphics.FromImage(bmp); + tmpGr.TextRenderingHint = TextRenderingHint.AntiAlias; + + SizeF size = CalculateSize(tmpGr, font); + + bmp = new Bitmap((int)size.Width, (int)size.Height); + bmp.SetResolution(100, 100); + + using Graphics gr = Graphics.FromImage(bmp); + + gr.TextRenderingHint = TextRenderingHint.AntiAlias; + gr.DrawStringNoLeftPadding(time, font, new SolidBrush(Application.Theme.PrimaryColor), 0, 0); + + return bmp; + } + + private SizeF CalculateSize(Graphics graphics, Font font) + { + string template = "##:##:##"; + SizeF size = new SizeF(); + for (int i = 0; i < 9; i++) + { + SizeF newSize = graphics.MeasureStringNoLeftPadding(template.Replace("#", i.ToString()), font); + + size.Width = Math.Max(size.Width, newSize.Width); + size.Height = Math.Max(size.Height, newSize.Height); + } + return size; + } + } +} \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml b/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml deleted file mode 100644 index b70eedd..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml.cs b/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml.cs deleted file mode 100644 index edc65b9..0000000 --- a/src/DesktopMagic/BuiltInWindowElements/TimeWindow.xaml.cs +++ /dev/null @@ -1,111 +0,0 @@ -using DesktopMagic.Helpers; - -using Microsoft.Win32; - -using System; -using System.Timers; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; - -namespace DesktopMagic -{ - public partial class TimeWindow : Window - { - private RegistryKey key; - - public TimeWindow() - { - InitializeComponent(); - - Window w = new Window(); - w.Top = -100; - w.Left = -100; - w.Width = 0; - w.Height = 0; - - w.WindowStyle = WindowStyle.ToolWindow; - w.ShowInTaskbar = false; - w.Show(); - Owner = w; - w.Hide(); - - Timer t = new Timer(); - t.Interval = 100; - t.Elapsed += UpdateTimer_Elapsed; - t.Start(); - - key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - Top = double.Parse(key.GetValue("TimeWindowTop", 100).ToString()); - Left = double.Parse(key.GetValue("TimeWindowLeft", 100).ToString()); - Height = double.Parse(key.GetValue("TimeWindowHeight", 200).ToString()); - Width = double.Parse(key.GetValue("TimeWindowWidth", 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); - textBlock.FontFamily = new FontFamily(MainWindow.Theme.Font); - textBlock.Foreground = MainWindow.Theme.PrimaryBrush; - //textBlock.Text = DateTime.Now.ToString("hh:mm:ss tt"); - textBlock.Text = DateTime.Now.ToString("HH:mm:ss"); - - ClculateWidth(); - }); - } - - private void ClculateWidth() - { - string template = "##:##:##"; - double lenght = 0; - for (int i = 0; i < 9; i++) - { - lenght = Math.Max(StringUtilities.MeasureString(template.Replace('#', i.ToString()[0]), textBlock).Width, lenght); - } - textBlock.Width = lenght; - } - - private void Window_LocationChanged(object sender, EventArgs e) - { - key.SetValue("TimeWindowTop", Top); - key.SetValue("TimeWindowLeft", Left); - } - - private void Window_SizeChanged(object sender, SizeChangedEventArgs e) - { - key.SetValue("TimeWindowHeight", Height); - key.SetValue("TimeWindowWidth", Width); - tileBar.CaptionHeight = ActualHeight - 10; - } - } -} \ No newline at end of file diff --git a/src/DesktopMagic/Helpers/SettingElementGenerator.cs b/src/DesktopMagic/Helpers/SettingElementGenerator.cs index 50248ef..95e9362 100644 --- a/src/DesktopMagic/Helpers/SettingElementGenerator.cs +++ b/src/DesktopMagic/Helpers/SettingElementGenerator.cs @@ -199,7 +199,7 @@ namespace DesktopMagic.Helpers { App.Logger.Log(message, "PluginInput"); _ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); - int index = MainWindow.WindowNames.IndexOf(((Tuple)optionsComboBox.SelectedItem).Item1.ToString()); + int index = MainWindow.WindowNames.IndexOf(optionsComboBox.SelectedItem.ToString()); PluginWindow window = MainWindow.Windows[index] as PluginWindow; window?.Exit(); diff --git a/src/DesktopMagic/MainWindow.xaml b/src/DesktopMagic/MainWindow.xaml index 9c998bc..1f0ff02 100644 --- a/src/DesktopMagic/MainWindow.xaml +++ b/src/DesktopMagic/MainWindow.xaml @@ -46,7 +46,7 @@ - + @@ -86,13 +86,21 @@ + - + + +