mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 00:46:12 +02:00
Refactor step 2
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DesktopMagic.Helpers;
|
||||
|
||||
public class ColorJsonConverter : JsonConverter<Color>
|
||||
{
|
||||
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return ColorTranslator.FromHtml(reader.GetString() ?? "#000000");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue("#" + value.A.ToString("X2").ToLower() + value.R.ToString("X2") + value.G.ToString("X2") + value.B.ToString("X2").ToLower());
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@
|
||||
|
||||
<Rectangle Fill="#FFC5C5C5" Stroke="#FFC5C5C5" Grid.ColumnSpan="3" />
|
||||
<StackPanel Margin="{StaticResource DefaultMargin}" VerticalAlignment="Bottom">
|
||||
<ComboBox ItemsSource="{Binding Settings.Layouts}" DisplayMemberPath="Name" SelectedValue="{Binding Settings.CurrentLayoutName}" SelectedValuePath="Name" HorizontalAlignment="Left" Margin="0,0,0,5" Width="170" Height="20" SelectionChanged="LayoutsComboBox_SelectionChanged" />
|
||||
<ComboBox x:Name="layoutsComboBox" ItemsSource="{Binding Settings.Layouts}" DisplayMemberPath="Name" SelectedValue="{Binding Settings.CurrentLayoutName}" SelectedValuePath="Name" HorizontalAlignment="Left" Margin="0,0,0,5" Width="170" Height="20" SelectionChanged="LayoutsComboBox_SelectionChanged" />
|
||||
<Button x:Name="newLayoutButton" Content="{DynamicResource newLayout}" Margin="0,0,0,5" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="170" Click="NewLayoutButton_Click" FontWeight="Regular" />
|
||||
<Button x:Name="removeLayoutButton" Content="{DynamicResource deleteLayout}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="170" Click="RemoveLayoutButton_Click" FontWeight="Regular" />
|
||||
</StackPanel>
|
||||
|
||||
@@ -84,12 +84,6 @@ namespace DesktopMagic
|
||||
}
|
||||
App.Logger.Log("Created Plugins Folder", "Main");
|
||||
|
||||
if (!File.Exists(App.ApplicationDataPath + "\\layouts.save"))
|
||||
{
|
||||
File.WriteAllText(App.ApplicationDataPath + "\\layouts.save", ";" + (string)FindResource("default"));
|
||||
}
|
||||
App.Logger.Log("Created layouts.save file", "Main");
|
||||
|
||||
//Write To Log File and Load Elements
|
||||
|
||||
App.Logger.Log("Loading Plugin names", "Main");
|
||||
@@ -111,8 +105,11 @@ namespace DesktopMagic
|
||||
|
||||
private void LoadPlugins()
|
||||
{
|
||||
pluginNames.Clear();
|
||||
|
||||
pluginNames.Add("MusicVisualizer");
|
||||
pluginNames.Add("Test");
|
||||
pluginNames.Add("Time");
|
||||
pluginNames.Add("Date");
|
||||
|
||||
string pluginsPath = App.ApplicationDataPath + "\\Plugins";
|
||||
|
||||
@@ -155,31 +152,64 @@ namespace DesktopMagic
|
||||
|
||||
#region Windows
|
||||
|
||||
private void EditCheckBox_Click(object sender, RoutedEventArgs e)
|
||||
private void EditCheckBox_Click(object? sender, RoutedEventArgs? e)
|
||||
{
|
||||
EditMode = EditCheckBox.IsChecked == true;
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
private void PluginCheckBox_Click(object sender, RoutedEventArgs e)
|
||||
private void PluginCheckBox_Click(object sender, RoutedEventArgs? e)
|
||||
{
|
||||
string pluginName = ((CheckBox)sender).Content.ToString() ?? string.Empty;
|
||||
if (sender is not CheckBox checkBox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
LoadPlugin(checkBox.Content.ToString() ?? string.Empty);
|
||||
}
|
||||
|
||||
private void LoadPlugin(string pluginName)
|
||||
{
|
||||
if (!Settings.CurrentLayout.Plugins.TryGetValue(pluginName, out PluginSettings? pluginSettings))
|
||||
{
|
||||
pluginSettings = new PluginSettings();
|
||||
Settings.CurrentLayout.Plugins.Add(pluginName, pluginSettings);
|
||||
}
|
||||
|
||||
CheckBox checkBox = (CheckBox)sender;
|
||||
PluginWindow window = new PluginWindow(pluginName, pluginSettings)
|
||||
PluginWindow window;
|
||||
|
||||
if (pluginName == "MusicVisualizer")
|
||||
{
|
||||
Title = pluginName
|
||||
};
|
||||
window = new PluginWindow(new MusicVisualizerPlugin(), pluginName, pluginSettings)
|
||||
{
|
||||
Title = pluginName
|
||||
};
|
||||
}
|
||||
else if (pluginName == "Time")
|
||||
{
|
||||
window = new PluginWindow(new TimePlugin(), pluginName, pluginSettings)
|
||||
{
|
||||
Title = pluginName
|
||||
};
|
||||
}
|
||||
else if (pluginName == "Date")
|
||||
{
|
||||
window = new PluginWindow(new DatePlugin(), pluginName, pluginSettings)
|
||||
{
|
||||
Title = pluginName
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
window = new PluginWindow(pluginName, pluginSettings)
|
||||
{
|
||||
Title = pluginName
|
||||
};
|
||||
}
|
||||
|
||||
blockWindowsClosing = false;
|
||||
|
||||
if (!WindowNames.Contains(window.Title) && checkBox.IsChecked == true)
|
||||
if (!WindowNames.Contains(window.Title) && pluginSettings.Enabled)
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
@@ -190,19 +220,18 @@ namespace DesktopMagic
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
if (!optionsComboBox.Items.Contains(checkBox.Content.ToString()))
|
||||
if (!optionsComboBox.Items.Contains(pluginName))
|
||||
{
|
||||
_ = optionsComboBox.Items.Add(checkBox.Content.ToString());
|
||||
_ = optionsComboBox.Items.Add(pluginName);
|
||||
}
|
||||
optionsComboBox.SelectedIndex = -1;
|
||||
optionsComboBox.SelectedIndex = optionsComboBox.Items.IndexOf(checkBox.Content.ToString());
|
||||
optionsComboBox.SelectedIndex = optionsComboBox.Items.IndexOf(pluginName);
|
||||
window.PluginLoaded -= onPluginLoaded;
|
||||
});
|
||||
};
|
||||
window.OnExit += () =>
|
||||
{
|
||||
checkBox.IsChecked = false;
|
||||
PluginCheckBox_Click(checkBox, null);
|
||||
pluginSettings.Enabled = false;
|
||||
};
|
||||
window.PluginLoaded += onPluginLoaded;
|
||||
|
||||
@@ -235,18 +264,22 @@ namespace DesktopMagic
|
||||
}
|
||||
}
|
||||
}
|
||||
pluginSettings.Enabled = checkBox.IsChecked == true;
|
||||
|
||||
blockWindowsClosing = true;
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
private void DisplayWindow_ContentRendered(object sender, EventArgs e)
|
||||
private void DisplayWindow_ContentRendered(object? sender, EventArgs e)
|
||||
{
|
||||
WindowPos.SendWpfWindowBack((Window)sender);
|
||||
WindowPos.SendWpfWindowBack((Window)sender);
|
||||
if (sender is not Window window)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
WindowPos.SendWpfWindowBack(window);
|
||||
WindowPos.SendWpfWindowBack(window);
|
||||
}
|
||||
|
||||
private void DisplayWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
private void DisplayWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
|
||||
{
|
||||
e.Cancel = blockWindowsClosing;
|
||||
}
|
||||
@@ -255,6 +288,7 @@ namespace DesktopMagic
|
||||
{
|
||||
MessageBoxResult msbRes = MessageBox.Show((string)FindResource("wantToCloseProgram"), App.AppName, MessageBoxButton.YesNo);
|
||||
e.Cancel = msbRes != MessageBoxResult.Yes;
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
private void Window_Closed(object sender, EventArgs e)
|
||||
@@ -268,7 +302,7 @@ namespace DesktopMagic
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
private void Window_StateChanged(object sender, EventArgs e)
|
||||
private void Window_StateChanged(object? sender, EventArgs? e)
|
||||
{
|
||||
if (WindowState == WindowState.Minimized)
|
||||
{
|
||||
@@ -291,7 +325,6 @@ namespace DesktopMagic
|
||||
private void FontComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
Settings.CurrentLayout.Theme.Font = fontComboBox.SelectedValue.ToString()!.Replace("System.Windows.Controls.ComboBoxItem: ", "");
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
private void OptionsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
@@ -368,8 +401,6 @@ namespace DesktopMagic
|
||||
{
|
||||
Settings.CurrentLayout.Theme.PrimaryColor = colorDialog.ResultColor;
|
||||
primaryColorRechtangle.Fill = colorDialog.ResultBrush;
|
||||
|
||||
SaveSettings();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -380,8 +411,6 @@ namespace DesktopMagic
|
||||
{
|
||||
Settings.CurrentLayout.Theme.SecondaryColor = colorDialog.ResultColor;
|
||||
secondaryColorRechtangle.Fill = colorDialog.ResultBrush;
|
||||
|
||||
SaveSettings();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,19 +421,16 @@ namespace DesktopMagic
|
||||
{
|
||||
Settings.CurrentLayout.Theme.BackgroundColor = colorDialog.ResultColor;
|
||||
backgroundColorRechtangle.Fill = colorDialog.ResultBrush;
|
||||
|
||||
SaveSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private void CornerRadiusTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
private void CornerRadiusTextBox_TextChanged(object? sender, TextChangedEventArgs? e)
|
||||
{
|
||||
bool sucess = int.TryParse(cornerRadiusTextBox.Text, out int cornerRadius);
|
||||
if (sucess)
|
||||
{
|
||||
cornerRadiusTextBox.Foreground = Brushes.Black;
|
||||
Settings.CurrentLayout.Theme.CornerRadius = cornerRadius;
|
||||
SaveSettings();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -412,14 +438,13 @@ namespace DesktopMagic
|
||||
}
|
||||
}
|
||||
|
||||
private void MarginTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
private void MarginTextBox_TextChanged(object? sender, TextChangedEventArgs? e)
|
||||
{
|
||||
bool sucess = int.TryParse(marginTextBox.Text, out int margin);
|
||||
if (sucess)
|
||||
{
|
||||
marginTextBox.Foreground = Brushes.Black;
|
||||
Settings.CurrentLayout.Theme.Margin = margin;
|
||||
SaveSettings();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -429,9 +454,21 @@ namespace DesktopMagic
|
||||
|
||||
#region Layout
|
||||
|
||||
private readonly JsonSerializerOptions jsonSettingsOptions = new()
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
new ColorJsonConverter()
|
||||
}
|
||||
};
|
||||
|
||||
private void LayoutsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
LoadLayout(false);
|
||||
if (loaded)
|
||||
{
|
||||
SaveSettings();
|
||||
LoadLayout(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void NewLayoutButton_Click(object sender, RoutedEventArgs e)
|
||||
@@ -458,7 +495,7 @@ namespace DesktopMagic
|
||||
return;
|
||||
}
|
||||
|
||||
string json = JsonSerializer.Serialize(Settings);
|
||||
string json = JsonSerializer.Serialize(Settings, jsonSettingsOptions);
|
||||
File.WriteAllText(Path.Combine(App.ApplicationDataPath, "settings.json"), json);
|
||||
}
|
||||
|
||||
@@ -479,7 +516,7 @@ namespace DesktopMagic
|
||||
|
||||
string json = File.ReadAllText(Path.Combine(App.ApplicationDataPath, "settings.json"));
|
||||
|
||||
Settings = JsonSerializer.Deserialize<DesktopMagicSettings>(json) ?? new DesktopMagicSettings()
|
||||
Settings = JsonSerializer.Deserialize<DesktopMagicSettings>(json, jsonSettingsOptions) ?? new DesktopMagicSettings()
|
||||
{
|
||||
Layouts =
|
||||
[
|
||||
@@ -513,8 +550,6 @@ namespace DesktopMagic
|
||||
WindowNames.Clear();
|
||||
optionsComboBox.Items.Clear();
|
||||
|
||||
_ = optionsComboBox.Items.Add((string)FindResource("musicVisualizer"));
|
||||
|
||||
bool showWindow = true;
|
||||
|
||||
foreach (string pluginName in pluginNames)
|
||||
@@ -522,16 +557,23 @@ namespace DesktopMagic
|
||||
if (!Settings.CurrentLayout.Plugins.TryGetValue(pluginName, out PluginSettings? pluginSettings))
|
||||
{
|
||||
Settings.CurrentLayout.Plugins.Add(pluginName, new PluginSettings());
|
||||
Settings.CurrentLayout.UpdatePlugins();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pluginSettings.Enabled)
|
||||
{
|
||||
LoadPlugin(pluginName);
|
||||
}
|
||||
|
||||
if (showWindow && pluginSettings.Enabled)
|
||||
{
|
||||
showWindow = false;
|
||||
}
|
||||
}
|
||||
|
||||
Settings.CurrentLayout.UpdatePlugins();
|
||||
|
||||
if (!showWindow && minimize)
|
||||
{
|
||||
WindowState = WindowState.Minimized;
|
||||
@@ -549,16 +591,7 @@ namespace DesktopMagic
|
||||
private void UpdatePluginsButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
LoadPlugins();
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
foreach (Window window in Windows.ToArray())
|
||||
{
|
||||
if (window.GetType() == typeof(PluginWindow))
|
||||
{
|
||||
((PluginWindow)window).UpdatePluginWindow();
|
||||
}
|
||||
}
|
||||
});
|
||||
LoadLayout(false);
|
||||
}
|
||||
|
||||
private void OpenPluginsFolderButton_Click(object sender, RoutedEventArgs e)
|
||||
@@ -573,7 +606,7 @@ namespace DesktopMagic
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void TaskbarIcon_TrayLeftClick(object sender, EventArgs e)
|
||||
private void TaskbarIcon_TrayLeftClick(object? sender, EventArgs e)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
|
||||
@@ -1,10 +1,28 @@
|
||||
using DesktopMagicPluginAPI.Settings;
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DesktopMagic.Plugins;
|
||||
|
||||
public class InputElement(Setting element, string name, int orderIndex)
|
||||
public class InputElement
|
||||
{
|
||||
public Setting Input { get; } = element;
|
||||
public string Name { get; } = name;
|
||||
public int OrderIndex { get; } = orderIndex;
|
||||
public Setting Input { get; }
|
||||
public string Name { get; }
|
||||
public int OrderIndex { get; }
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
public InputElement(Setting input, string name, int orderIndex)
|
||||
{
|
||||
Input = input;
|
||||
Name = name;
|
||||
OrderIndex = orderIndex;
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
private InputElement()
|
||||
{
|
||||
}
|
||||
|
||||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
}
|
||||
@@ -45,10 +45,10 @@ public partial class PluginWindow : Window
|
||||
|
||||
Window w = new()
|
||||
{
|
||||
Left = settings.Position.X,
|
||||
Top = settings.Position.Y,
|
||||
Width = settings.Size.X,
|
||||
Height = settings.Size.Y,
|
||||
Top = -100,
|
||||
Left = -100,
|
||||
Width = 0,
|
||||
Height = 0,
|
||||
|
||||
WindowStyle = WindowStyle.ToolWindow,
|
||||
ShowInTaskbar = false
|
||||
@@ -66,6 +66,11 @@ public partial class PluginWindow : Window
|
||||
|
||||
PluginName = pluginName;
|
||||
this.settings = settings;
|
||||
|
||||
Left = settings.Position.X;
|
||||
Top = settings.Position.Y;
|
||||
Width = settings.Size.X;
|
||||
Height = settings.Size.Y;
|
||||
}
|
||||
|
||||
public PluginWindow(Plugin pluginClassInstance, string pluginName, Settings.PluginSettings settings) : this(pluginName, settings)
|
||||
@@ -159,7 +164,7 @@ public partial class PluginWindow : Window
|
||||
{
|
||||
if (pluginClassInstance is null)
|
||||
{
|
||||
PluginFolderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{App.AppName}\\Plugins\\{PluginName}";
|
||||
PluginFolderPath = $"{App.ApplicationDataPath}\\Plugins\\{PluginName}";
|
||||
|
||||
if (!File.Exists($"{PluginFolderPath}\\{PluginName}.dll"))
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@ internal class DesktopMagicSettings : INotifyPropertyChanged
|
||||
|
||||
public string? CurrentLayoutName
|
||||
{
|
||||
get => currentLayoutName;
|
||||
get => currentLayoutName ?? Layouts.FirstOrDefault()?.Name;
|
||||
set
|
||||
{
|
||||
currentLayoutName = value;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DesktopMagic.Settings;
|
||||
@@ -46,7 +47,7 @@ internal class Layout(string name) : INotifyPropertyChanged
|
||||
|
||||
public void UpdatePlugins()
|
||||
{
|
||||
OnPropertyChanged(nameof(Plugins));
|
||||
Plugins = Plugins.ToDictionary();
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
||||
|
||||
@@ -14,9 +14,9 @@ public class PluginSettings : INotifyPropertyChanged
|
||||
|
||||
private readonly Theme? theme;
|
||||
private List<InputElement> settings = [];
|
||||
private bool enabled = true;
|
||||
private Point position = new Point(0, 0);
|
||||
private Point size = new Point(0, 0);
|
||||
private bool enabled = false;
|
||||
private Point position = new Point(100, 100);
|
||||
private Point size = new Point(300, 300);
|
||||
|
||||
[JsonIgnore]
|
||||
public Theme Theme => theme is null ? MainWindowDataContext.GetSettings().CurrentLayout.Theme : theme;
|
||||
|
||||
Reference in New Issue
Block a user