- add: pause button
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,21 @@ public partial class MainViewModel : ViewModelBase
|
||||
|
||||
[ObservableProperty] private bool _isSorting;
|
||||
|
||||
[ObservableProperty] private bool _isPaused;
|
||||
|
||||
[ObservableProperty] private ObservableCollection<SortItem> _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;
|
||||
|
||||
@@ -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">
|
||||
<Button Command="{Binding GenerateArrayCommand}" Content="Сгенерировать" Width="120"/>
|
||||
<Button Command="{Binding StartSortCommand}" Content="Старт" Width="120"
|
||||
<Button Command="{Binding GenerateArrayCommand}" HorizontalContentAlignment="Center"
|
||||
Content="Сгенерировать" Width="140"
|
||||
IsVisible="{Binding !IsSorting}"/>
|
||||
<Button Command="{Binding StopSortCommand}" Content="Стоп" Width="120"
|
||||
<Button Command="{Binding StartSortCommand}" HorizontalContentAlignment="Center"
|
||||
Content="Старт" Width="140"
|
||||
IsVisible="{Binding !IsSorting}"/>
|
||||
<Button Command="{Binding StopSortCommand}" HorizontalContentAlignment="Center"
|
||||
Content="Стоп" Width="140"
|
||||
IsVisible="{Binding IsSorting}"/>
|
||||
<Button Command="{Binding TogglePauseCommand}" HorizontalContentAlignment="Center"
|
||||
Width="140"
|
||||
IsVisible="{Binding IsSorting}">
|
||||
<Button.Content>
|
||||
<TextBlock Text="{Binding IsPaused, Converter={x:Static local:PauseConverter.Instance}}"/>
|
||||
</Button.Content>
|
||||
</Button>
|
||||
|
||||
<StackPanel Spacing="5" Orientation="Horizontal">
|
||||
<TextBlock Text="Размер:" VerticalAlignment="Center"/>
|
||||
|
||||
Reference in New Issue
Block a user