summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr55 <[email protected]>2020-06-03 18:25:20 +0100
committersr55 <[email protected]>2020-06-03 18:25:30 +0100
commitcf8281966e0ff934b9c5728f82afe6ab08f61b28 (patch)
tree5fc2269f3cde96b21e52fbc7f91de890e57859c6
parent769b48378969aa7cd927873804ab0ad4cb79e06e (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.cs93
-rw-r--r--win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs7
-rw-r--r--win/CS/HandBrakeWPF/Views/MainView.xaml2
-rw-r--r--win/CS/HandBrakeWPF/Views/MainView.xaml.cs34
4 files changed, 31 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 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 @@
<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;
+ }
+ }
+ }
}
}