From 845ea763f026d561bc89c132edae4681aec25d49 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Sun, 28 Apr 2019 00:19:56 +0200 Subject: [PATCH 01/24] Migration of classes that were implementing INotifyPropertyChanged to ObservableObject. --- .../ConversionPreset/ConversionPreset.cs | 36 ++++++++----------- .../ViewModels/InputExtensionCategory.cs | 21 +++-------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index ad0d330..9e2fcfd 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -7,18 +7,17 @@ namespace FileConverter using System.ComponentModel; using System.Globalization; using System.Linq; - using System.Runtime.CompilerServices; using System.Xml.Serialization; - using FileConverter.Annotations; using FileConverter.Controls; using FileConverter.Services; + using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; [XmlRoot] [XmlType] - public class ConversionPreset : INotifyPropertyChanged, IDataErrorInfo, IXmlSerializable + public class ConversionPreset : ObservableObject, IDataErrorInfo, IXmlSerializable { private string name; private OutputType outputType; @@ -68,7 +67,6 @@ namespace FileConverter } } - public event PropertyChangedEventHandler PropertyChanged; [XmlAttribute] public string Name @@ -81,7 +79,7 @@ namespace FileConverter set { this.name = value; - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -97,7 +95,7 @@ namespace FileConverter { this.outputType = value; this.InitializeDefaultSettings(this.outputType); - this.OnPropertyChanged(); + this.RaisePropertyChanged(); this.CoerceInputTypes(); } } @@ -125,7 +123,7 @@ namespace FileConverter this.inputTypes[index] = this.inputTypes[index].ToLowerInvariant(); } - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -140,7 +138,7 @@ namespace FileConverter set { this.inputPostConversionAction = value; - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -184,7 +182,7 @@ namespace FileConverter } } - this.OnPropertyChanged(nameof(this.Settings)); + this.RaisePropertyChanged(nameof(this.Settings)); } } @@ -199,7 +197,7 @@ namespace FileConverter set { this.outputFileNameTemplate = value; - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -239,7 +237,7 @@ namespace FileConverter } } - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -247,7 +245,7 @@ namespace FileConverter { get { - string errorString = this.Validate("Name"); + string errorString = this.Validate("ShortName"); if (!string.IsNullOrEmpty(errorString)) { return errorString; @@ -307,14 +305,14 @@ namespace FileConverter } this.inputTypes.Add(inputType); - this.OnPropertyChanged(nameof(this.InputTypes)); + this.RaisePropertyChanged(nameof(this.InputTypes)); } public void RemoveInputType(string inputType) { if (this.inputTypes.Remove(inputType)) { - this.OnPropertyChanged(nameof(this.InputTypes)); + this.RaisePropertyChanged(nameof(this.InputTypes)); } } @@ -347,7 +345,7 @@ namespace FileConverter this.settings[settingsKey] = value; - this.OnPropertyChanged(nameof(this.Settings)); + this.RaisePropertyChanged(nameof(this.Settings)); } public string GetSettingsValue(string settingsKey) @@ -388,12 +386,6 @@ namespace FileConverter return this.Settings.ContainsKey(settingsKey); } - [NotifyPropertyChangedInvocator] - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - private void CoerceInputTypes() { if (this.inputTypes == null) @@ -529,7 +521,7 @@ namespace FileConverter throw new System.Exception("Missing default settings for type " + outputType); } - this.OnPropertyChanged(nameof(this.Settings)); + this.RaisePropertyChanged(nameof(this.Settings)); } private void InitializeSettingsValue(string settingsKey, string value, bool force = false) diff --git a/Application/FileConverter/ViewModels/InputExtensionCategory.cs b/Application/FileConverter/ViewModels/InputExtensionCategory.cs index be240a9..95c9842 100644 --- a/Application/FileConverter/ViewModels/InputExtensionCategory.cs +++ b/Application/FileConverter/ViewModels/InputExtensionCategory.cs @@ -3,12 +3,9 @@ namespace FileConverter.ViewModels { using System.Collections.Generic; - using System.ComponentModel; - using System.Runtime.CompilerServices; + using GalaSoft.MvvmLight; - using FileConverter.Annotations; - - public class InputExtensionCategory : INotifyPropertyChanged + public class InputExtensionCategory : ObservableObject { private string name; private List inputExtensions = new List(); @@ -18,8 +15,6 @@ namespace FileConverter.ViewModels this.Name = name; } - public event PropertyChangedEventHandler PropertyChanged; - public string Name { get @@ -30,7 +25,7 @@ namespace FileConverter.ViewModels set { this.name = value; - this.OnPropertyChanged(); + this.RaisePropertyChanged(); } } @@ -60,15 +55,9 @@ namespace FileConverter.ViewModels { inputExtension = new InputExtension(extension); this.inputExtensions.Add(inputExtension); - this.OnPropertyChanged("InputExtensions"); - this.OnPropertyChanged("InputExtensionNames"); + this.RaisePropertyChanged("InputExtensions"); + this.RaisePropertyChanged("InputExtensionNames"); } } - - [NotifyPropertyChangedInvocator] - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } } } \ No newline at end of file From 4ff7b5148ec74448f813b751ec808e20cb204b37 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Tue, 30 Apr 2019 08:08:12 +0200 Subject: [PATCH 02/24] Add notion of name and folders on conversion jobs. --- Application/FileConverter/Application.xaml.cs | 2 +- .../ConversionJobs/ConversionJob.cs | 2 +- .../ConversionPreset/ConversionPreset.cs | 75 +++++++++++++++---- .../FileConverter/Services/SettingsService.cs | 2 +- Application/FileConverter/Settings.cs | 4 +- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 782ee0d..6994978 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -391,7 +391,7 @@ namespace FileConverter IConversionService conversionService = SimpleIoc.Default.GetInstance(); // Create conversion jobs. - Debug.Log("Create jobs for conversion preset: '{0}'", conversionPreset.Name); + Debug.Log("Create jobs for conversion preset: '{0}'", conversionPreset.FullName); try { for (int index = 0; index < filePaths.Count; index++) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 1523e82..5852a9f 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -319,7 +319,7 @@ namespace FileConverter.ConversionJobs this.State = ConversionState.Ready; } - Debug.Log("Job initialized: Preset: '{0}' Input: {1} Output: {2}", this.ConversionPreset.Name, this.InputFilePath, this.OutputFilePath); + Debug.Log("Job initialized: Preset: '{0}' Input: {1} Output: {2}", this.ConversionPreset.FullName, this.InputFilePath, this.OutputFilePath); if (this.State != ConversionState.Failed) { diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 9e2fcfd..959c0e1 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -19,7 +19,8 @@ namespace FileConverter [XmlType] public class ConversionPreset : ObservableObject, IDataErrorInfo, IXmlSerializable { - private string name; + private string shortName; + private OutputType outputType; private List inputTypes; private InputPostConversionAction inputPostConversionAction; @@ -28,12 +29,12 @@ namespace FileConverter public ConversionPreset() { - this.Name = Properties.Resources.DefaultPresetName; + this.FullName = Properties.Resources.DefaultPresetName; } - public ConversionPreset(string name, OutputType outputType, params string[] inputTypes) + public ConversionPreset(string fullName, OutputType outputType, params string[] inputTypes) { - this.Name = name; + this.FullName = fullName; this.OutputType = outputType; List inputTypeList = new List(); inputTypeList.AddRange(inputTypes); @@ -42,9 +43,9 @@ namespace FileConverter this.outputFileNameTemplate = "(p)(f)"; } - public ConversionPreset(string name, ConversionPreset source, params string[] additionalInputTypes) + public ConversionPreset(string fullName, ConversionPreset source, params string[] additionalInputTypes) { - this.Name = name; + this.FullName = fullName; this.OutputType = source.outputType; List inputTypeList = new List(); if (source.inputTypes != null) @@ -67,22 +68,61 @@ namespace FileConverter } } - - [XmlAttribute] - public string Name + [XmlAttribute("Name")] + public string FullName { get { - return this.name; + string fullName = string.Empty; + if (this.ParentFoldersNames != null) + { + foreach (string folder in this.ParentFoldersNames) + { + fullName += $"{folder}/"; + } + } + + fullName += this.shortName; + + return fullName; } set { - this.name = value; + string[] folders = value.Split('/'); + if (folders.Length == 0) + { + Diagnostics.Debug.Log("Invalid full name."); + this.ShortName = value; + return; + } + + this.ShortName = folders[folders.Length - 1]; + Array.Resize(ref folders, folders.Length - 1); + + this.ParentFoldersNames = folders; + } + } + + [XmlIgnore] + public string ShortName + { + get => this.shortName; + + set + { + this.shortName = value; this.RaisePropertyChanged(); } } + [XmlIgnore] + public string[] ParentFoldersNames + { + get; + private set; + } + [XmlAttribute] public OutputType OutputType { @@ -551,20 +591,25 @@ namespace FileConverter // Return error message if there is an error, else return empty or null string. switch (propertyName) { - case "Name": + case "ShortName": { - if (string.IsNullOrEmpty(this.Name)) + if (string.IsNullOrEmpty(this.ShortName)) { return "The preset name can't be empty."; } - if (this.Name.Contains(";")) + if (this.ShortName.Contains(";")) { return "The preset name can't contains the character ';'."; } + if (this.ShortName.Contains("/")) + { + return "The preset name can't contains the character '/'."; + } + ISettingsService settingsService = SimpleIoc.Default.GetInstance(); - int? count = settingsService.Settings?.ConversionPresets?.Count(match => match?.name == this.Name); + int? count = settingsService.Settings?.ConversionPresets?.Count(match => match?.shortName == this.ShortName); if (count > 1) { return "The preset name is already used."; diff --git a/Application/FileConverter/Services/SettingsService.cs b/Application/FileConverter/Services/SettingsService.cs index de4b0eb..c2d63e5 100644 --- a/Application/FileConverter/Services/SettingsService.cs +++ b/Application/FileConverter/Services/SettingsService.cs @@ -451,7 +451,7 @@ namespace FileConverter.Services registryEntries.Add(inputType, new List()); } - registryEntries[inputType].Add(conversionPreset.Name); + registryEntries[inputType].Add(conversionPreset.FullName); } } diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index 48a6b56..ca1fa1f 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -52,7 +52,7 @@ namespace FileConverter public ConversionPreset GetPresetFromName(string presetName) { - return this.conversionPresets.FirstOrDefault(match => match.Name == presetName); + return this.conversionPresets.FirstOrDefault(match => match.FullName == presetName); } public void Clean() @@ -73,7 +73,7 @@ namespace FileConverter for (int index = 0; index < settings.conversionPresets.Count; index++) { ConversionPreset conversionPreset = settings.conversionPresets[index]; - if (this.conversionPresets.Any(match => match.Name == conversionPreset.Name)) + if (this.conversionPresets.Any(match => match.FullName == conversionPreset.FullName)) { continue; } From d6336979467041bd2a5871c97a973095e967bc87 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Sat, 4 May 2019 12:05:20 +0200 Subject: [PATCH 03/24] Create ViewModel data structure for conversion presets. --- .../FileConverter/FileConverter.csproj | 1 + .../FileConverter/ViewModels/PresetFolder.cs | 105 ++++++++++++++++++ .../ViewModels/SettingsViewModel.cs | 28 ++++- 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 Application/FileConverter/ViewModels/PresetFolder.cs diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index b201df0..8202784 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -222,6 +222,7 @@ + diff --git a/Application/FileConverter/ViewModels/PresetFolder.cs b/Application/FileConverter/ViewModels/PresetFolder.cs new file mode 100644 index 0000000..3ca0ef1 --- /dev/null +++ b/Application/FileConverter/ViewModels/PresetFolder.cs @@ -0,0 +1,105 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +using System; +using System.Collections.ObjectModel; +using GalaSoft.MvvmLight; + +namespace FileConverter.ViewModels +{ + public class PresetFolder : ObservableObject + { + private string name; + private ObservableCollection children = new ObservableCollection(); + + public PresetFolder(string name, string[] parents) + { + this.Name = name; + this.ParentFoldersNames = parents; + } + +#if DEBUG + public PresetFolder() + { + } +#endif + + public string Name + { + get => this.name; + + set + { + this.name = value; + + this.RaisePropertyChanged(); + } + } + + public string[] ParentFoldersNames + { + get; + private set; + } + + public string[] FoldersNames + { + get + { + string[] folders; + if (this.ParentFoldersNames == null) + { + if (this.name == null) + { + // Root folder have no name. + return null; + } + + folders = new string[1]; + } + else + { + Diagnostics.Debug.Assert(this.name != null, "Preset folder name should not be null."); + + folders = new string[this.ParentFoldersNames.Length + 1]; + System.Array.Copy(this.ParentFoldersNames, folders, this.ParentFoldersNames.Length); + } + + folders[folders.Length - 1] = this.name; + return folders; + } + } + + public ObservableCollection Children + { + get => this.children; + + set + { + this.children = value; + this.RaisePropertyChanged(); + } + } + + public bool IsNodeInHierarchy(ObservableObject node, bool recurse) + { + Diagnostics.Debug.Assert(node != null, "node != null"); + foreach (ObservableObject child in this.Children) + { + if (child == node) + { + return true; + } + + if (recurse && child is PresetFolder subFolder) + { + if (subFolder.IsNodeInHierarchy(node, true)) + { + return true; + } + } + } + + return false; + } + } +} diff --git a/Application/FileConverter/ViewModels/SettingsViewModel.cs b/Application/FileConverter/ViewModels/SettingsViewModel.cs index a930e20..bc56916 100644 --- a/Application/FileConverter/ViewModels/SettingsViewModel.cs +++ b/Application/FileConverter/ViewModels/SettingsViewModel.cs @@ -35,6 +35,7 @@ namespace FileConverter.ViewModels public class SettingsViewModel : ViewModelBase { private InputExtensionCategory[] inputCategories; + private PresetFolder presetsRootFolder; private ConversionPreset selectedPreset; private Settings settings; private bool displaySeeChangeLogLink = true; @@ -91,6 +92,7 @@ namespace FileConverter.ViewModels this.SupportedCultures = Helpers.GetSupportedCultures().ToArray(); this.InitializeCompatibleInputExtensions(); + this.InitializePresetFolders(); } } @@ -349,7 +351,31 @@ namespace FileConverter.ViewModels this.inputCategories = categories.ToArray(); this.RaisePropertyChanged(nameof(this.InputCategories)); } - + + private void InitializePresetFolders() + { + this.presetsRootFolder = new PresetFolder(null, null); + foreach (ConversionPreset preset in this.Settings.ConversionPresets) + { + PresetFolder folder = this.presetsRootFolder; + foreach (string folderName in preset.ParentFoldersNames) + { + PresetFolder subFolder = folder.Children.FirstOrDefault(match => match is PresetFolder && ((PresetFolder)match).Name == folderName) as PresetFolder; + if (subFolder == null) + { + subFolder = new PresetFolder(folderName, folder.FoldersNames); + folder.Children.Add(subFolder); + } + + folder = subFolder; + } + + folder.Children.Add(preset); + } + + this.RaisePropertyChanged(nameof(this.PresetsRootFolder)); + } + private void CloseSettings(CancelEventArgs args) { ISettingsService settingsService = SimpleIoc.Default.GetInstance(); From 3f8fc70345bb8e94d4edff72712d99a90e0c508a Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Sat, 4 May 2019 12:07:34 +0200 Subject: [PATCH 04/24] Add sample data to test the view. --- .../FileConverter/FileConverter.csproj | 4 ++ .../SampleData/ConversionJobSampleData.xaml | 1 - .../SampleData/SettingsSampleData.xaml | 41 +++++++++++++++++++ .../FileConverter/Views/SettingsWindow.xaml | 6 ++- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 Application/FileConverter/SampleData/SettingsSampleData.xaml diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 8202784..08166f8 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -258,6 +258,10 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + Designer MSBuild:Compile diff --git a/Application/FileConverter/SampleData/ConversionJobSampleData.xaml b/Application/FileConverter/SampleData/ConversionJobSampleData.xaml index a18a557..2a5267e 100644 --- a/Application/FileConverter/SampleData/ConversionJobSampleData.xaml +++ b/Application/FileConverter/SampleData/ConversionJobSampleData.xaml @@ -3,7 +3,6 @@ ErrorMessage="There is no error for now" InputFilePath="C:\Path\To\AVery\Long\Location\WithAVeryNiceFile.png" OutputFilePath="C:\Path\To\AVery\Long\Location\WithAVeryNiceFile.jpg" - IsCancelable="False" StartTime="08/07/2018 22:10:55" State="InProgress" StateFlags="None" diff --git a/Application/FileConverter/SampleData/SettingsSampleData.xaml b/Application/FileConverter/SampleData/SettingsSampleData.xaml new file mode 100644 index 0000000..df3ae8e --- /dev/null +++ b/Application/FileConverter/SampleData/SettingsSampleData.xaml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Application/FileConverter/Views/SettingsWindow.xaml b/Application/FileConverter/Views/SettingsWindow.xaml index 9490b12..359b330 100644 --- a/Application/FileConverter/Views/SettingsWindow.xaml +++ b/Application/FileConverter/Views/SettingsWindow.xaml @@ -1,6 +1,8 @@  + mc:Ignorable="d" + DataContext="{Binding Settings, Source={StaticResource Locator}}" + d:DataContext="{d:DesignData /SampleData/SettingsSampleData.xaml}"> From 6536acdd43f14d8a8964613d84613e3374bab68b Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Sat, 4 May 2019 12:11:54 +0200 Subject: [PATCH 05/24] Add SelectedItem dependency property on TreeViews. --- .../FileConverter/FileConverter.csproj | 1 + .../Views/BindableSelectedItemBehavior.cs | 199 ++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 Application/FileConverter/Views/BindableSelectedItemBehavior.cs diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 08166f8..fd0163e 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -228,6 +228,7 @@ + HelpWindow.xaml diff --git a/Application/FileConverter/Views/BindableSelectedItemBehavior.cs b/Application/FileConverter/Views/BindableSelectedItemBehavior.cs new file mode 100644 index 0000000..19dc6fc --- /dev/null +++ b/Application/FileConverter/Views/BindableSelectedItemBehavior.cs @@ -0,0 +1,199 @@ +// Source: https://tyrrrz.me/Blog/WPF-TreeView-SelectedItem-TwoWay-binding + +using System.Collections.Specialized; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Interactivity; + +namespace FileConverter.Views +{ + public class TreeViewSelectionBehavior : Behavior + { + public delegate bool IsChildOfPredicate(object nodeA, object nodeB); + + public static readonly DependencyProperty SelectedItemProperty = + DependencyProperty.Register(nameof(SelectedItem), typeof(object), + typeof(TreeViewSelectionBehavior), + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + OnSelectedItemChanged)); + + public static readonly DependencyProperty HierarchyPredicateProperty = + DependencyProperty.Register(nameof(HierarchyPredicate), typeof(IsChildOfPredicate), + typeof(TreeViewSelectionBehavior), + new FrameworkPropertyMetadata(null)); + + public static readonly DependencyProperty ExpandSelectedProperty = + DependencyProperty.Register(nameof(ExpandSelected), typeof(bool), + typeof(TreeViewSelectionBehavior), + new FrameworkPropertyMetadata(false)); + + private readonly EventSetter treeViewItemEventSetter; + private bool modelHandled; + + public TreeViewSelectionBehavior() + { + this.treeViewItemEventSetter = new EventSetter(FrameworkElement.LoadedEvent, new RoutedEventHandler(this.OnTreeViewItemLoaded)); + } + + // Bindable selected item + public object SelectedItem + { + get => this.GetValue(SelectedItemProperty); + set => this.SetValue(SelectedItemProperty, value); + } + + // Predicate that checks if two items are hierarchically related + public IsChildOfPredicate HierarchyPredicate + { + get => (IsChildOfPredicate)this.GetValue(HierarchyPredicateProperty); + set => this.SetValue(HierarchyPredicateProperty, value); + } + + // Should expand selected? + public bool ExpandSelected + { + get => (bool)this.GetValue(ExpandSelectedProperty); + set => this.SetValue(ExpandSelectedProperty, value); + } + + protected override void OnAttached() + { + base.OnAttached(); + + this.AssociatedObject.SelectedItemChanged += this.OnTreeViewSelectedItemChanged; + ((INotifyCollectionChanged)this.AssociatedObject.Items).CollectionChanged += this.OnTreeViewItemsChanged; + + this.UpdateTreeViewItemStyle(); + this.modelHandled = true; + this.UpdateAllTreeViewItems(); + this.modelHandled = false; + } + + protected override void OnDetaching() + { + base.OnDetaching(); + + if (this.AssociatedObject != null) + { + this.AssociatedObject.ItemContainerStyle?.Setters?.Remove(this.treeViewItemEventSetter); + this.AssociatedObject.SelectedItemChanged -= this.OnTreeViewSelectedItemChanged; + ((INotifyCollectionChanged)this.AssociatedObject.Items).CollectionChanged -= this.OnTreeViewItemsChanged; + } + } + + private static void OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) + { + var behavior = (TreeViewSelectionBehavior)sender; + if (behavior.modelHandled) + { + return; + } + + if (behavior.AssociatedObject == null) + { + return; + } + + behavior.modelHandled = true; + behavior.UpdateAllTreeViewItems(); + behavior.modelHandled = false; + } + + // Update state of all items starting with given, with optional recursion + private void UpdateTreeViewItem(TreeViewItem item, bool recurse) + { + if (this.SelectedItem == null) + { + return; + } + + var model = item.DataContext; + + // If the selected item is this model and is not yet selected - select and return + if (this.SelectedItem == model && !item.IsSelected) + { + item.IsSelected = true; + } + // If the selected item is a parent of this model - expand + else + { + bool isParentOfModel = this.HierarchyPredicate?.Invoke(this.SelectedItem, model) ?? true; + if (isParentOfModel) + { + item.IsExpanded = true; + } + } + + if (item.IsSelected && this.ExpandSelected) + { + item.IsExpanded = true; + } + + // Recurse into children + if (recurse) + { + foreach (var subitem in item.Items) + { + if (item.ItemContainerGenerator.ContainerFromItem(subitem) is TreeViewItem tvi) + { + this.UpdateTreeViewItem(tvi, true); + } + } + } + } + + // Update state of all items + private void UpdateAllTreeViewItems() + { + var treeView = this.AssociatedObject; + foreach (var item in treeView.Items) + { + if (treeView.ItemContainerGenerator.ContainerFromItem(item) is TreeViewItem tvi) + { + this.UpdateTreeViewItem(tvi, true); + } + } + } + + // Inject Loaded event handler into ItemContainerStyle + private void UpdateTreeViewItemStyle() + { + if (this.AssociatedObject.ItemContainerStyle == null) + { + this.AssociatedObject.ItemContainerStyle = new Style( + typeof(TreeViewItem), + Application.Current.TryFindResource(typeof(TreeViewItem)) as Style); + } + + if (!this.AssociatedObject.ItemContainerStyle.Setters.Contains(this.treeViewItemEventSetter)) + { + this.AssociatedObject.ItemContainerStyle.Setters.Add(this.treeViewItemEventSetter); + } + } + + private void OnTreeViewItemsChanged(object sender, + NotifyCollectionChangedEventArgs args) + { + this.UpdateAllTreeViewItems(); + } + + private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs args) + { + if (this.modelHandled) + { + return; + } + if (this.AssociatedObject.Items.SourceCollection == null) + { + return; + } + + this.SelectedItem = args.NewValue; + } + + private void OnTreeViewItemLoaded(object sender, RoutedEventArgs args) + { + this.UpdateTreeViewItem((TreeViewItem)sender, false); + } + } +} \ No newline at end of file From b9756ea8e7c6288a2be6a524277f6cfa7a732233 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Sat, 4 May 2019 12:13:09 +0200 Subject: [PATCH 06/24] Add representation of the presets tree view with commands to edit it. --- .../FileConverter/FileConverter.csproj | 3 + .../Properties/Resources.Designer.cs | 36 +++ .../FileConverter/Properties/Resources.resx | 12 + .../FileConverter/Resources/FolderIcon.ico | Bin 0 -> 34494 bytes .../ViewModels/SettingsViewModel.cs | 233 +++++++++++++++--- .../FileConverter/Views/SettingsWindow.xaml | 65 ++++- .../Views/SettingsWindow.xaml.cs | 5 + 7 files changed, 302 insertions(+), 52 deletions(-) create mode 100644 Application/FileConverter/Resources/FolderIcon.ico diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index fd0163e..b0a834a 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -398,6 +398,9 @@ + + + copy /Y "$(SolutionDir)Middleware\ffmpeg\$(PlatformName)\ffmpeg.exe" "$(TargetDir)ffmpeg.exe" diff --git a/Application/FileConverter/Properties/Resources.Designer.cs b/Application/FileConverter/Properties/Resources.Designer.cs index a3b0a46..c882f45 100644 --- a/Application/FileConverter/Properties/Resources.Designer.cs +++ b/Application/FileConverter/Properties/Resources.Designer.cs @@ -303,6 +303,24 @@ namespace FileConverter.Properties { } } + /// + /// Looks up a localized string similar to Create a new folder.. + /// + public static string CreateANewFolder { + get { + return ResourceManager.GetString("CreateANewFolder", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to New folder. + /// + public static string DefaultFolderName { + get { + return ResourceManager.GetString("DefaultFolderName", resourceCulture); + } + } + /// /// Looks up a localized string similar to New preset. /// @@ -940,6 +958,24 @@ namespace FileConverter.Properties { } } + /// + /// Looks up a localized string similar to Folder. + /// + public static string PresetFolder { + get { + return ResourceManager.GetString("PresetFolder", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Folder Name. + /// + public static string PresetFolderName { + get { + return ResourceManager.GetString("PresetFolderName", resourceCulture); + } + } + /// /// Looks up a localized string similar to Preset Name. /// diff --git a/Application/FileConverter/Properties/Resources.resx b/Application/FileConverter/Properties/Resources.resx index 12f4b04..b5ac963 100644 --- a/Application/FileConverter/Properties/Resources.resx +++ b/Application/FileConverter/Properties/Resources.resx @@ -550,4 +550,16 @@ use maj for uppercase version Error during conversion job initialization. + + Create a new folder. + + + New folder + + + Folder + + + Folder Name + \ No newline at end of file diff --git a/Application/FileConverter/Resources/FolderIcon.ico b/Application/FileConverter/Resources/FolderIcon.ico new file mode 100644 index 0000000000000000000000000000000000000000..5f6e3a0d93863204427c8d6d04cfd9b23f3956e7 GIT binary patch literal 34494 zcmeHPO^aMr5UmCy>LO%kRx%qQ+2tRkF@xZQxDa(EI1rGCab~j9jT3{2tA?@=<{u&3#CR$ zEfr_+0yHjlJid-@(rdrm8lChxcn-AmnJ05|{>%aCNuPEU|L$C+lONt4nMchJx&L_n ze~wq^ue;aiAv{`ycXsHap5JxuqPnLs#_u!bt%}Oq(e(X`*W4}dS@~*npyj==%N_-r zT5f9`b%CvQYV%MRSj%gTqb{(uPHi6Q0&97#anuF2)~U@yU0^M*HIBN#);hI$s0*y+ zwZ>5w*jlGH4|RdHyw*7C0$c0U=AkaIme(3bUEsaz{jMXIt&6%q{(pL1YshN|SzliZ z=>96#20T~SlYJgPygdCc9h7<@U(3|i;P`iM(9^vNW^yM7_&RK>9OH8E=SY6t<`01R zN(T%FKHQ_{k8aVy7rXS^viHE&@vlm4UW3=t6gb{mx9>b&ORqnqZq1K<(Ch1*q5HZ1 zyaPk~t?SRPPNc_&?&tdR4h-$Lu0Oweo`3Dn(Eap(-hrY0*7fIC&;76c8M1F(f2`{> zY!}|v^%sBU9GU~`ey~T^t?N(unYUAWyZiloz1OY&bj?z;)7n?*cmC{b)oG2@KBHTF z*1a}!UUZ*8au{OmR?l+Y>JIRI3jVHWw;Ye}QALHH;d^LLK4St$8`WW|);H%L)Z}8K+(Vx8U=bk6HS_r-x ztz2)h$wB@8+t<6JlZX53{ayRI^)I^r$=~O)hRL2!4eb4*`_Ih((^qHD_^sQwRq8SC zF8BZC_vtIOuh2hF;`?~!{LvNqb0zMj2Dl%-P0#4}h#~ujJM`8g4S%<4c+CBfPW>rv z{;kHxJM{gk+=ujdS#8$v!8Yym)v&B?j;90ZKspf30sfuVxE#03f!}FS`P(A-N(a(` zbRZo_2hxFbV37ky`*iW>V08EXdz*3pP~MCA@g^NJch;~-qaM5e4n4QYx}^u)Zt;PA z({(nz&zkhPw)%YTH*vo&J3rHX(Ba=g#B&dCy6l07=N|fb4_JF%#*iBorB@mQBi8O+EH~wm!(<8kQWuDR;wLVH*{H=Wej;yQEIi?QFTL?rCG+17Zs&es~uH0 zbXl5Z40%yeYPH%?bwih>S;mkT6{S|I9aT4US(;@Gc~Mbnwc1g2Lzksl#*h~krB@mQBi8O+EH~wm!(<8kQWuDR;wLVH*{H=Wej;yQEIi?QFTL?rCG+17Zs&e zs~uH0bXl5Z40%yeYPH%?bwih>S;mkT6{S|I9aT4US(;@Gc~Mbnwc1g2Lzksl#`rlu z;L51>{;0e z`(S$Bd;AU*=U?I``tYFR-xjsbztlcdo1cf}XFB!zPr5(lRsIbOrJL(|)z04YIr+b8 r$KD%i4|qQB(2hN@ZVxs^`w%{CqIT&+w$p)hARR~t(t&g!mIMC+0Kna7 literal 0 HcmV?d00001 diff --git a/Application/FileConverter/ViewModels/SettingsViewModel.cs b/Application/FileConverter/ViewModels/SettingsViewModel.cs index bc56916..6721d1a 100644 --- a/Application/FileConverter/ViewModels/SettingsViewModel.cs +++ b/Application/FileConverter/ViewModels/SettingsViewModel.cs @@ -12,6 +12,7 @@ namespace FileConverter.ViewModels using System.Windows.Input; using FileConverter.Services; + using FileConverter.Views; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; @@ -19,6 +20,7 @@ namespace FileConverter.ViewModels using GalaSoft.MvvmLight.Messaging; using Microsoft.Win32; + using ImageMagick; /// /// This class contains properties that the main View can data bind to. @@ -36,12 +38,14 @@ namespace FileConverter.ViewModels { private InputExtensionCategory[] inputCategories; private PresetFolder presetsRootFolder; + private PresetFolder selectedFolder; private ConversionPreset selectedPreset; private Settings settings; private bool displaySeeChangeLogLink = true; private RelayCommand openUrlCommand; private RelayCommand getChangeLogContentCommand; + private RelayCommand createFolderCommand; private RelayCommand movePresetUpCommand; private RelayCommand movePresetDownCommand; private RelayCommand addNewPresetCommand; @@ -123,6 +127,67 @@ namespace FileConverter.ViewModels InputPostConversionAction.Delete, }; + public PresetFolder PresetsRootFolder + { + get => this.presetsRootFolder; + + set + { + this.presetsRootFolder = value; + this.RaisePropertyChanged(); + } + } + + public ObservableObject SelectedItem + { + get + { + if (this.SelectedFolder != null) + { + return this.SelectedFolder; + } + + return this.SelectedPreset; + } + + set + { + if (value is ConversionPreset preset) + { + this.SelectedPreset = preset; + this.SelectedFolder = null; + } + else if (value is PresetFolder folder) + { + this.SelectedFolder = folder; + this.SelectedPreset = null; + } + else + { + this.SelectedPreset = null; + this.SelectedFolder = null; + } + + this.RaisePropertyChanged(); + } + } + + public PresetFolder SelectedFolder + { + get => this.selectedFolder; + + set + { + this.selectedFolder = value; + + this.RaisePropertyChanged(); + this.RaisePropertyChanged(nameof(this.SelectedItem)); + this.movePresetUpCommand?.RaiseCanExecuteChanged(); + this.movePresetDownCommand?.RaiseCanExecuteChanged(); + this.removePresetCommand?.RaiseCanExecuteChanged(); + } + } + public ConversionPreset SelectedPreset { get => this.selectedPreset; @@ -142,9 +207,11 @@ namespace FileConverter.ViewModels } this.RaisePropertyChanged(); + this.RaisePropertyChanged(nameof(this.SelectedItem)); this.RaisePropertyChanged(nameof(this.InputCategories)); this.movePresetUpCommand?.RaiseCanExecuteChanged(); this.movePresetDownCommand?.RaiseCanExecuteChanged(); + this.removePresetCommand?.RaiseCanExecuteChanged(); } } @@ -220,6 +287,19 @@ namespace FileConverter.ViewModels } } + public ICommand CreateFolderCommand + { + get + { + if (this.createFolderCommand == null) + { + this.createFolderCommand = new RelayCommand(this.CreateFolder); + } + + return this.createFolderCommand; + } + } + public ICommand MovePresetUpCommand { get @@ -298,6 +378,19 @@ namespace FileConverter.ViewModels } } + public TreeViewSelectionBehavior.IsChildOfPredicate PresetsHierarchyPredicate => (object nodeA, object nodeB) => + { + if (nodeA is ConversionPreset) + { + return false; + } + + PresetFolder parentFolder = nodeA as PresetFolder; + Diagnostics.Debug.Assert(parentFolder != null, "Node should be a preset folder."); + + return parentFolder.IsNodeInHierarchy(nodeB as ObservableObject, true); + }; + private void SelectedPresetPropertyChanged(object sender, PropertyChangedEventArgs eventArgs) { if (eventArgs.PropertyName == "OutputType") @@ -399,91 +492,122 @@ namespace FileConverter.ViewModels INavigationService navigationService = SimpleIoc.Default.GetInstance(); navigationService.Close(Pages.Settings, false); } - - private bool CanMoveSelectedPresetUp() + + private void CreateFolder() { - ConversionPreset presetToMove = this.SelectedPreset; - if (presetToMove == null) + PresetFolder parent; + if (this.SelectedFolder != null) { - return false; + parent = this.SelectedFolder; + } + else + { + parent = this.GetSelectedItemParentFolder(); } - int indexOfSelectedPreset = this.settings.ConversionPresets.IndexOf(presetToMove); + int insertIndex = parent.Children.IndexOf(this.SelectedItem) + 1; + if (insertIndex < 0) + { + insertIndex = parent.Children.Count; + } + + // Generate a unique folder name. + string folderName = Properties.Resources.DefaultFolderName; + int index = 1; + while (parent.Children.Any(match => match is PresetFolder folder && folder.Name == folderName)) + { + index++; + folderName = $"{Properties.Resources.DefaultFolderName} ({index})"; + } + + PresetFolder newFolder = new PresetFolder(folderName, parent.FoldersNames); + + parent.Children.Insert(insertIndex, newFolder); + + this.SelectedItem = newFolder; + + Messenger.Default.Send("FolderName", "DoFocus"); + } + + private bool CanMoveSelectedPresetUp() + { + PresetFolder parent = this.GetSelectedItemParentFolder(); + + int indexOfSelectedPreset = parent.Children.IndexOf(this.SelectedItem); return indexOfSelectedPreset > 0; } private void MoveSelectedPresetUp() { - ConversionPreset presetToMove = this.SelectedPreset; - if (presetToMove == null) - { - return; - } + PresetFolder parent = this.GetSelectedItemParentFolder(); - int indexOfSelectedPreset = this.settings.ConversionPresets.IndexOf(presetToMove); + int indexOfSelectedPreset = parent.Children.IndexOf(this.SelectedItem); int newIndexOfSelectedPreset = System.Math.Max(0, indexOfSelectedPreset - 1); - this.settings.ConversionPresets.Move(indexOfSelectedPreset, newIndexOfSelectedPreset); + parent.Children.Move(indexOfSelectedPreset, newIndexOfSelectedPreset); this.movePresetUpCommand?.RaiseCanExecuteChanged(); this.movePresetDownCommand?.RaiseCanExecuteChanged(); } private bool CanMoveSelectedPresetDown() { - ConversionPreset presetToMove = this.SelectedPreset; - if (presetToMove == null) - { - return false; - } - - int indexOfSelectedPreset = this.settings.ConversionPresets.IndexOf(presetToMove); - return indexOfSelectedPreset < this.settings.ConversionPresets.Count - 1; + PresetFolder parent = this.GetSelectedItemParentFolder(); + int indexOfSelectedPreset = parent.Children.IndexOf(this.SelectedItem); + return indexOfSelectedPreset < parent.Children.Count - 1; } private void MoveSelectedPresetDown() { - ConversionPreset presetToMove = this.SelectedPreset; - if (presetToMove == null) - { - return; - } + PresetFolder parent = this.GetSelectedItemParentFolder(); + int indexOfSelectedPreset = parent.Children.IndexOf(this.SelectedItem); + int newIndexOfSelectedPreset = System.Math.Min(parent.Children.Count - 1, indexOfSelectedPreset + 1); - int indexOfSelectedPreset = this.settings.ConversionPresets.IndexOf(presetToMove); - int newIndexOfSelectedPreset = System.Math.Min(this.settings.ConversionPresets.Count - 1, indexOfSelectedPreset + 1); - - this.settings.ConversionPresets.Move(indexOfSelectedPreset, newIndexOfSelectedPreset); + parent.Children.Move(indexOfSelectedPreset, newIndexOfSelectedPreset); this.movePresetUpCommand?.RaiseCanExecuteChanged(); this.movePresetDownCommand?.RaiseCanExecuteChanged(); } private void AddNewPreset() { + PresetFolder parent; + if (this.SelectedFolder != null) + { + parent = this.SelectedFolder; + } + else + { + parent = this.GetSelectedItemParentFolder(); + } + + int insertIndex = parent.Children.IndexOf(this.SelectedItem) + 1; + if (insertIndex < 0) + { + insertIndex = parent.Children.Count; + } + // Generate a unique preset name. - ISettingsService settingsService = SimpleIoc.Default.GetInstance(); string presetName = Properties.Resources.DefaultPresetName; int index = 1; - while (settingsService.Settings.ConversionPresets.Any(match => match.Name == presetName)) + while (parent.Children.Any(match => match is ConversionPreset folder && folder.ShortName == presetName)) { index++; presetName = $"{Properties.Resources.DefaultPresetName} ({index})"; } - // Create preset by coping the selected one. - int insertIndex = 0; + // Create preset by copying the selected one. ConversionPreset newPreset = null; if (this.SelectedPreset != null) { newPreset = new ConversionPreset(presetName, this.SelectedPreset); - insertIndex = this.settings.ConversionPresets.IndexOf(this.SelectedPreset) + 1; } else { newPreset = new ConversionPreset(presetName, OutputType.Mkv, new string[0]); - insertIndex = this.settings.ConversionPresets.Count; } - settingsService.Settings.ConversionPresets.Insert(insertIndex, newPreset); - this.SelectedPreset = newPreset; + parent.Children.Insert(insertIndex, newPreset); + + this.SelectedItem = newPreset; Messenger.Default.Send("PresetName", "DoFocus"); @@ -492,15 +616,42 @@ namespace FileConverter.ViewModels private void RemoveSelectedPreset() { - ISettingsService settingsService = SimpleIoc.Default.GetInstance(); - settingsService.Settings.ConversionPresets.Remove(this.selectedPreset); + PresetFolder parent = this.GetSelectedItemParentFolder(); + parent.Children.Remove(this.SelectedItem); + + this.SelectedItem = null; this.removePresetCommand.RaiseCanExecuteChanged(); } private bool CanRemoveSelectedPreset() { - return this.SelectedPreset != null; + return this.SelectedItem != null; + } + + private PresetFolder GetSelectedItemParentFolder() + { + PresetFolder parent = this.PresetsRootFolder; + if (this.SelectedPreset != null) + { + foreach (string folderName in this.SelectedPreset.ParentFoldersNames) + { + parent = parent.Children.First(match => match is PresetFolder folder && folder.Name == folderName) as PresetFolder; + } + } + else if (this.SelectedFolder != null) + { + string[] parentFolderNames = this.SelectedFolder.ParentFoldersNames; + if (parentFolderNames != null) + { + foreach (string folderName in parentFolderNames) + { + parent = parent.Children.First(match => match is PresetFolder folder && folder.Name == folderName) as PresetFolder; + } + } + } + + return parent; } } } diff --git a/Application/FileConverter/Views/SettingsWindow.xaml b/Application/FileConverter/Views/SettingsWindow.xaml index 359b330..05a3f20 100644 --- a/Application/FileConverter/Views/SettingsWindow.xaml +++ b/Application/FileConverter/Views/SettingsWindow.xaml @@ -82,19 +82,42 @@ - - - - - + + + + + + + + - + - public class SettingsViewModel : ViewModelBase + public class SettingsViewModel : ViewModelBase, IDataErrorInfo { private InputExtensionCategory[] inputCategories; private PresetFolderNode presetsRootFolder; @@ -61,6 +61,16 @@ namespace FileConverter.ViewModels /// public SettingsViewModel() { + this.getChangeLogContentCommand = new RelayCommand(this.DownloadChangeLogAction); + this.openUrlCommand = new RelayCommand((url) => Process.Start(url)); + this.createFolderCommand = new RelayCommand(this.CreateFolder); + this.movePresetUpCommand = new RelayCommand(this.MoveSelectedPresetUp, this.CanMoveSelectedPresetUp); + this.movePresetDownCommand = new RelayCommand(this.MoveSelectedPresetDown, this.CanMoveSelectedPresetDown); + this.addNewPresetCommand = new RelayCommand(this.AddNewPreset); + this.removePresetCommand = new RelayCommand(this.RemoveSelectedPreset, this.CanRemoveSelectedPreset); + this.saveCommand = new RelayCommand(this.SaveSettings, this.CanSaveSettings); + this.closeCommand = new RelayCommand(this.CloseSettings); + if (this.IsInDesignMode) { // Code runs in Blend --> create design time data. @@ -260,122 +270,23 @@ namespace FileConverter.ViewModels } } - public ICommand GetChangeLogContentCommand - { - get - { - if (this.getChangeLogContentCommand == null) - { - this.getChangeLogContentCommand = new RelayCommand(this.DownloadChangeLogAction); - } + public ICommand GetChangeLogContentCommand => this.getChangeLogContentCommand; - return this.getChangeLogContentCommand; - } - } + public ICommand OpenUrlCommand => this.openUrlCommand; - public ICommand OpenUrlCommand - { - get - { - if (this.openUrlCommand == null) - { - this.openUrlCommand = new RelayCommand((url) => Process.Start(url)); - } + public ICommand CreateFolderCommand => this.createFolderCommand; - return this.openUrlCommand; - } - } + public ICommand MovePresetUpCommand => this.movePresetUpCommand; - public ICommand CreateFolderCommand - { - get - { - if (this.createFolderCommand == null) - { - this.createFolderCommand = new RelayCommand(this.CreateFolder); - } + public ICommand MovePresetDownCommand => this.movePresetDownCommand; - return this.createFolderCommand; - } - } + public ICommand AddNewPresetCommand => this.addNewPresetCommand; - public ICommand MovePresetUpCommand - { - get - { - if (this.movePresetUpCommand == null) - { - this.movePresetUpCommand = new RelayCommand(this.MoveSelectedPresetUp, this.CanMoveSelectedPresetUp); - } + public ICommand RemoveSelectedPresetCommand => this.removePresetCommand; - return this.movePresetUpCommand; - } - } + public ICommand SaveCommand => this.saveCommand; - public ICommand MovePresetDownCommand - { - get - { - if (this.movePresetDownCommand == null) - { - this.movePresetDownCommand = new RelayCommand(this.MoveSelectedPresetDown, this.CanMoveSelectedPresetDown); - } - - return this.movePresetDownCommand; - } - } - - public ICommand AddNewPresetCommand - { - get - { - if (this.addNewPresetCommand == null) - { - this.addNewPresetCommand = new RelayCommand(this.AddNewPreset); - } - - return this.addNewPresetCommand; - } - } - - public ICommand RemoveSelectedPresetCommand - { - get - { - if (this.removePresetCommand == null) - { - this.removePresetCommand = new RelayCommand(this.RemoveSelectedPreset, this.CanRemoveSelectedPreset); - } - - return this.removePresetCommand; - } - } - - public ICommand SaveCommand - { - get - { - if (this.saveCommand == null) - { - this.saveCommand = new RelayCommand(this.SaveSettings, this.CanSaveSettings); - } - - return this.saveCommand; - } - } - - public ICommand CloseCommand - { - get - { - if (this.closeCommand == null) - { - this.closeCommand = new RelayCommand(this.CloseSettings); - } - - return this.closeCommand; - } - } + public ICommand CloseCommand => this.closeCommand; public TreeViewSelectionBehavior.IsChildOfPredicate PresetsHierarchyPredicate => (object nodeA, object nodeB) => { @@ -390,6 +301,45 @@ namespace FileConverter.ViewModels return parentFolder.IsNodeInHierarchy(nodeB as AbstractTreeNode, true); }; + public string Error + { + get + { + string nodeError = this.CheckErrorRecursively(this.presetsRootFolder); + if (!string.IsNullOrEmpty(nodeError)) + { + return nodeError; + } + + return string.Empty; + } + } + + public string this[string columnName] => this.Error; + + private string CheckErrorRecursively(AbstractTreeNode node) + { + string nodeError = node.Error; + if (!string.IsNullOrEmpty(nodeError)) + { + return nodeError; + } + + if (node is PresetFolderNode folder) + { + foreach (AbstractTreeNode child in folder.Children) + { + nodeError = this.CheckErrorRecursively(child); + if (!string.IsNullOrEmpty(nodeError)) + { + return nodeError; + } + } + } + + return string.Empty; + } + private void SelectedPresetPropertyChanged(object sender, PropertyChangedEventArgs eventArgs) { if (eventArgs.PropertyName == "OutputType") @@ -400,6 +350,11 @@ namespace FileConverter.ViewModels this.saveCommand.RaiseCanExecuteChanged(); } + private void NodePropertyChanged(object sender, PropertyChangedEventArgs eventArgs) + { + this.saveCommand.RaiseCanExecuteChanged(); + } + private void DownloadChangeLogAction() { IUpgradeService upgradeService = SimpleIoc.Default.GetInstance(); @@ -457,6 +412,8 @@ namespace FileConverter.ViewModels { subFolder = new PresetFolderNode(folderName, parent); parent.Children.Add(subFolder); + + subFolder.PropertyChanged += this.NodePropertyChanged; } parent = subFolder; @@ -464,6 +421,8 @@ namespace FileConverter.ViewModels PresetNode presetNode = new PresetNode(preset, parent); parent.Children.Add(presetNode); + + presetNode.PropertyChanged += this.NodePropertyChanged; } this.RaisePropertyChanged(nameof(this.PresetsRootFolder)); @@ -505,7 +464,7 @@ namespace FileConverter.ViewModels private bool CanSaveSettings() { - return this.settings != null && string.IsNullOrEmpty(this.settings.Error); + return string.IsNullOrEmpty(this.Error); } private void SaveSettings() @@ -556,8 +515,12 @@ namespace FileConverter.ViewModels parent.Children.Insert(insertIndex, newFolder); + newFolder.PropertyChanged += this.NodePropertyChanged; + this.SelectedItem = newFolder; + this.saveCommand.RaiseCanExecuteChanged(); + Messenger.Default.Send("FolderName", "DoFocus"); } @@ -612,8 +575,9 @@ namespace FileConverter.ViewModels } this.SelectedItem = itemToMoveUp; - this.movePresetUpCommand?.RaiseCanExecuteChanged(); - this.movePresetDownCommand?.RaiseCanExecuteChanged(); + this.movePresetUpCommand.RaiseCanExecuteChanged(); + this.movePresetDownCommand.RaiseCanExecuteChanged(); + this.saveCommand.RaiseCanExecuteChanged(); } private bool CanMoveSelectedPresetDown() @@ -668,8 +632,9 @@ namespace FileConverter.ViewModels this.SelectedItem = itemToMoveDown; - this.movePresetUpCommand?.RaiseCanExecuteChanged(); - this.movePresetDownCommand?.RaiseCanExecuteChanged(); + this.movePresetUpCommand.RaiseCanExecuteChanged(); + this.movePresetDownCommand.RaiseCanExecuteChanged(); + this.saveCommand.RaiseCanExecuteChanged(); } private void AddNewPreset() @@ -718,25 +683,51 @@ namespace FileConverter.ViewModels parent.Children.Insert(insertIndex, node); + node.PropertyChanged += this.NodePropertyChanged; + this.SelectedItem = node; Messenger.Default.Send("PresetName", "DoFocus"); this.removePresetCommand.RaiseCanExecuteChanged(); + this.saveCommand.RaiseCanExecuteChanged(); } private void RemoveSelectedPreset() { + this.SelectedItem.PropertyChanged -= this.NodePropertyChanged; + this.SelectedItem.Parent.Children.Remove(this.SelectedItem); this.SelectedItem = null; this.removePresetCommand.RaiseCanExecuteChanged(); + this.saveCommand.RaiseCanExecuteChanged(); } private bool CanRemoveSelectedPreset() { return this.SelectedItem != null; } + + public override void Cleanup() + { + base.Cleanup(); + + this.UnbindNode(this.presetsRootFolder); + } + + private void UnbindNode(AbstractTreeNode node) + { + node.PropertyChanged -= this.NodePropertyChanged; + + if (node is PresetFolderNode folder) + { + foreach (AbstractTreeNode child in folder.Children) + { + this.UnbindNode(child); + } + } + } } } diff --git a/Application/FileConverter/Views/SettingsWindow.xaml b/Application/FileConverter/Views/SettingsWindow.xaml index 62f4149..a26cd70 100644 --- a/Application/FileConverter/Views/SettingsWindow.xaml +++ b/Application/FileConverter/Views/SettingsWindow.xaml @@ -274,8 +274,9 @@