181 lines
9.7 KiB
C#
181 lines
9.7 KiB
C#
using bts.spark;
|
|
using mbts.ui.controls;
|
|
using mbts.ui.utilities;
|
|
using mbts.ui.utility;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
namespace mbts.ui.spark.algo {
|
|
/// <summary>
|
|
/// Interaction logic for QuantityMeasure.xaml
|
|
/// </summary>
|
|
class QuantityMeasureControl : Control {
|
|
// defaults
|
|
static readonly QuantityMeasureImmutable _default_quantity_measure = new QuantityMeasureImmutable(new QuantityMeasure { Absolute = 0 });
|
|
static uint _default_absolute => _default_quantity_measure.QuantityCase == bts.spark.QuantityMeasure.QuantityOneofCase.Ratio ? _default_quantity_measure.Absolute : default;
|
|
static double _default_ratio => _default_quantity_measure.QuantityCase == bts.spark.QuantityMeasure.QuantityOneofCase.Ratio ? _default_quantity_measure.Ratio : default;
|
|
|
|
readonly PropertyUpdateGate _update_gate = new PropertyUpdateGate();
|
|
// TickUpDown
|
|
LazyPropagator<(TickUpDownUInt absolute, TickUpDownDouble ratio), QuantityMeasureControl> _explicit_props = new LazyPropagator<(TickUpDownUInt absolute, TickUpDownDouble ratio), QuantityMeasureControl>((t, v) => {
|
|
t._enable_arrow_key_tick.target = v;
|
|
t._arrow_key_tick_modifiers.target = v;
|
|
t._arrow_key_tick_alt_modifiers.target = v;
|
|
});
|
|
LazyPropagator<bool, (TickUpDownUInt absolute, TickUpDownDouble ratio)> _enable_arrow_key_tick = new LazyPropagator<bool, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((t, v) => {
|
|
if (t.absolute != null) t.absolute.EnableArrowKeyTick = v;
|
|
if (t.ratio != null) t.ratio.EnableArrowKeyTick = v;
|
|
}, true);
|
|
LazyPropagator<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>
|
|
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((t, v) => {
|
|
if (t.absolute != null) t.absolute.ArrowKeyTickModifiers = v;
|
|
if (t.ratio != null) t.ratio.ArrowKeyTickModifiers = v;
|
|
}, ModifierKeys.None),
|
|
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((t, v) => {
|
|
t.absolute.ArrowKeyTickAltModifiers = v;
|
|
t.ratio.ArrowKeyTickAltModifiers = v;
|
|
}, ModifierKeys.Alt);
|
|
|
|
static QuantityMeasureControl() =>
|
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(QuantityMeasureControl), new FrameworkPropertyMetadata(typeof(QuantityMeasureControl)));
|
|
|
|
public QuantityMeasureControl() =>
|
|
_explicit_props.target = this;
|
|
|
|
public override void OnApplyTemplate() {
|
|
string hcn = HeaderColName;
|
|
if (hcn != null) {
|
|
var root = GetTemplateChild("Root") as Grid;
|
|
if (root != null) root.ColumnDefinitions[0].SharedSizeGroup = hcn;
|
|
}
|
|
var absolute = GetTemplateChild("PART_Absolute_TickUpDown") as TickUpDownUInt;
|
|
var ratio = GetTemplateChild("PART_Ratio_TickUpDown") as TickUpDownDouble;
|
|
_explicit_props.value = (absolute, ratio);
|
|
// this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus.
|
|
if (absolute != null) absolute.InputBindings.AddRange(InputBindings.OfType<KeyBinding>());
|
|
if (ratio != null) ratio.InputBindings.AddRange(InputBindings.OfType<KeyBinding>());
|
|
}
|
|
|
|
public static readonly DependencyProperty HeaderColNameProperty = DependencyProperty.Register("HeaderColName", typeof(string), typeof(QuantityMeasureControl));
|
|
public string HeaderColName { get => GetValue(HeaderColNameProperty) as string; set => SetValue(HeaderColNameProperty, value); }
|
|
|
|
#region QuantityCase
|
|
public static readonly DependencyProperty QuantityCaseProperty =
|
|
WPFHelper.create_dp<QuantityMeasure.QuantityOneofCase, QuantityMeasureControl>(
|
|
nameof(QuantityCase),
|
|
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, oldval, newval) => {
|
|
o._update_gate.try_update(() => {
|
|
var mutable = o.QuantityMeasure?.CloneToMutable() ?? new bts.spark.QuantityMeasure();
|
|
switch (oldval) {
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o._cached_absolute = mutable.Absolute; break;
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o._cached_ratio = mutable.Ratio; break;
|
|
}
|
|
switch (newval) {
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute:
|
|
mutable.Absolute = o._cached_absolute;
|
|
o.SetCurrentValue(AbsoluteProperty, o._cached_absolute);
|
|
break;
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio:
|
|
mutable.Ratio = o._cached_ratio;
|
|
o.SetCurrentValue(RatioProperty, o._cached_ratio);
|
|
break;
|
|
}
|
|
o.SetCurrentValue(QuantityMeasureProperty, new QuantityMeasureImmutable(mutable));
|
|
});
|
|
},
|
|
_default_quantity_measure.QuantityCase);
|
|
|
|
public QuantityMeasure.QuantityOneofCase QuantityCase {
|
|
get => (QuantityMeasure.QuantityOneofCase)GetValue(QuantityCaseProperty);
|
|
set => SetValue(QuantityCaseProperty, value);
|
|
}
|
|
#endregion
|
|
|
|
#region Absolute
|
|
uint _cached_absolute = _default_absolute;
|
|
public static readonly DependencyProperty AbsoluteProperty =
|
|
WPFHelper.create_dp<uint, QuantityMeasureControl>(
|
|
nameof(Absolute),
|
|
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, v) => {
|
|
o._update_gate.try_update(() => {
|
|
var mutable = o.QuantityMeasure?.CloneToMutable() ?? new QuantityMeasure();
|
|
mutable.Absolute = v;
|
|
o.SetCurrentValue(QuantityMeasureProperty, new QuantityMeasureImmutable(mutable));
|
|
o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.Absolute);
|
|
o.SetCurrentValue(RatioProperty, o._cached_ratio);
|
|
});
|
|
},
|
|
_default_absolute);
|
|
|
|
public uint Absolute { get => (uint)GetValue(AbsoluteProperty); set => SetValue(AbsoluteProperty, value); }
|
|
#endregion
|
|
|
|
#region Ratio
|
|
double _cached_ratio = _default_ratio;
|
|
public static readonly DependencyProperty RatioProperty =
|
|
WPFHelper.create_dp<double, QuantityMeasureControl>(
|
|
nameof(Ratio),
|
|
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, v) => {
|
|
o._update_gate.try_update(() => {
|
|
var mutable = o.QuantityMeasure?.CloneToMutable() ?? new QuantityMeasure();
|
|
mutable.Ratio = v;
|
|
o.SetCurrentValue(QuantityMeasureProperty, new QuantityMeasureImmutable(mutable));
|
|
o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.Ratio);
|
|
o.SetCurrentValue(AbsoluteProperty, o._cached_absolute);
|
|
});
|
|
},
|
|
_default_ratio);
|
|
|
|
public double Ratio { get => (double)GetValue(RatioProperty); set => SetValue(RatioProperty, value); }
|
|
#endregion
|
|
|
|
#region QuantityMeasure
|
|
public static readonly DependencyProperty QuantityMeasureProperty = WPFHelper.create_dp<QuantityMeasureImmutable?, QuantityMeasureControl>(
|
|
nameof(QuantityMeasure),
|
|
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, oldval, newval) => o._update_gate.try_update(() => {
|
|
if (oldval.HasValue) {
|
|
switch (oldval.Value.QuantityCase) {
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o._cached_absolute = oldval.Value.Absolute; break;
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o._cached_ratio = oldval.Value.Ratio; break;
|
|
}
|
|
}
|
|
if (newval.HasValue) {
|
|
switch (newval.Value.QuantityCase) {
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o.SetCurrentValue(AbsoluteProperty, newval.Value.Absolute); break;
|
|
case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o.SetCurrentValue(RatioProperty, newval.Value.Ratio); break;
|
|
}
|
|
o.SetCurrentValue(QuantityCaseProperty, newval.Value.QuantityCase);
|
|
} else {
|
|
o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.None);
|
|
}
|
|
}),
|
|
_default_quantity_measure);
|
|
|
|
public QuantityMeasureImmutable? QuantityMeasure {
|
|
get => GetValue(QuantityMeasureProperty) as QuantityMeasureImmutable?;
|
|
set => SetValue(QuantityMeasureProperty, value);
|
|
}
|
|
#endregion
|
|
|
|
// explicit props
|
|
public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; }
|
|
public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; }
|
|
public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; }
|
|
|
|
static class VisualStateNames {
|
|
public static class QuantityCase {
|
|
public const string GroupName = nameof(QuantityCase);
|
|
|
|
public static readonly string
|
|
Absolute = $"{GroupName}{nameof(Absolute)}",
|
|
Ratio = $"{GroupName}{nameof(Ratio)}";
|
|
}
|
|
}
|
|
}
|
|
} |