diff options
author | sr55 <[email protected]> | 2020-06-03 18:25:20 +0100 |
---|---|---|
committer | sr55 <[email protected]> | 2020-06-05 17:28:59 +0100 |
commit | 08fe19bc6e10919179791720171b79d905ceaae2 (patch) | |
tree | 567c68568771df77f82743d2f49b40a121a98763 | |
parent | 9527541672654db9921352d8a5621d705450a2b6 (diff) |
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
-rw-r--r-- | win/CS/HandBrakeWPF/Helpers/TreeViewHelper.cs | 93 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 2 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Views/MainView.xaml | 2 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Views/MainView.xaml.cs | 34 |
4 files changed, 26 insertions, 105 deletions
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 @@ -// -------------------------------------------------------------------------------------------------------------------- -// <copyright file="TreeViewHelper.cs" company="HandBrake Project (http://handbrake.fr)"> -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// </copyright> -// <summary> -// Helper functions for handling <see cref="TimeSpan" /> 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 -// </summary> -// -------------------------------------------------------------------------------------------------------------------- -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<object> 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 989ddd7b6..347d14bf5 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -1988,7 +1988,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 6cd729555..39588c912 100644 --- a/win/CS/HandBrakeWPF/Views/MainView.xaml +++ b/win/CS/HandBrakeWPF/Views/MainView.xaml @@ -654,7 +654,7 @@ <TreeView x:Name="presetListTree" HorizontalAlignment="Stretch" AutomationProperties.Name="Presets List" ToolTip="{x:Static Properties:ResourcesTooltips.MainView_Presets}"
VerticalAlignment="Stretch" BorderThickness="0,0,0,1" BorderBrush="LightGray"
ItemsSource="{Binding PresetsCategories}" MaxWidth="265"
- helpers:TreeViewHelper.TreeViewSelectedItem="{Binding Path=SelectedPreset, Mode=TwoWay}"
+ SelectedItemChanged="PresetListTree_OnSelectedItemChanged"
PreviewMouseRightButtonDown="PresetListTree_OnPreviewMouseRightButtonDown">
<TreeView.ItemContainerStyle>
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<object> e)
+ {
+ if (e.Source.GetType() == typeof(TreeView))
+ {
+ if (e.NewValue != null && e.NewValue.GetType() == typeof(Preset))
+ {
+ ((MainViewModel)this.DataContext).SelectedPreset = (Preset)e.NewValue;
+ }
+ }
+ }
}
}
|