Skip to content

Commit f722781

Browse files
committed
DragAndDrop
1 parent e92b72c commit f722781

File tree

4 files changed

+108
-17
lines changed

4 files changed

+108
-17
lines changed

src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public enum BroswerActionEnum
77
Typing = 3,
88
Hover = 4,
99
Scroll = 5,
10+
DragAndDrop = 6
1011
}

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementPosition.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ public class ElementPosition
55
public float X { get; set; } = default!;
66

77
public float Y { get; set; } = default!;
8+
9+
public override string ToString()
10+
{
11+
return $"[{X}, {Y}]";
12+
}
813
}

src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,23 @@ public async Task SubscribeAsync(string channel, string group, bool priorityEnab
4242

4343
while (true)
4444
{
45-
try
45+
if (priorityEnabled)
4646
{
47-
if (priorityEnabled)
47+
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
4848
{
49-
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
50-
{
51-
continue;
52-
}
53-
54-
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
55-
{
56-
continue;
57-
}
58-
59-
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
49+
continue;
6050
}
61-
else
51+
52+
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
6253
{
63-
await HandleGroupMessage(db, channel, group, received);
54+
continue;
6455
}
56+
57+
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
6558
}
66-
catch (Exception ex)
59+
else
6760
{
68-
_logger.LogError($"Error processing message: {ex.Message}\r\n{ex}");
61+
await HandleGroupMessage(db, channel, group, received);
6962
}
7063
}
7164
}

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,102 @@ await locator.ClickAsync(new LocatorClickOptions
7979
{
8080
await locator.HoverAsync();
8181
}
82+
else if (action.Action == BroswerActionEnum.DragAndDrop)
83+
{
84+
// Locate the element to drag
85+
var box = await locator.BoundingBoxAsync();
86+
87+
if (box != null)
88+
{
89+
// Calculate start position
90+
float startX = box.X + box.Width / 2; // Start at the center of the element
91+
float startY = box.Y + box.Height / 2;
92+
93+
// Drag offsets
94+
float offsetX = action.Position.X;
95+
// Move horizontally
96+
if (action.Position.Y == 0)
97+
{
98+
// Perform drag-and-move
99+
// Move mouse to the start position
100+
var mouse = page.Mouse;
101+
await mouse.MoveAsync(startX, startY);
102+
await mouse.DownAsync();
103+
104+
// Move mouse smoothly in increments
105+
var tracks = GetVelocityTrack(offsetX);
106+
foreach (var track in tracks)
107+
{
108+
startX += track;
109+
await page.Mouse.MoveAsync(startX, 0, new MouseMoveOptions
110+
{
111+
Steps = 3
112+
});
113+
}
114+
115+
// Release mouse button
116+
await mouse.UpAsync();
117+
}
118+
else
119+
{
120+
throw new NotImplementedException();
121+
}
122+
}
123+
}
82124

83125
if (action.WaitTime > 0)
84126
{
85127
await Task.Delay(1000 * action.WaitTime);
86128
}
87129
}
130+
131+
public static List<int> GetVelocityTrack(float distance)
132+
{
133+
// Initialize the track list to store the movement distances
134+
List<int> track = new List<int>();
135+
136+
// Initialize variables
137+
float current = 0; // Current position
138+
float mid = distance * 4 / 5; // Deceleration threshold
139+
float t = 0.2f; // Time interval
140+
float v = 1; // Initial velocity
141+
142+
// Generate the track
143+
while (current < distance)
144+
{
145+
float a; // Acceleration
146+
147+
// Determine acceleration based on position
148+
if (current < mid)
149+
{
150+
a = 4; // Accelerate
151+
}
152+
else
153+
{
154+
a = -3; // Decelerate
155+
}
156+
157+
// Calculate new velocity
158+
float v0 = v;
159+
v = v0 + a * t;
160+
161+
// Calculate the movement during this interval
162+
float move = v0 * t + 0.5f * a * t * t;
163+
164+
// Update current position
165+
if (current + move > distance)
166+
{
167+
move = distance - current;
168+
track.Add((int)Math.Round(move));
169+
break;
170+
}
171+
172+
current += move;
173+
174+
// Add rounded movement to the track
175+
track.Add((int)Math.Round(move));
176+
}
177+
178+
return track;
179+
}
88180
}

0 commit comments

Comments
 (0)