1- using System . Windows . Input ;
1+ using System . Windows ;
2+ using System . Windows . Input ;
23
34namespace Nodify
45{
56 /// <summary>The default state of the <see cref="ItemContainer"/>.</summary>
67 public class ContainerDefaultState : ContainerState
78 {
8- private bool _canBeDragging ;
9- private bool _canceled ;
9+ private Point _initialPosition ;
10+ private SelectionType ? _selectionType ;
11+ private bool _isDragging ;
1012
1113 /// <summary>Creates a new instance of the <see cref="ContainerDefaultState"/>.</summary>
1214 /// <param name="container">The owner of the state.</param>
@@ -17,80 +19,68 @@ public ContainerDefaultState(ItemContainer container) : base(container)
1719 /// <inheritdoc />
1820 public override void ReEnter ( ContainerState from )
1921 {
20- if ( from is ContainerDraggingState drag )
21- {
22- Container . IsSelected = true ;
23- _canceled = drag . Canceled ;
24- }
25-
26- _canBeDragging = false ;
22+ _isDragging = false ;
23+ _selectionType = null ;
24+ _initialPosition = Editor . MouseLocation ;
2725 }
2826
2927 /// <inheritdoc />
3028 public override void HandleMouseDown ( MouseButtonEventArgs e )
3129 {
32- _canceled = false ;
33-
3430 EditorGestures . ItemContainerGestures gestures = EditorGestures . Mappings . ItemContainer ;
3531 if ( gestures . Drag . Matches ( e . Source , e ) )
3632 {
37- _canBeDragging = Container . IsDraggable ;
33+ _isDragging = Container . IsDraggable ;
34+ }
3835
39- // Clear the selection if dragging an item that is not part of the selection will not add it to the selection
40- if ( _canBeDragging && ! Container . IsSelected && ! gestures . Selection . Append . Matches ( e . Source , e ) && ! gestures . Selection . Invert . Matches ( e . Source , e ) )
41- {
42- Editor . UnselectAll ( ) ;
43- }
36+ if ( gestures . Selection . Select . Matches ( e . Source , e ) )
37+ {
38+ _selectionType = gestures . Selection . GetSelectionType ( e ) ;
4439 }
40+
41+ _initialPosition = Editor . MouseLocation ;
4542 }
4643
4744 /// <inheritdoc />
48- public override void HandleMouseUp ( MouseButtonEventArgs e )
45+ public override void HandleMouseMove ( MouseEventArgs e )
4946 {
50- EditorGestures . ItemContainerGestures gestures = EditorGestures . Mappings . ItemContainer ;
51- if ( ! _canceled && gestures . Selection . Select . Matches ( e . Source , e ) )
47+ double dragThreshold = NodifyEditor . HandleRightClickAfterPanningThreshold * NodifyEditor . HandleRightClickAfterPanningThreshold ;
48+ double dragDistance = ( Editor . MouseLocation - _initialPosition ) . LengthSquared ;
49+
50+ if ( _isDragging && ( dragDistance > dragThreshold ) )
5251 {
53- if ( gestures . Selection . Append . Matches ( e . Source , e ) )
54- {
55- Container . IsSelected = true ;
56- }
57- else if ( gestures . Selection . Invert . Matches ( e . Source , e ) )
58- {
59- Container . IsSelected = ! Container . IsSelected ;
60- }
61- else if ( gestures . Selection . Remove . Matches ( e . Source , e ) )
62- {
63- Container . IsSelected = false ;
64- }
65- else
52+ if ( ! Container . IsSelected )
6653 {
67- // Allow context menu on selection
68- if ( ! ( e . ChangedButton == MouseButton . Right && e . RightButton == MouseButtonState . Released ) || ! Container . IsSelected )
69- {
70- Editor . UnselectAll ( ) ;
71- }
72-
73- Container . IsSelected = true ;
54+ var selectionType = GetSelectionTypeForDragging ( _selectionType ) ;
55+ Container . Select ( selectionType ) ;
7456 }
7557
76- _canBeDragging = false ;
58+ PushState ( new ContainerDraggingState ( Container ) ) ;
7759 }
60+ }
7861
79- if ( ! _canceled && gestures . Drag . Matches ( e . Source , e ) )
62+ /// <inheritdoc />
63+ public override void HandleMouseUp ( MouseButtonEventArgs e )
64+ {
65+ if ( _selectionType . HasValue )
8066 {
81- _canBeDragging = false ;
67+ bool allowContextMenu = e . ChangedButton == MouseButton . Right && Container . IsSelected ;
68+ if ( ! ( _selectionType == SelectionType . Replace && allowContextMenu ) )
69+ {
70+ Container . Select ( _selectionType . Value ) ;
71+ }
8272 }
8373
84- _canceled = false ;
74+ _isDragging = false ;
75+ _selectionType = null ;
8576 }
8677
87- /// <inheritdoc />
88- public override void HandleMouseMove ( MouseEventArgs e )
78+ private static SelectionType GetSelectionTypeForDragging ( SelectionType ? selectionType )
8979 {
90- if ( _canBeDragging )
91- {
92- PushState ( new ContainerDraggingState ( Container ) ) ;
93- }
80+ // Always select the container when dragging
81+ return selectionType == SelectionType . Remove
82+ ? SelectionType . Replace
83+ : selectionType . GetValueOrDefault ( SelectionType . Replace ) ;
9484 }
9585 }
9686}
0 commit comments