Skip to content

Commit

Permalink
完成对AddTaskPage的部分改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 7, 2024
1 parent e44bff4 commit 127c537
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 138 deletions.
17 changes: 13 additions & 4 deletions SimpleFFmpegGUI.WPF/Pages/AddTaskPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:n="clr-namespace:Enterwell.Clients.Wpf.Notifications.Controls;assembly=Enterwell.Clients.Wpf.Notifications"
xmlns:panels="clr-namespace:SimpleFFmpegGUI.WPF.Panels"
xmlns:r="clr-namespace:SimpleFFmpegGUI.WPF"
xmlns:system="clr-namespace:System;assembly=netstandard"
xmlns:ui="http://schemas.modernwpf.com/2019"
Loaded="Window_Loaded"
mc:Ignorable="d">
Expand Down Expand Up @@ -102,14 +103,22 @@
</ui:AppBarButton.Icon>
</ui:AppBarButton>
<ui:AppBarButton
Click="AddToQueueButton_Click"
Command="{Binding AddToQueueCommand}"
Icon="Add"
Label="创建任务" />
Label="创建任务">
<ui:AppBarButton.CommandParameter>
<system:Boolean>false</system:Boolean>
</ui:AppBarButton.CommandParameter>
</ui:AppBarButton>
<ui:AppBarButton
Click="AddToQueueButton_Click"
Command="{Binding AddToQueueCommand}"
Icon="Add"
Label="创建并启动"
Tag="queue" />
Tag="queue">
<ui:AppBarButton.CommandParameter>
<system:Boolean>true</system:Boolean>
</ui:AppBarButton.CommandParameter>
</ui:AppBarButton>
<ui:AppBarSeparator />
<ui:CommandBar.SecondaryCommands>
<ui:AppBarToggleButton
Expand Down
65 changes: 8 additions & 57 deletions SimpleFFmpegGUI.WPF/Pages/AddTaskPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Enterwell.Clients.Wpf.Notifications;
using FFMpegCore.Exceptions;
using FzLib;
using FzLib.Collection;
using FzLib.WPF;
using Mapster;
Expand All @@ -11,8 +10,8 @@
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand All @@ -32,46 +31,6 @@

namespace SimpleFFmpegGUI.WPF.Pages
{
public class AddTaskPageViewModel : INotifyPropertyChanged
{
public AddTaskPageViewModel(QueueManager queue)
{
Queue = queue;
}

public IEnumerable TaskTypes => Enum.GetValues(typeof(TaskType));
private TaskType type;

public event PropertyChangedEventHandler PropertyChanged;

public TaskType Type
{
get => type;
set
{
this.SetValueAndNotify(ref type, value, nameof(Type));
CanAddFile = value is TaskType.Code or TaskType.Concat;
}
}

public QueueManager Queue { get; }

private bool allowChangeType = true;

public bool AllowChangeType
{
get => allowChangeType;
set => this.SetValueAndNotify(ref allowChangeType, value, nameof(AllowChangeType));
}

private bool canAddFile;

public bool CanAddFile
{
get => canAddFile;
set => this.SetValueAndNotify(ref canAddFile, value, nameof(CanAddFile));
}
}

/// <summary>
/// Interaction logic for AddTaskPage.xaml
Expand All @@ -85,8 +44,12 @@ public AddTaskPage(AddTaskPageViewModel viewModel)
ViewModel = viewModel;
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
DataContext = ViewModel;
ViewModel = this.SetDataContext<AddTaskPageViewModel>();
InitializeComponent();
presetsPanel.ViewModel.CodeArgumentsViewModel = argumentsPanel.ViewModel;
ViewModel.CodeArgumentsViewModel = argumentsPanel.ViewModel;
ViewModel.PresetsViewModel= presetsPanel.ViewModel;
ViewModel.FileIOViewModel = fileIOPanel.ViewModel;
}

private bool canInitializeType = true;
Expand Down Expand Up @@ -133,24 +96,21 @@ private async void AddToQueueButton_Click(object sender, RoutedEventArgs e)
case TaskType.Code://需要将输入文件单独加入任务
foreach (var input in inputs)
{
TaskInfo task = null;
await tm.AddTaskAsync(TaskType.Code, new List<InputArguments>() { input }, fileIOPanel.GetOutput(input), args);
TaskInfo task = await tm.AddTaskAsync(TaskType.Code, new List<InputArguments>() { input }, fileIOPanel.GetOutput(input), args);
Dispatcher.Invoke(() => App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task)));
}
this.CreateMessage().QueueSuccess($"已加入{inputs.Count}个任务队列");
break;
case TaskType.Custom or TaskType.Compare://不存在文件输出
{
TaskInfo task = null;
await tm.AddTaskAsync(ViewModel.Type, inputs, null, args);
TaskInfo task = await tm.AddTaskAsync(ViewModel.Type, inputs, null, args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
this.CreateMessage().QueueSuccess("已加入队列");
}
break;
default:
{
TaskInfo task = null;
await tm.AddTaskAsync(ViewModel.Type, inputs, fileIOPanel.GetOutput(inputs[0]), args);
TaskInfo task = await tm.AddTaskAsync(ViewModel.Type, inputs, fileIOPanel.GetOutput(inputs[0]), args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
this.CreateMessage().QueueSuccess("已加入队列");
}
Expand Down Expand Up @@ -303,15 +263,6 @@ public static async Task PostAsync(RemoteHost host, string subUrl, object data)
}
}

private void SaveAsLastOutputArguments(OutputArguments arguments)
{
if (!Config.Instance.RememberLastArguments)
{
return;
}
Config.Instance.LastOutputArguments.AddOrSetValue(ViewModel.Type, arguments);
Config.Instance.Save();
}

private void ClearFilesButton_Click(object sender, RoutedEventArgs e)
{
Expand Down
34 changes: 1 addition & 33 deletions SimpleFFmpegGUI.WPF/Panels/FileIOPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,8 @@ public void BrowseAndAddInput()
}
}

public List<InputArguments> GetInputs()
{
foreach (var input in ViewModel.Inputs)
{
input.Apply();
}
var inputs = ViewModel.Inputs.Where(p => !string.IsNullOrEmpty(p.FilePath));
if (inputs.Count() < ViewModel.MinInputsCount)
{
throw new Exception("输入文件少于需要的文件数量");
}
return inputs.Cast<InputArguments>().ToList();
}

public string GetOutput(InputArguments inputArgs)
{
var input = inputArgs.FilePath;
string dir = ViewModel.OutputDir;
if (string.IsNullOrWhiteSpace(dir))//没有指定输出位置
{
dir = Config.Instance.DefaultOutputDirType switch
{
DefaultOutputDirType.InputDir => Path.GetDirectoryName(input),
DefaultOutputDirType.InputNewDir => Path.Combine(Path.GetDirectoryName(input), Config.Instance.DefaultOutputDirInputSubDirName),
DefaultOutputDirType.SpecialDir => Config.Instance.DefaultOutputDirSpecialDirPath,
_ => throw new NotImplementedException()
};
}
if (ViewModel.CanSetOutputFileName && !string.IsNullOrWhiteSpace(ViewModel.OutputFileName))
{
return Path.Combine(dir, ViewModel.OutputFileName);
}
return Path.Combine(dir, Path.GetFileName(input));
}


/// <summary>
/// 用于添加到远程主机,获取输出文件名
Expand Down
126 changes: 126 additions & 0 deletions SimpleFFmpegGUI.WPF/ViewModels/AddTaskPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using CommunityToolkit.Mvvm.ComponentModel;
using FzLib;
using Microsoft.Extensions.DependencyInjection;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.Panels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using SimpleFFmpegGUI.FFmpegArgument;
using SimpleFFmpegGUI.WPF.Messages;
using FzLib.Collection;

namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class AddTaskPageViewModel : ViewModelBase
{
public AddTaskPageViewModel(QueueManager queue, TaskManager taskManager, TasksAndStatuses tasksAndStatuses)
{
Queue = queue;
this.taskManager = taskManager;
this.tasksAndStatuses = tasksAndStatuses;
}

public FileIOPanelViewModel FileIOViewModel { get; set; }
public CodeArgumentsPanelViewModel CodeArgumentsViewModel { get; set; }
public PresetsPanelViewModel PresetsViewModel { get; set; }

public IEnumerable TaskTypes => Enum.GetValues(typeof(TaskType));

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanAddFile))]
private TaskType type;

public QueueManager Queue { get; }


[ObservableProperty]
private bool allowChangeType = true;
private readonly TaskManager taskManager;
private readonly TasksAndStatuses tasksAndStatuses;

public bool CanAddFile => Type is TaskType.Code or TaskType.Concat;

private async void AddToQueue(bool addToQueue)
{
var args = CodeArgumentsViewModel.GetArguments();
try
{
if (Type is TaskType.Code)
{
FFmpegManager.TestOutputArguments(args);
}
}
catch (FFmpegArgumentException ex)
{
QueueErrorMessage("参数错误", ex);
return;
}

SendMessage(new WindowEnableMessage(false));
try
{
List<InputArguments> inputs = FileIOViewModel.GetInputs();

switch (Type)
{
case TaskType.Code://需要将输入文件单独加入任务
foreach (var input in inputs)
{
TaskInfo task = await taskManager.AddTaskAsync(TaskType.Code, new List<InputArguments>() { input }, FileIOViewModel.GetOutput(input), args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
}
QueueSuccessMessage($"已加入{inputs.Count}个任务队列");
break;
case TaskType.Custom or TaskType.Compare://不存在文件输出
{
TaskInfo task = await taskManager.AddTaskAsync(Type, inputs, null, args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
QueueSuccessMessage("已加入队列");
}
break;
default:
{
TaskInfo task = await taskManager.AddTaskAsync(Type, inputs, FileIOViewModel.GetOutput(inputs[0]), args);
App.ServiceProvider.GetService<TasksAndStatuses>().Tasks.Insert(0, UITaskInfo.FromTask(task));
QueueSuccessMessage("已加入队列");
}
break;
}
if (Config.Instance.ClearFilesAfterAddTask)
{
FileIOViewModel.Reset(false);
}
if (addToQueue)
{
await Task.Run(() => App.ServiceProvider.GetService<QueueManager>().StartQueue());
QueueSuccessMessage("已开始队列");
}
SaveAsLastOutputArguments(args);
}
catch (Exception ex)
{
QueueErrorMessage("加入队列失败", ex);
}
finally
{
SendMessage(new WindowEnableMessage(true));
}
}
private void SaveAsLastOutputArguments(OutputArguments arguments)
{
if (!Config.Instance.RememberLastArguments)
{
return;
}
Config.Instance.LastOutputArguments.AddOrSetValue(Type, arguments);
Config.Instance.Save();
}
}
}
Loading

0 comments on commit 127c537

Please sign in to comment.