Skip to content

Commit 7f6768e

Browse files
authored
Add HasCustomContextMenu to NodifyEditor and ItemContainer (#165)
* Add HasCustomContextMenu to NodifyEditor and ItemContainer
1 parent ee4fc35 commit 7f6768e

File tree

11 files changed

+50
-16
lines changed

11 files changed

+50
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
> - Added UpdateCuttingLine to NodifyEditor
1616
> - Added BeginSelecting, UpdateSelection, EndSelecting, CancelSelecting and AllowSelectionCancellation to NodifyEditor
1717
> - Added IsDragging, BeginDragging, UpdateDragging, EndDragging and CancelDragging to NodifyEditor
18-
> - Added AlignSelection and AlignContainers to NodifyEditor
18+
> - Added AlignSelection and AlignContainers methods to NodifyEditor
19+
> - Added HasCustomContextMenu dependency property to NodifyEditor and ItemContainer
1920
> - Added Select, BeginDragging, UpdateDragging, EndDragging and CancelDragging to ItemContainer
2021
> - Added PreserveSelectionOnRightClick configuration field to ItemContainer
2122
> - Bugfixes:

Examples/Nodify.Calculator/EditorView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
ConnectionTemplate="{StaticResource ConnectionTemplate}"
136136
Background="{StaticResource SmallGridLinesDrawingBrush}"
137137
ItemContainerStyle="{StaticResource ItemContainerStyle}"
138+
HasCustomContextMenu="True"
138139
GridCellSize="15"
139140
AllowDrop="True"
140141
Drop="OnDropNode"

Examples/Nodify.Calculator/EditorView.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public EditorView()
1616

1717
private void OpenOperationsMenu(object sender, MouseButtonEventArgs e)
1818
{
19-
if (!e.Handled && e.OriginalSource is NodifyEditor editor && !editor.IsPanning && editor.DataContext is CalculatorViewModel calculator)
19+
if (!e.Handled && e.OriginalSource is NodifyEditor editor && editor.DataContext is CalculatorViewModel calculator)
2020
{
2121
e.Handled = true;
2222
calculator.OperationsMenu.OpenAt(editor.MouseLocation);

Nodify/EditorStates/ContainerDefaultState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
6969
// explicit context menu or is configured to preserve the selection on right-click, the selection
7070
// remains unchanged. This ensures that the context menu applies to the entire selection rather
7171
// than only the clicked item.
72-
bool hasContextMenu = Container.ContextMenu != null || ItemContainer.PreserveSelectionOnRightClick;
72+
bool hasContextMenu = Container.HasContextMenu || ItemContainer.PreserveSelectionOnRightClick;
7373
bool allowContextMenu = e.ChangedButton == MouseButton.Right && Container.IsSelected && hasContextMenu;
7474
if (!(_selectionType == SelectionType.Replace && allowContextMenu))
7575
{

Nodify/EditorStates/ContainerDraggingState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
5656
if (gestures.Drag.Matches(e.Source, e))
5757
{
5858
// Suppress the context menu if the mouse moved beyond the defined drag threshold
59-
if (e.ChangedButton == MouseButton.Right && Editor.ContextMenu != null)
59+
if (e.ChangedButton == MouseButton.Right && (Container.HasContextMenu || Editor.HasContextMenu))
6060
{
6161
double dragThreshold = NodifyEditor.MouseActionSuppressionThreshold * NodifyEditor.MouseActionSuppressionThreshold;
6262
double dragDistance = (Editor.MouseLocation - _initialMousePosition).LengthSquared;

Nodify/EditorStates/EditorCuttingState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
3939
if (gestures.Cutting.Matches(e.Source, e))
4040
{
4141
// Suppress the context menu if the mouse moved beyond the defined drag threshold
42-
if (e.ChangedButton == MouseButton.Right && Editor.ContextMenu != null)
42+
if (e.ChangedButton == MouseButton.Right && Editor.HasContextMenu)
4343
{
4444
double dragThreshold = NodifyEditor.MouseActionSuppressionThreshold * NodifyEditor.MouseActionSuppressionThreshold;
4545
double dragDistance = (Editor.MouseLocation - _initialPosition).LengthSquared;

Nodify/EditorStates/EditorPanningState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
5959
if (gestures.Pan.Matches(e.Source, e))
6060
{
6161
// Suppress the context menu if the mouse moved beyond the defined drag threshold or the editor is selecting
62-
if (e.ChangedButton == MouseButton.Right && Editor.ContextMenu != null)
62+
if (e.ChangedButton == MouseButton.Right && Editor.HasContextMenu)
6363
{
6464
double dragThreshold = NodifyEditor.MouseActionSuppressionThreshold * NodifyEditor.MouseActionSuppressionThreshold;
6565
double dragDistance = (_currentMousePosition - _initialMousePosition).LengthSquared;

Nodify/EditorStates/EditorPushingItemsState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
6363
if (gestures.PushItems.Matches(e.Source, e))
6464
{
6565
// Suppress the context menu if the mouse moved beyond the defined drag threshold
66-
if (e.ChangedButton == MouseButton.Right && Editor.ContextMenu != null)
66+
if (e.ChangedButton == MouseButton.Right && Editor.HasContextMenu)
6767
{
6868
double dragThreshold = NodifyEditor.MouseActionSuppressionThreshold * NodifyEditor.MouseActionSuppressionThreshold;
6969
double dragDistance = (Editor.MouseLocation - _initialPosition).LengthSquared;

Nodify/EditorStates/EditorSelectingState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void HandleMouseUp(MouseButtonEventArgs e)
6161
if (gestures.Select.Matches(e.Source, e))
6262
{
6363
// Suppress the context menu if the mouse moved beyond the defined drag threshold
64-
if (e.ChangedButton == MouseButton.Right && Editor.ContextMenu != null)
64+
if (e.ChangedButton == MouseButton.Right && Editor.HasContextMenu)
6565
{
6666
double dragThreshold = NodifyEditor.MouseActionSuppressionThreshold * NodifyEditor.MouseActionSuppressionThreshold;
6767
double dragDistance = (Editor.MouseLocation - _initialPosition).LengthSquared;

Nodify/ItemContainer.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public class ItemContainer : ContentControl, INodifyCanvasItem
3333
private static readonly DependencyPropertyKey IsPreviewingLocationPropertyKey = DependencyProperty.RegisterReadOnly(nameof(IsPreviewingLocation), typeof(bool), typeof(ItemContainer), new FrameworkPropertyMetadata(BoxValue.False));
3434
public static readonly DependencyProperty IsPreviewingLocationProperty = IsPreviewingLocationPropertyKey.DependencyProperty;
3535
public static readonly DependencyProperty IsDraggableProperty = DependencyProperty.Register(nameof(IsDraggable), typeof(bool), typeof(ItemContainer), new FrameworkPropertyMetadata(BoxValue.True));
36+
public static readonly DependencyProperty HasCustomContextMenuProperty = NodifyEditor.HasCustomContextMenuProperty.AddOwner(typeof(ItemContainer));
37+
38+
private static void OnLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
39+
{
40+
var item = (ItemContainer)d;
41+
item.OnLocationChanged();
42+
43+
if (item.Editor.IsLoaded && !item.Editor.IsBulkUpdatingItems)
44+
{
45+
item.Editor.ItemsHost.InvalidateArrange();
46+
}
47+
}
3648

3749
/// <summary>
3850
/// Gets or sets the brush used when the <see cref="PendingConnection.IsOverElementProperty"/> attached property is true for this <see cref="ItemContainer"/>.
@@ -135,17 +147,21 @@ public bool IsDraggable
135147
set => SetValue(IsDraggableProperty, value);
136148
}
137149

138-
private static void OnLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
150+
/// <summary>
151+
/// Gets or sets a value indicating whether the container uses a custom context menu.
152+
/// </summary>
153+
/// <remarks>When set to true, the container handles the right-click event for specific operations.</remarks>
154+
public bool HasCustomContextMenu
139155
{
140-
var item = (ItemContainer)d;
141-
item.OnLocationChanged();
142-
143-
if (item.Editor.IsLoaded && !item.Editor.IsBulkUpdatingItems)
144-
{
145-
item.Editor.ItemsHost.InvalidateArrange();
146-
}
156+
get => (bool)GetValue(HasCustomContextMenuProperty);
157+
set => SetValue(HasCustomContextMenuProperty, value);
147158
}
148159

160+
/// <summary>
161+
/// Gets a value indicating whether the container has a context menu.
162+
/// </summary>
163+
public bool HasContextMenu => ContextMenu != null || HasCustomContextMenu;
164+
149165
#endregion
150166

151167
#region Routed Events

0 commit comments

Comments
 (0)