using Avalonia.Layout; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace InABox.Avalonia.Dialogs; public partial class DoubleDialogViewModel : BasePopupViewModel, IAlignedPopup { public HorizontalAlignment HorizontalAlignment => HorizontalAlignment.Stretch; public VerticalAlignment VerticalAlignment => VerticalAlignment.Center; [ObservableProperty] private string _title = ""; [ObservableProperty] private string? _text; [ObservableProperty] private double? _value; public override Task Activate() { Text = Value is null ? "" : $"{Value}"; return base.Activate(); } [RelayCommand] private void Cancel() { Close(null); } [RelayCommand] private void Ok() { Value = Double.TryParse(Text, out var _v) ? _v : 0.0; Close(Value); } [RelayCommand] private void KeyPressed(object value) { Text = $"{Text}{value}"; } [RelayCommand] private void NegativePressed(object value) { Text = Text?.StartsWith("-") == true ? Text.Substring(1) : $"-{Text}"; } [RelayCommand] private void DecimalPressed(object value) { if (string.IsNullOrWhiteSpace(Text)) Text = "0."; else if (!Text.Contains('.')) Text = $"{Text}."; } [RelayCommand] private void BackPressed(object value) { Text = string.IsNullOrWhiteSpace(Text) ? "" : Text.Remove(Text.Length - 1); } }