Skip to content

Commit

Permalink
基本完成FileIOPanel的改造,使用WeakReferenceMessenger实现ViewModel调用View
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 5, 2024
1 parent c0e5e04 commit 36957b1
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 192 deletions.
63 changes: 59 additions & 4 deletions SimpleFFmpegGUI.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using FzLib;
using CommunityToolkit.Mvvm.Messaging;
using FzLib;
using FzLib.WPF;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using ModernWpf.FzExtension.CommonDialog;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Converters;
using SimpleFFmpegGUI.WPF.Messages;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.Pages;
using SimpleFFmpegGUI.WPF.Panels;
Expand All @@ -23,10 +26,12 @@
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CommonDialog = ModernWpf.FzExtension.CommonDialog.CommonDialog;
using Task = System.Threading.Tasks.Task;

namespace SimpleFFmpegGUI.WPF
Expand All @@ -51,6 +56,56 @@ public MainWindow(MainWindowViewModel viewModel, QueueManager queue)
DataContext = ViewModel;
InitializeComponent();
this.queue = queue;
RegisterMessages();
}

private void RegisterMessages()
{
WeakReferenceMessenger.Default.Register<FileDialogMessage>(this, (_, m) =>
{
switch (m.Dialog)
{
case OpenFileDialog ofd:
m.Result = ofd.ShowDialog(this);
break;
case SaveFileDialog sfd:
m.Result = sfd.ShowDialog(this);
break;
case OpenFolderDialog ofod:
m.Result = ofod.ShowDialog(this);
break;
default:
break;
}
});

WeakReferenceMessenger.Default.Register<WindowHandleMessage>(this, (_, m) =>
{
m.Handle = new WindowInteropHelper(this).Handle;
});

WeakReferenceMessenger.Default.Register<WindowEnableMessage>(this, (_, m) =>
{
IsEnabled = m.IsEnabled;
});

WeakReferenceMessenger.Default.Register<QueueMessagesMessage>(this, (_, m) =>
{
switch (m.Type)
{
case 'S':
this.CreateMessage().QueueSuccess(m.Message);
break;
case 'E' when m.Exception == null:
this.CreateMessage().QueueError(m.Message);
break;
case 'E' when m.Exception != null:
this.CreateMessage().QueueError(m.Message, m.Exception);
break;
default:
break;
}
});
}

public event EventHandler UiCompressModeChanged;
Expand Down Expand Up @@ -158,13 +213,13 @@ protected override async void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
await Task.Yield();
string[] files = new[]
{
string[] files =
[
"ffmpeg.exe",
"ffprobe.exe",
"ffplay.exe",
"mediainfo.exe"
};
];

foreach (var file in files)
{
Expand Down
14 changes: 14 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/FileDialogMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleFFmpegGUI.WPF.Messages
{
public class FileDialogMessage(CommonItemDialog dialog)
{
public CommonItemDialog Dialog { get; } = dialog;
public bool? Result { get; set; }
}
}
11 changes: 11 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/QueueMessagesMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SimpleFFmpegGUI.WPF.Messages
{
public class QueueMessagesMessage(char type, string message, Exception exception = null)
{
public char Type { get; } = type;
public string Message { get; } = message;
public Exception Exception { get; } = exception;
}
}
7 changes: 7 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/WindowEnableMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimpleFFmpegGUI.WPF.Messages
{
public class WindowEnableMessage(bool isEnabled)
{
public bool IsEnabled { get; } = isEnabled;
}
}
7 changes: 7 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/WindowHandleMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimpleFFmpegGUI.WPF.Messages
{
public class WindowHandleMessage()
{
public nint Handle { get; set; }
}
}
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/Pages/AddTaskPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private async void ViewModel_PropertyChanged(object sender, PropertyChangedEvent
switch (e.PropertyName)
{
case nameof(ViewModel.Type):
fileIOPanel.Update(ViewModel.Type);
fileIOPanel.UpdateType(ViewModel.Type);
await argumentsPanel.UpdateTypeAsync(ViewModel.Type);
await presetsPanel.UpdateTypeAsync(ViewModel.Type);
break;
Expand Down
12 changes: 8 additions & 4 deletions SimpleFFmpegGUI.WPF/Panels/FileIOPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@
VerticalAlignment="Center"
Orientation="Horizontal"
Spacing="8">
<Button Click="BrowseFileButton_Click">
<Button
Command="{Binding DataContext.BrowseFileCommand, Source={x:Reference root}}"
CommandParameter="{Binding .}">
<ui:SymbolIcon Symbol="OpenFile" />
</Button>
<Button
Click="ClipButton_Click"
Command="{Binding DataContext.ClipCommand, Source={x:Reference root}}"
CommandParameter="{Binding .}"
Visibility="{Binding ElementName=root, Path=DataContext.ShowTimeClip, Converter={StaticResource Bool2VisibilityConverter}}">
<ui:SymbolIcon Symbol="Cut" />
</Button>
<Button
Click="DeleteFileButton_Click"
Command="{Binding DataContext.RemoveFileCommand, Source={x:Reference root}}"
CommandParameter="{Binding .}"
IsEnabled="{Binding CanDelete}"
Visibility="{Binding ElementName=root, Path=DataContext.CanChangeInputsCount, Converter={StaticResource Bool2VisibilityConverter}}">
<ui:SymbolIcon Symbol="Delete" />
Expand Down Expand Up @@ -244,7 +248,7 @@
<Button
Grid.Column="2"
VerticalAlignment="Bottom"
Click="BrowseOutputFileButton_Click">
Command="{Binding BrowseOutputFileCommand}">
<ui:SymbolIcon Symbol="OpenFile" />
</Button>
</Grid>
Expand Down
120 changes: 8 additions & 112 deletions SimpleFFmpegGUI.WPF/Panels/FileIOPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using FzLib.WPF;
using CommunityToolkit.Mvvm.Messaging;
using FzLib.WPF;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using ModernWpf.FzExtension.CommonDialog;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Messages;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.Pages;
using SimpleFFmpegGUI.WPF.ViewModels;
Expand Down Expand Up @@ -32,16 +34,15 @@

namespace SimpleFFmpegGUI.WPF.Panels
{

public partial class FileIOPanel : UserControl
{
public FileIOPanel()
{
DataContext = ViewModel;
ViewModel = this.SetDataContext<FileIOPanelViewModel>();
InitializeComponent();
}

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

public void AddInput()
{
Expand Down Expand Up @@ -126,15 +127,12 @@ public void Reset()

public void Update(TaskType type, List<InputArguments> inputs, string output)
{
if (!ViewModel.Update(type, inputs, output))
{
this.GetWindow().CreateMessage().QueueError("输入文件超过该类型最大数量");
}
ViewModel.Update(type, inputs, output);
}

public void Update(TaskType type)
public void UpdateType(TaskType type)
{
ViewModel.Update(type);
ViewModel.UpdateType(type);
ViewModel.Reset(true);
}

Expand Down Expand Up @@ -172,107 +170,5 @@ protected override void OnDrop(DragEventArgs e)
ViewModel.Inputs.Add(new InputArgumentsDetail());
}
}

private async void BrowseFileButton_Click(object sender, RoutedEventArgs e)
{
var input = (sender as FrameworkElement).DataContext as InputArgumentsDetail;

var dialog = new OpenFileDialog().AddAllFilesFilter();
string path = dialog.GetPath(this.GetWindow());
if (path != null)
{
if (input.Image2)
{
string seqFilename = FileSystemUtility.GetSequence(path);
if (seqFilename != null)
{
bool rename = await CommonDialog.ShowYesNoDialogAsync("图像序列", $"指定的文件可能是图像序列中的一个,是否将输入路径修改为{seqFilename}?");
if (rename)
{
path = seqFilename;
}
}
}


input.FilePath = path;
}
}

private void BrowseOutputFileButton_Click(object sender, RoutedEventArgs e)
{
string path = new OpenFolderDialog().GetPath(this.GetWindow());
if (path != null)
{
ViewModel.OutputDir = path;
}
}

private async void ClipButton_Click(object sender, RoutedEventArgs e)
{
try
{
var input = (sender as FrameworkElement).DataContext as InputArgumentsDetail;
Debug.Assert(input != null);
if (string.IsNullOrEmpty(input.FilePath))
{
this.CreateMessage().QueueError("请先设置文件地址");
return;
}
if (!File.Exists(input.FilePath))
{
this.CreateMessage().QueueError($"找不到文件{input.FilePath}");
return;
}
this.GetWindow().IsEnabled = false;
(TimeSpan From, TimeSpan To)? result = null;
Process p = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = FzLib.Program.App.ProgramFilePath,
RedirectStandardOutput = true,
}
};
p.StartInfo.ArgumentList.Add("cut");
p.StartInfo.ArgumentList.Add(new WindowInteropHelper(this.GetWindow()).Handle.ToString());
p.StartInfo.ArgumentList.Add(input.FilePath);
p.StartInfo.ArgumentList.Add(input.From.HasValue ? input.From.Value.ToString() : "-");
p.StartInfo.ArgumentList.Add(input.To.HasValue ? input.To.Value.ToString() : "-");
p.Start();
var output = await p.StandardOutput.ReadToEndAsync();
string[] outputs = output.Split(',');
if (outputs.Length == 2)
{
if (TimeSpan.TryParse(outputs[0], out TimeSpan from))
{
if (TimeSpan.TryParse(outputs[1], out TimeSpan to))
{
result = (from, to);
}
}
}
if (result.HasValue)
{
var time = result.Value;
input.From = time.From;
input.To = time.To;
input.Duration = null;
}
}
catch (Exception ex)
{
await CommonDialog.ShowErrorDialogAsync(ex);
}
finally
{
this.GetWindow().IsEnabled = true;
}
}

private void DeleteFileButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.Inputs.Remove((sender as FrameworkElement).DataContext as InputArgumentsDetail);
}
}
}
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/Panels/PresetsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Cursor="Hand">
<Border.InputBindings>
<MouseBinding
Command="{Binding DataContext.ApplyCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"
Command="{Binding DataContext.ApplyCommand, Source={x:Reference root}}"
CommandParameter="{Binding .}"
MouseAction="LeftClick" />
</Border.InputBindings>
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/Panels/PresetsPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public partial class PresetsPanel : UserControl
{
public PresetsPanel()
{
DataContext = ViewModel = ViewModelBase.Bind<PresetsPanelViewModel>(this);
ViewModel = this.SetDataContext<PresetsPanelViewModel>();
InitializeComponent();
}

Expand Down
Loading

0 comments on commit 36957b1

Please sign in to comment.