forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationView.xaml.cs
More file actions
473 lines (408 loc) · 19.4 KB
/
AnnotationView.xaml.cs
File metadata and controls
473 lines (408 loc) · 19.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Linq;
using Dynamo.Selection;
using Dynamo.UI;
using Dynamo.UI.Prompts;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using DynCmd = Dynamo.Models.DynamoModel;
using TextBox = System.Windows.Controls.TextBox;
using Dynamo.Wpf.Utilities;
using Dynamo.Graph.Annotations;
using Dynamo.Logging;
using Dynamo.Configuration;
namespace Dynamo.Nodes
{
/// <summary>
/// Interaction logic for AnnotationView.xaml
/// </summary>
public partial class AnnotationView : IViewModelView<AnnotationViewModel>
{
public AnnotationViewModel ViewModel { get; private set; }
public static DependencyProperty SelectAllTextOnFocus;
public AnnotationView()
{
Resources.MergedDictionaries.Add(SharedDictionaryManager.DynamoModernDictionary);
Resources.MergedDictionaries.Add(SharedDictionaryManager.DynamoColorsAndBrushesDictionary);
Resources.MergedDictionaries.Add(SharedDictionaryManager.DataTemplatesDictionary);
Resources.MergedDictionaries.Add(SharedDictionaryManager.DynamoConvertersDictionary);
InitializeComponent();
Unloaded += AnnotationView_Unloaded;
Loaded += AnnotationView_Loaded;
DataContextChanged += AnnotationView_DataContextChanged;
this.GroupTextBlock.SizeChanged += GroupTextBlock_SizeChanged;
// Because the size of the CollapsedAnnotationRectangle doesn't necessarily change
// when going from Visible to collapse (and other way around), we need to also listen
// to IsVisibleChanged. Both of these handlers will set the ModelAreaHeight on the ViewModel
this.CollapsedAnnotationRectangle.SizeChanged += CollapsedAnnotationRectangle_SizeChanged;
this.CollapsedAnnotationRectangle.IsVisibleChanged += CollapsedAnnotationRectangle_IsVisibleChanged;
}
private void AnnotationView_Unloaded(object sender, RoutedEventArgs e)
{
Loaded -= AnnotationView_Loaded;
DataContextChanged -= AnnotationView_DataContextChanged;
this.GroupTextBlock.SizeChanged -= GroupTextBlock_SizeChanged;
this.CollapsedAnnotationRectangle.SizeChanged -= CollapsedAnnotationRectangle_SizeChanged;
this.CollapsedAnnotationRectangle.IsVisibleChanged -= CollapsedAnnotationRectangle_IsVisibleChanged;
}
private void AnnotationView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (ViewModel != null ||
!(this.DataContext is AnnotationViewModel viewModel))
{
return;
}
ViewModel = viewModel;
}
private void AnnotationView_Loaded(object sender, RoutedEventArgs e)
{
ViewModel = this.DataContext as AnnotationViewModel;
if (ViewModel != null)
{
//Set the height and width of Textblock based on the content.
if (!ViewModel.AnnotationModel.loadFromXML)
{
SetTextMaxWidth();
SetTextHeight();
}
ViewModel.UpdateProxyPortsPosition();
}
}
//Set the max width of text area based on the width of the longest word in the text
private void SetTextMaxWidth()
{
var words = this.ViewModel.AnnotationText.Split(' ');
var maxLength = 0;
string longestWord = words[0];
foreach (var w in words)
{
if (w.Length > maxLength)
{
longestWord = w;
maxLength = w.Length;
}
}
var formattedText = new FormattedText(
longestWord,
System.Globalization.CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(this.GroupTextBlock.FontFamily, this.GroupTextBlock.FontStyle, this.GroupTextBlock.FontWeight, this.GroupTextBlock.FontStretch),
this.GroupTextBlock.FontSize,
Brushes.Black);
var margin = this.TextBlockGrid.Margin.Right + this.TextBlockGrid.Margin.Left;
this.ViewModel.AnnotationModel.Width = formattedText.Width + margin;
this.ViewModel.AnnotationModel.TextMaxWidth = formattedText.Width + margin;
}
private void OnNodeColorSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems == null || (e.AddedItems.Count <= 0))
return;
//store the old one
if (e.RemovedItems != null || e.RemovedItems.Count > 0)
{
var orectangle = e.AddedItems[0] as Rectangle;
if (orectangle != null)
{
var brush = orectangle.Fill as SolidColorBrush;
if (brush != null)
{
ViewModel.WorkspaceViewModel.DynamoViewModel.ExecuteCommand(
new DynCmd.UpdateModelValueCommand(
System.Guid.Empty, ViewModel.AnnotationModel.GUID, "Background", brush.Color.ToString()));
}
}
}
var rectangle = e.AddedItems[0] as Rectangle;
if (rectangle != null)
{
var brush = rectangle.Fill as SolidColorBrush;
if (brush != null)
ViewModel.Background = brush.Color;
}
}
/// <summary>
/// This function will clear the selection and then select only the annotation node to delete it for ungrouping.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void OnUngroupAnnotation(object sender, RoutedEventArgs e)
{
if (ViewModel != null)
{
DynamoSelection.Instance.ClearSelection();
Guid annotationGuid = this.ViewModel.AnnotationModel.GUID;
// Expand the group before deleting it
// otherwise collapsed content will be "lost"
if (!this.ViewModel.IsExpanded)
{
this.ViewModel.IsExpanded = true;
}
ViewModel.WorkspaceViewModel.DynamoViewModel.ExecuteCommand(
new DynCmd.SelectModelCommand(annotationGuid, Keyboard.Modifiers.AsDynamoType()));
ViewModel.WorkspaceViewModel.DynamoViewModel.DeleteCommand.Execute(null);
ViewModel.WorkspaceViewModel.HasUnsavedChanges = true;
Analytics.TrackEvent(Actions.Ungroup, Categories.GroupOperations);
}
}
/// <summary>
/// Handles the OnMouseLeftButtonDown event of the AnnotationView control.
/// Selects the models inside the group
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
private void AnnotationView_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (GroupTextBlock.IsVisible ||
(!GroupTextBlock.IsVisible && !GroupTextBox.IsVisible))
{
ViewModel.SelectAll();
}
//When Textbox is visible,clear the selection. That way, models will not be added to
//dragged nodes one more time. Ref: MAGN-7321
if (GroupTextBlock.IsVisible && e.ClickCount >= 2)
{
DynamoSelection.Instance.ClearSelection();
//Set the panning mode to false if a group is in editing mode.
if (ViewModel.WorkspaceViewModel.IsPanning)
{
ViewModel.WorkspaceViewModel.DynamoViewModel.BackgroundPreviewViewModel.TogglePan(null);
}
e.Handled = true;
}
//When the Zoom * Fontsized factor is less than 7, then
//show the edit window
if (!GroupTextBlock.IsVisible && e.ClickCount >= 2)
{
var editWindow = new EditWindow(ViewModel.WorkspaceViewModel.DynamoViewModel, true)
{
Title = Dynamo.Wpf.Properties.Resources.EditAnnotationTitle
};
editWindow.BindToProperty(DataContext, new Binding("AnnotationText")
{
Mode = BindingMode.TwoWay,
Source = (DataContext as AnnotationViewModel),
UpdateSourceTrigger = UpdateSourceTrigger.Explicit
});
editWindow.ShowDialog();
e.Handled = true;
}
}
private void AnnotationView_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ViewModel.SelectAll();
}
/// <summary>
/// Handles the OnTextChanged event of the GroupTextBox control.
/// Calculates the height of a Group based on the height of textblock
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
private void GroupTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (ViewModel is null || !IsLoaded) return;
SetTextMaxWidth();
SetTextHeight();
ViewModel.WorkspaceViewModel.HasUnsavedChanges = true;
}
/// <summary>
/// Handles the SizeChanged event of the GroupTextBlock control.
/// This function calculates the height of a group based on font size
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
private void GroupTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (ViewModel != null && (e.HeightChanged || e.WidthChanged))
{
SetTextMaxWidth();
SetTextHeight();
}
ViewModel.UpdateProxyPortsPosition();
}
/// <summary>
/// Select the text in textbox
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private void GroupTextBox_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var textbox = sender as TextBox;
if (textbox == null || textbox.Visibility != Visibility.Visible) return;
//Record the value here, this is useful when title is popped from stack during undo
ViewModel.WorkspaceViewModel.DynamoViewModel.ExecuteCommand(
new DynCmd.UpdateModelValueCommand(
Guid.Empty, ViewModel.AnnotationModel.GUID, "TextBlockText",
GroupTextBox.Text));
ViewModel.WorkspaceViewModel.DynamoViewModel.RaiseCanExecuteUndoRedo();
textbox.Focus();
if (textbox.Text.Equals(Properties.Resources.GroupNameDefaultText))
{
textbox.SelectAll();
}
}
/// <summary>
/// Set the Mouse caret at the end
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void GroupTextBox_OnGotFocus(object sender, RoutedEventArgs e)
{
this.GroupTextBox.CaretIndex = Int32.MaxValue;
}
/// <summary>
/// This function will delete the group with modes
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void OnDeleteAnnotation(object sender, RoutedEventArgs e)
{
//Select the group and the models within that group
if (ViewModel != null)
{
DynamoSelection.Instance.ClearSelection();
ViewModel.SelectAll();
ViewModel.WorkspaceViewModel.DynamoViewModel.DeleteCommand.Execute(null);
Analytics.TrackEvent(Actions.Delete, Categories.GroupOperations);
}
}
/// <summary>
/// This function will run graph layout algorithm to the nodes inside the selected group.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void OnGraphLayoutAnnotation(object sender, RoutedEventArgs e)
{
if (ViewModel != null)
{
DynamoSelection.Instance.ClearSelection();
ViewModel.SelectAll();
ViewModel.WorkspaceViewModel.DynamoViewModel.GraphAutoLayoutCommand.Execute(null);
}
}
private void GroupDescriptionTextBox_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var textbox = sender as TextBox;
if (textbox == null || textbox.Visibility != Visibility.Visible) return;
//Record the value here, this is useful when title is popped from stack during undo
ViewModel.WorkspaceViewModel.DynamoViewModel.ExecuteCommand(
new DynCmd.UpdateModelValueCommand(
Guid.Empty, ViewModel.AnnotationModel.GUID, nameof(AnnotationModel.AnnotationDescriptionText),
GroupDescriptionTextBox.Text));
ViewModel.WorkspaceViewModel.DynamoViewModel.RaiseCanExecuteUndoRedo();
textbox.Focus();
if (textbox.Text.Equals(Properties.Resources.GroupDefaultText))
{
textbox.SelectAll();
}
}
private void GroupDescriptionTextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.GroupDescriptionTextBox.CaretIndex = Int32.MaxValue;
}
private void GroupDescriptionTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (ViewModel is null || !IsLoaded) return;
SetTextHeight();
ViewModel.WorkspaceViewModel.HasUnsavedChanges = true;
}
private void SetTextHeight()
{
if (GroupDescriptionTextBlock is null || GroupTextBlock is null || ViewModel is null)
{
return;
}
// Use the DesiredSize and not the Actual height. Because when Textblock is collapsed,
// Actual height is same as previous size.
ViewModel.AnnotationModel.TextBlockHeight =
this.GroupDescriptionControls.DesiredSize.Height +
this.GroupNameControl.DesiredSize.Height;
}
private void CollapsedAnnotationRectangle_SizeChanged(object sender, SizeChangedEventArgs e)
{
SetModelAreaHeight();
}
private void CollapsedAnnotationRectangle_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
SetModelAreaHeight();
}
private void SetModelAreaHeight()
{
// We only want to change the ModelAreaHeight
// if the CollapsedAnnotationRectangle is visible,
// as if its not it will be equal to the height of the
// contained nodes + the TextBlockHeight
if (ViewModel is null || !this.CollapsedAnnotationRectangle.IsVisible) return;
ViewModel.ModelAreaHeight = this.CollapsedAnnotationRectangle.ActualHeight;
ViewModel.AnnotationModel.UpdateBoundaryFromSelection();
}
private void contextMenu_Click(object sender, RoutedEventArgs e)
{
ViewModel.SelectAll();
this.AnnotationGrid.ContextMenu.DataContext = ViewModel;
this.AnnotationGrid.ContextMenu.IsOpen = true;
}
private void AnnotationRectangleThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
var xAdjust = (ViewModel.Width) + e.HorizontalChange;
var yAdjust = (ViewModel.Height) + e.VerticalChange;
if (xAdjust >= ViewModel.Width - ViewModel.AnnotationModel.WidthAdjustment)
{
ViewModel.AnnotationModel.WidthAdjustment += e.HorizontalChange;
ViewModel.WorkspaceViewModel.HasUnsavedChanges = true;
}
if (yAdjust >= ViewModel.Height - ViewModel.AnnotationModel.HeightAdjustment)
{
ViewModel.AnnotationModel.HeightAdjustment += e.VerticalChange;
ViewModel.WorkspaceViewModel.HasUnsavedChanges = true;
}
}
private void Thumb_MouseEnter(object sender, MouseEventArgs e)
{
ViewModel.WorkspaceViewModel.CurrentCursor = CursorLibrary.GetCursor(CursorSet.ResizeDiagonal);
}
private void Thumb_MouseLeave(object sender, MouseEventArgs e)
{
ViewModel.WorkspaceViewModel.CurrentCursor = CursorLibrary.GetCursor(CursorSet.Pointer);
}
private void GroupDescriptionTextBlock_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
SetTextHeight();
}
private void GroupDescriptionControls_SizeChanged(object sender, SizeChangedEventArgs e)
{
SetTextHeight();
}
/// <summary>
/// According to the current GroupStyle selected (or not selected) in the ContextMenu several actions can be executed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GroupStyleCheckmark_Click(object sender, RoutedEventArgs e)
{
var menuItemSelected = sender as MenuItem;
if (menuItemSelected == null) return;
var groupStyleItemSelected = menuItemSelected.DataContext as GroupStyleItem;
if (groupStyleItemSelected == null) return;
ViewModel.UpdateGroupStyle(groupStyleItemSelected);
// Tracking selecting group style item and if it is a default style by Dynamo
Logging.Analytics.TrackEvent(Actions.Select, Categories.GroupStyleOperations, nameof(GroupStyleItem), groupStyleItemSelected.IsDefault ? 1 : 0);
}
/// <summary>
/// When the GroupStyle Submenu is opened then we need to re-load the GroupStyles in the ContextMenu (in case more Styles were added in Preferences panel).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GroupStyleAnnotation_SubmenuOpened(object sender, RoutedEventArgs e)
{
ViewModel.ReloadGroupStyles();
// Tracking loading group style items
Logging.Analytics.TrackEvent(Actions.Load, Categories.GroupStyleOperations, nameof(GroupStyleItem) + "s");
}
}
}