diff --git a/src/DynamoCoreWpf/Controls/NodeAutoCompleteSearchControl.xaml.cs b/src/DynamoCoreWpf/Controls/NodeAutoCompleteSearchControl.xaml.cs index db1bc9b25b8..11f2f42c071 100644 --- a/src/DynamoCoreWpf/Controls/NodeAutoCompleteSearchControl.xaml.cs +++ b/src/DynamoCoreWpf/Controls/NodeAutoCompleteSearchControl.xaml.cs @@ -169,8 +169,7 @@ internal void OnShowNodeAutoCompleteSearch() ViewModel.ResetAutoCompleteSearchViewState(); Dispatcher.BeginInvoke(new Action(() => { - SearchTextBox?.Focus(); - ViewModel?.PopulateAutoCompleteCandidates(); + ViewModel?.PopulateAutoCompleteCandidates(()=>SearchTextBox?.Focus()); }), DispatcherPriority.Loaded); } diff --git a/src/DynamoCoreWpf/ViewModels/Search/NodeAutoCompleteSearchViewModel.cs b/src/DynamoCoreWpf/ViewModels/Search/NodeAutoCompleteSearchViewModel.cs index 62e1fc5163d..5b8b93bffe2 100644 --- a/src/DynamoCoreWpf/ViewModels/Search/NodeAutoCompleteSearchViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Search/NodeAutoCompleteSearchViewModel.cs @@ -307,7 +307,7 @@ internal MLNodeAutoCompletionRequest GenerateRequestForMLAutocomplete() return request; } - internal void ShowNodeAutocompleMLResults() + internal void ShowNodeAutocompleMLResults(Action? afterLoaded) { //this should run on the UI thread, so thread safety is not a concern LastRequestGuid = Guid.NewGuid(); @@ -326,24 +326,25 @@ internal void ShowNodeAutocompleMLResults() try { MLresults = GetMLNodeAutocompleteResults(jsonRequest); - this.dynamoViewModel.UIDispatcher.BeginInvoke(() => UpdateUIWithRresults(MLresults, myRequest)); + this.dynamoViewModel.UIDispatcher.BeginInvoke(() => UpdateUIWithRresults(MLresults, myRequest, afterLoaded)); } catch (Exception ex) { this.dynamoViewModel.UIDispatcher.BeginInvoke(() => { + if (LastRequestGuid != myRequest || !IsOpen) return; ResultsLoaded = true; //fail gracefully + afterLoaded?.Invoke(); dynamoViewModel.Model.Logger.Log("Unable to fetch ML Node autocomplete results: " + ex.Message); DisplayAutocompleteMLStaticPage = true; AutocompleteMLTitle = Resources.LoginNeededTitle; AutocompleteMLMessage = Resources.LoginNeededMessage; Analytics.TrackEvent(Actions.View, Categories.NodeAutoCompleteOperations, "UnabletoFetch"); - return; }); } }); } - private void UpdateUIWithRresults(MLNodeAutoCompletionResponse MLresults, Guid myRequest) + private void UpdateUIWithRresults(MLNodeAutoCompletionResponse MLresults, Guid myRequest, Action? afterLoaded) { if (LastRequestGuid != myRequest) { @@ -358,6 +359,7 @@ private void UpdateUIWithRresults(MLNodeAutoCompletionResponse MLresults, Guid m return; } ResultsLoaded = true; + afterLoaded?.Invoke(); // no results if (MLresults == null || MLresults.Results.Count() == 0) { @@ -455,6 +457,9 @@ private void UpdateUIWithRresults(MLNodeAutoCompletionResponse MLresults, Guid m OrganizeConfidenceSection(results); } + + // Save the filtered results for search. + searchElementsCache = FilteredResults.ToList(); } /// @@ -588,7 +593,7 @@ internal HostNames GetHostNameEnum(string HostName) /// /// Key function to populate node autocomplete results to display /// - internal void PopulateAutoCompleteCandidates() + internal void PopulateAutoCompleteCandidates(Action afterLoaded = null) { if (PortViewModel == null) return; @@ -596,7 +601,7 @@ internal void PopulateAutoCompleteCandidates() if (IsDisplayingMLRecommendation) { - ShowNodeAutocompleMLResults(); + ShowNodeAutocompleMLResults(afterLoaded); //Tracking Analytics when raising Node Autocomplete with the Recommended Nodes option selected (Machine Learning) Analytics.TrackEvent( Actions.Show, @@ -622,10 +627,9 @@ internal void PopulateAutoCompleteCandidates() { FilteredResults = GetViewModelForNodeSearchElements(objectTypeMatchingElements); } + // Save the filtered results for search. + searchElementsCache = FilteredResults.ToList(); } - - // Save the filtered results for search. - searchElementsCache = FilteredResults.ToList(); } internal void PopulateDefaultAutoCompleteCandidates()