From f09b4a20bfb07ccd8805c83d9e36579d9ed33cbf Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 24 Jul 2022 14:25:13 +0200 Subject: [PATCH] Gradient picker button - Always update gradient on UI thread Data bindings - Added AlwaysApplyDataBindings setting --- .../GradientPicker/GradientPickerButton.cs | 5 ++-- .../Panels/Playback/PlaybackViewModel.cs | 2 -- .../DataBinding/DataBindingViewModel.cs | 27 ++++++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Artemis.UI.Shared/Controls/GradientPicker/GradientPickerButton.cs b/src/Artemis.UI.Shared/Controls/GradientPicker/GradientPickerButton.cs index 1b30c6707..5c7ee300c 100644 --- a/src/Artemis.UI.Shared/Controls/GradientPicker/GradientPickerButton.cs +++ b/src/Artemis.UI.Shared/Controls/GradientPicker/GradientPickerButton.cs @@ -10,6 +10,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Data; using Avalonia.Interactivity; using Avalonia.Media; +using Avalonia.Threading; using FluentAvalonia.Core; using Button = FluentAvalonia.UI.Controls.Button; @@ -138,12 +139,12 @@ public class GradientPickerButton : TemplatedControl private void ColorGradientOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { - UpdateGradient(); + Dispatcher.UIThread.Post(UpdateGradient); } private void ColorGradientOnStopChanged(object? sender, EventArgs e) { - UpdateGradient(); + Dispatcher.UIThread.Post(UpdateGradient); } private void OnButtonClick(object? sender, RoutedEventArgs e) diff --git a/src/Artemis.UI/Screens/ProfileEditor/Panels/Playback/PlaybackViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/Panels/Playback/PlaybackViewModel.cs index e05a3158c..e0c8bba80 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/Panels/Playback/PlaybackViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/Panels/Playback/PlaybackViewModel.cs @@ -6,8 +6,6 @@ using Artemis.Core; using Artemis.Core.Services; using Artemis.UI.Shared; using Artemis.UI.Shared.Services.ProfileEditor; -using Avalonia.Controls; -using Avalonia.Input; using Avalonia.Threading; using ReactiveUI; diff --git a/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/DataBinding/DataBindingViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/DataBinding/DataBindingViewModel.cs index eb3feee92..aea8f0a19 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/DataBinding/DataBindingViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/DataBinding/DataBindingViewModel.cs @@ -1,6 +1,10 @@ -using System.Reactive.Linq; +using System; +using System.Reactive; +using System.Reactive.Disposables; +using System.Reactive.Linq; using System.Threading.Tasks; using Artemis.Core; +using Artemis.Core.Services; using Artemis.UI.Extensions; using Artemis.UI.Ninject.Factories; using Artemis.UI.Screens.VisualScripting; @@ -8,7 +12,7 @@ using Artemis.UI.Shared; using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services.ProfileEditor; using Artemis.UI.Shared.Services.ProfileEditor.Commands; -using Avalonia.Controls.Mixins; +using Avalonia.Threading; using ReactiveUI; namespace Artemis.UI.Screens.ProfileEditor.Properties.DataBinding; @@ -17,14 +21,17 @@ public class DataBindingViewModel : ActivatableViewModelBase { private readonly IProfileEditorService _profileEditorService; private readonly IWindowService _windowService; + private readonly PluginSetting _alwaysApplyDataBindings; private ObservableAsPropertyHelper? _dataBindingEnabled; private ObservableAsPropertyHelper? _layerProperty; private ObservableAsPropertyHelper? _nodeScriptViewModel; + private bool _playing; - public DataBindingViewModel(IProfileEditorService profileEditorService, INodeVmFactory nodeVmFactory, IWindowService windowService) + public DataBindingViewModel(IProfileEditorService profileEditorService, INodeVmFactory nodeVmFactory, IWindowService windowService, ISettingsService settingsService) { _profileEditorService = profileEditorService; _windowService = windowService; + _alwaysApplyDataBindings = settingsService.GetSetting("ProfileEditor.AlwaysApplyDataBindings", false); this.WhenActivated(d => { @@ -41,6 +48,11 @@ public class DataBindingViewModel : ActivatableViewModelBase .Select(b => b.IsEnabled) .ToProperty(this, vm => vm.DataBindingEnabled) .DisposeWith(d); + _profileEditorService.Playing.CombineLatest(_profileEditorService.SuspendedEditing).Subscribe(tuple => _playing = tuple.First || tuple.Second).DisposeWith(d); + + DispatcherTimer updateTimer = new(TimeSpan.FromMilliseconds(60.0 / 1000), DispatcherPriority.Render, Update); + updateTimer.Start(); + Disposable.Create(() => updateTimer.Stop()).DisposeWith(d); }); } @@ -62,4 +74,13 @@ public class DataBindingViewModel : ActivatableViewModelBase if (LayerProperty != null && LayerProperty.BaseDataBinding.IsEnabled) await _windowService.ShowDialogAsync(("nodeScript", LayerProperty.BaseDataBinding.Script)); } + + private void Update(object? sender, EventArgs e) + { + // If playing the data binding will already be updated, no need to do it here + if (_playing || !_alwaysApplyDataBindings.Value) + return; + + LayerProperty?.UpdateDataBinding(); + } } \ No newline at end of file