diff --git a/src/DesktopMagic.sln b/src/DesktopMagic.sln index 19346aa..9bd026d 100644 --- a/src/DesktopMagic.sln +++ b/src/DesktopMagic.sln @@ -11,11 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagicPlugin.Test", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopMagic.Installer", "DesktopMagic.Installer\DesktopMagic.Installer.csproj", "{A06BD685-7F92-4799-846B-3EC38345108E}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{10A138D6-41F0-4DBB-BC39-D93467B4D982}" - ProjectSection(SolutionItems) = preProject - .editorconfig = .editorconfig - EndProjectSection -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/DesktopMagic/App.xaml.cs b/src/DesktopMagic/App.xaml.cs index a1afefd..34aac77 100644 --- a/src/DesktopMagic/App.xaml.cs +++ b/src/DesktopMagic/App.xaml.cs @@ -1,6 +1,8 @@ using AlwaysUpToDate; + +using Stone_Red_Utilities.Logging; + using System; -using System.Diagnostics; using System.Threading; using System.Windows; @@ -11,22 +13,30 @@ namespace DesktopMagic /// public partial class App : Application { - private Mutex _mutex; + private readonly string logFilePath; + private readonly Mutex _mutex; #if DEBUG private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/develop/update/updateInfo.json"); #else private readonly Updater updater = new Updater(TimeSpan.FromHours(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/main/update/updateInfo.json"); #endif + public const string AppName = "Desktop Magic"; + public static string ApplicationDataPath { get; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + AppName; + + public static Logger Logger { get; } = new Logger(); + public App() { + logFilePath = ApplicationDataPath + "\\Log.log"; + Setup(); // Try to grab mutex - bool createdNew; - _mutex = new Mutex(true, $"Stone_Red{DesktopMagic.MainWindow.AppName}", out createdNew); + _mutex = new Mutex(true, $"Stone_Red{AppName}", out bool createdNew); //check if creating new was succesfull if (!createdNew) { + Logger.Log("Shutting down because other instance already running.", "Setup"); //Shutdown Aplication Current.Shutdown(); } @@ -49,22 +59,69 @@ namespace DesktopMagic private void Updater_NoUpdateAvailible() { - Debug.WriteLine("No update avalible."); + Logger.Log("No update avalible.", "Updater"); } private void Updater_OnException(Exception exception) { - Debug.WriteLine("Update exception: " + exception); + Logger.Log(exception.ToString(), "Updater"); } private void Updater_ProgressChanged(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage) { - Debug.WriteLine($"{progressPercentage}% {totalBytesDownloaded}/{totalFileSize}"); + Logger.Log($"{progressPercentage}% {totalBytesDownloaded}/{totalFileSize}", "Updater"); } - protected virtual void CloseMutexHandler(object sender, EventArgs e) + protected void CloseMutexHandler(object sender, EventArgs e) { _mutex?.Close(); } + + private void Setup() + { + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + + Logger.Config = new LogConfig() + { + FatalConfig = new OutputConfig() + { + Color = ConsoleColor.DarkRed, + LogTarget = LogTarget.DebugConsole | LogTarget.File, + FilePath = logFilePath + }, + ErrorConfig = new OutputConfig() + { + Color = ConsoleColor.Red, + LogTarget = LogTarget.DebugConsole | LogTarget.File, + FilePath = logFilePath + }, + WarnConfig = new OutputConfig() + { + Color = ConsoleColor.Yellow, + LogTarget = LogTarget.DebugConsole | LogTarget.File, + FilePath = logFilePath + }, + InfoConfig = new OutputConfig() + { + Color = ConsoleColor.White, + LogTarget = LogTarget.DebugConsole | LogTarget.File, + FilePath = logFilePath + }, + DebugConfig = new OutputConfig() + { + Color = ConsoleColor.Gray, + LogTarget = LogTarget.DebugConsole, + }, + }; + + Logger.ClearLogFile(LogSeverity.Info); + Logger.Log("Log setup complete.", "Setup"); + } + + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Exception exception = (Exception)e.ExceptionObject; + Logger.Log(exception + (e.IsTerminating ? "\t Process terminating!" : ""), exception.Source, LogSeverity.Fatal); + } } } \ No newline at end of file diff --git a/src/DesktopMagic/CalendarManagment.cs b/src/DesktopMagic/CalendarManagment.cs index 6f326a9..6a59223 100644 --- a/src/DesktopMagic/CalendarManagment.cs +++ b/src/DesktopMagic/CalendarManagment.cs @@ -3,6 +3,7 @@ using Google.Apis.Calendar.v3; using Google.Apis.Calendar.v3.Data; using Google.Apis.Services; using Google.Apis.Util.Store; + using System; using System.Collections.Generic; using System.Diagnostics; @@ -15,7 +16,7 @@ namespace DesktopMagic { private static string[] Scopes = { CalendarService.Scope.CalendarReadonly }; - private static string ApplicationName = "Google Calendar API .NET " + MainWindow.AppName; + private static string ApplicationName = "Google Calendar API .NET " + App.AppName; [Obsolete] public (List, List) GetEvents() @@ -25,8 +26,11 @@ namespace DesktopMagic UserCredential credential; if (!File.Exists("credentials.json")) + { return (new(), new()); - using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) + } + + using (FileStream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) { // The file token.json stores the user's access and refresh tokens, and is created // automatically when the authorization flow completes for the first time. @@ -59,7 +63,7 @@ namespace DesktopMagic Events events = request.Execute(); if (events.Items != null && events.Items.Count > 0) { - foreach (var eventItem in events.Items) + foreach (Event eventItem in events.Items) { if (upcomingEventNames.Count < 10) { diff --git a/src/DesktopMagic/CalendarWindow.xaml.cs b/src/DesktopMagic/CalendarWindow.xaml.cs index 3020bd7..21fc554 100644 --- a/src/DesktopMagic/CalendarWindow.xaml.cs +++ b/src/DesktopMagic/CalendarWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; + using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -33,7 +34,7 @@ namespace DesktopMagic w.WindowStyle = WindowStyle.ToolWindow; w.ShowInTaskbar = false; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); Timer t = new Timer(); @@ -43,14 +44,15 @@ namespace DesktopMagic Timer valueTimer = new Timer(); valueTimer.Interval = 600000; - valueTimer.Elapsed += ValueTimer_Elapsed; ; + valueTimer.Elapsed += ValueTimer_Elapsed; + ; valueTimer.Start(); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); - this.Top = double.Parse(key.GetValue("CalendarWindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue("CalendarWindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue("CalendarWindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue("CalendarWindowWidth", 500).ToString()); + 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(() => @@ -143,15 +145,15 @@ namespace DesktopMagic private void Window_LocationChanged(object sender, EventArgs e) { - key.SetValue("CalendarWindowTop", this.Top); - key.SetValue("CalendarWindowLeft", this.Left); + key.SetValue("CalendarWindowTop", Top); + key.SetValue("CalendarWindowLeft", Left); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue("CalendarWindowHeight", this.Height); - key.SetValue("CalendarWindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue("CalendarWindowHeight", Height); + key.SetValue("CalendarWindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } } } \ No newline at end of file diff --git a/src/DesktopMagic/CpuUsageWindow.xaml.cs b/src/DesktopMagic/CpuUsageWindow.xaml.cs index 8315df3..d5afd15 100644 --- a/src/DesktopMagic/CpuUsageWindow.xaml.cs +++ b/src/DesktopMagic/CpuUsageWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; + using System; using System.Diagnostics; using System.Timers; @@ -31,7 +32,7 @@ namespace DesktopMagic ShowInTaskbar = false }; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); cpuCounter.CategoryName = "Processor"; @@ -45,15 +46,16 @@ namespace DesktopMagic Timer valueTimer = new Timer(); valueTimer.Interval = 1000; - valueTimer.Elapsed += ValueTimer_Elapsed; ; + valueTimer.Elapsed += ValueTimer_Elapsed; + ; valueTimer.Start(); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); - this.Top = double.Parse(key.GetValue("CpuUsageWindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue("CpuUsageWindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue("CpuUsageWindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue("CpuUsageWindowWidth", 500).ToString()); - this.IsEnabled = false; + 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) @@ -102,15 +104,15 @@ namespace DesktopMagic private void Window_LocationChanged(object sender, EventArgs e) { - key.SetValue("CpuUsageWindowTop", this.Top); - key.SetValue("CpuUsageWindowLeft", this.Left); + key.SetValue("CpuUsageWindowTop", Top); + key.SetValue("CpuUsageWindowLeft", Left); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue("CpuUsageWindowHeight", this.Height); - key.SetValue("CpuUsageWindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue("CpuUsageWindowHeight", Height); + key.SetValue("CpuUsageWindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } } } \ No newline at end of file diff --git a/src/DesktopMagic/DateWindow.xaml.cs b/src/DesktopMagic/DateWindow.xaml.cs index 96b2db3..ad58d75 100644 --- a/src/DesktopMagic/DateWindow.xaml.cs +++ b/src/DesktopMagic/DateWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; + using System; using System.Timers; using System.Windows; @@ -27,7 +28,7 @@ namespace DesktopMagic w.WindowStyle = WindowStyle.ToolWindow; w.ShowInTaskbar = false; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); Timer t = new Timer(); @@ -35,12 +36,12 @@ namespace DesktopMagic t.Elapsed += T_Elapsed; t.Start(); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); - this.Top = double.Parse(key.GetValue("DateWindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue("DateWindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue("DateWindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue("DateWindowWidth", 500).ToString()); - this.IsEnabled = false; + 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) @@ -75,15 +76,15 @@ namespace DesktopMagic private void Window_LocationChanged(object sender, EventArgs e) { - key.SetValue("DateWindowTop", this.Top); - key.SetValue("DateWindowLeft", this.Left); + key.SetValue("DateWindowTop", Top); + key.SetValue("DateWindowLeft", Left); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue("DateWindowHeight", this.Height); - key.SetValue("DateWindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue("DateWindowHeight", Height); + key.SetValue("DateWindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } } } \ No newline at end of file diff --git a/src/DesktopMagic/DesktopMagic.csproj b/src/DesktopMagic/DesktopMagic.csproj index b189ae4..70bf8f8 100644 --- a/src/DesktopMagic/DesktopMagic.csproj +++ b/src/DesktopMagic/DesktopMagic.csproj @@ -25,10 +25,9 @@ - - + diff --git a/src/DesktopMagic/GlobalSuppressions.cs b/src/DesktopMagic/GlobalSuppressions.cs index 7ef5263..ba7f009 100644 --- a/src/DesktopMagic/GlobalSuppressions.cs +++ b/src/DesktopMagic/GlobalSuppressions.cs @@ -6,3 +6,4 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("Performance", "CA1822:Member als statisch markieren", Justification = "", Scope = "member", Target = "~M:DesktopMagic.PluginWindow.LoadOptions(System.Object)")] +[assembly: SuppressMessage("Style", "IDE0090:Use 'new(...)'", Justification = "", Scope = "member", Target = "~M:DesktopMagic.MainWindow.GithubButton_Click(System.Object,System.Windows.RoutedEventArgs)")] \ No newline at end of file diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index f652e68..ab0c97f 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -1,17 +1,16 @@ using Microsoft.Win32; + using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; -using System.Text.Json; -using Stone_Red_Utilities.Logging; -using System.Diagnostics; -using System.Reflection; namespace DesktopMagic { @@ -46,17 +45,11 @@ namespace DesktopMagic #endregion Plugins settings - public const string AppName = "Desktop Magic"; - - public static Logger Logger { get; private set; } - public static List Windows { get; protected set; } = new List(); public static List WindowNames { get; protected set; } = new List(); private readonly RegistryKey key; private readonly System.Windows.Forms.NotifyIcon notifyIcon = new(); - private readonly string applicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + AppName; - private readonly string logFilePath; private bool loaded = false; private bool blockWindowsClosing = true; @@ -65,25 +58,23 @@ namespace DesktopMagic { try { - logFilePath = applicationDataPath + "\\Log.txt"; - Logger = new Logger(LogTarget.File, logFilePath, "{:HH:mm:ss} | {,-7} | {,-15} | {,-4} | {,10} | {}"); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + AppName); + key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/DesktopMagic;component/icon.ico")).Stream; notifyIcon.Click += TaskbarIcon_TrayLeftClick; notifyIcon.Visible = true; - notifyIcon.Text = AppName; + notifyIcon.Text = App.AppName; notifyIcon.Icon = new System.Drawing.Icon(iconStream); notifyIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); InitializeComponent(); SetLanguageDictionary(); - this.Title = $"{AppName} - {Assembly.GetExecutingAssembly().GetName().Version}"; + Title = $"{App.AppName} - {Assembly.GetExecutingAssembly().GetName().Version}"; } catch (Exception ex) { - MessageBox.Show(ex.ToString()); + _ = MessageBox.Show(ex.ToString()); } } @@ -93,39 +84,37 @@ namespace DesktopMagic { try { - Logger.ClearLogFile(); - - if (!Directory.Exists(applicationDataPath)) + if (!Directory.Exists(App.ApplicationDataPath)) { - _ = Directory.CreateDirectory(applicationDataPath); + _ = Directory.CreateDirectory(App.ApplicationDataPath); } - Logger.Log("Created ApplicationData Folder", "Main"); + App.Logger.Log("Created ApplicationData Folder", "Main"); - if (!Directory.Exists(applicationDataPath + "\\Plugins")) + if (!Directory.Exists(App.ApplicationDataPath + "\\Plugins")) { - _ = Directory.CreateDirectory(applicationDataPath + "\\Plugins"); + _ = Directory.CreateDirectory(App.ApplicationDataPath + "\\Plugins"); } - Logger.Log("Created Plugins Folder", "Main"); + App.Logger.Log("Created Plugins Folder", "Main"); - if (!File.Exists(applicationDataPath + "\\layouts.save")) + if (!File.Exists(App.ApplicationDataPath + "\\layouts.save")) { - File.WriteAllText(applicationDataPath + "\\layouts.save", ";" + (string)FindResource("default")); + File.WriteAllText(App.ApplicationDataPath + "\\layouts.save", ";" + (string)FindResource("default")); } - Logger.Log("Created layouts.save file", "Main"); + App.Logger.Log("Created layouts.save file", "Main"); _ = optionsComboBox.Items.Add(new Tuple((string)FindResource("musicVisualizer"), 0)); //Write To Log File and Load Elements - Logger.Log("Loading Plugin names", "Main"); + App.Logger.Log("Loading Plugin names", "Main"); LoadPlugins(); - Logger.Log("Loading Layout names", "Main"); + App.Logger.Log("Loading Layout names", "Main"); LoadLayoutNames(); - Logger.Log("Loading Layout", "Main"); + App.Logger.Log("Loading Layout", "Main"); LoadLayout(); loaded = true; - Logger.Log("Window Loaded", "Main"); + App.Logger.Log("Window Loaded", "Main"); } catch (Exception ex) { @@ -135,7 +124,7 @@ namespace DesktopMagic private void LoadPlugins() { - string PluginsPath = applicationDataPath + "\\Plugins"; + string PluginsPath = App.ApplicationDataPath + "\\Plugins"; foreach (string fileName in Directory.GetFiles(PluginsPath, "*.dll")) { @@ -208,11 +197,26 @@ namespace DesktopMagic switch (checkBox.Name) { - case "TimeCb": window = new TimeWindow(); break; - case "DateCb": window = new DateWindow(); break; - case "CpuUsageCb": window = new CpuUsageWindow(); break; - case "CalendarCb": window = new CalendarWindow(); break; - case "MusicVisualizerCb": window = new MusicVisualizerWindow(); break; + case "TimeCb": + window = new TimeWindow(); + break; + + case "DateCb": + window = new DateWindow(); + break; + + case "CpuUsageCb": + window = new CpuUsageWindow(); + break; + + case "CalendarCb": + window = new CalendarWindow(); + break; + + case "MusicVisualizerCb": + window = new MusicVisualizerWindow(); + break; + default: if (checkBox.Name.Contains("_PluginCb_")) { @@ -298,14 +302,14 @@ namespace DesktopMagic private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { - MessageBoxResult msbRes = MessageBox.Show((string)FindResource("wantToCloseProgram"), AppName, MessageBoxButton.YesNo); + MessageBoxResult msbRes = MessageBox.Show((string)FindResource("wantToCloseProgram"), App.AppName, MessageBoxButton.YesNo); e.Cancel = msbRes != MessageBoxResult.Yes; } private void Window_Closed(object sender, EventArgs e) { - this.Visibility = Visibility.Collapsed; - this.UpdateLayout(); + Visibility = Visibility.Collapsed; + UpdateLayout(); foreach (Window window in Windows) { window.Hide(); @@ -315,12 +319,12 @@ namespace DesktopMagic private void Window_StateChanged(object sender, EventArgs e) { - if (this.WindowState == WindowState.Minimized) + if (WindowState == WindowState.Minimized) { EditCheckBox.IsChecked = false; EditCheckBox_Click(null, null); - this.ShowInTaskbar = false; - this.Visibility = Visibility.Collapsed; + ShowInTaskbar = false; + Visibility = Visibility.Collapsed; foreach (Window item in Windows) { WindowPos.SendWpfWindowBack(item); @@ -651,7 +655,7 @@ namespace DesktopMagic private void DisplayException(string message) { - Logger.Log(message, "PluginInput"); + App.Logger.Log(message, "PluginInput"); _ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); int index = WindowNames.IndexOf(((Tuple)optionsComboBox.SelectedItem).Item1.ToString()); @@ -664,7 +668,6 @@ namespace DesktopMagic private void TextBlock_Loaded(object sender, RoutedEventArgs e) { int index = 0; - char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; foreach (FontFamily ff in Fonts.SystemFontFamilies) { ComboBoxItem comboBoxItem = new() @@ -764,7 +767,7 @@ namespace DesktopMagic key.SetValue("SelectedLayout", layoutsComboBox.SelectedIndex); - string[] lines = File.ReadAllLines(applicationDataPath + "\\layouts.save"); + string[] lines = File.ReadAllLines(App.ApplicationDataPath + "\\layouts.save"); string[] data = lines[layoutsComboBox.SelectedIndex].Split(';'); foreach (string dat in data) { @@ -801,7 +804,7 @@ namespace DesktopMagic } content += inputDialog.ResponseText + "\n"; - File.AppendAllText(applicationDataPath + "\\layouts.save", content); + File.AppendAllText(App.ApplicationDataPath + "\\layouts.save", content); key.SetValue("SelectedLayout", -1); LoadLayoutNames(); layoutsComboBox.SelectedIndex = layoutsComboBox.Items.Count - 1; @@ -815,9 +818,9 @@ namespace DesktopMagic return; } - List lines = File.ReadAllLines(applicationDataPath + "\\layouts.save").ToList(); + List lines = File.ReadAllLines(App.ApplicationDataPath + "\\layouts.save").ToList(); lines.RemoveAt(layoutsComboBox.SelectedIndex); - File.WriteAllLines(applicationDataPath + "\\layouts.save", lines); + File.WriteAllLines(App.ApplicationDataPath + "\\layouts.save", lines); LoadLayoutNames(); layoutsComboBox.SelectedIndex = 0; } @@ -826,9 +829,9 @@ namespace DesktopMagic { _ = Task.Run(() => { - lock (applicationDataPath) + lock (App.ApplicationDataPath) { - List lines = File.ReadAllLines(applicationDataPath + "\\layouts.save").ToList(); + List lines = File.ReadAllLines(App.ApplicationDataPath + "\\layouts.save").ToList(); string content = ""; foreach (string valueName in key.GetValueNames()) { @@ -842,7 +845,7 @@ namespace DesktopMagic content += layoutsComboBox.SelectedItem.ToString(); lines[layoutsComboBox.SelectedIndex] = content; }); - File.WriteAllLines(applicationDataPath + "\\layouts.save", lines); + File.WriteAllLines(App.ApplicationDataPath + "\\layouts.save", lines); } }); } @@ -850,7 +853,7 @@ namespace DesktopMagic private void LoadLayoutNames() { layoutsComboBox.Items.Clear(); - string[] lines = File.ReadAllLines(applicationDataPath + "\\layouts.save"); + string[] lines = File.ReadAllLines(App.ApplicationDataPath + "\\layouts.save"); foreach (string line in lines) { @@ -910,14 +913,14 @@ namespace DesktopMagic } catch (Exception ex) { - Logger.Log(ex.ToString(), "Main"); + App.Logger.Log(ex.ToString(), "Main"); MessageBox.Show(ex.ToString()); } } } catch (Exception ex) { - Logger.Log(ex.ToString(), "Main"); + App.Logger.Log(ex.ToString(), "Main"); _ = MessageBox.Show(ex.ToString()); } @@ -952,7 +955,7 @@ namespace DesktopMagic private void OpenPluginsFolderButton_Click(object sender, RoutedEventArgs e) { - _ = Process.Start("explorer.exe", applicationDataPath + "\\Plugins"); + _ = Process.Start("explorer.exe", App.ApplicationDataPath + "\\Plugins"); } private void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) @@ -966,12 +969,12 @@ namespace DesktopMagic { for (int i = 0; i < 10; i++) { - this.ShowInTaskbar = true; - this.Visibility = Visibility.Visible; + ShowInTaskbar = true; + Visibility = Visibility.Visible; SystemCommands.RestoreWindow(this); - this.Topmost = true; - this.Activate(); - this.Topmost = false; + Topmost = true; + Activate(); + Topmost = false; } } @@ -988,7 +991,7 @@ namespace DesktopMagic { dict.Source = new Uri("..\\Resources\\StringResources.en.xaml", UriKind.Relative); } - this.Resources.MergedDictionaries.Add(dict); + Resources.MergedDictionaries.Add(dict); } private void GithubButton_Click(object sender, RoutedEventArgs e) diff --git a/src/DesktopMagic/MusicVisualizerWindow.xaml.cs b/src/DesktopMagic/MusicVisualizerWindow.xaml.cs index 5244bb0..8bb8ab9 100644 --- a/src/DesktopMagic/MusicVisualizerWindow.xaml.cs +++ b/src/DesktopMagic/MusicVisualizerWindow.xaml.cs @@ -1,5 +1,7 @@ using Microsoft.Win32; + using NAudio.Wave; + using System; using System.Collections.Generic; using System.Diagnostics; @@ -42,7 +44,7 @@ namespace DesktopMagic }; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); sampleAggregator.FftCalculated += new EventHandler(FftCalculated); @@ -58,17 +60,17 @@ namespace DesktopMagic t.Elapsed += T_Elapsed; t.Start(); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); + key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); - this.Top = double.Parse(key.GetValue("MusicVisualizerWindowTop", 100).ToString()); + Top = double.Parse(key.GetValue("MusicVisualizerWindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue("MusicVisualizerWindowLeft", 100).ToString()); + Left = double.Parse(key.GetValue("MusicVisualizerWindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue("MusicVisualizerWindowHeight", 200).ToString()); + Height = double.Parse(key.GetValue("MusicVisualizerWindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue("MusicVisualizerWindowWidth", 500).ToString()); + Width = double.Parse(key.GetValue("MusicVisualizerWindowWidth", 500).ToString()); - this.IsEnabled = false; + IsEnabled = false; } protected override void OnSourceInitialized(EventArgs e) @@ -95,7 +97,7 @@ namespace DesktopMagic panel.Visibility = Visibility.Collapsed; WindowPos.SetIsLocked(this, true); } - if (!this.IsLoaded) + if (!IsLoaded) { calculate = false; } @@ -126,7 +128,10 @@ namespace DesktopMagic private void FftCalculated(object sender, FftEventArgs e) { - if (!calculate) return; + if (!calculate) + { + return; + } List fft = new List(); for (int i = 0; i < e.Result.Length / 2 - 70; i++) @@ -193,7 +198,9 @@ namespace DesktopMagic for (int j = 0; j < flattenValue; j++) { if (i + j < scaledFft.Count) + { temp += scaledFft[i + j]; + } } scaledFft[i] = temp / flattenValue; } @@ -207,7 +214,9 @@ namespace DesktopMagic for (int j = 0; j < flattenValue; j++) { if (i - j >= 0) + { temp += scaledFft[i - j]; + } } scaledFft[i] = temp / flattenValue; } @@ -224,10 +233,14 @@ namespace DesktopMagic int offset = 0; if (!MainWindow.MirrorMode && MainWindow.SpectrumMode != 1) + { scaledFft.Insert(0, 0); + } if (!MainWindow.LineMode) + { offset = 1; + } using (Graphics gr = Graphics.FromImage(bm)) { @@ -256,8 +269,13 @@ namespace DesktopMagic } break; - case 2: points[pointIndex + 1] = new PointF(2 * pointIndex, value); break; - default: points[pointIndex + 1] = new PointF(2 * pointIndex, bm.Height - value - 1 + offset); break; + case 2: + points[pointIndex + 1] = new PointF(2 * pointIndex, value); + break; + + default: + points[pointIndex + 1] = new PointF(2 * pointIndex, bm.Height - value - 1 + offset); + break; } if (MainWindow.MirrorMode || MainWindow.SpectrumMode == 1) @@ -364,9 +382,9 @@ namespace DesktopMagic private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue("MusicVisualizerWindowHeight", this.Height); - key.SetValue("MusicVisualizerWindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue("MusicVisualizerWindowHeight", Height); + key.SetValue("MusicVisualizerWindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } } } \ No newline at end of file diff --git a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs index a63b32b..c86dbd0 100644 --- a/src/DesktopMagic/Plugin/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugin/PluginWindow.xaml.cs @@ -6,7 +6,6 @@ using Microsoft.Win32; using System; using System.Collections.Generic; -using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -55,7 +54,7 @@ namespace DesktopMagic ShowInTaskbar = false }; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); System.Timers.Timer t = new System.Timers.Timer(); @@ -63,13 +62,13 @@ namespace DesktopMagic t.Elapsed += Elapsed; t.Start(); - this.PluginName = pluginName; + PluginName = pluginName; - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); - this.Top = double.Parse(key.GetValue(pluginName + "WindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue(pluginName + "WindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue(pluginName + "WindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue(pluginName + "WindowWidth", 500).ToString()); + key = Registry.CurrentUser.CreateSubKey(@"Software\" + App.AppName); + Top = double.Parse(key.GetValue(pluginName + "WindowTop", 100).ToString()); + Left = double.Parse(key.GetValue(pluginName + "WindowLeft", 100).ToString()); + Height = double.Parse(key.GetValue(pluginName + "WindowHeight", 200).ToString()); + Width = double.Parse(key.GetValue(pluginName + "WindowWidth", 500).ToString()); } protected override void OnSourceInitialized(EventArgs e) @@ -91,7 +90,10 @@ namespace DesktopMagic pluginThread.Start(); } - public void UpdatePluginWindow() => ValueTimer_Elapsed(valueTimer, null); + public void UpdatePluginWindow() + { + ValueTimer_Elapsed(valueTimer, null); + } private void Elapsed(object sender, ElapsedEventArgs e) { @@ -101,15 +103,15 @@ namespace DesktopMagic { panel.Visibility = Visibility.Visible; WindowPos.SetIsLocked(this, false); - tileBar.CaptionHeight = tileBar.CaptionHeight = this.ActualHeight - 10 < 0 ? 0 : this.ActualHeight - 10; - this.ResizeMode = ResizeMode.CanResize; + tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10; + ResizeMode = ResizeMode.CanResize; } else { panel.Visibility = Visibility.Collapsed; WindowPos.SetIsLocked(this, true); tileBar.CaptionHeight = 0; - this.ResizeMode = ResizeMode.NoResize; + ResizeMode = ResizeMode.NoResize; } if (stop) { @@ -120,7 +122,7 @@ namespace DesktopMagic private void LoadPlugin() { - PluginFolderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{MainWindow.AppName}\\Plugins\\{PluginName}"; + PluginFolderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{App.AppName}\\Plugins\\{PluginName}"; if (!File.Exists($"{PluginFolderPath}\\{PluginName}.dll")) { @@ -135,7 +137,7 @@ namespace DesktopMagic } catch (Exception ex) { - MainWindow.Logger.Log(ex.ToString(), "Plugin"); + App.Logger.Log(ex.ToString(), "Plugin"); _ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Exit(); return; @@ -149,12 +151,6 @@ namespace DesktopMagic Assembly dll = Assembly.Load(assemblyBytes); Type instanceType = dll.GetTypes().FirstOrDefault(type => type.GetTypeInfo().BaseType == typeof(Plugin)); - foreach (Type item in dll.GetTypes()) - { - Debug.WriteLine(item.Name); - Debug.WriteLine(typeof(Plugin).Name); - } - if (instanceType is null) { _ = MessageBox.Show($"The \"Plugin\" class could not be found! It has to inherit from \"{typeof(Plugin).FullName}\"", "Error", MessageBoxButton.OK, MessageBoxImage.Error); @@ -195,7 +191,6 @@ namespace DesktopMagic { try { - Debug.WriteLine(instance.GetType().FullName); FieldInfo[] props = instance.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetField); List settingElements = new List(); @@ -228,7 +223,7 @@ namespace DesktopMagic catch (Exception ex) { stop = true; - MainWindow.Logger.Log(ex.ToString(), "Plugin"); + App.Logger.Log(ex.ToString(), "Plugin"); _ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Exit(); return; @@ -274,7 +269,7 @@ namespace DesktopMagic catch (Exception ex) { stop = true; - MainWindow.Logger.Log(ex.ToString(), "Plugin"); + App.Logger.Log(ex.ToString(), "Plugin"); _ = MessageBox.Show("File execution error:\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Exit(); return; @@ -315,15 +310,15 @@ namespace DesktopMagic private void Window_LocationChanged(object sender, EventArgs e) { - key.SetValue(PluginName + "WindowTop", this.Top); - key.SetValue(PluginName + "WindowLeft", this.Left); + key.SetValue(PluginName + "WindowTop", Top); + key.SetValue(PluginName + "WindowLeft", Left); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue(PluginName + "WindowHeight", this.Height); - key.SetValue(PluginName + "WindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue(PluginName + "WindowHeight", Height); + key.SetValue(PluginName + "WindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) @@ -354,7 +349,8 @@ namespace DesktopMagic mouseButton = MouseButton.Right; break; - default: return; + default: + return; }; System.Drawing.Point point = new System.Drawing.Point((int)pixelMousePositionX, (int)pixelMousePositionY); diff --git a/src/DesktopMagic/TimeWindow.xaml.cs b/src/DesktopMagic/TimeWindow.xaml.cs index e314cad..4713c08 100644 --- a/src/DesktopMagic/TimeWindow.xaml.cs +++ b/src/DesktopMagic/TimeWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; + using System; using System.Timers; using System.Windows; @@ -27,7 +28,7 @@ namespace DesktopMagic w.WindowStyle = WindowStyle.ToolWindow; w.ShowInTaskbar = false; w.Show(); - this.Owner = w; + Owner = w; w.Hide(); Timer t = new Timer(); @@ -35,12 +36,12 @@ namespace DesktopMagic t.Elapsed += T_Elapsed; t.Start(); - key = Registry.CurrentUser.CreateSubKey(@"Software\" + MainWindow.AppName); - this.Top = double.Parse(key.GetValue("TimeWindowTop", 100).ToString()); - this.Left = double.Parse(key.GetValue("TimeWindowLeft", 100).ToString()); - this.Height = double.Parse(key.GetValue("TimeWindowHeight", 200).ToString()); - this.Width = double.Parse(key.GetValue("TimeWindowWidth", 500).ToString()); - this.IsEnabled = false; + 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) @@ -77,15 +78,15 @@ namespace DesktopMagic private void Window_LocationChanged(object sender, EventArgs e) { - key.SetValue("TimeWindowTop", this.Top); - key.SetValue("TimeWindowLeft", this.Left); + key.SetValue("TimeWindowTop", Top); + key.SetValue("TimeWindowLeft", Left); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - key.SetValue("TimeWindowHeight", this.Height); - key.SetValue("TimeWindowWidth", this.Width); - tileBar.CaptionHeight = this.ActualHeight - 10; + key.SetValue("TimeWindowHeight", Height); + key.SetValue("TimeWindowWidth", Width); + tileBar.CaptionHeight = ActualHeight - 10; } } } \ No newline at end of file