mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 08:56:15 +02:00
Improve plugin window performance by not constantly updating properties of UI elements
This commit is contained in:
@@ -39,7 +39,6 @@ namespace DesktopMagic
|
|||||||
private bool blockWindowsClosing = true;
|
private bool blockWindowsClosing = true;
|
||||||
public static List<PluginWindow> Windows { get; } = [];
|
public static List<PluginWindow> Windows { get; } = [];
|
||||||
public static List<string> WindowNames { get; } = [];
|
public static List<string> WindowNames { get; } = [];
|
||||||
internal static bool EditMode { get; set; } = false;
|
|
||||||
|
|
||||||
private DesktopMagicSettings Settings
|
private DesktopMagicSettings Settings
|
||||||
{
|
{
|
||||||
@@ -173,7 +172,11 @@ namespace DesktopMagic
|
|||||||
|
|
||||||
private void EditCheckBox_Click(object? sender, RoutedEventArgs? e)
|
private void EditCheckBox_Click(object? sender, RoutedEventArgs? e)
|
||||||
{
|
{
|
||||||
EditMode = EditCheckBox.IsChecked == true;
|
foreach (PluginWindow window in Windows)
|
||||||
|
{
|
||||||
|
window.SetEditMode(EditCheckBox.IsChecked == true);
|
||||||
|
}
|
||||||
|
|
||||||
SaveSettings();
|
SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,6 +264,7 @@ namespace DesktopMagic
|
|||||||
|
|
||||||
window.ShowInTaskbar = false;
|
window.ShowInTaskbar = false;
|
||||||
window.Show();
|
window.Show();
|
||||||
|
window.SetEditMode(EditCheckBox.IsChecked == true);
|
||||||
window.ContentRendered += DisplayWindow_ContentRendered;
|
window.ContentRendered += DisplayWindow_ContentRendered;
|
||||||
window.Closing += DisplayWindow_Closing;
|
window.Closing += DisplayWindow_Closing;
|
||||||
Windows.Add(window);
|
Windows.Add(window);
|
||||||
@@ -565,7 +569,6 @@ namespace DesktopMagic
|
|||||||
}
|
}
|
||||||
|
|
||||||
EditCheckBox.IsChecked = false;
|
EditCheckBox.IsChecked = false;
|
||||||
EditMode = false;
|
|
||||||
blockWindowsClosing = true;
|
blockWindowsClosing = true;
|
||||||
Windows.Clear();
|
Windows.Clear();
|
||||||
WindowNames.Clear();
|
WindowNames.Clear();
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
<Grid>
|
<Grid>
|
||||||
<Rectangle x:Name="panel" Fill="#4CBAFFEF" Stroke="White" Margin="0,0,0,0" Visibility="Collapsed" />
|
<Rectangle x:Name="panel" Fill="#4CBAFFEF" Stroke="White" Margin="0,0,0,0" Visibility="Collapsed" />
|
||||||
|
|
||||||
<Border x:Name="border" />
|
<Border SizeChanged="Border_SizeChanged" x:Name="border" />
|
||||||
<Viewbox x:Name="viewBox" StretchDirection="Both" Stretch="Uniform">
|
<Viewbox SizeChanged="ViewBox_SizeChanged" x:Name="viewBox" StretchDirection="Both" Stretch="Uniform">
|
||||||
<Viewbox.Clip>
|
<Viewbox.Clip>
|
||||||
<RectangleGeometry x:Name="rectangleGeometry" RadiusX="{Binding ElementName=border, Path=CornerRadius.TopLeft}" RadiusY="{Binding ElementName=border, Path=CornerRadius.TopLeft}" />
|
<RectangleGeometry x:Name="rectangleGeometry" RadiusX="{Binding ElementName=border, Path=CornerRadius.TopLeft}" RadiusY="{Binding ElementName=border, Path=CornerRadius.TopLeft}" />
|
||||||
</Viewbox.Clip>
|
</Viewbox.Clip>
|
||||||
|
|||||||
@@ -28,9 +28,8 @@ public partial class PluginWindow : Window
|
|||||||
public event Action? OnExit;
|
public event Action? OnExit;
|
||||||
|
|
||||||
private readonly PluginSettings settings;
|
private readonly PluginSettings settings;
|
||||||
private readonly System.Timers.Timer? updateTimer;
|
|
||||||
private Thread? pluginThread;
|
private Thread? pluginThread;
|
||||||
private System.Timers.Timer? valueTimer;
|
private System.Timers.Timer? updateTimer;
|
||||||
private Plugin? pluginClassInstance;
|
private Plugin? pluginClassInstance;
|
||||||
|
|
||||||
public bool IsRunning { get; private set; } = true;
|
public bool IsRunning { get; private set; } = true;
|
||||||
@@ -55,12 +54,7 @@ public partial class PluginWindow : Window
|
|||||||
Owner = w;
|
Owner = w;
|
||||||
w.Hide();
|
w.Hide();
|
||||||
|
|
||||||
updateTimer = new System.Timers.Timer
|
settings.Theme.PropertyChanged += (e, s) => ThemeChanged();
|
||||||
{
|
|
||||||
Interval = 100
|
|
||||||
};
|
|
||||||
updateTimer.Elapsed += UpdateTimer_Elapsed;
|
|
||||||
updateTimer.Start();
|
|
||||||
|
|
||||||
PluginMetadata = pluginMetadata;
|
PluginMetadata = pluginMetadata;
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
@@ -80,7 +74,8 @@ public partial class PluginWindow : Window
|
|||||||
|
|
||||||
public void UpdatePluginWindow()
|
public void UpdatePluginWindow()
|
||||||
{
|
{
|
||||||
ValueTimer_Elapsed(valueTimer, null);
|
Dispatcher.Invoke(ThemeChanged);
|
||||||
|
UpdateTimer_Elapsed(updateTimer, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Exit()
|
public void Exit()
|
||||||
@@ -93,6 +88,28 @@ public partial class PluginWindow : Window
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetEditMode(bool enabled)
|
||||||
|
{
|
||||||
|
if (enabled)
|
||||||
|
{
|
||||||
|
panel.Visibility = Visibility.Visible;
|
||||||
|
imageBorder.BorderThickness = new Thickness(3);
|
||||||
|
image.Margin = new(-3);
|
||||||
|
WindowPos.SetIsLocked(this, false);
|
||||||
|
tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10;
|
||||||
|
ResizeMode = ResizeMode.CanResize;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
panel.Visibility = Visibility.Collapsed;
|
||||||
|
imageBorder.BorderThickness = new Thickness(0);
|
||||||
|
image.Margin = new Thickness(0);
|
||||||
|
WindowPos.SetIsLocked(this, true);
|
||||||
|
tileBar.CaptionHeight = 0;
|
||||||
|
ResizeMode = ResizeMode.NoResize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnSourceInitialized(EventArgs e)
|
protected override void OnSourceInitialized(EventArgs e)
|
||||||
{
|
{
|
||||||
base.OnSourceInitialized(e);
|
base.OnSourceInitialized(e);
|
||||||
@@ -126,48 +143,11 @@ public partial class PluginWindow : Window
|
|||||||
pluginThread.Start();
|
pluginThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateTimer_Elapsed(object? sender, ElapsedEventArgs e)
|
private void ThemeChanged()
|
||||||
{
|
{
|
||||||
Dispatcher.Invoke(() =>
|
viewBox.Margin = new Thickness(settings.Theme.Margin);
|
||||||
{
|
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
|
||||||
if (MainWindow.EditMode)
|
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
|
||||||
{
|
|
||||||
panel.Visibility = Visibility.Visible;
|
|
||||||
imageBorder.BorderThickness = new Thickness(3);
|
|
||||||
image.Margin = new(-3);
|
|
||||||
WindowPos.SetIsLocked(this, false);
|
|
||||||
tileBar.CaptionHeight = tileBar.CaptionHeight = ActualHeight - 10 < 0 ? 0 : ActualHeight - 10;
|
|
||||||
ResizeMode = ResizeMode.CanResize;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
panel.Visibility = Visibility.Collapsed;
|
|
||||||
imageBorder.BorderThickness = new Thickness(0);
|
|
||||||
image.Margin = new Thickness(0);
|
|
||||||
WindowPos.SetIsLocked(this, true);
|
|
||||||
tileBar.CaptionHeight = 0;
|
|
||||||
ResizeMode = ResizeMode.NoResize;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!IsRunning)
|
|
||||||
{
|
|
||||||
(sender as System.Timers.Timer)?.Stop();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
viewBox.Margin = new Thickness(settings.Theme.Margin);
|
|
||||||
border.Width = viewBox.ActualWidth + (settings.Theme.Margin * 2);
|
|
||||||
border.Height = viewBox.ActualHeight + (settings.Theme.Margin * 2);
|
|
||||||
rectangleGeometry.Rect = new Rect(-settings.Theme.Margin, -settings.Theme.Margin, border.ActualWidth, border.ActualHeight);
|
|
||||||
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
|
|
||||||
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!IsRunning)
|
|
||||||
{
|
|
||||||
updateTimer?.Stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadPlugin()
|
private void LoadPlugin()
|
||||||
@@ -232,19 +212,19 @@ public partial class PluginWindow : Window
|
|||||||
LoadOptions(pluginClassInstance);
|
LoadOptions(pluginClassInstance);
|
||||||
BindDefaultSettings(pluginClassInstance);
|
BindDefaultSettings(pluginClassInstance);
|
||||||
|
|
||||||
valueTimer = new System.Timers.Timer
|
updateTimer = new System.Timers.Timer
|
||||||
{
|
{
|
||||||
Interval = 1000
|
Interval = 1000
|
||||||
};
|
};
|
||||||
valueTimer.Elapsed += ValueTimer_Elapsed;
|
updateTimer.Elapsed += UpdateTimer_Elapsed;
|
||||||
|
|
||||||
pluginClassInstance.Start();
|
pluginClassInstance.Start();
|
||||||
UpdatePluginWindow();
|
UpdatePluginWindow();
|
||||||
|
|
||||||
if (pluginClassInstance.UpdateInterval > 0)
|
if (pluginClassInstance.UpdateInterval > 0)
|
||||||
{
|
{
|
||||||
valueTimer.Interval = pluginClassInstance.UpdateInterval;
|
updateTimer.Interval = pluginClassInstance.UpdateInterval;
|
||||||
valueTimer.Start();
|
updateTimer.Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,24 +241,30 @@ public partial class PluginWindow : Window
|
|||||||
|
|
||||||
void SetVerticalAlignment()
|
void SetVerticalAlignment()
|
||||||
{
|
{
|
||||||
viewBox.VerticalAlignment = pluginClassInstance.verticalAlignment.Value switch
|
VerticalAlignment verticalAlignment = pluginClassInstance.verticalAlignment.Value switch
|
||||||
{
|
{
|
||||||
"Top" => VerticalAlignment.Top,
|
"Top" => VerticalAlignment.Top,
|
||||||
"Center" => VerticalAlignment.Center,
|
"Center" => VerticalAlignment.Center,
|
||||||
"Bottom" => VerticalAlignment.Bottom,
|
"Bottom" => VerticalAlignment.Bottom,
|
||||||
_ => VerticalAlignment.Center
|
_ => VerticalAlignment.Center
|
||||||
};
|
};
|
||||||
|
|
||||||
|
viewBox.VerticalAlignment = verticalAlignment;
|
||||||
|
border.VerticalAlignment = verticalAlignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetHorizontalAlignment()
|
void SetHorizontalAlignment()
|
||||||
{
|
{
|
||||||
viewBox.HorizontalAlignment = pluginClassInstance.horizontalAlignment.Value switch
|
HorizontalAlignment horizontalAlignment = pluginClassInstance.horizontalAlignment.Value switch
|
||||||
{
|
{
|
||||||
"Left" => HorizontalAlignment.Left,
|
"Left" => HorizontalAlignment.Left,
|
||||||
"Center" => HorizontalAlignment.Center,
|
"Center" => HorizontalAlignment.Center,
|
||||||
"Right" => HorizontalAlignment.Right,
|
"Right" => HorizontalAlignment.Right,
|
||||||
_ => HorizontalAlignment.Center
|
_ => HorizontalAlignment.Center
|
||||||
};
|
};
|
||||||
|
|
||||||
|
viewBox.HorizontalAlignment = horizontalAlignment;
|
||||||
|
border.HorizontalAlignment = horizontalAlignment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,7 +314,7 @@ public partial class PluginWindow : Window
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ValueTimer_Elapsed(object? sender, ElapsedEventArgs? e)
|
private void UpdateTimer_Elapsed(object? sender, ElapsedEventArgs? e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -338,11 +324,11 @@ public partial class PluginWindow : Window
|
|||||||
|
|
||||||
if (pluginClassInstance.UpdateInterval > 0)
|
if (pluginClassInstance.UpdateInterval > 0)
|
||||||
{
|
{
|
||||||
valueTimer!.Interval = pluginClassInstance.UpdateInterval;
|
updateTimer!.Interval = pluginClassInstance.UpdateInterval;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
valueTimer!.Stop();
|
updateTimer!.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result is not null)
|
if (result is not null)
|
||||||
@@ -375,10 +361,21 @@ public partial class PluginWindow : Window
|
|||||||
|
|
||||||
if (!IsRunning)
|
if (!IsRunning)
|
||||||
{
|
{
|
||||||
valueTimer!.Stop();
|
updateTimer!.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Border_SizeChanged(object sender, SizeChangedEventArgs e)
|
||||||
|
{
|
||||||
|
rectangleGeometry.Rect = new Rect(-settings.Theme.Margin, -settings.Theme.Margin, e.NewSize.Width, e.NewSize.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ViewBox_SizeChanged(object sender, SizeChangedEventArgs e)
|
||||||
|
{
|
||||||
|
border.Width = e.NewSize.Width + (settings.Theme.Margin * 2);
|
||||||
|
border.Height = e.NewSize.Height + (settings.Theme.Margin * 2);
|
||||||
|
}
|
||||||
|
|
||||||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||||
{
|
{
|
||||||
App.Logger.LogInfo($"\"{PluginMetadata.Name}\" - Stopping plugin", source: "Plugin");
|
App.Logger.LogInfo($"\"{PluginMetadata.Name}\" - Stopping plugin", source: "Plugin");
|
||||||
|
|||||||
Reference in New Issue
Block a user