From 4a93204ffdba1a61212b94da504c41418ccbc611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayta=C3=A7=20Kayadelen?= Date: Fri, 29 Dec 2023 18:03:41 +0100 Subject: [PATCH] fix UI bugs of Device Manager --- .../Controls/Control_DeviceItem.xaml.cs | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/Project-Aurora/Project-Aurora/Controls/Control_DeviceItem.xaml.cs b/Project-Aurora/Project-Aurora/Controls/Control_DeviceItem.xaml.cs index 3e7e38d80..1d738b401 100644 --- a/Project-Aurora/Project-Aurora/Controls/Control_DeviceItem.xaml.cs +++ b/Project-Aurora/Project-Aurora/Controls/Control_DeviceItem.xaml.cs @@ -17,7 +17,9 @@ namespace Aurora.Controls; public partial class Control_DeviceItem { [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] - public static readonly DependencyProperty DeviceProperty = DependencyProperty.Register(nameof(Device), typeof(DeviceContainer), typeof(Control_DeviceItem), new PropertyMetadata(DevicePropertyUpdated)); + public static readonly DependencyProperty DeviceProperty = DependencyProperty.Register( + nameof(Device), typeof(DeviceContainer), typeof(Control_DeviceItem), new PropertyMetadata(DevicePropertyUpdated) + ); private static void DevicePropertyUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e) { @@ -33,6 +35,8 @@ private static void DevicePropertyUpdated(DependencyObject d, DependencyProperty private readonly Timer _updateControlsTimer; + private bool _deviceRunning; + public DeviceContainer Device { get => (DeviceContainer)GetValue(DeviceProperty); @@ -69,17 +73,20 @@ private void btnToggleOnOff_Click(object? sender, EventArgs e) btnStart.Content = "Working..."; btnStart.IsEnabled = false; var device = Device; - Task.Run(async () => + if (device.Device.IsInitialized) { - if (device.Device.IsInitialized) + Task.Run(async () => { await device.DisableDevice(); - } - else + }); + } + else + { + Task.Run(async () => { await device.EnableDevice(); - } - }); + }); + } } private void btnToggleEnableDisable_Click(object? sender, EventArgs e) @@ -93,21 +100,22 @@ private void btnToggleEnableDisable_Click(object? sender, EventArgs e) { Global.DeviceConfiguration.EnabledDevices.Remove(Device.Device.DeviceName); } - btnStart.Content = "Working..."; - btnStart.IsEnabled = false; - btnEnable.IsEnabled = false; + UpdateDynamic(); - var device = Device; - Task.Run(async () => + if (_deviceRunning) { - if (deviceEnabled) + btnStart.Content = "Working..."; + btnStart.IsEnabled = false; + btnEnable.IsEnabled = false; + + var device = Device; + Task.Run(async () => { await device.DisableDevice(); - } - else - { - await device.EnableDevice(); - } + }); + } + Task.Run(() => + { ConfigManager.Save(Global.DeviceConfiguration, DeviceConfig.ConfigFile); }); } @@ -149,7 +157,7 @@ private void OnDeviceOnUpdated(object? o, EventArgs eventArgs) private void UpdateStatic() { Beta.Visibility = Device.Device.Tooltips.Beta ? Visibility.Visible : Visibility.Hidden; - + var infoTooltip = Device.Device.Tooltips.Info; if (infoTooltip != null) { @@ -184,6 +192,7 @@ private void UpdateDynamic() { if (Device.Device.isDoingWork) { + _deviceRunning = false; btnStart.Content = "Working..."; btnStart.IsEnabled = false; btnEnable.IsEnabled = false; @@ -191,6 +200,7 @@ private void UpdateDynamic() } else if (Device.Device.IsInitialized) { + _deviceRunning = true; btnStart.Content = "Stop"; btnStart.IsEnabled = true; btnEnable.IsEnabled = true; @@ -198,6 +208,7 @@ private void UpdateDynamic() } else { + _deviceRunning = false; btnStart.Content = "Start"; btnStart.IsEnabled = true; btnEnable.IsEnabled = true; @@ -237,10 +248,7 @@ private void btnViewOptions_Click(object? sender, RoutedEventArgs e) RegisteredVariables = Device.Device.RegisteredVariables } }; - optionsWindow.Closing += (_, _) => - { - ConfigManager.Save(Global.DeviceConfiguration, DeviceConfig.ConfigFile); - }; + optionsWindow.Closing += (_, _) => { ConfigManager.Save(Global.DeviceConfiguration, DeviceConfig.ConfigFile); }; optionsWindow.ShowDialog(); }