112 lines
3.5 KiB
C#
112 lines
3.5 KiB
C#
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.ViewModels;
|
|
using SukiUI.Controls;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Views
|
|
{
|
|
public partial class MainWindow : SukiWindow
|
|
{
|
|
private readonly HashSet<string> activeMotionTags = [];
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
Closed += OnClosed;
|
|
Deactivated += OnDeactivated;
|
|
}
|
|
|
|
private async void MotionButton_PointerPressed(object? sender, PointerPressedEventArgs e)
|
|
{
|
|
if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!activeMotionTags.Add(tag))
|
|
{
|
|
return;
|
|
}
|
|
|
|
e.Pointer.Capture(button);
|
|
e.Handled = true;
|
|
await StartMotionAsync(viewModel, tag);
|
|
}
|
|
|
|
private async void MotionButton_PointerReleased(object? sender, PointerReleasedEventArgs e)
|
|
{
|
|
if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
e.Pointer.Capture(null);
|
|
e.Handled = true;
|
|
await StopMotionAsync(viewModel, tag);
|
|
}
|
|
|
|
private async void MotionButton_PointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
|
|
{
|
|
if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await StopMotionAsync(viewModel, tag);
|
|
}
|
|
|
|
private async void OnDeactivated(object? sender, EventArgs e)
|
|
{
|
|
await StopAllMotionAsync();
|
|
}
|
|
|
|
private async void OnClosed(object? sender, EventArgs e)
|
|
{
|
|
await StopAllMotionAsync();
|
|
if (DataContext is MainWindowViewModel viewModel)
|
|
{
|
|
viewModel.Dispose();
|
|
}
|
|
}
|
|
|
|
private static Task StartMotionAsync(MainWindowViewModel viewModel, string tag) =>
|
|
tag switch
|
|
{
|
|
"Lift" => viewModel.StartLiftMotionAsync(),
|
|
"Lower" => viewModel.StartLowerMotionAsync(),
|
|
"MoveLeft" => viewModel.StartMoveLeftMotionAsync(),
|
|
"MoveRight" => viewModel.StartMoveRightMotionAsync(),
|
|
_ => Task.CompletedTask
|
|
};
|
|
|
|
private async Task StopMotionAsync(MainWindowViewModel viewModel, string tag)
|
|
{
|
|
if (!activeMotionTags.Remove(tag))
|
|
{
|
|
return;
|
|
}
|
|
|
|
await (tag switch
|
|
{
|
|
"Lift" => viewModel.StopLiftMotionAsync(),
|
|
"Lower" => viewModel.StopLowerMotionAsync(),
|
|
"MoveLeft" => viewModel.StopMoveLeftMotionAsync(),
|
|
"MoveRight" => viewModel.StopMoveRightMotionAsync(),
|
|
_ => Task.CompletedTask
|
|
});
|
|
}
|
|
|
|
private async Task StopAllMotionAsync()
|
|
{
|
|
activeMotionTags.Clear();
|
|
if (DataContext is MainWindowViewModel viewModel)
|
|
{
|
|
await viewModel.StopAllMotionAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|