Skip to content

Commit b869823

Browse files
committed
Usability
1 parent f278c8b commit b869823

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

CSharpCodeAnalyst/Areas/TreeArea/TreeControl.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:local="clr-namespace:CSharpCodeAnalyst.Areas.TreeArea"
77
xmlns:resources="clr-namespace:CSharpCodeAnalyst.Resources"
88
xmlns:dd="urn:gong-wpf-dragdrop"
9+
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
910
d:DataContext="{d:DesignInstance local:TreeViewModel}"
1011
mc:Ignorable="d"
1112
d:DesignHeight="300" d:DesignWidth="300">
@@ -64,6 +65,13 @@
6465
Margin="5"
6566
dd:DragDrop.IsDragSource="True">
6667

68+
<b:Interaction.Triggers>
69+
<b:EventTrigger EventName="SelectedItemChanged">
70+
<b:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"
71+
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=TreeView}}" />
72+
</b:EventTrigger>
73+
</b:Interaction.Triggers>
74+
6775
<TreeView.ContextMenu>
6876
<ContextMenu>
6977
<MenuItem Header="{x:Static resources:Strings.AddToGraph_MenuItem}"

CSharpCodeAnalyst/Areas/TreeArea/TreeViewModel.cs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TreeViewModel : INotifyPropertyChanged
1818
private readonly MessageBus _messaging;
1919
private readonly RefactoringService _refactoringService;
2020
private CodeGraph? _codeGraph;
21+
private string? _lastSelectedCodeElement;
2122
private string _searchText;
2223
private ObservableCollection<TreeItemViewModel> _treeItems;
2324

@@ -41,30 +42,11 @@ public TreeViewModel(MessageBus messaging, RefactoringService refactoringService
4142

4243
SetMovementTargetCommand = new WpfCommand<TreeItemViewModel>(RefactoringSetMovementTarget, RefactoringCanSetMovementTarget);
4344
MoveCommand = new WpfCommand<TreeItemViewModel>(RefactoringMoveCodeElement, RefactoringCanMoveCodeElement);
45+
SelectedItemChangedCommand = new WpfCommand<TreeItemViewModel>(OnSelectedItemChanged);
4446

4547
_treeItems = [];
4648
}
4749

48-
private bool RefactoringCanMoveCodeElement(TreeItemViewModel tvm)
49-
{
50-
return _refactoringService.CanMoveCodeElement(tvm?.CodeElement?.Id);
51-
}
52-
53-
private void RefactoringMoveCodeElement(TreeItemViewModel? tvm)
54-
{
55-
_refactoringService.MoveCodeElement(tvm?.CodeElement?.Id);
56-
}
57-
58-
private bool RefactoringCanSetMovementTarget(TreeItemViewModel tvm)
59-
{
60-
return _refactoringService.CanSetMovementTarget(tvm?.CodeElement?.Id);
61-
}
62-
63-
private void RefactoringSetMovementTarget(TreeItemViewModel tvm)
64-
{
65-
_refactoringService.SetMovementTarget(tvm?.CodeElement?.Id);
66-
}
67-
6850
public ICommand CollapseTreeCommand { get; }
6951

7052
public ICommand ClearSearchCommand { get; }
@@ -79,7 +61,7 @@ public ObservableCollection<TreeItemViewModel> TreeItems
7961
OnPropertyChanged(nameof(TreeItems));
8062
}
8163
}
82-
64+
8365
public string SearchText
8466
{
8567
get => _searchText;
@@ -97,12 +79,42 @@ public string SearchText
9779
public ICommand PartitionWithBaseTreeCommand { get; private set; }
9880
public ICommand CopyToClipboardCommand { get; private set; }
9981
public ICommand CreateCodeElementCommand { get; private set; }
100-
82+
10183
public ICommand SetMovementTargetCommand { get; private set; }
10284
public ICommand MoveCommand { get; private set; }
10385

86+
public ICommand SelectedItemChangedCommand { get; }
87+
10488
public event PropertyChangedEventHandler? PropertyChanged;
10589

90+
private void OnSelectedItemChanged(TreeItemViewModel item)
91+
{
92+
if (item?.CodeElement != null)
93+
{
94+
_lastSelectedCodeElement = item.CodeElement.Id;
95+
}
96+
}
97+
98+
private bool RefactoringCanMoveCodeElement(TreeItemViewModel tvm)
99+
{
100+
return _refactoringService.CanMoveCodeElement(tvm?.CodeElement?.Id);
101+
}
102+
103+
private void RefactoringMoveCodeElement(TreeItemViewModel? tvm)
104+
{
105+
_refactoringService.MoveCodeElement(tvm?.CodeElement?.Id);
106+
}
107+
108+
private bool RefactoringCanSetMovementTarget(TreeItemViewModel tvm)
109+
{
110+
return _refactoringService.CanSetMovementTarget(tvm?.CodeElement?.Id);
111+
}
112+
113+
private void RefactoringSetMovementTarget(TreeItemViewModel tvm)
114+
{
115+
_refactoringService.SetMovementTarget(tvm?.CodeElement?.Id);
116+
}
117+
106118

107119

108120
private static void OnCopyToClipboard(TreeItemViewModel vm)
@@ -161,12 +173,12 @@ private void AddNodeToGraph(TreeItemViewModel? item)
161173
_messaging.Publish(new AddNodeToGraphRequest(item.CodeElement));
162174
}
163175
}
164-
176+
165177

166178
public void HandleCodeGraphRefactored(CodeGraphRefactored message)
167179
{
168180
// Note: Graph is actually the same
169-
181+
170182
// Tree updates are slow, so do the update manually.
171183
if (message is CodeElementCreated created)
172184
{
@@ -192,7 +204,7 @@ public void HandleCodeGraphRefactored(CodeGraphRefactored message)
192204
private void RefactoringCodeElementDeleted(string deletedElementId, string? parentId, HashSet<string> deletedIds)
193205
{
194206
// Refresh the tree to show the new element
195-
207+
196208
// Delete from tree.
197209
if (!CodeElementIdToViewModel.TryGetValue(deletedElementId, out _))
198210
{
@@ -247,7 +259,7 @@ private void RefactoringCodeElementAdded(CodeElementCreated created)
247259
}
248260
}
249261
}
250-
262+
251263
/// <summary>
252264
/// Creates a code element at the root level (e.g., Assembly).
253265
/// Public method to be called from UI when right-clicking empty space.
@@ -256,7 +268,7 @@ public void RefactoringCreateCodeElementAtRoot()
256268
{
257269
RefactoringCreateCodeElement(null);
258270
}
259-
271+
260272
private bool RefactoringCanCreateCodeElement(TreeItemViewModel? tvm)
261273
{
262274
// null tvm means root level (empty space in tree) - this is allowed
@@ -356,6 +368,14 @@ public void ExecuteSearch()
356368
else if (SearchText.Trim() == "!")
357369
{
358370
ResetVisibility(TreeItems, true);
371+
if (!string.IsNullOrEmpty(_lastSelectedCodeElement))
372+
{
373+
// Since I want to keep the highlighting, I likely want to keep the location.
374+
if ((bool)_codeGraph?.Nodes.ContainsKey(_lastSelectedCodeElement))
375+
{
376+
_messaging.Publish(new LocateInTreeRequest(_lastSelectedCodeElement));
377+
}
378+
}
359379
}
360380
else
361381
{
@@ -422,11 +442,9 @@ private bool SearchAndExpandNodes(IEnumerable<TreeItemViewModel> items, IExpress
422442
return anyMatch;
423443
}
424444

425-
public string? GetRefactoringNewMoveParent()
445+
public string GetRefactoringNewMoveParent()
426446
{
427447
var target = _refactoringService.GetMovementTarget();
428448
return target?.Name != null ? target.Name : string.Empty;
429449
}
430-
431-
432450
}

0 commit comments

Comments
 (0)