forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeAutoCompleteSearchControl.xaml.cs
More file actions
501 lines (434 loc) · 20.4 KB
/
Copy pathNodeAutoCompleteSearchControl.xaml.cs
File metadata and controls
501 lines (434 loc) · 20.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.ZeroTouch;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels;
using Res = Dynamo.Wpf.Properties.Resources;
namespace Dynamo.UI.Controls
{
/// <summary>
/// Interaction logic for AutoCompleteSearchControl.xaml
/// Notice this control shares a lot of logic with InCanvasSearchControl for now
/// But they will diverge eventually because of UI improvements to auto complete.
/// </summary>
[Obsolete("This class will be removed in a future version of Dynamo")]
public partial class NodeAutoCompleteSearchControl : IDisposable
{
private double currentX;
private ListBoxItem HighlightedItem;
private ListBoxItem currentListBoxItem;
private static NodeAutoCompleteSearchControl _controlInstance;
// Prepare the autocomplete window and reuse it whenever possible.
// Only a single instance of the window will be allowed at any given time.
static internal void PrepareAndShowNodeAutoCompleteSearch(Window window, NodeAutoCompleteSearchViewModel viewModel)
{
// A new window will be created (replacing any existing one) whenever a new viewModel is provided.
// Each workspace gets its own viewModel—for example, when opening a custom node alongside the current workspace.
if (_controlInstance is null || !ReferenceEquals(_controlInstance.ViewModel, viewModel))
{
_controlInstance?.ResetNodeAutoCompleteSearch();
_controlInstance = new NodeAutoCompleteSearchControl(window, viewModel);
}
// When a window is already open, adjust its position to the target port without repeating the full event subscription setup.
if (_controlInstance?.IsVisible is true)
{
Analytics.TrackEvent(Actions.Open, Categories.NodeAutoCompleteOperations);
if (_controlInstance?.ViewModel?.PortViewModel != null)
{
_controlInstance.ViewModel.PortViewModel.Highlight = Visibility.Collapsed;
_controlInstance.ViewModel.PortViewModel?.SetupNodeAutoCompleteWindowPlacement(_controlInstance);
}
_controlInstance?.ViewModel?.PopulateAutoCompleteCandidates();
}
else
{
_controlInstance?.OnShowNodeAutoCompleteSearch();
}
}
/// <summary>
/// Node AutoComplete Search ViewModel DataContext
/// </summary>
[Obsolete("This method will be removed in a future version of Dynamo")]
public NodeAutoCompleteSearchViewModel ViewModel => DataContext as NodeAutoCompleteSearchViewModel;
[Obsolete("This method will be removed in a future version of Dynamo")]
public NodeAutoCompleteSearchControl()
{
InitializeComponent();
}
private NodeAutoCompleteSearchControl(Window window, NodeAutoCompleteSearchViewModel viewModel)
{
Owner = window;
DataContext = viewModel;
InitializeComponent();
SubscribeToAppEvents();
}
//Unsubscribe from events and destroy the node autocomplete window.
private void ResetNodeAutoCompleteSearch()
{
UnsubscribeFromAppEvents();
OnHideNodeAutoCompleteSearch();
Close();
_controlInstance = null;
}
private void OnMainAppClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
ResetNodeAutoCompleteSearch();
}
private void OnWorkspaceClosed()
{
ResetNodeAutoCompleteSearch();
}
void OnWorkspaceRemoved(WorkspaceModel workspace)
{
if (ViewModel?.PortViewModel?.NodeViewModel?.WorkspaceViewModel?.Model?.Guid == workspace?.Guid)
{
ResetNodeAutoCompleteSearch();
}
}
// When triggered, they will result in the autocomplete window being destroyed.
private void SubscribeToAppEvents()
{
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing += OnMainAppClosing;
}
}
HomeWorkspaceModel.WorkspaceClosed += OnWorkspaceClosed; //Only hits for main workspace (does not hit for custom nodes workspace).
ViewModel.dynamoViewModel.Model.WorkspaceHidden += OnWorkspaceRemoved; //De-activating current workspace (including custom node workspace).
ViewModel.dynamoViewModel.Model.WorkspaceRemoveStarted += OnWorkspaceRemoved; //Closing custom node workspace.
}
private void UnsubscribeFromAppEvents()
{
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing -= OnMainAppClosing;
}
}
HomeWorkspaceModel.WorkspaceClosed -= OnWorkspaceClosed;
ViewModel.dynamoViewModel.Model.WorkspaceHidden -= OnWorkspaceRemoved;
ViewModel.dynamoViewModel.Model.WorkspaceRemoveStarted -= OnWorkspaceRemoved;
}
//Hide the window and unsubscribe from model events.
//Note that the window is not destroyed, it is just hidden so that it can be reused.
internal void OnHideNodeAutoCompleteSearch()
{
ViewModel.ParentNodeRemoved -= OnParentNodeRemoved;
ViewModel.OnNodeAutoCompleteWindowClosed();
Hide();
}
internal void OnShowNodeAutoCompleteSearch()
{
Analytics.TrackEvent(Actions.Open, Categories.NodeAutoCompleteOperations);
if (ViewModel != null)
{
ViewModel.ParentNodeRemoved += OnParentNodeRemoved;
ViewModel.PortViewModel?.SetupNodeAutoCompleteWindowPlacement(this);
ViewModel.OnNodeAutoCompleteWindowOpened();
}
Show();
// Visibility of textbox changed, but text box has not been initialized(rendered) yet.
// Call asynchronously focus, when textbox will be ready.
ViewModel.ResetAutoCompleteSearchViewState();
Dispatcher.BeginInvoke(new Action(() =>
{
ViewModel?.PopulateAutoCompleteCandidates(()=>SearchTextBox?.Focus());
}), DispatcherPriority.Loaded);
}
private void OnSearchTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
BindingExpression binding = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
if (binding != null)
binding.UpdateSource();
// Search the filtered results to match the user input.
if (ViewModel != null)
{
Dispatcher.BeginInvoke(new Action(() =>
{
ViewModel.SearchAutoCompleteCandidates(SearchTextBox.Text);
}), DispatcherPriority.Loaded);
}
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!(sender is ListBoxItem listBoxItem) || e.OriginalSource is Thumb) return;
ExecuteSearchElement(listBoxItem);
OnHideNodeAutoCompleteSearch();
e.Handled = true;
}
private void ExecuteSearchElement(ListBoxItem listBoxItem)
{
var searchElement = listBoxItem.DataContext as NodeSearchElementViewModel;
if (searchElement != null)
{
searchElement.Position = ViewModel.InCanvasSearchPosition;
PortViewModel port = ViewModel.PortViewModel;
if (searchElement.CreateAndConnectCommand.CanExecute(port.PortModel))
{
searchElement.CreateAndConnectCommand.Execute(port.PortModel);
var selectedNodeName = (searchElement.Model is Search.SearchElements.ZeroTouchSearchElement) ?
searchElement.Model.CreationName :
// Same as NameTypeId.ToStrng() format
string.Format("{0}, {1}", searchElement.Model.CreationName, searchElement.Assembly.Split('\\').Last().Split('.').First());
var originalNodeName = (port.NodeViewModel.NodeModel is DSFunctionBase) ?
port.NodeViewModel.NodeModel.CreationName :
string.Format("{0}, {1}", port.NodeViewModel.NodeModel.GetType().FullName, port.NodeViewModel.NodeModel.GetType().Assembly.GetName().Name) ;
var searchElementInfo = ViewModel.IsDisplayingMLRecommendation ?
selectedNodeName + " " + port.PortModel.Index.ToString() + " " + port.PortName + " " + originalNodeName + " " +
searchElement.Model.AutoCompletionNodeElementInfo.PortToConnect.ToString() + " " +
searchElement.AutoCompletionNodeMachineLearningInfo.ConfidenceScore.ToString() + " " + ViewModel.ServiceVersion
: selectedNodeName;
Analytics.TrackEvent(
Dynamo.Logging.Actions.Select,
Dynamo.Logging.Categories.NodeAutoCompleteOperations,
searchElementInfo);
}
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!(sender is FrameworkElement fromSender)) return;
currentX = e.GetPosition(MembersListBox).X;
DisplayOrHideConfidenceTooltip(sender);
}
private void DisplayOrHideConfidenceTooltip(object sender)
{
currentListBoxItem = sender as ListBoxItem;
if (currentX <= 35)
{
confidenceToolTip.PlacementTarget = currentListBoxItem;
confidenceToolTip.Placement = PlacementMode.Bottom;
confidenceToolTip.IsOpen = true;
}
else
{
confidenceToolTip.IsOpen = false;
confidenceToolTip.PlacementTarget = null;
}
}
private void OnMouseEnter(object sender, MouseEventArgs e)
{
if (!(sender is FrameworkElement fromSender)) return;
if (HighlightedItem != null)
{
HighlightedItem.IsSelected = false;
}
toolTipPopup.DataContext = fromSender.DataContext;
toolTipPopup.IsOpen = true;
confidenceToolTip.IsOpen = false;
DisplayOrHideConfidenceTooltip(sender);
dynamic currentNodeSearchElement = currentListBoxItem.DataContext;
var scoreFormatter = new Dynamo.Controls.ConfidenceScoreFormattingConverter();
var score = scoreFormatter.Convert(currentNodeSearchElement.AutoCompletionNodeMachineLearningInfo.ConfidenceScore, null, null, CultureInfo.CurrentCulture);
confidenceScoreTitle.Text = $"{Res.ConfidenceToolTipTitle}: {score}%";
}
private void onCloseConfidenceToolTip(object sender, RoutedEventArgs e)
{
confidenceToolTip.IsOpen = false;
}
private void OnMouseLeave(object sender, MouseEventArgs e)
{
if (!(sender is FrameworkElement)) return;
HighlightedItem.IsSelected = true;
toolTipPopup.DataContext = null;
toolTipPopup.IsOpen = false;
}
private void onConfidenceToolTipLearnMoreClicked(object sender, MouseButtonEventArgs e)
{
confidenceToolTip.IsOpen = false;
ViewModel.dynamoViewModel.OpenDocumentationLinkCommand.Execute(new OpenDocumentationLinkEventArgs(new Uri(Res.NodeAutocompleteDocumentationUriString, UriKind.Relative)));
}
//Removes nodeautocomplete menu when the associated parent node is removed.
private void OnParentNodeRemoved(NodeModel node)
{
NodeModel parent_node = ViewModel?.PortViewModel?.PortModel?.Owner;
if (ReferenceEquals(node,parent_node))
{
OnHideNodeAutoCompleteSearch();
}
}
private void OnMembersListBoxUpdated(object sender, DataTransferEventArgs e)
{
var membersListBox = sender as ListBox;
// As soon as listbox renders, select first member.
membersListBox.ItemContainerGenerator.StatusChanged += OnMembersListBoxIcgStatusChanged;
}
private void OnMembersListBoxIcgStatusChanged(object sender, EventArgs e)
{
if (MembersListBox.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
MembersListBox.ItemContainerGenerator.StatusChanged -= OnMembersListBoxIcgStatusChanged;
Dispatcher.BeginInvoke(new Action(() =>
{
var scrollViewer = MembersListBox.ChildOfType<ScrollViewer>();
scrollViewer.ScrollToTop();
UpdateHighlightedItem(GetListItemByIndex(MembersListBox, 0));
}),
DispatcherPriority.Loaded);
}
}
private void UpdateHighlightedItem(ListBoxItem newItem)
{
if (HighlightedItem == newItem)
return;
// Unselect old value.
if (HighlightedItem != null)
HighlightedItem.IsSelected = false;
HighlightedItem = newItem;
// Select new value.
if (HighlightedItem != null)
{
HighlightedItem.IsSelected = true;
HighlightedItem.BringIntoView();
}
}
private ListBoxItem GetListItemByIndex(ListBox parent, int index)
{
if (parent.Equals(null)) return null;
var generator = parent.ItemContainerGenerator;
if ((index >= 0) && (index < parent.Items.Count))
return generator.ContainerFromIndex(index) as ListBoxItem;
return null;
}
private void OnInCanvasSearchKeyDown(object sender, KeyEventArgs e)
{
var key = e.Key;
int index;
var members = MembersListBox.Items.Cast<NodeSearchElementViewModel>();
NodeSearchElementViewModel highlightedMember = null;
if (HighlightedItem != null)
highlightedMember = HighlightedItem.DataContext as NodeSearchElementViewModel;
switch (key)
{
case Key.Escape:
OnHideNodeAutoCompleteSearch();
break;
case Key.Enter:
if (HighlightedItem != null)
{
ExecuteSearchElement(HighlightedItem);
OnHideNodeAutoCompleteSearch();
}
break;
case Key.Up:
index = MoveToNextMember(false, members, highlightedMember);
UpdateHighlightedItem(GetListItemByIndex(MembersListBox, index));
break;
case Key.Down:
index = MoveToNextMember(true, members, highlightedMember);
UpdateHighlightedItem(GetListItemByIndex(MembersListBox, index));
break;
}
}
internal int MoveToNextMember(bool moveForward,
IEnumerable<NodeSearchElementViewModel> members, NodeSearchElementViewModel selectedMember)
{
int selectedMemberIndex = -1;
for (int i = 0; i < members.Count(); i++)
{
var member = members.ElementAt(i);
if (member.Equals(selectedMember))
{
selectedMemberIndex = i;
break;
}
}
int nextselectedMemberIndex = selectedMemberIndex;
if (moveForward)
nextselectedMemberIndex++;
else
nextselectedMemberIndex--;
if (nextselectedMemberIndex < 0 || (nextselectedMemberIndex >= members.Count()))
return selectedMemberIndex;
return nextselectedMemberIndex;
}
private void OnMembersListBoxMouseWheel(object sender, MouseWheelEventArgs e)
{
var listBox = sender as FrameworkElement;
if (listBox == null)
return;
var scrollViewer = listBox.ChildOfType<ScrollViewer>();
if (scrollViewer == null)
return;
// Make delta less to achieve smooth scrolling and not jump over other elements.
var delta = e.Delta / 100;
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - delta);
// do not propagate to child items with scrollable content
e.Handled = true;
}
private void OnMoreInfoClicked(object sender, RoutedEventArgs e)
{
ViewModel.dynamoViewModel.OpenDocumentationLinkCommand.Execute(new OpenDocumentationLinkEventArgs(new Uri(Dynamo.Wpf.Properties.Resources.NodeAutocompleteDocumentationUriString, UriKind.Relative)));
}
internal void CloseAutoCompleteWindow(object sender, RoutedEventArgs e)
{
OnHideNodeAutoCompleteSearch();
}
/// <summary>
/// A common method to handle the suggestions Button being clicked
/// </summary>
private void DisplaySuggestions(object sender, RoutedEventArgs e)
{
var cm = this.SuggestionsContextMenu;
cm.PlacementTarget = sender as Button;
cm.IsOpen = true;
}
private void ShowLowConfidenceResults(object sender, RoutedEventArgs e)
{
ViewModel.ShowLowConfidenceResults();
//Tracking Analytics when the Low Confidence combobox (located in the Autocomplete popup) is clicked
Analytics.TrackEvent(
Actions.Expanded,
Categories.NodeAutoCompleteOperations,
"LowConfidenceResults",
ViewModel.dynamoViewModel.Model.PreferenceSettings.MLRecommendationConfidenceLevel);
}
private void OnSuggestion_Click(object sender, RoutedEventArgs e)
{
MenuItem selectedSuggestion = sender as MenuItem;
if (selectedSuggestion.Name.Contains(nameof(Models.NodeAutocompleteSuggestion.MLRecommendation)))
{
if(ViewModel.IsMLAutocompleteTOUApproved)
{
ViewModel.dynamoViewModel.PreferenceSettings.DefaultNodeAutocompleteSuggestion = Models.NodeAutocompleteSuggestion.MLRecommendation;
Analytics.TrackEvent(Actions.Switch, Categories.Preferences, nameof(NodeAutocompleteSuggestion.MLRecommendation));
}
else
{
ViewModel.dynamoViewModel.MainGuideManager.CreateRealTimeInfoWindow(Res.NotificationToAgreeMLNodeautocompleteTOU);
// Do nothing for now, do not report analytics since the switch did not happen
}
}
else
{
ViewModel.dynamoViewModel.PreferenceSettings.DefaultNodeAutocompleteSuggestion = Models.NodeAutocompleteSuggestion.ObjectType;
Analytics.TrackEvent(Actions.Switch, Categories.Preferences, nameof(NodeAutocompleteSuggestion.ObjectType));
}
ViewModel.PopulateAutoCompleteCandidates();
}
/// <summary>
/// Dispose the control
/// </summary>
[Obsolete("This method will be removed in a future version of Dynamo")]
public void Dispose()
{
ResetNodeAutoCompleteSearch();
}
}
}