forked from parnic/VSQuickOpenFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListFiles.xaml.cs
245 lines (220 loc) · 8.47 KB
/
ListFiles.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using EnvDTE;
namespace PerniciousGames.OpenFileInSolution
{
/// <summary>
/// Interaction logic for ListFiles.xaml
/// </summary>
public partial class ListFiles : System.Windows.Window
{
public ObservableCollection<ProjectItemWrapper> items { get; set; }
private CollectionViewSource viewSource;
public bool bSearchFullPath { get; set; } // todo: config-ize me
private string[] filterStrings;
public static string FilterText { get; set; }
// todo: save static list of last-entered strings. restore most recent one when opening.
public ListFiles(IEnumerable<ProjectItemWrapper> inItems)
{
items = new ObservableCollection<ProjectItemWrapper>(inItems);
viewSource = new CollectionViewSource();
viewSource.Source = items;
InitializeComponent();
viewSource.Filter += FilterProjectItems;
lstFiles.ItemsSource = viewSource.View;
txtFilter.Focus();
txtFilter.SelectAll();
if (!string.IsNullOrEmpty(FilterText))
{
txtFilterChanged(txtFilter, TextChangedEventArgs.Empty as TextChangedEventArgs);
}
}
private void FilterProjectItems(object sender, FilterEventArgs e)
{
e.Accepted = true;
if (!string.IsNullOrEmpty(FilterText))
{
foreach (var filter in filterStrings)
{
var searchStr = (e.Item as ProjectItemWrapper).Filename.ToLower();
if (!bSearchFullPath)
{
searchStr = Path.GetFileName(searchStr);
}
if (!string.IsNullOrWhiteSpace(filter) && !searchStr.Contains(filter.ToLower()))
{
e.Accepted = false;
break;
}
}
}
}
private string GetSelectedFilename()
{
var selectedFile = (lstFiles.SelectedItem as ProjectItemWrapper);
var selectedFilename = string.Empty;
if (selectedFile != null)
{
selectedFilename = selectedFile.Filename;
if (!bSearchFullPath)
{
selectedFilename = Path.GetFileName(selectedFilename);
}
}
return selectedFilename;
}
private void txtFilterChanged(object sender, TextChangedEventArgs e)
{
filterStrings = (sender as TextBox).Text.Split(' ');
viewSource.View.Refresh();
if (lstFiles.Items.Count > 0)
{
if (filterStrings.Length > 0)
{
var selectedFilename = GetSelectedFilename();
foreach (var fileItem in lstFiles.Items)
{
var file = (fileItem as ProjectItemWrapper).Filename;
if (!bSearchFullPath)
{
file = Path.GetFileName(file.ToString());
}
if (string.IsNullOrEmpty(selectedFilename) || file.Length < selectedFilename.Length)
{
lstFiles.SelectedItem = fileItem;
selectedFilename = file;
}
}
}
if (lstFiles.SelectedIndex == -1)
{
lstFiles.SelectedIndex = 0;
}
}
}
private void OpenSelectedFiles(bool bInSolutionExplorer)
{
foreach (var item in lstFiles.SelectedItems)
{
if (!bInSolutionExplorer)
{
var w = (item as ProjectItemWrapper).ProjItem.Open();
w.Visible = true;
}
else
{
var projItem = (item as ProjectItemWrapper).ProjItem;
var ide = OpenFileInSolutionPackage.GetActiveIDE();
ide.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate();
projItem.ExpandView();
//((Microsoft.VisualStudio.PlatformUI.UIHierarchyMarshaler)ide.ActiveWindow.Object).GetItem(projItem.Name).Select(vsUISelectionType.vsUISelectionTypeSelect);
break;
}
}
Close();
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (lstFiles.Items.Count > 0)
{
if (e.Key == Key.Down)
{
e.Handled = true;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
var lastSelectedIndex = -1;
if (lstFiles.SelectedItems.Count > 0)
{
lastSelectedIndex = lstFiles.SelectedItems.Cast<object>().Max(x => lstFiles.Items.IndexOf(x));
}
if (lstFiles.Items.Count > lastSelectedIndex + 1 && lastSelectedIndex >= -1)
{
lstFiles.SelectedItems.Add(lstFiles.Items[lastSelectedIndex + 1]);
}
}
else
{
if (lstFiles.SelectedIndex == lstFiles.Items.Count - 1)
{
lstFiles.SelectedIndex = 0;
}
else
{
lstFiles.SelectedIndex++;
}
}
lstFiles.ScrollIntoView(lstFiles.SelectedItems[lstFiles.SelectedItems.Count - 1]);
}
else if (e.Key == Key.Up)
{
e.Handled = true;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
var firstSelectedIndex = lstFiles.Items.Count;
if (lstFiles.SelectedItems.Count > 0)
{
firstSelectedIndex = lstFiles.SelectedItems.Cast<object>().Min(x => lstFiles.Items.IndexOf(x));
}
if (firstSelectedIndex > 0)
{
lstFiles.SelectedItems.Add(lstFiles.Items[firstSelectedIndex - 1]);
}
}
else
{
if (lstFiles.SelectedIndex == 0)
{
lstFiles.SelectedIndex = lstFiles.Items.Count - 1;
}
else
{
lstFiles.SelectedIndex--;
}
}
lstFiles.ScrollIntoView(lstFiles.SelectedItems[0]);
}
}
}
private void lstFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (lstFiles.SelectedItems.Count > 0)
{
OpenSelectedFiles(false);
}
}
private void btnSettings_Click(object sender, RoutedEventArgs e)
{
if (grdSettings.Visibility == Visibility.Collapsed)
{
grdSettings.Visibility = Visibility.Visible;
}
else
{
grdSettings.Visibility = Visibility.Collapsed;
}
}
private void chkSearchFullPath_Checked(object sender, RoutedEventArgs e)
{
viewSource.View.Refresh();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter || e.Key == Key.Return)
{
e.Handled = true;
OpenSelectedFiles(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
}
else if (e.Key == Key.Escape)
{
e.Handled = true;
Close();
}
}
}
}