Files
spark/src/client/spark/view/Spark/Shell/CalculatorLogic.cs
T
John Colvin 88302c9470 build: Upgrade TargetFramework to net7.0
We've been using several net7.0 preview features for a while now. Now that a
stable net7.0 release is available, it is time upgrade officially. This also
aids the effort to build csharp on Linux, where these preview features didn't
work out of the box. Lastly, trying to make these preview features work would
be a waste of time, as namespaces/interfaces in the official release have
changed in a way that isn't backwards compatible w/ the preview features.
2023-03-20 13:23:49 -04:00

135 lines
4.9 KiB
C#

namespace mbts.spark {
using bts.api;
using bts.api.domain;
using bts.utils;
using mbts.ui;
using mbts.ui.shell;
using Spark.Core.Disposables;
using System;
using System.Numerics;
using System.Reactive.Linq;
using System.Windows.Input;
public class CalculatorLogic<T> : DisposableViewModelBase where T : struct, IFloatingPoint<T> {
readonly OptionalDisplayQuantityMultiplier<T> _display_translator = new();
TickDisplaySettingsImmutable _display_settings = Security.Defaults.quantity_display_settingsi;
bool _value_setter_called;
IDisposable _prop_changes_echo;
T[] _buttons;
public CalculatorLogic(T? initial_size = null) {
if (initial_size.HasValue) {
bypass_value_setter(initial_size.Value, true);
}
_echo_props_changed();
}
public CalculatorLogic(T? initial_size, IObservable<T[]> buttons_source) : this(initial_size) =>
_dispose = buttons_source.Subscribe(newval => buttons = newval);
public T display_divisor {
get => _display_translator.conversion_data.value;
set => _display_translator.conversion_data = new(
value, QuantityConversionOpType.DivideActual);
}
public T? value {
get => _display_translator.actual_value;
set => _display_translator.actual_value = value;
}
public T? display_value {
get => _display_translator.display_value;
set {
_display_translator.display_value = value;
value_setter_called = true;
}
}
public TickDisplaySettingsImmutable display_settings {
get => _display_settings;
set {
using var _ = detect_property_changes(nameof(display_settings), nameof(unit_quantity));
_display_settings = value;
}
}
public double unit_quantity =>
display_settings.TickFactors.to_tick_factors().tick_unit_frac();
public bool value_setter_called {
get => _value_setter_called;
set => Set(ref _value_setter_called, value);
}
public T[] buttons { get => _buttons; set => Set(ref _buttons, value); }
public ICommand click => new DelegateCommand<T>(handle_calculator_click);
public ICommand set_display_value_command => new DelegateCommand<T>(v => _display_translator.display_value = v);
public ICommand set_actual_value_command => new DelegateCommand<T>(v => _display_translator.actual_value = v);
public ICommand clear => new DelegateCommand(() => _display_translator.actual_value = null);
public IObservable<T?> observe_actual_value() =>
_display_translator.subscribe_actual_value();
// Don't use a default value for 'is_actual_value.'
// Quietly multiplying (or not multiplying) the size input is dangerous -
// force the caller to be specific.
public void bypass_value_setter(T value, bool is_actual_value) {
Disposable.dispose(ref _prop_changes_echo);
if (is_actual_value) {
this.value = value;
} else {
display_value = value;
}
if (_prop_changes_echo != null)
_echo_props_changed();
}
public bool validate() {
if (!value.HasValue || value.Value == T.Zero) {
flash_error(nameof(value)).ignore();
return false;
} else return true;
}
public void flash_error() =>
flash_error(nameof(value)).ignore();
protected override void Dispose(bool disposing) {
Disposable.dispose(ref _prop_changes_echo);
base.Dispose(disposing);
}
void _echo_props_changed() {
// dispose the echo to avoid raising prop changed events more than once
_prop_changes_echo?.Dispose();
_prop_changes_echo = _display_translator.connect_owner(this, nameof(display_value), nameof(value));
}
void handle_calculator_click(T value) {
if (Keyboard.IsKeyDown(Key.LeftShift) ||
Keyboard.IsKeyDown(Key.RightShift) ||
value == T.Zero
) {
_display_translator.actual_value = value;
} else if (KeyboardExtensions.IsCtrlDown()) {
if (!_display_translator.actual_value.HasValue) {
_display_translator.actual_value = T.Zero;
} else if (value > _display_translator.actual_value) {
_display_translator.actual_value = T.Zero;
} else {
_display_translator.actual_value -= value;
}
} else {
var actual_val = _display_translator.actual_value;
if (!actual_val.HasValue || actual_val.Value.is_nan()) {
_display_translator.actual_value = value;
} else {
_display_translator.actual_value += value;
}
}
}
}
}