refactor<WorkingCopyChanges>: use PreviewKeyDown instead of RoutedUICommand for staging/unstaging hot keys

This commit is contained in:
leo 2021-07-14 14:54:44 +08:00
parent 208af69ea1
commit 62c182f5ca
3 changed files with 35 additions and 41 deletions

View file

@ -48,12 +48,7 @@ namespace SourceGit.Views.Controls {
public List<object> Selected { public List<object> Selected {
get; get;
set; set;
} } = new List<object>();
public Tree() {
Selected = new List<object>();
PreviewMouseDown += OnPreviewMouseDown;
}
public TreeItem FindItem(DependencyObject elem) { public TreeItem FindItem(DependencyObject elem) {
if (elem == null) return null; if (elem == null) return null;
@ -102,21 +97,18 @@ namespace SourceGit.Views.Controls {
} }
} }
private TreeItem FindItemByDataContext(ItemsControl control, object data) { protected override void OnPreviewKeyDown(KeyEventArgs e) {
if (control == null) return null; base.OnPreviewKeyDown(e);
for (int i = 0; i < control.Items.Count; i++) { if (MultiSelection && e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control) {
var child = control.ItemContainerGenerator.ContainerFromIndex(i) as TreeItem; SelectAll();
if (control.Items[i] == data) return child; e.Handled = true;
var found = FindItemByDataContext(child, data);
if (found != null) return found;
} }
return null;
} }
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { protected override void OnPreviewMouseDown(MouseButtonEventArgs e) {
base.OnPreviewMouseDown(e);
var hit = VisualTreeHelper.HitTest(this, e.GetPosition(this)); var hit = VisualTreeHelper.HitTest(this, e.GetPosition(this));
if (hit == null || hit.VisualHit == null) return; if (hit == null || hit.VisualHit == null) return;
@ -157,6 +149,20 @@ namespace SourceGit.Views.Controls {
} }
} }
private TreeItem FindItemByDataContext(ItemsControl control, object data) {
if (control == null) return null;
for (int i = 0; i < control.Items.Count; i++) {
var child = control.ItemContainerGenerator.ContainerFromIndex(i) as TreeItem;
if (control.Items[i] == data) return child;
var found = FindItemByDataContext(child, data);
if (found != null) return found;
}
return null;
}
private void AddSelected(TreeItem item, bool removeOthers) { private void AddSelected(TreeItem item, bool removeOthers) {
if (removeOthers && Selected.Count > 0) { if (removeOthers && Selected.Count > 0) {
UnselectAllChildren(this); UnselectAllChildren(this);

View file

@ -25,22 +25,8 @@
MultiSelection="True" MultiSelection="True"
ItemsSource="{Binding ElementName=me, Path=Nodes}" ItemsSource="{Binding ElementName=me, Path=Nodes}"
SelectionChanged="OnTreeSelectionChanged" SelectionChanged="OnTreeSelectionChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
Visibility="Visible"> Visibility="Visible">
<TreeView.Resources>
<RoutedUICommand x:Key="SelectWholeTreeCommand" Text="SelectWholeTree"/>
<RoutedUICommand x:Key="StageChangeCommand" Text="StageChange"/>
</TreeView.Resources>
<TreeView.InputBindings>
<KeyBinding Key="A" Modifiers="Ctrl" Command="{StaticResource SelectWholeTreeCommand}"/>
<KeyBinding Key="Space" Command="{StaticResource StageChangeCommand}"/>
</TreeView.InputBindings>
<TreeView.CommandBindings>
<CommandBinding Command="{StaticResource SelectWholeTreeCommand}" Executed="SelectWholeTree"/>
<CommandBinding Command="{StaticResource StageChangeCommand}" Executed="StageChange"/>
</TreeView.CommandBindings>
<controls:Tree.ItemContainerStyle> <controls:Tree.ItemContainerStyle>
<Style TargetType="{x:Type controls:TreeItem}" BasedOn="{StaticResource Style.TreeItem}"> <Style TargetType="{x:Type controls:TreeItem}" BasedOn="{StaticResource Style.TreeItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
@ -95,6 +81,7 @@
SelectionUnit="FullRow" SelectionUnit="FullRow"
SelectionChanged="OnListSelectionChanged" SelectionChanged="OnListSelectionChanged"
SizeChanged="OnListSizeChanged" SizeChanged="OnListSizeChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
ItemsSource="{Binding ElementName=me, Path=Changes}" ItemsSource="{Binding ElementName=me, Path=Changes}"
RowStyle="{StaticResource Style.DataGridRow.Change}" RowStyle="{StaticResource Style.DataGridRow.Change}"
Visibility="Collapsed"> Visibility="Collapsed">
@ -123,6 +110,7 @@
SelectionUnit="FullRow" SelectionUnit="FullRow"
SelectionChanged="OnGridSelectionChanged" SelectionChanged="OnGridSelectionChanged"
SizeChanged="OnGridSizeChanged" SizeChanged="OnGridSizeChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
ItemsSource="{Binding ElementName=me, Path=Changes}" ItemsSource="{Binding ElementName=me, Path=Changes}"
RowStyle="{StaticResource Style.DataGridRow.Change}" RowStyle="{StaticResource Style.DataGridRow.Change}"
Visibility="Collapsed"> Visibility="Collapsed">

View file

@ -686,15 +686,15 @@ namespace SourceGit.Views.Widgets {
#endregion #endregion
#region EVENTS #region EVENTS
private void SelectWholeTree(object sender, ExecutedRoutedEventArgs e) { private void OnChangePreviewKeyDown(object sender, KeyEventArgs e) {
modeTree.SelectAll(); if (e.Key == Key.Space && Keyboard.Modifiers == ModifierKeys.None) {
} if (!IsUnstaged) {
UnstageSelected();
} else {
StageSelected();
}
private void StageChange(object sender, ExecutedRoutedEventArgs e) { e.Handled = true;
if (!IsUnstaged) {
UnstageSelected();
} else {
StageSelected();
} }
} }