forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeAutoCompleteBarView.xaml.cs
More file actions
170 lines (148 loc) · 5.62 KB
/
Copy pathNodeAutoCompleteBarView.xaml.cs
File metadata and controls
170 lines (148 loc) · 5.62 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
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.NodeAutoComplete.ViewModels;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace Dynamo.NodeAutoComplete.Views
{
/// <summary>
/// Interaction logic for NodeAutoCompleteBarView.xaml
/// </summary>
public partial class NodeAutoCompleteBarView
{
private NodeAutoCompleteBarViewModel ViewModel => DataContext as NodeAutoCompleteBarViewModel;
public NodeAutoCompleteBarView(Window window, NodeAutoCompleteBarViewModel viewModel)
{
Owner = window;
DataContext = viewModel;
InitializeComponent();
viewModel.PortViewModel.Highlight = Visibility.Visible;
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing += UnsubscribeEvents;
}
}
HomeWorkspaceModel.WorkspaceClosed += CloseAutoComplete;
viewModel.IsOpen = true;
LoadAndPopulate();
}
private void UnsubscribeEvents(object sender, System.ComponentModel.CancelEventArgs e)
{
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing -= UnsubscribeEvents;
}
}
HomeWorkspaceModel.WorkspaceClosed -= CloseAutoComplete;
}
private void LoadAndPopulate()
{
Analytics.TrackEvent(
Dynamo.Logging.Actions.Open,
Dynamo.Logging.Categories.NodeAutoCompleteOperations);
ViewModel.DropdownResults = null;
// Visibility of textbox changed, but text box has not been initialized(rendered) yet.
// Call asynchronously focus, when textbox will be ready.
Dispatcher.BeginInvoke(new Action(() =>
{
ViewModel.PopulateAutoComplete();
//ViewModel.PopulateAutoCompleteCandidates();
}), DispatcherPriority.Loaded);
ViewModel.ParentNodeRemoved += OnParentNodeRemoved;
}
private void GripHandle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void MoveIndex(int step)
{
ViewModel.SelectedIndex = Math.Min(ViewModel.FilteredView.Cast<object>().Count() - 1, Math.Max(0, ViewModel.SelectedIndex + step));
}
private void PrevButton_OnClick(object sender, RoutedEventArgs e)
{
MoveIndex(-1);
}
private void NextButton_OnClick(object sender, RoutedEventArgs e)
{
MoveIndex(+1);
}
private void DockButton_OnClick(object sender, RoutedEventArgs e)
{
if (ViewModel.ResultsLoaded)
{
ViewModel.PortViewModel.NodeViewModel.WorkspaceViewModel.OnRequestNodeAutoCompleteViewExtension(ViewModel.FullResults);
}
}
//Removes nodeautocomplete menu when the associated parent node is removed.
private void OnParentNodeRemoved(NodeModel node)
{
NodeModel parent_node = ViewModel.PortViewModel?.PortModel.Owner;
if (node == parent_node)
{
CloseAutoComplete();
ViewModel.ParentNodeRemoved -= OnParentNodeRemoved;
}
}
private void OnAutoCompleteKeyDown(object sender, KeyEventArgs e)
{
var key = e.Key;
switch (key)
{
case Key.Escape:
CloseAutoComplete();
e.Handled = true;
break;
case Key.Enter:
ViewModel?.ConsolidateTransientNodes();
CloseAutoComplete();
e.Handled = true;
break;
case Key.Up:
case Key.Left:
MoveIndex(-1);
e.Handled = true;
break;
case Key.Down:
case Key.Right:
MoveIndex(+1);
e.Handled = true;
break;
default:
break;
}
}
internal void ConfirmAutoCompleteWindow(object sender, RoutedEventArgs e)
{
ViewModel?.ConsolidateTransientNodes();
CloseAutoComplete();
}
internal void CloseAutoCompleteWindow(object sender, RoutedEventArgs e)
{
CloseAutoComplete();
}
internal void CloseAutoComplete()
{
ViewModel.IsOpen = false;
ViewModel.PortViewModel.Highlight = Visibility.Hidden;
//if we're doing this while a node is being deleted, doing this synchronously will
//cause an exception because of the current undo stack being open
//TODO: Transient node operations shouldn't be recorded in the undo-redo stack
Dispatcher.BeginInvoke(new Action(() =>
{
ViewModel?.DeleteTransientNodes();
ViewModel?.ToggleUndoRedoLocked(false);
}), DispatcherPriority.Loaded);
Close();
UnsubscribeEvents(this, null);
}
}
}