Skip to content

Commit

Permalink
Profiles - Avoid unnecessary OnBrokenStateChanged calls
Browse files Browse the repository at this point in the history
Profiles - Fixed int range/float range deserialization
Profiles - Use Random.Shared where applicable
  • Loading branch information
RobertBeekman committed Mar 2, 2024
1 parent 5c7497a commit 28640f9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/Artemis.Core/Models/BreakableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public bool TryOrBreak(Action action, string breakMessage)
/// <inheritdoc />
public void SetBrokenState(string state, Exception? exception = null)
{
if (state == BrokenState && BrokenStateException?.StackTrace == exception?.StackTrace)
return;

BrokenState = state ?? throw new ArgumentNullException(nameof(state));
BrokenStateException = exception;
OnBrokenStateChanged();
Expand Down
6 changes: 4 additions & 2 deletions src/Artemis.Core/Models/Profile/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ public BaseLayerBrush? LayerBrush
public LayerAdapter Adapter { get; }

/// <inheritdoc />
public override bool ShouldBeEnabled => !Suspended && DisplayConditionMet;
public override bool ShouldBeEnabled => !Suspended && DisplayConditionMet && HasBounds;

internal override RenderElementEntity RenderElementEntity => LayerEntity;

private bool HasBounds => Bounds.Width > 0 && Bounds.Height > 0;

/// <inheritdoc />
public override List<ILayerProperty> GetAllLayerProperties()
{
Expand Down Expand Up @@ -383,7 +385,7 @@ public override void Update(double deltaTime)

if (ShouldBeEnabled)
Enable();
else if (Suspended || (Timeline.IsFinished && !_renderCopies.Any()))
else if (Suspended || !HasBounds || (Timeline.IsFinished && !_renderCopies.Any()))
Disable();

if (!Enabled || Timeline.Delta == TimeSpan.Zero)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;

namespace Artemis.Core;

Expand All @@ -14,12 +15,13 @@ public readonly struct FloatRange
/// </summary>
/// <param name="start">The start value of the range</param>
/// <param name="end">The end value of the range</param>
[JsonConstructor]
public FloatRange(float start, float end)
{
Start = start;
End = end;

_rand = new Random();
_rand = Random.Shared;
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Artemis.Core/Models/Profile/LayerProperties/IntRange.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;

namespace Artemis.Core;

Expand All @@ -14,12 +15,13 @@ public readonly struct IntRange
/// </summary>
/// <param name="start">The start value of the range</param>
/// <param name="end">The end value of the range</param>
[JsonConstructor]
public IntRange(int start, int end)
{
Start = start;
End = end;

_rand = new Random();
_rand = Random.Shared;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void LoadIcon()
// Preselect the icon or fall back to a random one
SelectedMaterialIcon = !IsNew && Enum.TryParse(_profileConfiguration.Icon.IconName, out MaterialIconKind enumValue)
? icons.FirstOrDefault(m => m.Icon == enumValue)
: icons.ElementAt(new Random().Next(0, icons.Count - 1));
: icons.ElementAt(Random.Shared.Next(0, icons.Count - 1));
}

private async Task SaveIcon()
Expand Down

0 comments on commit 28640f9

Please sign in to comment.