Skip to content

Commit

Permalink
完成了TaskList的部分改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 5, 2024
1 parent a373699 commit c5cdb83
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 89 deletions.
3 changes: 3 additions & 0 deletions SimpleFFmpegGUI.WPF/Model/TasksAndStatuses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ private async void Queue_TaskManagersChanged(object sender, System.Collections.S
{
var manager = e.NewItems[0] as FFmpegManager;
var unstartStatus = new StatusDto(manager.Task); //先放入一个StatusDto进行占位,因为此时Status还未生成

throw new NotImplementedException();
//Null reference
var task = Tasks.FirstOrDefault(p => p.Id == manager.Task.Id);//找到对应的UITaskInfo
Debug.Assert(task != null);
await task.UpdateSelfAsync(); //用TaskInfo实体更新UITaskInfo
Expand Down
6 changes: 3 additions & 3 deletions SimpleFFmpegGUI.WPF/Panels/TaskList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,18 @@
</Style>
</ui:SimpleStackPanel.Resources>
<Button
Click="StartButton_Click"
Command="{Binding StartStandaloneCommand}"
Content="独立执行"
IsEnabled="{Binding CanStart}"
Visibility="{Binding ShowAllTasks, Converter={StaticResource Bool2VisibilityConverter}, ConverterParameter=i}" />
<Button
Click="CancelButton_Click"
Command="{Binding CancelCommand}"
Content="取消"
IsEnabled="{Binding CanCancel}"
ToolTip="取消执行任务,正在执行的任务会被停止"
Visibility="{Binding ShowAllTasks, Converter={StaticResource Bool2VisibilityConverter}, ConverterParameter=i}" />
<Button
Click="ResetButton_Click"
Command="{Binding ResetCommand}"
Content="重置"
IsEnabled="{Binding CanReset}"
ToolTip="将任务设置为排队状态"
Expand Down
93 changes: 13 additions & 80 deletions SimpleFFmpegGUI.WPF/Panels/TaskList.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FzLib;
using FzLib.WPF;
using FzLib.WPF;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using ModernWpf.FzExtension.CommonDialog;
Expand All @@ -8,10 +7,10 @@
using SimpleFFmpegGUI.WPF;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.Pages;
using SimpleFFmpegGUI.WPF.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand All @@ -34,16 +33,22 @@ public partial class TaskList : UserControl
public TaskList()
{
InitializeComponent();
(Content as FrameworkElement).DataContext = ViewModel;
ViewModel = this.SetDataContext<TaskListViewModel>();
}


public static readonly DependencyProperty ShowAllTasksProperty = DependencyProperty.Register(
nameof(ShowAllTasks), typeof(bool),
typeof(TaskList)
);

public bool ShowAllTasks
{
get => ViewModel.ShowAllTasks;
set => ViewModel.ShowAllTasks = value;
get => (bool)GetValue(ShowAllTasksProperty);
set => SetValue(ShowAllTasksProperty, value);
}

public TaskListViewModel ViewModel { get; } = App.ServiceProvider.GetService<TaskListViewModel>();
public TaskListViewModel ViewModel { get; }

private void ArgumentsButton_Click(object sender, RoutedEventArgs e)
{
Expand Down Expand Up @@ -210,42 +215,7 @@ private void OpenOutputFileOrFolder(UITaskInfo task, bool folder)
}
OpenFileOrFolder(task.RealOutput, folder);
}

private async void ResetButton_Click(object sender, RoutedEventArgs e)
{
var tasks = App.ServiceProvider.GetRequiredService<TasksAndStatuses>().SelectedTasks;
var tm = App.ServiceProvider.GetRequiredService<TaskManager>();
Debug.Assert(tasks.Count > 0);
foreach (var task in tasks)
{
await tm.ResetTaskAsync(task.Id);
await task.UpdateSelfAsync();
App.ServiceProvider.GetService<TasksAndStatuses>().NotifyTaskReseted(task);
}

ViewModel.NotifyCanExecute();
}

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
try
{
var tasks = App.ServiceProvider.GetService<TasksAndStatuses>().SelectedTasks;
Debug.Assert(tasks.Count > 0);
foreach (var task in tasks)
{
ViewModel.Queue.StartStandalone(task.Id);
await task.UpdateSelfAsync();
}
}
catch (Exception ex)
{
this.CreateMessage().QueueError("启动失败", ex);
}

ViewModel.NotifyCanExecute();
}


private void UpdateDetailHeight()
{
bdDetail.Height = App.ServiceProvider.GetService<MainWindow>().IsUiCompressMode && !ShowAllTasks ? 108 : double.NaN;
Expand All @@ -263,41 +233,4 @@ private void Tasks_SelectionChanged(object sender, SelectionChangedEventArgs e)
ViewModel.NotifyCanExecute();
}
}

public class TaskListViewModel : INotifyPropertyChanged
{
private bool showAllTasks;

public TaskListViewModel(QueueManager queue)
{
Queue = queue;
}

public event PropertyChangedEventHandler PropertyChanged;

public bool CanCancel => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Queue or TaskStatus.Processing);

public bool CanReset => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Done or TaskStatus.Cancel or TaskStatus.Error);

public bool CanStart => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Queue);

public void NotifyCanExecute()
{
this.Notify(nameof(CanCancel), nameof(CanReset), nameof(CanStart));
}

public QueueManager Queue { get; }

public bool ShowAllTasks
{
get => showAllTasks;
set => this.SetValueAndNotify(ref showAllTasks, value, nameof(ShowAllTasks), nameof(Tasks));
}

public TaskCollectionBase Tasks => ShowAllTasks ?
App.ServiceProvider.GetService<AllTasks>()
: App.ServiceProvider.GetService<TasksAndStatuses>();

public SelectionMode SelectionMode => ShowAllTasks ? SelectionMode.Single : SelectionMode.Extended;
}
}
8 changes: 4 additions & 4 deletions SimpleFFmpegGUI.WPF/ViewModels/FileIOPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private void Inputs_CollectionChanged(object sender, System.Collections.Speciali
private async Task BrowseFile(InputArgumentsDetail input)
{
var dialog = new Microsoft.Win32.OpenFileDialog().AddAllFilesFilter();
WeakReferenceMessenger.Default.Send(new FileDialogMessage(dialog));
SendMessage(new FileDialogMessage(dialog));
string path = dialog.FileName;
if (path != null)
{
Expand Down Expand Up @@ -237,7 +237,7 @@ private void RemoveFile(InputArgumentsDetail input)
private void BrowseOutputFile()
{
var dialog = new OpenFolderDialog();
WeakReferenceMessenger.Default.Send(new FileDialogMessage(dialog));
SendMessage(new FileDialogMessage(dialog));
string path = dialog.FolderName;
if (path != null)
{
Expand All @@ -261,7 +261,7 @@ private async Task ClipAsync(InputArgumentsDetail input)
QueueErrorMessage($"找不到文件{input.FilePath}");
return;
}
WeakReferenceMessenger.Default.Send(new WindowEnableMessage(false));
SendMessage(new WindowEnableMessage(false));
(TimeSpan From, TimeSpan To)? result = null;

var handle = WeakReferenceMessenger.Default.Send(new WindowHandleMessage()).Handle;
Expand Down Expand Up @@ -306,7 +306,7 @@ private async Task ClipAsync(InputArgumentsDetail input)
}
finally
{
WeakReferenceMessenger.Default.Send(new WindowEnableMessage(true));
SendMessage(new WindowEnableMessage(true));
}
}
}
Expand Down
117 changes: 117 additions & 0 deletions SimpleFFmpegGUI.WPF/ViewModels/TaskListViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using FzLib;
using Microsoft.Extensions.DependencyInjection;
using ModernWpf.FzExtension.CommonDialog;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.WPF.Messages;
using SimpleFFmpegGUI.WPF.Model;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TaskStatus = SimpleFFmpegGUI.Model.TaskStatus;

namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class TaskListViewModel(QueueManager queue, TasksAndStatuses tasksAndStatuses, TaskManager taskManager) : ViewModelBase
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Tasks))]
private bool showAllTasks;

private readonly QueueManager queue = queue;
private readonly TasksAndStatuses tasksAndStatuses = tasksAndStatuses;
private readonly TaskManager taskManager = taskManager;

public bool CanCancel => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Queue or TaskStatus.Processing);

public bool CanReset => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Done or TaskStatus.Cancel or TaskStatus.Error);

public bool CanStart => Tasks.SelectedTasks.All(p => p.Status is TaskStatus.Queue);

public void NotifyCanExecute()
{
this.Notify(nameof(CanCancel), nameof(CanReset), nameof(CanStart));
}

public TaskCollectionBase Tasks => ShowAllTasks ?
App.ServiceProvider.GetService<AllTasks>()
: App.ServiceProvider.GetService<TasksAndStatuses>();

public SelectionMode SelectionMode => ShowAllTasks ? SelectionMode.Single : SelectionMode.Extended;

[RelayCommand]
private async Task ResetAsync()
{
var tasks = tasksAndStatuses.SelectedTasks;
Debug.Assert(tasks.Count > 0);
foreach (var task in tasks)
{
await taskManager.ResetTaskAsync(task.Id);
await task.UpdateSelfAsync();
App.ServiceProvider.GetService<TasksAndStatuses>().NotifyTaskReseted(task);
}

NotifyCanExecute();
}

[RelayCommand]
private async Task StartStandaloneAsync()
{
try
{
var tasks = tasksAndStatuses.SelectedTasks;
Debug.Assert(tasks.Count > 0);
foreach (var task in tasks)
{
queue.StartStandalone(task.Id);
await task.UpdateSelfAsync();
}
}
catch (Exception ex)
{
QueueErrorMessage("启动失败", ex);
}

NotifyCanExecute();
}

[RelayCommand]
private async Task CancelAsync()
{
var tasks = tasksAndStatuses.SelectedTasks;
Debug.Assert(tasks.Count > 0);
if (tasks.Any(p => p.Status == TaskStatus.Processing))
{
if (!await CommonDialog.ShowYesNoDialogAsync("取消任务", "任务正在执行,是否取消?"))
{
return;
}
}
try
{
SendMessage(new WindowEnableMessage(false));
foreach (var task in tasks)
{
await taskManager.CancelTaskAsync(task.Id);
await task.UpdateSelfAsync();
}
}
catch (Exception ex)
{
QueueErrorMessage("取消失败", ex);
}
finally
{
SendMessage(new WindowEnableMessage(true));
}

NotifyCanExecute();
}
}
}
8 changes: 6 additions & 2 deletions SimpleFFmpegGUI.WPF/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class ViewModelBase : ObservableObject
{
protected TMessage SendMessage<TMessage>(TMessage message) where TMessage : class
{
return WeakReferenceMessenger.Default.Send(message);
}
protected void QueueSuccessMessage(string message)
{
WeakReferenceMessenger.Default.Send(new QueueMessagesMessage('S', message));
SendMessage(new QueueMessagesMessage('S', message));
}
protected void QueueErrorMessage(string message, Exception ex = null)
{
WeakReferenceMessenger.Default.Send(new QueueMessagesMessage('E', message, ex));
SendMessage(new QueueMessagesMessage('E', message, ex));
}

protected ContentControl View { get; set; }
Expand Down

0 comments on commit c5cdb83

Please sign in to comment.