Refactor step 2

This commit is contained in:
Stone_Red
2023-11-20 20:31:35 +01:00
parent 74bf7c2fb8
commit 2331544a80
8 changed files with 146 additions and 70 deletions
@@ -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());
}
}
+1 -1
View File
@@ -130,7 +130,7 @@
<Rectangle Fill="#FFC5C5C5" Stroke="#FFC5C5C5" Grid.ColumnSpan="3" /> <Rectangle Fill="#FFC5C5C5" Stroke="#FFC5C5C5" Grid.ColumnSpan="3" />
<StackPanel Margin="{StaticResource DefaultMargin}" VerticalAlignment="Bottom"> <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="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" /> <Button x:Name="removeLayoutButton" Content="{DynamicResource deleteLayout}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="170" Click="RemoveLayoutButton_Click" FontWeight="Regular" />
</StackPanel> </StackPanel>
+88 -55
View File
@@ -84,12 +84,6 @@ namespace DesktopMagic
} }
App.Logger.Log("Created Plugins Folder", "Main"); 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 //Write To Log File and Load Elements
App.Logger.Log("Loading Plugin names", "Main"); App.Logger.Log("Loading Plugin names", "Main");
@@ -111,8 +105,11 @@ namespace DesktopMagic
private void LoadPlugins() private void LoadPlugins()
{ {
pluginNames.Clear();
pluginNames.Add("MusicVisualizer"); pluginNames.Add("MusicVisualizer");
pluginNames.Add("Test"); pluginNames.Add("Time");
pluginNames.Add("Date");
string pluginsPath = App.ApplicationDataPath + "\\Plugins"; string pluginsPath = App.ApplicationDataPath + "\\Plugins";
@@ -155,31 +152,64 @@ namespace DesktopMagic
#region Windows #region Windows
private void EditCheckBox_Click(object sender, RoutedEventArgs e) private void EditCheckBox_Click(object? sender, RoutedEventArgs? e)
{ {
EditMode = EditCheckBox.IsChecked == true; EditMode = EditCheckBox.IsChecked == true;
SaveSettings(); 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)) if (!Settings.CurrentLayout.Plugins.TryGetValue(pluginName, out PluginSettings? pluginSettings))
{ {
pluginSettings = new PluginSettings(); pluginSettings = new PluginSettings();
Settings.CurrentLayout.Plugins.Add(pluginName, pluginSettings); Settings.CurrentLayout.Plugins.Add(pluginName, pluginSettings);
} }
CheckBox checkBox = (CheckBox)sender; PluginWindow window;
PluginWindow window = new PluginWindow(pluginName, pluginSettings)
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; blockWindowsClosing = false;
if (!WindowNames.Contains(window.Title) && checkBox.IsChecked == true) if (!WindowNames.Contains(window.Title) && pluginSettings.Enabled)
{ {
_ = Task.Run(() => _ = Task.Run(() =>
{ {
@@ -190,19 +220,18 @@ namespace DesktopMagic
{ {
Dispatcher.Invoke(() => 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 = -1;
optionsComboBox.SelectedIndex = optionsComboBox.Items.IndexOf(checkBox.Content.ToString()); optionsComboBox.SelectedIndex = optionsComboBox.Items.IndexOf(pluginName);
window.PluginLoaded -= onPluginLoaded; window.PluginLoaded -= onPluginLoaded;
}); });
}; };
window.OnExit += () => window.OnExit += () =>
{ {
checkBox.IsChecked = false; pluginSettings.Enabled = false;
PluginCheckBox_Click(checkBox, null);
}; };
window.PluginLoaded += onPluginLoaded; window.PluginLoaded += onPluginLoaded;
@@ -235,18 +264,22 @@ namespace DesktopMagic
} }
} }
} }
pluginSettings.Enabled = checkBox.IsChecked == true;
blockWindowsClosing = true; blockWindowsClosing = true;
SaveSettings();
} }
private void DisplayWindow_ContentRendered(object sender, EventArgs e) private void DisplayWindow_ContentRendered(object? sender, EventArgs e)
{ {
WindowPos.SendWpfWindowBack((Window)sender); if (sender is not Window window)
WindowPos.SendWpfWindowBack((Window)sender); {
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; e.Cancel = blockWindowsClosing;
} }
@@ -255,6 +288,7 @@ namespace DesktopMagic
{ {
MessageBoxResult msbRes = MessageBox.Show((string)FindResource("wantToCloseProgram"), App.AppName, MessageBoxButton.YesNo); MessageBoxResult msbRes = MessageBox.Show((string)FindResource("wantToCloseProgram"), App.AppName, MessageBoxButton.YesNo);
e.Cancel = msbRes != MessageBoxResult.Yes; e.Cancel = msbRes != MessageBoxResult.Yes;
SaveSettings();
} }
private void Window_Closed(object sender, EventArgs e) private void Window_Closed(object sender, EventArgs e)
@@ -268,7 +302,7 @@ namespace DesktopMagic
Environment.Exit(0); Environment.Exit(0);
} }
private void Window_StateChanged(object sender, EventArgs e) private void Window_StateChanged(object? sender, EventArgs? e)
{ {
if (WindowState == WindowState.Minimized) if (WindowState == WindowState.Minimized)
{ {
@@ -291,7 +325,6 @@ namespace DesktopMagic
private void FontComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) private void FontComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
Settings.CurrentLayout.Theme.Font = fontComboBox.SelectedValue.ToString()!.Replace("System.Windows.Controls.ComboBoxItem: ", ""); Settings.CurrentLayout.Theme.Font = fontComboBox.SelectedValue.ToString()!.Replace("System.Windows.Controls.ComboBoxItem: ", "");
SaveSettings();
} }
private void OptionsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) private void OptionsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -368,8 +401,6 @@ namespace DesktopMagic
{ {
Settings.CurrentLayout.Theme.PrimaryColor = colorDialog.ResultColor; Settings.CurrentLayout.Theme.PrimaryColor = colorDialog.ResultColor;
primaryColorRechtangle.Fill = colorDialog.ResultBrush; primaryColorRechtangle.Fill = colorDialog.ResultBrush;
SaveSettings();
} }
} }
@@ -380,8 +411,6 @@ namespace DesktopMagic
{ {
Settings.CurrentLayout.Theme.SecondaryColor = colorDialog.ResultColor; Settings.CurrentLayout.Theme.SecondaryColor = colorDialog.ResultColor;
secondaryColorRechtangle.Fill = colorDialog.ResultBrush; secondaryColorRechtangle.Fill = colorDialog.ResultBrush;
SaveSettings();
} }
} }
@@ -392,19 +421,16 @@ namespace DesktopMagic
{ {
Settings.CurrentLayout.Theme.BackgroundColor = colorDialog.ResultColor; Settings.CurrentLayout.Theme.BackgroundColor = colorDialog.ResultColor;
backgroundColorRechtangle.Fill = colorDialog.ResultBrush; 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); bool sucess = int.TryParse(cornerRadiusTextBox.Text, out int cornerRadius);
if (sucess) if (sucess)
{ {
cornerRadiusTextBox.Foreground = Brushes.Black; cornerRadiusTextBox.Foreground = Brushes.Black;
Settings.CurrentLayout.Theme.CornerRadius = cornerRadius; Settings.CurrentLayout.Theme.CornerRadius = cornerRadius;
SaveSettings();
} }
else 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); bool sucess = int.TryParse(marginTextBox.Text, out int margin);
if (sucess) if (sucess)
{ {
marginTextBox.Foreground = Brushes.Black; marginTextBox.Foreground = Brushes.Black;
Settings.CurrentLayout.Theme.Margin = margin; Settings.CurrentLayout.Theme.Margin = margin;
SaveSettings();
} }
else else
{ {
@@ -429,9 +454,21 @@ namespace DesktopMagic
#region Layout #region Layout
private readonly JsonSerializerOptions jsonSettingsOptions = new()
{
Converters =
{
new ColorJsonConverter()
}
};
private void LayoutsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) private void LayoutsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
LoadLayout(false); if (loaded)
{
SaveSettings();
LoadLayout(false);
}
} }
private void NewLayoutButton_Click(object sender, RoutedEventArgs e) private void NewLayoutButton_Click(object sender, RoutedEventArgs e)
@@ -458,7 +495,7 @@ namespace DesktopMagic
return; return;
} }
string json = JsonSerializer.Serialize(Settings); string json = JsonSerializer.Serialize(Settings, jsonSettingsOptions);
File.WriteAllText(Path.Combine(App.ApplicationDataPath, "settings.json"), json); 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")); 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 = Layouts =
[ [
@@ -513,8 +550,6 @@ namespace DesktopMagic
WindowNames.Clear(); WindowNames.Clear();
optionsComboBox.Items.Clear(); optionsComboBox.Items.Clear();
_ = optionsComboBox.Items.Add((string)FindResource("musicVisualizer"));
bool showWindow = true; bool showWindow = true;
foreach (string pluginName in pluginNames) foreach (string pluginName in pluginNames)
@@ -522,16 +557,23 @@ namespace DesktopMagic
if (!Settings.CurrentLayout.Plugins.TryGetValue(pluginName, out PluginSettings? pluginSettings)) if (!Settings.CurrentLayout.Plugins.TryGetValue(pluginName, out PluginSettings? pluginSettings))
{ {
Settings.CurrentLayout.Plugins.Add(pluginName, new PluginSettings()); Settings.CurrentLayout.Plugins.Add(pluginName, new PluginSettings());
Settings.CurrentLayout.UpdatePlugins();
continue; continue;
} }
if (pluginSettings.Enabled)
{
LoadPlugin(pluginName);
}
if (showWindow && pluginSettings.Enabled) if (showWindow && pluginSettings.Enabled)
{ {
showWindow = false; showWindow = false;
} }
} }
Settings.CurrentLayout.UpdatePlugins();
if (!showWindow && minimize) if (!showWindow && minimize)
{ {
WindowState = WindowState.Minimized; WindowState = WindowState.Minimized;
@@ -549,16 +591,7 @@ namespace DesktopMagic
private void UpdatePluginsButton_Click(object sender, RoutedEventArgs e) private void UpdatePluginsButton_Click(object sender, RoutedEventArgs e)
{ {
LoadPlugins(); LoadPlugins();
_ = Task.Run(() => LoadLayout(false);
{
foreach (Window window in Windows.ToArray())
{
if (window.GetType() == typeof(PluginWindow))
{
((PluginWindow)window).UpdatePluginWindow();
}
}
});
} }
private void OpenPluginsFolderButton_Click(object sender, RoutedEventArgs e) private void OpenPluginsFolderButton_Click(object sender, RoutedEventArgs e)
@@ -573,7 +606,7 @@ namespace DesktopMagic
e.Handled = true; 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++) for (int i = 0; i < 10; i++)
{ {
+22 -4
View File
@@ -1,10 +1,28 @@
using DesktopMagicPluginAPI.Settings; using DesktopMagicPluginAPI.Settings;
using System.Text.Json.Serialization;
namespace DesktopMagic.Plugins; namespace DesktopMagic.Plugins;
public class InputElement(Setting element, string name, int orderIndex) public class InputElement
{ {
public Setting Input { get; } = element; public Setting Input { get; }
public string Name { get; } = name; public string Name { get; }
public int OrderIndex { get; } = orderIndex; 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.
} }
+10 -5
View File
@@ -45,10 +45,10 @@ public partial class PluginWindow : Window
Window w = new() Window w = new()
{ {
Left = settings.Position.X, Top = -100,
Top = settings.Position.Y, Left = -100,
Width = settings.Size.X, Width = 0,
Height = settings.Size.Y, Height = 0,
WindowStyle = WindowStyle.ToolWindow, WindowStyle = WindowStyle.ToolWindow,
ShowInTaskbar = false ShowInTaskbar = false
@@ -66,6 +66,11 @@ public partial class PluginWindow : Window
PluginName = pluginName; PluginName = pluginName;
this.settings = settings; 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) 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) 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")) if (!File.Exists($"{PluginFolderPath}\\{PluginName}.dll"))
{ {
@@ -28,7 +28,7 @@ internal class DesktopMagicSettings : INotifyPropertyChanged
public string? CurrentLayoutName public string? CurrentLayoutName
{ {
get => currentLayoutName; get => currentLayoutName ?? Layouts.FirstOrDefault()?.Name;
set set
{ {
currentLayoutName = value; currentLayoutName = value;
+2 -1
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace DesktopMagic.Settings; namespace DesktopMagic.Settings;
@@ -46,7 +47,7 @@ internal class Layout(string name) : INotifyPropertyChanged
public void UpdatePlugins() public void UpdatePlugins()
{ {
OnPropertyChanged(nameof(Plugins)); Plugins = Plugins.ToDictionary();
} }
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
+3 -3
View File
@@ -14,9 +14,9 @@ public class PluginSettings : INotifyPropertyChanged
private readonly Theme? theme; private readonly Theme? theme;
private List<InputElement> settings = []; private List<InputElement> settings = [];
private bool enabled = true; private bool enabled = false;
private Point position = new Point(0, 0); private Point position = new Point(100, 100);
private Point size = new Point(0, 0); private Point size = new Point(300, 300);
[JsonIgnore] [JsonIgnore]
public Theme Theme => theme is null ? MainWindowDataContext.GetSettings().CurrentLayout.Theme : theme; public Theme Theme => theme is null ? MainWindowDataContext.GetSettings().CurrentLayout.Theme : theme;