1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-02-04 10:53:31 +00:00

UI - Use Timer instead of DispatcherTimer where feasible

This commit is contained in:
Robert 2023-05-20 23:11:44 +02:00
parent 4ffe8d8848
commit fa710777d7
7 changed files with 42 additions and 52 deletions

View File

@ -34,7 +34,7 @@ public class DeviceVisualizer : Control
/// <inheritdoc /> /// <inheritdoc />
public DeviceVisualizer() public DeviceVisualizer()
{ {
_timer = new DispatcherTimer(DispatcherPriority.Render) {Interval = TimeSpan.FromMilliseconds(1000.0 / UpdateFrameRate)}; _timer = new DispatcherTimer(DispatcherPriority.Background) {Interval = TimeSpan.FromMilliseconds(1000.0 / UpdateFrameRate)};
_deviceVisualizerLeds = new List<DeviceVisualizerLed>(); _deviceVisualizerLeds = new List<DeviceVisualizerLed>();
PointerReleased += OnPointerReleased; PointerReleased += OnPointerReleased;

View File

@ -78,6 +78,6 @@ public class OpenFileDialogBuilder
public async Task<string[]?> ShowAsync() public async Task<string[]?> ShowAsync()
{ {
IReadOnlyList<IStorageFile> files = await _parent.StorageProvider.OpenFilePickerAsync(_options); IReadOnlyList<IStorageFile> files = await _parent.StorageProvider.OpenFilePickerAsync(_options);
return files.Select(f => f.Path.LocalPath).ToArray(); return files.Count == 0 ? null : files.Select(f => f.Path.LocalPath).ToArray();
} }
} }

View File

@ -2,11 +2,11 @@
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Timers;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Services.ProfileEditor; using Artemis.UI.Shared.Services.ProfileEditor;
using Avalonia.Threading;
using ReactiveUI; using ReactiveUI;
namespace Artemis.UI.Screens.ProfileEditor.Playback; namespace Artemis.UI.Screens.ProfileEditor.Playback;
@ -53,13 +53,17 @@ public class PlaybackViewModel : ActivatableViewModelBase
_formattedCurrentTime = _profileEditorService.Time.Select(t => $"{Math.Floor(t.TotalSeconds):00}.{t.Milliseconds:000}").ToProperty(this, vm => vm.FormattedCurrentTime).DisposeWith(d); _formattedCurrentTime = _profileEditorService.Time.Select(t => $"{Math.Floor(t.TotalSeconds):00}.{t.Milliseconds:000}").ToProperty(this, vm => vm.FormattedCurrentTime).DisposeWith(d);
_playing = _profileEditorService.Playing.ToProperty(this, vm => vm.Playing).DisposeWith(d); _playing = _profileEditorService.Playing.ToProperty(this, vm => vm.Playing).DisposeWith(d);
_keyBindingsEnabled = Shared.UI.KeyBindingsEnabled.ToProperty(this, vm => vm.KeyBindingsEnabled).DisposeWith(d); _keyBindingsEnabled = Shared.UI.KeyBindingsEnabled.ToProperty(this, vm => vm.KeyBindingsEnabled).DisposeWith(d);
// Update timer
Timer updateTimer = new(TimeSpan.FromMilliseconds(60.0 / 1000));
updateTimer.Elapsed += (_, _) => Update();
updateTimer.DisposeWith(d);
_profileEditorService.Playing.Subscribe(_ => _lastUpdate = DateTime.Now).DisposeWith(d);
_profileEditorService.Playing.Subscribe(p => updateTimer.Enabled = p).DisposeWith(d);
_lastUpdate = DateTime.MinValue; _lastUpdate = DateTime.MinValue;
DispatcherTimer updateTimer = new(TimeSpan.FromMilliseconds(60.0 / 1000), DispatcherPriority.Background, Update);
updateTimer.Start();
Disposable.Create(() => Disposable.Create(() =>
{ {
updateTimer.Stop();
_settingsService.GetSetting("ProfileEditor.RepeatTimeline", true).Value = _repeating && _repeatTimeline; _settingsService.GetSetting("ProfileEditor.RepeatTimeline", true).Value = _repeating && _repeatTimeline;
_settingsService.GetSetting("ProfileEditor.RepeatSegment", false).Value = _repeating && _repeatSegment; _settingsService.GetSetting("ProfileEditor.RepeatSegment", false).Value = _repeating && _repeatSegment;
}).DisposeWith(d); }).DisposeWith(d);
@ -206,13 +210,10 @@ public class PlaybackViewModel : ActivatableViewModelBase
return TimeSpan.Zero; return TimeSpan.Zero;
} }
private void Update(object? sender, EventArgs e) private void Update()
{ {
try try
{ {
if (!Playing)
return;
if (_lastUpdate == DateTime.MinValue) if (_lastUpdate == DateTime.MinValue)
_lastUpdate = DateTime.Now; _lastUpdate = DateTime.Now;

View File

@ -2,6 +2,7 @@
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.DryIoc.Factories; using Artemis.UI.DryIoc.Factories;
@ -50,20 +51,16 @@ public class DataBindingViewModel : ActivatableViewModelBase
.DisposeWith(d); .DisposeWith(d);
_profileEditorService.Playing.CombineLatest(_profileEditorService.SuspendedEditing).Subscribe(tuple => _playing = tuple.First || tuple.Second).DisposeWith(d); _profileEditorService.Playing.CombineLatest(_profileEditorService.SuspendedEditing).Subscribe(tuple => _playing = tuple.First || tuple.Second).DisposeWith(d);
DispatcherTimer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000), DispatcherPriority.Background, Update); Timer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000));
// TODO: Remove in favor of saving each time a node editor command is executed Timer saveTimer = new(TimeSpan.FromMinutes(2));
DispatcherTimer saveTimer = new(TimeSpan.FromMinutes(2), DispatcherPriority.Normal, Save);
updateTimer.Elapsed += (_, _) => Update();
saveTimer.Elapsed += (_, _) => Save();
updateTimer.Start(); updateTimer.Start();
saveTimer.Start(); saveTimer.Start();
Disposable.Create(() => updateTimer.DisposeWith(d);
{ saveTimer.DisposeWith(d);
updateTimer.Stop();
saveTimer.Stop();
_profileEditorService.SaveProfile();
}).DisposeWith(d);
}); });
} }
@ -97,7 +94,7 @@ public class DataBindingViewModel : ActivatableViewModelBase
} }
} }
private void Update(object? sender, EventArgs e) private void Update()
{ {
// If playing the data binding will already be updated, no need to do it here // If playing the data binding will already be updated, no need to do it here
if (_playing || !_alwaysApplyDataBindings.Value) if (_playing || !_alwaysApplyDataBindings.Value)
@ -106,7 +103,7 @@ public class DataBindingViewModel : ActivatableViewModelBase
LayerProperty?.UpdateDataBinding(); LayerProperty?.UpdateDataBinding();
} }
private void Save(object? sender, EventArgs e) private void Save()
{ {
if (!_editorOpen) if (!_editorOpen)
_profileEditorService.SaveProfile(); _profileEditorService.SaveProfile();

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Timers;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Avalonia.Threading;
using Humanizer; using Humanizer;
using ReactiveUI; using ReactiveUI;
@ -13,7 +13,6 @@ public class ModuleActivationRequirementViewModel : ActivatableViewModelBase
private readonly IModuleActivationRequirement _activationRequirement; private readonly IModuleActivationRequirement _activationRequirement;
private string _requirementDescription; private string _requirementDescription;
private bool _requirementMet; private bool _requirementMet;
private DispatcherTimer? _updateTimer;
public ModuleActivationRequirementViewModel(IModuleActivationRequirement activationRequirement) public ModuleActivationRequirementViewModel(IModuleActivationRequirement activationRequirement)
{ {
@ -23,14 +22,10 @@ public class ModuleActivationRequirementViewModel : ActivatableViewModelBase
this.WhenActivated(d => this.WhenActivated(d =>
{ {
_updateTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(500), DispatcherPriority.Background, Update); Timer updateTimer = new(TimeSpan.FromMilliseconds(500));
_updateTimer.Start(); updateTimer.Elapsed += (_, _) => Update();
updateTimer.Start();
Disposable.Create(() => updateTimer.DisposeWith(d);
{
_updateTimer?.Stop();
_updateTimer = null;
}).DisposeWith(d);
}); });
} }
@ -48,7 +43,7 @@ public class ModuleActivationRequirementViewModel : ActivatableViewModelBase
set => RaiseAndSetIfChanged(ref _requirementMet, value); set => RaiseAndSetIfChanged(ref _requirementMet, value);
} }
private void Update(object? sender, EventArgs e) private void Update()
{ {
RequirementDescription = _activationRequirement.GetUserFriendlyDescription(); RequirementDescription = _activationRequirement.GetUserFriendlyDescription();
RequirementMet = _activationRequirement.Evaluate(); RequirementMet = _activationRequirement.Evaluate();

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.DryIoc.Factories; using Artemis.UI.DryIoc.Factories;
@ -12,9 +13,7 @@ using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.NodeEditor; using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands; using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Avalonia; using Avalonia;
using Avalonia.Threading;
using DynamicData; using DynamicData;
using DynamicData.List;
using ReactiveUI; using ReactiveUI;
namespace Artemis.UI.Screens.VisualScripting; namespace Artemis.UI.Screens.VisualScripting;
@ -66,19 +65,17 @@ public class NodeScriptWindowViewModel : NodeScriptWindowViewModelBase
this.WhenActivated(d => this.WhenActivated(d =>
{ {
_keyBindingsEnabled = Shared.UI.KeyBindingsEnabled.ToProperty(this, vm => vm.KeyBindingsEnabled).DisposeWith(d); _keyBindingsEnabled = Shared.UI.KeyBindingsEnabled.ToProperty(this, vm => vm.KeyBindingsEnabled).DisposeWith(d);
DispatcherTimer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000), DispatcherPriority.Background, Update);
// TODO: Remove in favor of saving each time a node editor command is executed
DispatcherTimer saveTimer = new(TimeSpan.FromMinutes(2), DispatcherPriority.Background, Save);
Timer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000));
Timer saveTimer = new(TimeSpan.FromMinutes(2));
updateTimer.Elapsed += (_, _) => Update();
saveTimer.Elapsed += (_, _) => Save();
updateTimer.Start(); updateTimer.Start();
saveTimer.Start(); saveTimer.Start();
Disposable.Create(() => updateTimer.DisposeWith(d);
{ saveTimer.DisposeWith(d);
updateTimer.Stop();
saveTimer.Stop();
}).DisposeWith(d);
}); });
} }
@ -93,7 +90,7 @@ public class NodeScriptWindowViewModel : NodeScriptWindowViewModelBase
public ReactiveCommand<Unit, Unit> Export { get; } public ReactiveCommand<Unit, Unit> Export { get; }
public ReactiveCommand<Unit, Unit> Import { get; } public ReactiveCommand<Unit, Unit> Import { get; }
public bool KeyBindingsEnabled => _keyBindingsEnabled?.Value ?? false; public bool KeyBindingsEnabled => _keyBindingsEnabled?.Value ?? false;
public PluginSetting<bool> ShowDataModelValues => _settingsService.GetSetting("ProfileEditor.ShowDataModelValues", false); public PluginSetting<bool> ShowDataModelValues => _settingsService.GetSetting("ProfileEditor.ShowDataModelValues", false);
public PluginSetting<bool> ShowFullPaths => _settingsService.GetSetting("ProfileEditor.ShowFullPaths", false); public PluginSetting<bool> ShowFullPaths => _settingsService.GetSetting("ProfileEditor.ShowFullPaths", false);
public PluginSetting<bool> AlwaysShowValues => _settingsService.GetSetting("ProfileEditor.AlwaysShowValues", true); public PluginSetting<bool> AlwaysShowValues => _settingsService.GetSetting("ProfileEditor.AlwaysShowValues", true);
@ -176,13 +173,13 @@ public class NodeScriptWindowViewModel : NodeScriptWindowViewModelBase
} }
} }
private void Update(object? sender, EventArgs e) private void Update()
{ {
if (!_pauseUpdate) if (!_pauseUpdate)
NodeScript.Run(); NodeScript.Run();
} }
private void Save(object? sender, EventArgs e) private void Save()
{ {
if (NodeScript.Context is Profile profile) if (NodeScript.Context is Profile profile)
_profileService.SaveProfile(profile, true); _profileService.SaveProfile(profile, true);

View File

@ -1,7 +1,6 @@
using System.Reactive.Disposables; using System.Reactive.Disposables;
using Artemis.Core; using Artemis.Core;
using Artemis.UI.Shared.VisualScripting; using Artemis.UI.Shared.VisualScripting;
using Avalonia.Threading;
using ReactiveUI; using ReactiveUI;
namespace Artemis.VisualScripting.Nodes.Static.Screens; namespace Artemis.VisualScripting.Nodes.Static.Screens;
@ -18,9 +17,10 @@ public class DisplayValueNodeCustomViewModel : CustomNodeViewModel
// Because the DisplayValueNode has no output it never evaluates, manually do so here // Because the DisplayValueNode has no output it never evaluates, manually do so here
this.WhenActivated(d => this.WhenActivated(d =>
{ {
DispatcherTimer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000), DispatcherPriority.Background, Update); System.Timers.Timer updateTimer = new(TimeSpan.FromMilliseconds(25.0 / 1000));
updateTimer.Elapsed += (_, _) => Update();
updateTimer.Start(); updateTimer.Start();
Disposable.Create(() => updateTimer.Stop()).DisposeWith(d); updateTimer.DisposeWith(d);
}); });
} }
@ -30,7 +30,7 @@ public class DisplayValueNodeCustomViewModel : CustomNodeViewModel
private set => this.RaiseAndSetIfChanged(ref _currentValue, value); private set => this.RaiseAndSetIfChanged(ref _currentValue, value);
} }
private void Update(object? sender, EventArgs e) private void Update()
{ {
try try
{ {