Skip to content

Commit

Permalink
完成对PresetsPage的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 8, 2024
1 parent 2e791f3 commit 44a0f52
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 166 deletions.
18 changes: 10 additions & 8 deletions SimpleFFmpegGUI.WPF/Pages/PresetsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
xmlns:n="clr-namespace:Enterwell.Clients.Wpf.Notifications.Controls;assembly=Enterwell.Clients.Wpf.Notifications"
xmlns:panels="clr-namespace:SimpleFFmpegGUI.WPF.Panels"
xmlns:ui="http://schemas.modernwpf.com/2019"
x:Name="root"
mc:Ignorable="d">
<DockPanel>

<ui:CommandBar
Grid.Row="99"
DockPanel.Dock="Bottom">
<ui:AppBarButton
Click="DeleteAllButton_Click"
Command="{Binding DeleteAllCommand}"
Icon="Delete"
Label="删除所有预设" />
<ui:AppBarButton
Click="ImportButton_Click"
Command="{Binding ImportCommand}"
Icon="Download"
Label="导入" />
<ui:AppBarButton
Click="ExportButton_Click"
Command="{Binding ExportCommand}"
Icon="Upload"
Label="导出所有预设" />
</ui:CommandBar>
Expand Down Expand Up @@ -96,7 +97,8 @@
<Button
Grid.Column="4"
Background="Transparent"
Click="DeleteButton_Click">
Command="{Binding DataContext.DeleteCommand, ElementName=root}"
CommandParameter="{Binding .}">
<ui:SymbolIcon Symbol="Delete" />
</Button>
</Grid>
Expand Down Expand Up @@ -124,16 +126,16 @@
Spacing="8">
<Button
Width="96"
Click="SetDefaultButton_Click"
Command="{Binding SetDefaultAsyncCommand}"
Content="设为默认"
IsEnabled="{Binding SelectedPreset.Default, Converter={StaticResource InverseBoolConverter}}" />
<Button
Width="96"
Click="CancelButton_Click"
Content="取消" />
Command="{Binding UnselectCommand}"
Content="关闭" />
<Button
Width="96"
Click="SaveButton_Click"
Command="{Binding SaveCommand}"
Content="保存" />
</ui:SimpleStackPanel>
</Grid>
Expand Down
158 changes: 5 additions & 153 deletions SimpleFFmpegGUI.WPF/Pages/PresetsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
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.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand All @@ -33,94 +30,22 @@

namespace SimpleFFmpegGUI.WPF.Pages
{
/// <summary>
/// Interaction logic for PresetsPage.xaml
/// </summary>
public partial class PresetsPage : UserControl
{
private readonly PresetManager presetManager;

public PresetsPage(PresetsPageViewModel viewModel, PresetManager presetManager)
public PresetsPage()
{
ViewModel = viewModel;
this.presetManager = presetManager;
DataContext = ViewModel;
ViewModel = this.SetDataContext<PresetsPageViewModel>();
InitializeComponent();
ViewModel.CodeArgumentsViewModel = argumentsPanel.ViewModel;
Loaded += async (s, e) => await ViewModel.FillPresetsAsync();
}

public PresetsPageViewModel ViewModel { get; set; }
private async void DeleteAllButton_Click(object sender, RoutedEventArgs e)
{
if (await CommonDialog.ShowYesNoDialogAsync("删除预设", $"是否删除所有类型的所有预设?"))
{
IsEnabled = false;
try
{
await presetManager.DeletePresetsAsync();
await ViewModel.FillPresetsAsync();
}
catch (Exception ex)
{
await CommonDialog.ShowErrorDialogAsync(ex, "删除失败");
}
finally
{
IsEnabled = true;
}
}
}

private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
var preset = (sender as Button).DataContext as CodePreset;
if (await CommonDialog.ShowYesNoDialogAsync("删除预设", $"是否删除“{preset.Name}”?"))
{
await presetManager.DeletePresetAsync(preset.Id);
ViewModel.Presets.Remove(preset);
}
}

private async void ExportButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog().AddFilter("配置文件", "json");
dialog.FileName = "FFmpeg工具箱 预设.json";
string path = dialog.GetPath(this.GetWindow());
if (path != null)
{
var json = await presetManager.ExportAsync();
File.WriteAllText(path, json, new UTF8Encoding());
this.CreateMessage().QueueSuccess("导出成功");
}
}

private async void ImportButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog().AddFilter("配置文件", "json");
string path = dialog.GetPath(this.GetWindow());
if (path != null)
{
try
{
await presetManager.ImportAsync(File.ReadAllText(path, new UTF8Encoding()));
await ViewModel.FillPresetsAsync();
this.CreateMessage().QueueSuccess("导入成功,同名预设已被更新");
}
catch (Exception ex)
{
await CommonDialog.ShowErrorDialogAsync(ex, "导入失败");
}
}
}


private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0 && e.RemovedItems[0] is CodePreset oldPreset)
{
}
if (e.AddedItems.Count > 0 && e.AddedItems[0] is CodePreset preset)
{

grd.RowDefinitions[2].Height = new GridLength(48);
lvw.IsHitTestVisible = false;
lvw.ScrollIntoView(preset);
Expand All @@ -134,78 +59,5 @@ private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs
grd.RowDefinitions[4].Height = new GridLength(0);
}
}

private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
Debug.Assert(ViewModel.SelectedPreset != null);
try
{
ViewModel.SelectedPreset.Arguments = argumentsPanel.GetOutputArguments();
await presetManager.UpdatePresetAsync(ViewModel.SelectedPreset);
ViewModel.SelectedPreset = null;
}
catch (Exception ex)
{
this.CreateMessage().QueueError("更新预设失败", ex);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.SelectedPreset = null;
}

private async void SetDefaultButton_Click(object sender, RoutedEventArgs e)
{
Debug.Assert(ViewModel.SelectedPreset != null);
ViewModel.Presets.ForEach(p => p.Default = false);
ViewModel.SelectedPreset.Default = true;
await presetManager.SetDefaultPresetAsync(ViewModel.SelectedPreset.Id);
}
}

public class PresetsPageViewModel : INotifyPropertyChanged
{
private ObservableCollection<CodePreset> presets;

private CodePreset selectedPreset;

private TaskType type = TaskType.Code;
private readonly PresetManager presetManager;

public PresetsPageViewModel(PresetManager presetManager)
{
this.presetManager = presetManager;
}
public event PropertyChangedEventHandler PropertyChanged;

public ObservableCollection<CodePreset> Presets
{
get => presets;
set => this.SetValueAndNotify(ref presets, value, nameof(Presets));
}

public CodePreset SelectedPreset
{
get => selectedPreset;
set => this.SetValueAndNotify(ref selectedPreset, value, nameof(SelectedPreset));
}


public IEnumerable TaskTypes => Enum.GetValues(typeof(TaskType));
public TaskType Type
{
get => type;
set
{
this.SetValueAndNotify(ref type, value, nameof(Type));
FillPresetsAsync();
}
}

public async Task FillPresetsAsync()
{
Presets = new ObservableCollection<CodePreset>(await presetManager.GetPresetsAsync(Type));
}

}
}
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/Pages/SettingPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void CommandBar_MouseEnter(object sender, MouseEventArgs e)
private void BrowseSpecialDirPathButton_Click(object sender, RoutedEventArgs e)
{
var path = new OpenFolderDialog().GetPath(this.GetWindow());
if (path != null)
if (!string.IsNullOrEmpty(path))
{
ViewModel.DefaultOutputDirSpecialDirPath = path;
}
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/TestWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void BrowseTestVideoButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog().AddFilter("视频", "mp4", "mov", "mkv", "avi");
string path = dialog.GetPath(this);
if (path != null)
if (!string.IsNullOrEmpty(path))
{
ViewModel.TestVideo = path;
}
Expand Down
4 changes: 2 additions & 2 deletions SimpleFFmpegGUI.WPF/ViewModels/FileIOPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private async Task BrowseFile(InputArgumentsDetail input)
var dialog = new Microsoft.Win32.OpenFileDialog().AddAllFilesFilter();
SendMessage(new FileDialogMessage(dialog));
string path = dialog.FileName;
if (path != null)
if (!string.IsNullOrEmpty(path))
{
if (input.Image2)
{
Expand All @@ -264,7 +264,7 @@ private void BrowseOutputFile()
var dialog = new OpenFolderDialog();
SendMessage(new FileDialogMessage(dialog));
string path = dialog.FolderName;
if (path != null)
if (!string.IsNullOrEmpty(path))
{
OutputDir = path;
}
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/ViewModels/MediaInfoPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void BrowseFile()
SendMessage(new FileDialogMessage(dialog));

string path = dialog.FileName;
if (path != null)
if (!string.IsNullOrEmpty(path))
{
FilePath = path;
}
Expand Down
Loading

0 comments on commit 44a0f52

Please sign in to comment.