DoubleDialogViewModel.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Avalonia.Layout;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using CommunityToolkit.Mvvm.Input;
  4. namespace InABox.Avalonia.Dialogs;
  5. public partial class DoubleDialogViewModel : BasePopupViewModel<double?>, IAlignedPopup
  6. {
  7. public HorizontalAlignment HorizontalAlignment => HorizontalAlignment.Stretch;
  8. public VerticalAlignment VerticalAlignment => VerticalAlignment.Center;
  9. [ObservableProperty]
  10. private string _title = "";
  11. [ObservableProperty]
  12. private string? _text;
  13. [ObservableProperty]
  14. private double? _value;
  15. public override Task Activate()
  16. {
  17. Text = Value is null ? "" : $"{Value}";
  18. return base.Activate();
  19. }
  20. [RelayCommand]
  21. private void Cancel()
  22. {
  23. Close(null);
  24. }
  25. [RelayCommand]
  26. private void Ok()
  27. {
  28. Value = Double.TryParse(Text, out var _v) ? _v : 0.0;
  29. Close(Value);
  30. }
  31. [RelayCommand]
  32. private void KeyPressed(object value)
  33. {
  34. Text = $"{Text}{value}";
  35. }
  36. [RelayCommand]
  37. private void NegativePressed(object value)
  38. {
  39. Text = Text?.StartsWith("-") == true
  40. ? Text.Substring(1)
  41. : $"-{Text}";
  42. }
  43. [RelayCommand]
  44. private void DecimalPressed(object value)
  45. {
  46. if (string.IsNullOrWhiteSpace(Text))
  47. Text = "0.";
  48. else if (!Text.Contains('.'))
  49. Text = $"{Text}.";
  50. }
  51. [RelayCommand]
  52. private void BackPressed(object value)
  53. {
  54. Text = string.IsNullOrWhiteSpace(Text)
  55. ? ""
  56. : Text.Remove(Text.Length - 1);
  57. }
  58. }