Files
spark/src/client/spark_api/data/ticks/TickDisplaySettings.cs
T
John Colvin 0d26401961 fix: Preserve tick_factors in TickDisplaySettings "with" ext
Not sure this was a bug in practice, but it was definitely wrong.
2025-01-21 01:24:23 -05:00

107 lines
3.6 KiB
C#

#nullable enable
namespace mbts.user.settings {
using bts.utils;
using ProtoBuf;
using Spark.Core.Protobuf;
using System;
[ProtoContract]
public class TickDisplaySettings : IProtobuf<TickDisplaySettings> {
public static TickDisplaySettings Default => new() {
pad_with_leading_zeros = true,
display_multiplier = double.NaN,
show_commas = false,
nan_text = "-",
show_decimal_part = true,
show_zero_integer = true
};
[ProtoMember(1)]
public bool pad_with_leading_zeros { get; set; }
[ProtoMember(2)]
public double display_multiplier { get; set; }
[ProtoMember(3)]
public bool show_commas { get; set; }
[ProtoMember(4)]
public string? nan_text { get; set; }
[ProtoMember(5)]
public bool show_decimal_part { get; set; }
[ProtoMember(6)]
public bool show_zero_integer { get; set; }
[ProtoMember(7)]
public TickFactors? tick_factors { get; set; }
public TickDisplaySettings Clone() =>
new() {
pad_with_leading_zeros = pad_with_leading_zeros,
display_multiplier = display_multiplier,
show_commas = show_commas,
nan_text = nan_text,
show_decimal_part = show_decimal_part,
show_zero_integer = show_zero_integer
};
public bool Equals(TickDisplaySettings? other) =>
other is not null &&
pad_with_leading_zeros == other.pad_with_leading_zeros &&
display_multiplier.Equals(other.display_multiplier) &&
show_commas == other.show_commas &&
nan_text == other.nan_text &&
show_decimal_part == other.show_decimal_part &&
show_zero_integer == other.show_zero_integer;
public override bool Equals(object? obj) =>
Equals(obj as TickDisplaySettings);
public override int GetHashCode() =>
// All props are mutable, and thus can't be incorporated into the hash code
GetType().GetHashCode();
object ICloneable.Clone() =>
Clone();
}
public static class TickDisplaySettingsExtensions {
public static TickDisplaySettings with(
this TickDisplaySettings settings,
bool? pad_with_leading_zeros = null,
double? display_multiplier = null,
bool? show_commas = null,
string? nan_text = null,
bool? show_decimal_part = null,
bool? show_zero_integer = null,
TickFactors? tick_factors = null) =>
new() {
pad_with_leading_zeros = pad_with_leading_zeros ?? settings.pad_with_leading_zeros,
display_multiplier = display_multiplier ?? settings.display_multiplier,
show_commas = show_commas ?? settings.show_commas,
nan_text = nan_text ?? settings.nan_text,
show_decimal_part = show_decimal_part ?? settings.show_decimal_part,
show_zero_integer = show_zero_integer ?? settings.show_zero_integer,
tick_factors = tick_factors ?? settings.tick_factors,
};
public static double to_display_value(this TickDisplaySettings settings, double value) =>
settings.display_multiplier.is_nan() ? value : value * settings.display_multiplier;
public static double from_display_value(
this TickDisplaySettings settings, double display_value) =>
settings.display_multiplier switch {
// ignore nan
double.NaN => display_value,
// don't divide by 0
0 => double.NaN,
_ => display_value / settings.display_multiplier
};
}
}