1- using System . Windows ;
1+ using Nodify . Interactivity ;
2+ using System . Windows ;
23using System . Windows . Controls ;
34using System . Windows . Input ;
45
56namespace Nodify . Calculator
67{
7- public partial class EditorView : UserControl
8+ public class OperationsMenuHandler : InputElementState < NodifyEditor >
89 {
9- public EditorView ( )
10- {
11- InitializeComponent ( ) ;
10+ private static InputGesture OpenGesture { get ; } = new Interactivity . MouseGesture ( MouseAction . RightClick ) ;
11+ private static InputGesture CloseGesture { get ; } = new Interactivity . MouseGesture ( MouseAction . LeftClick ) ;
12+
13+ private OperationsMenuViewModel ViewModel => ( ( CalculatorViewModel ) Element . DataContext ) . OperationsMenu ;
1214
13- EventManager . RegisterClassHandler ( typeof ( NodifyEditor ) , MouseLeftButtonDownEvent , new MouseButtonEventHandler ( CloseOperationsMenu ) , true ) ;
14- EventManager . RegisterClassHandler ( typeof ( NodifyEditor ) , MouseRightButtonUpEvent , new MouseButtonEventHandler ( OpenOperationsMenu ) ) ;
15+ public OperationsMenuHandler ( NodifyEditor element ) : base ( element )
16+ {
1517 }
1618
17- private void OpenOperationsMenu ( object sender , MouseButtonEventArgs e )
19+ protected override void OnMouseUp ( MouseButtonEventArgs e )
1820 {
19- if ( e . OriginalSource is NodifyEditor editor && editor . DataContext is CalculatorViewModel calculator )
21+ if ( OpenGesture . Matches ( e . Source , e ) )
2022 {
21- e . Handled = true ;
22- calculator . OperationsMenu . OpenAt ( editor . MouseLocation ) ;
23+ ViewModel . OpenAt ( Element . MouseLocation ) ;
2324 }
2425 }
2526
26- private void CloseOperationsMenu ( object sender , MouseButtonEventArgs e )
27+ protected override void OnMouseDown ( MouseButtonEventArgs e )
2728 {
28- ItemContainer ? itemContainer = sender as ItemContainer ;
29- NodifyEditor ? editor = sender as NodifyEditor ?? itemContainer ? . Editor ;
30-
31- if ( editor ? . DataContext is CalculatorViewModel calculator )
29+ if ( CloseGesture . Matches ( e . Source , e ) )
3230 {
33- calculator . OperationsMenu . Close ( ) ;
31+ ViewModel . Close ( ) ;
3432 }
3533 }
34+ }
35+
36+ public partial class EditorView : UserControl
37+ {
38+ public EditorView ( )
39+ {
40+ InitializeComponent ( ) ;
41+ }
42+
43+ static EditorView ( )
44+ {
45+ InputProcessor . Shared < NodifyEditor > . RegisterHandlerFactory ( editor => new OperationsMenuHandler ( editor ) ) ;
46+
47+ // Ensure the selecting handler is executed after the OperationsMenuHandler, otherwise left click events will not be received.
48+ InputProcessor . Shared < NodifyEditor > . RemoveHandlerFactory < EditorState . Selecting > ( ) ;
49+ InputProcessor . Shared < NodifyEditor > . RegisterHandlerFactory ( editor => new EditorState . Selecting ( editor ) ) ;
50+ }
3651
3752 private void OnDropNode ( object sender , DragEventArgs e )
3853 {
39- if ( e . Source is NodifyEditor editor && editor . DataContext is CalculatorViewModel calculator
54+ if ( e . Source is NodifyEditor editor && editor . DataContext is CalculatorViewModel calculator
4055 && e . Data . GetData ( typeof ( OperationInfoViewModel ) ) is OperationInfoViewModel operation )
4156 {
4257 OperationViewModel op = OperationFactory . GetOperation ( operation ) ;
@@ -49,8 +64,8 @@ private void OnDropNode(object sender, DragEventArgs e)
4964
5065 private void OnNodeDrag ( object sender , MouseEventArgs e )
5166 {
52- if ( e . LeftButton == MouseButtonState . Pressed && ( ( FrameworkElement ) sender ) . DataContext is OperationInfoViewModel operation )
53- {
67+ if ( e . LeftButton == MouseButtonState . Pressed && ( ( FrameworkElement ) sender ) . DataContext is OperationInfoViewModel operation )
68+ {
5469 var data = new DataObject ( typeof ( OperationInfoViewModel ) , operation ) ;
5570 DragDrop . DoDragDrop ( this , data , DragDropEffects . Copy ) ;
5671 }
0 commit comments