Skip to content

Commit

Permalink
完成对SettingPage的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 8, 2024
1 parent 44a0f52 commit 79b3aef
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 117 deletions.
60 changes: 16 additions & 44 deletions SimpleFFmpegGUI.WPF/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FzLib;
using CommunityToolkit.Mvvm.ComponentModel;
using FzLib;
using FzLib.DataStorage.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
Expand All @@ -22,29 +23,36 @@ public enum DefaultOutputDirType
SpecialDir
}

public class Config : IJsonSerializable, INotifyPropertyChanged
public partial class Config : ObservableObject, IJsonSerializable, INotifyPropertyChanged
{
private const string path = "config.json";

private static bool loaded = false;

[ObservableProperty]
private bool clearFilesAfterAddTask;

[ObservableProperty]
private string defaultOutputDirInputSubDirName = "output";

[ObservableProperty]
private string defaultOutputDirSpecialDirPath = "C:\\output";

[ObservableProperty]
private DefaultOutputDirType defaultOutputDirType = DefaultOutputDirType.InputDir;

[ObservableProperty]
private bool rememberLastArguments = true;

[ObservableProperty]
private List<RemoteHost> remoteHosts = new List<RemoteHost>();

[ObservableProperty]
private bool smoothScroll = true;

[ObservableProperty]
private bool startQueueAfterAddTask = true;

public event PropertyChangedEventHandler PropertyChanged;
public static Config Instance
{
get
Expand All @@ -63,50 +71,9 @@ public static Config Instance
return App.ServiceProvider.GetService<Config>();
}
}
public bool ClearFilesAfterAddTask
{
get => clearFilesAfterAddTask;
set => this.SetValueAndNotify(ref clearFilesAfterAddTask, value, nameof(ClearFilesAfterAddTask));
}

public string DefaultOutputDirInputSubDirName
{
get => defaultOutputDirInputSubDirName;
set => this.SetValueAndNotify(ref defaultOutputDirInputSubDirName, value, nameof(DefaultOutputDirInputSubDirName));
}

public string DefaultOutputDirSpecialDirPath
{
get => defaultOutputDirSpecialDirPath;
set => this.SetValueAndNotify(ref defaultOutputDirSpecialDirPath, value, nameof(DefaultOutputDirSpecialDirPath));
}

public DefaultOutputDirType DefaultOutputDirType
{
get => defaultOutputDirType;
set => this.SetValueAndNotify(ref defaultOutputDirType, value, nameof(DefaultOutputDirType));
}

public Dictionary<TaskType, OutputArguments> LastOutputArguments { get; set; } = new Dictionary<TaskType, OutputArguments>();

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

public List<RemoteHost> RemoteHosts
{
get => remoteHosts;
set => this.SetValueAndNotify(ref remoteHosts, value, nameof(RemoteHosts));
}

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

public PerformanceTestCodecParameter[] TestCodecs { get; set; }
public PerformanceTestLine[] TestItems { get; set; }
public int TestQCMode { get; set; } = 0;
Expand All @@ -116,6 +83,11 @@ public void Save()
{
this.Save(path, new JsonSerializerSettings().SetIndented());
}
public Config DeepCopy()
{
var serialized = JsonConvert.SerializeObject(this);
return JsonConvert.DeserializeObject<Config>(serialized);
}
}

public class RemoteHost
Expand Down
28 changes: 14 additions & 14 deletions SimpleFFmpegGUI.WPF/Pages/SettingPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
Spacing="8">
<CheckBox
Content="平滑滚动"
IsChecked="{Binding SmoothScroll}" />
IsChecked="{Binding Configs.SmoothScroll}" />

<ui:SimpleStackPanel
Orientation="Horizontal"
Spacing="8">
<CheckBox
VerticalAlignment="Center"
Content="记住上次的输出参数"
IsChecked="{Binding RememberLastArguments}" />
IsChecked="{Binding Configs.RememberLastArguments}" />
<TextBlock
VerticalAlignment="Center"
Opacity="0.5"
Text="开启该选项时,默认预设功能将不可用"
Visibility="{Binding RememberLastArguments, Converter={StaticResource Bool2VisibilityConverter}}" />
Visibility="{Binding Configs.RememberLastArguments, Converter={StaticResource Bool2VisibilityConverter}}" />
</ui:SimpleStackPanel>
<ui:SimpleStackPanel
Orientation="Horizontal"
Expand All @@ -49,7 +49,7 @@
Width="200"
ui:ControlHelper.Header="默认输出文件夹"
ItemsSource="{Binding DefaultOutputDirTypes}"
SelectedItem="{Binding DefaultOutputDirType}">
SelectedItem="{Binding Configs.DefaultOutputDirType}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ., Converter={StaticResource DescriptionConverter}}" />
Expand All @@ -59,17 +59,17 @@
<TextBox
Width="360"
ui:ControlHelper.Header="指定文件夹的名称"
Text="{Binding DefaultOutputDirInputSubDirName}"
Visibility="{Binding DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=InputNewDir}" />
Text="{Binding Configs.DefaultOutputDirInputSubDirName}"
Visibility="{Binding Configs.DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=InputNewDir}" />
<TextBox
Width="360"
ui:ControlHelper.Header="指定文件夹的路径"
Text="{Binding DefaultOutputDirSpecialDirPath}"
Visibility="{Binding DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=SpecialDir}" />
Text="{Binding Configs.DefaultOutputDirSpecialDirPath}"
Visibility="{Binding Configs.DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=SpecialDir}" />
<Button
VerticalAlignment="Bottom"
Click="BrowseSpecialDirPathButton_Click"
Visibility="{Binding DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=SpecialDir}">
Command="{Binding BrowseSpecialDirPathCommand}"
Visibility="{Binding Configs.DefaultOutputDirType, Converter={StaticResource EqualConverter}, ConverterParameter=SpecialDir}">
<ui:SymbolIcon Symbol="OpenFile" />
</Button>
</ui:SimpleStackPanel>
Expand Down Expand Up @@ -106,7 +106,7 @@
Grid.Row="2"
Header="远程管理主机">
<DataGrid
c:SmoothScrollViewerHelper.SmoothScroll="{Binding SmoothScroll}"
c:SmoothScrollViewerHelper.SmoothScroll="{Binding Configs.SmoothScroll}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="True"
Expand All @@ -129,19 +129,19 @@
Margin="-8"
MouseEnter="CommandBar_MouseEnter">
<ui:AppBarButton
Click="AddRemoteHost_Click"
Command="{Binding AddRemoteHostCommand}"
Icon="Add"
Label="新增远程主机" />
<ui:AppBarSeparator />
<ui:AppBarButton
Click="CancelButton_Click"
Command="{Binding CancelCommand}"
Label="取消">
<ui:AppBarButton.Icon>
<ui:FontIcon Glyph="&#xE106;" />
</ui:AppBarButton.Icon>
</ui:AppBarButton>
<ui:AppBarButton
Click="OKButton_Click"
Command="{Binding SaveCommand}"
Label="保存">
<ui:AppBarButton.Icon>
<ui:FontIcon Glyph="&#xE73E;" />
Expand Down
66 changes: 7 additions & 59 deletions SimpleFFmpegGUI.WPF/Pages/SettingPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using Enterwell.Clients.Wpf.Notifications;
using FzLib;
using FzLib.WPF;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using SimpleFFmpegGUI.Dto;
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.Globalization;
using System.Linq;
Expand All @@ -29,74 +26,25 @@

namespace SimpleFFmpegGUI.WPF.Pages
{
public class SettingPageViewModel : Config
{
private readonly ConfigManager configManager;

public SettingPageViewModel(ConfigManager configManager)
{
Config.Instance.Adapt(this);
this.configManager = configManager;
ObservableRemoteHosts = new ObservableCollection<RemoteHost>(RemoteHosts);
this.Notify(nameof(ObservableRemoteHosts));
}

public IEnumerable DefaultOutputDirTypes => Enum.GetValues<DefaultOutputDirType>();

public ObservableCollection<RemoteHost> ObservableRemoteHosts { get; set; }

public int DefaultProcessPriority
{
get => configManager.DefaultProcessPriority;
set => configManager.DefaultProcessPriority = value;
}
}

/// <summary>
/// Interaction logic for SettingPage.xaml
/// </summary>
public partial class SettingPage : UserControl, ICloseablePage
{
public SettingPage(SettingPageViewModel viewModel)
public SettingPage()
{
ViewModel = viewModel;
DataContext = ViewModel;
ViewModel = this.SetDataContext<SettingPageViewModel>();
InitializeComponent();
}

public SettingPageViewModel ViewModel { get; set; }

public event EventHandler RequestToClose;

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
RequestToClose?.Invoke(sender, e);
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.RemoteHosts = ViewModel.ObservableRemoteHosts.ToList();
ViewModel.Adapt(Config.Instance);
Config.Instance.Save();
RequestToClose?.Invoke(sender, e);
}

private void AddRemoteHost_Click(object sender, RoutedEventArgs e)
public event EventHandler RequestToClose
{
ViewModel.ObservableRemoteHosts.Add(new RemoteHost());
add => ViewModel.RequestToClose += value;
remove => ViewModel.RequestToClose -= value;
}

public SettingPageViewModel ViewModel { get; set; }
private void CommandBar_MouseEnter(object sender, MouseEventArgs e)
{
Keyboard.ClearFocus();
}

private void BrowseSpecialDirPathButton_Click(object sender, RoutedEventArgs e)
{
var path = new OpenFolderDialog().GetPath(this.GetWindow());
if (!string.IsNullOrEmpty(path))
{
ViewModel.DefaultOutputDirSpecialDirPath = path;
}
}
}
}
75 changes: 75 additions & 0 deletions SimpleFFmpegGUI.WPF/ViewModels/SettingPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FzLib;
using Mapster;
using Microsoft.Win32;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.WPF.Messages;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class SettingPageViewModel : ViewModelBase
{
private readonly ConfigManager configManager;

[ObservableProperty]
private Config configs;

public SettingPageViewModel(ConfigManager configManager)
{
configs = Config.Instance.DeepCopy();
this.configManager = configManager;
ObservableRemoteHosts = new ObservableCollection<RemoteHost>(Configs.RemoteHosts);
this.Notify(nameof(ObservableRemoteHosts));
}

public event EventHandler RequestToClose;

public IEnumerable DefaultOutputDirTypes => Enum.GetValues<DefaultOutputDirType>();

public int DefaultProcessPriority
{
get => configManager.DefaultProcessPriority;
set => configManager.DefaultProcessPriority = value;
}

public ObservableCollection<RemoteHost> ObservableRemoteHosts { get; set; }
[RelayCommand]
private void AddRemoteHost()
{
ObservableRemoteHosts.Add(new RemoteHost());
}

[RelayCommand]
private void BrowseSpecialDirPath()
{
var dialog = new OpenFolderDialog();
SendMessage(new FileDialogMessage(dialog));
var path = dialog.FolderName;
if (!string.IsNullOrEmpty(path))
{
Configs.DefaultOutputDirSpecialDirPath = path;
}
}

[RelayCommand]
private void Cancel()
{
RequestToClose?.Invoke(this, EventArgs.Empty);
}

[RelayCommand]
private void Save()
{
Configs.RemoteHosts = ObservableRemoteHosts.ToList();
Configs.Adapt(Config.Instance);
Config.Instance.Save();
RequestToClose?.Invoke(this, EventArgs.Empty);
}
}
}

0 comments on commit 79b3aef

Please sign in to comment.