From f8c190e399d16ad4beb549016f67b224bbd4b2d0 Mon Sep 17 00:00:00 2001 From: CinkaFox Date: Fri, 10 Apr 2026 12:15:39 +0300 Subject: [PATCH] - add: pause button --- LaborWork/Converters.cs | 20 ++++++++++++---- LaborWork/ViewModels/MainViewModel.cs | 34 ++++++++++++++++++++++++++- LaborWork/Views/MainView.axaml | 18 +++++++++++--- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/LaborWork/Converters.cs b/LaborWork/Converters.cs index 90512ef..7c7a6f0 100644 --- a/LaborWork/Converters.cs +++ b/LaborWork/Converters.cs @@ -5,6 +5,21 @@ using Avalonia.Data.Converters; namespace LaborWork; +public class PauseConverter : IValueConverter +{ + public static readonly PauseConverter Instance = new(); + + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) + { + return value is true ? "Продолжить" : "Пауза"; + } + + public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } +} + public class HeightConverter : IMultiValueConverter { public static readonly HeightConverter Instance = new(); @@ -13,13 +28,8 @@ public class HeightConverter : IMultiValueConverter { if (values.Count >= 2 && values[0] is int value && values[1] is double windowHeight) { - Console.WriteLine("YAY"); - // value - это число от 10 до 100 - // windowHeight - высота окна - // Делаем отступ снизу 50 пикселей return (value / 100.0) * (windowHeight - 50); } - Console.WriteLine($"NOO " + values[0].GetType().Name); return 0; } } \ No newline at end of file diff --git a/LaborWork/ViewModels/MainViewModel.cs b/LaborWork/ViewModels/MainViewModel.cs index 74d91ea..a510679 100644 --- a/LaborWork/ViewModels/MainViewModel.cs +++ b/LaborWork/ViewModels/MainViewModel.cs @@ -13,9 +13,21 @@ public partial class MainViewModel : ViewModelBase [ObservableProperty] private bool _isSorting; + [ObservableProperty] private bool _isPaused; + [ObservableProperty] private ObservableCollection _items = new(); - [ObservableProperty] private int _speed = 50; + [ObservableProperty] private int _speed = 50; + + partial void OnArraySizeChanged(int value) + { + GenerateArray(); + } + + public MainViewModel() + { + GenerateArray(); + } [RelayCommand] private void GenerateArray() @@ -34,6 +46,7 @@ public partial class MainViewModel : ViewModelBase if (IsSorting || Items.Count == 0) return; IsSorting = true; + IsPaused = false; foreach (var item in Items) item.State = ItemState.Default; @@ -47,8 +60,23 @@ public partial class MainViewModel : ViewModelBase [RelayCommand] private void StopSort() { + IsPaused = false; IsSorting = false; } + + [RelayCommand] + private void TogglePause() + { + IsPaused = !IsPaused; + } + + private async Task WaitIfPaused() + { + while (IsPaused && IsSorting) + { + await Task.Delay(100); + } + } private async Task QuickSort(int left, int right) { @@ -59,6 +87,7 @@ public partial class MainViewModel : ViewModelBase Items[pivotIndex].State = ItemState.Pivot; await Task.Delay(Speed); + await WaitIfPaused(); var i = left; var j = right; @@ -70,6 +99,7 @@ public partial class MainViewModel : ViewModelBase if(!IsSorting) return; Items[i].State = ItemState.Compare; await Task.Delay(Speed / 2); + await WaitIfPaused(); Items[i].State = ItemState.Default; i++; } @@ -79,6 +109,7 @@ public partial class MainViewModel : ViewModelBase if(!IsSorting) return; Items[j].State = ItemState.Compare; await Task.Delay(Speed / 2); + await WaitIfPaused(); Items[j].State = ItemState.Default; j--; } @@ -92,6 +123,7 @@ public partial class MainViewModel : ViewModelBase (Items[i].Value, Items[j].Value) = (Items[j].Value, Items[i].Value); await Task.Delay(Speed); + await WaitIfPaused(); Items[i].State = ItemState.Default; Items[j].State = ItemState.Default; diff --git a/LaborWork/Views/MainView.axaml b/LaborWork/Views/MainView.axaml index 6c98782..01d4d56 100644 --- a/LaborWork/Views/MainView.axaml +++ b/LaborWork/Views/MainView.axaml @@ -5,6 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewModels="clr-namespace:LaborWork.ViewModels" xmlns:panels="clr-namespace:Avalonia.Labs.Panels;assembly=Avalonia.Labs.Panels" + xmlns:local="using:LaborWork" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600" x:Class="LaborWork.Views.MainView" x:DataType="viewModels:MainViewModel"> @@ -14,11 +15,22 @@ Direction="Row" AlignItems="Center" Wrap="Wrap" Margin="0 0 0 20"> -