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 {
get;
set;
}
public Tree() {
Selected = new List<object>();
PreviewMouseDown += OnPreviewMouseDown;
}
} = new List<object>();
public TreeItem FindItem(DependencyObject elem) {
if (elem == null) return null;
@ -102,21 +97,18 @@ namespace SourceGit.Views.Controls {
}
}
private TreeItem FindItemByDataContext(ItemsControl control, object data) {
if (control == null) return null;
protected override void OnPreviewKeyDown(KeyEventArgs e) {
base.OnPreviewKeyDown(e);
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;
if (MultiSelection && e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control) {
SelectAll();
e.Handled = true;
}
}
return null;
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e) {
base.OnPreviewMouseDown(e);
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) {
var hit = VisualTreeHelper.HitTest(this, e.GetPosition(this));
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) {
if (removeOthers && Selected.Count > 0) {
UnselectAllChildren(this);

View file

@ -25,22 +25,8 @@
MultiSelection="True"
ItemsSource="{Binding ElementName=me, Path=Nodes}"
SelectionChanged="OnTreeSelectionChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
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>
<Style TargetType="{x:Type controls:TreeItem}" BasedOn="{StaticResource Style.TreeItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
@ -95,6 +81,7 @@
SelectionUnit="FullRow"
SelectionChanged="OnListSelectionChanged"
SizeChanged="OnListSizeChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
ItemsSource="{Binding ElementName=me, Path=Changes}"
RowStyle="{StaticResource Style.DataGridRow.Change}"
Visibility="Collapsed">
@ -123,6 +110,7 @@
SelectionUnit="FullRow"
SelectionChanged="OnGridSelectionChanged"
SizeChanged="OnGridSizeChanged"
PreviewKeyDown="OnChangePreviewKeyDown"
ItemsSource="{Binding ElementName=me, Path=Changes}"
RowStyle="{StaticResource Style.DataGridRow.Change}"
Visibility="Collapsed">

View file

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