From cf8281966e0ff934b9c5728f82afe6ab08f61b28 Mon Sep 17 00:00:00 2001 From: sr55 Date: Wed, 3 Jun 2020 18:25:20 +0100 Subject: WinGui: Fix an issue where the treeview control can, when categories are closed, trigger a series of events that cause the preset to be de-selected. Fix an issue where removing a preset can leave none selected. #2875 --- win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs | 93 ------------------------- win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 7 +- win/CS/HandBrakeWPF/Views/MainView.xaml | 2 +- win/CS/HandBrakeWPF/Views/MainView.xaml.cs | 34 ++++++--- 4 files changed, 31 insertions(+), 105 deletions(-) delete mode 100644 win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs diff --git a/win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs b/win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs deleted file mode 100644 index 4ca799665..000000000 --- a/win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs +++ /dev/null @@ -1,93 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// Helper functions for handling structures -// Based of https://social.technet.microsoft.com/wiki/contents/articles/18188.wpf-treeview-selecteditem-twoway-mvvm-plus-expand-to-selected-and-close-all-others.aspx -// -// -------------------------------------------------------------------------------------------------------------------- -namespace HandBrakeWPF.Helpers -{ - using System.Windows; - using System.Windows.Controls; - - public class TreeViewHelper - { - public static object GetTreeViewSelectedItem(DependencyObject obj) - { - return (object)obj.GetValue(TreeViewSelectedItemProperty); - } - - public static void SetTreeViewSelectedItem(DependencyObject obj, object value) - { - obj.SetValue(TreeViewSelectedItemProperty, value); - } - - public static readonly DependencyProperty TreeViewSelectedItemProperty = - DependencyProperty.RegisterAttached("TreeViewSelectedItem", typeof(object), typeof(TreeViewHelper), new PropertyMetadata(new object(), TreeViewSelectedItemChanged)); - - private static void TreeViewSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) - { - TreeView treeView = sender as TreeView; - if (treeView == null) - { - return; - } - - treeView.SelectedItemChanged -= TreeView_SelectedItemChanged; - treeView.SelectedItemChanged += TreeView_SelectedItemChanged; - - TreeViewItem thisItem = treeView.ItemContainerGenerator.ContainerFromItem(e.NewValue) as TreeViewItem; - if (thisItem != null) - { - thisItem.IsSelected = true; - return; - } - - for (int i = 0; i < treeView.Items.Count; i++) - { - if (SelectItem(e.NewValue, treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem)) - { - return; // Break out the loop. We've found the item and expanded it's parent if necessary. - } - } - } - - private static void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) - { - TreeView treeView = sender as TreeView; - SetTreeViewSelectedItem(treeView, e.NewValue); - } - - private static bool SelectItem(object o, TreeViewItem parentItem) - { - if (parentItem == null) - { - return false; - } - - bool found = false; - foreach (var item in parentItem.Items) - { - if (item.Equals(o)) - { - found = true; - break; - } - } - - if (found) - { - bool isExpanded = parentItem.IsExpanded; - if (!isExpanded) - { - parentItem.IsExpanded = true; - parentItem.UpdateLayout(); - } - } - - return found; - } - } -} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index 68fe8672f..f0b005625 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -315,6 +315,11 @@ namespace HandBrakeWPF.ViewModels { if (!object.Equals(this.selectedPreset, value)) { + if (value == null) + { + this.errorService.ShowError("Null Preset", null, Environment.StackTrace.ToString()); + } + if (value != null) { this.PresetSelect(value); @@ -1732,7 +1737,7 @@ namespace HandBrakeWPF.ViewModels this.presetService.Remove(this.selectedPreset); this.NotifyOfPropertyChange(() => this.CategoryPresets); - this.SelectedPreset = this.CategoryPresets.FirstOrDefault(); + this.SelectedPreset = this.presetService.DefaultPreset; } else { diff --git a/win/CS/HandBrakeWPF/Views/MainView.xaml b/win/CS/HandBrakeWPF/Views/MainView.xaml index a57759a2a..20afae2a8 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml @@ -641,7 +641,7 @@ diff --git a/win/CS/HandBrakeWPF/Views/MainView.xaml.cs b/win/CS/HandBrakeWPF/Views/MainView.xaml.cs index cdcbc65ef..b436bcca3 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml.cs +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml.cs @@ -14,6 +14,7 @@ namespace HandBrakeWPF.Views using System.Windows.Input; using System.Windows.Media; + using HandBrakeWPF.Services.Presets.Model; using HandBrakeWPF.ViewModels; using HandBrakeWPF.ViewModels.Interfaces; @@ -112,16 +113,6 @@ namespace HandBrakeWPF.Views } } } - - private void PresetTreeviewItemCollasped(object sender, RoutedEventArgs e) - { - if (e.Source.GetType() == typeof(TreeViewItem)) - { - TreeViewItem item = e.Source as TreeViewItem; - if (item != null) item.IsSelected = false; - } - } - private void PresetListTree_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject); @@ -140,5 +131,28 @@ namespace HandBrakeWPF.Views return source as TreeViewItem; } + + private void PresetTreeviewItemCollasped(object sender, RoutedEventArgs e) + { + if (e.Source.GetType() == typeof(TreeViewItem)) + { + TreeViewItem item = e.Source as TreeViewItem; + if (item != null && item.GetType() == typeof(PresetDisplayCategory)) + { + item.IsSelected = false; + } + } + } + + private void PresetListTree_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) + { + if (e.Source.GetType() == typeof(TreeView)) + { + if (e.NewValue != null && e.NewValue.GetType() == typeof(Preset)) + { + ((MainViewModel)this.DataContext).SelectedPreset = (Preset)e.NewValue; + } + } + } } } -- cgit v1.2.3