-
Notifications
You must be signed in to change notification settings - Fork 20
/
DragInitializer.razor.cs
99 lines (81 loc) · 2.79 KB
/
DragInitializer.razor.cs
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
using Fluxor;
using Fluxor.Blazor.Web.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Luthetus.Common.RazorLib.Keys.Models;
using Luthetus.Common.RazorLib.Dynamics.Models;
using Luthetus.Common.RazorLib.Reactives.Models;
namespace Luthetus.Common.RazorLib.Drags.Displays;
public partial class DragInitializer : FluxorComponent
{
[Inject]
private IState<DragState> DragStateWrap { get; set; } = null!;
[Inject]
private IDispatcher Dispatcher { get; set; } = null!;
private string StyleCss => DragStateWrap.Value.ShouldDisplay
? string.Empty
: "display: none;";
private ThrottleOptimized<MouseEvent> _throttle;
public struct MouseEvent
{
public MouseEvent(bool isOnMouseMove, MouseEventArgs mouseEventArgs)
{
IsOnMouseMove = isOnMouseMove;
MouseEventArgs = mouseEventArgs;
}
public bool IsOnMouseMove { get; }
public MouseEventArgs MouseEventArgs { get; }
}
private IDropzone? _onMouseOverDropzone = null;
protected override void OnInitialized()
{
_throttle = new(ThrottleFacts.TwentyFour_Frames_Per_Second, async (args, _) =>
{
if (args.IsOnMouseMove)
{
if ((args.MouseEventArgs.Buttons & 1) != 1)
DispatchClearDragStateAction();
else
Dispatcher.Dispatch(new DragState.ShouldDisplayAndMouseEventArgsSetAction(true, args.MouseEventArgs));
return;
}
else
{
var dragState = DragStateWrap.Value;
var localOnMouseOverDropzone = _onMouseOverDropzone;
DispatchClearDragStateAction();
var draggableViewModel = dragState.Drag;
if (draggableViewModel is not null)
{
await draggableViewModel
.OnDragEndAsync(args.MouseEventArgs, localOnMouseOverDropzone)
.ConfigureAwait(false);
}
}
});
base.OnInitialized();
}
private void DispatchClearDragStateAction()
{
_onMouseOverDropzone = null;
Dispatcher.Dispatch(new DragState.ShouldDisplayAndMouseEventArgsAndDragSetAction(
false,
null,
null));
}
private void DispatchSetDragStateActionOnMouseMove(MouseEventArgs mouseEventArgs)
{
_throttle.Run(new(isOnMouseMove: true, mouseEventArgs));
}
private void DispatchSetDragStateActionOnMouseUp(MouseEventArgs mouseEventArgs)
{
_throttle.Run(new(isOnMouseMove: false, mouseEventArgs));
}
private string GetIsActiveCssClass(IDropzone dropzone)
{
var onMouseOverDropzoneKey = _onMouseOverDropzone?.DropzoneKey ?? Key<IDropzone>.Empty;
return onMouseOverDropzoneKey == dropzone.DropzoneKey
? "luth_active"
: string.Empty;
}
}