Skip to content

Commit c1e0abe

Browse files
authored
Improve Scroll API and docs (#43)
* Add duration and steps * Fix tests and improve API * Making code more simple * Fixing the direction for linux
1 parent 95348aa commit c1e0abe

File tree

17 files changed

+151
-71
lines changed

17 files changed

+151
-71
lines changed

Desktop.Robot.TestApp/MouseTests.fs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ let tests (window:Window) = testList "Mouse tests" [
112112

113113
uiTest "Can scroll vertical" <| async {
114114
let wheelDeltas = window.PointerWheelChanged.Select(fun x -> x.Delta)
115-
115+
let robot = Robot();
116+
robot.AutoDelay <- 100;
116117
let! deltaEvents = attemptUIActionList wheelDeltas <| async {
117-
Robot().MouseScrollVertical(100) // scroll down
118-
Robot().MouseScrollVertical(-100) // then scroll up
118+
robot.MouseScroll(100) // scroll down
119+
robot.MouseScroll(-100) // then scroll up
119120
}
120121
Expect.hasLength deltaEvents 2 "Should have a wheel event for each mouse scroll"
121122
let xDeltas = deltaEvents |> List.map (fun p -> p.X)

Desktop.Robot/AbstractRobot.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Desktop.Robot.Clicks;
2+
using System;
23
using System.Drawing;
34
using System.Drawing.Imaging;
45
using System.Threading;
@@ -7,7 +8,7 @@ namespace Desktop.Robot
78
{
89
public abstract class AbstractRobot : IRobot
910
{
10-
public uint AutoDelay { get; set; }
11+
public int AutoDelay { get; set; }
1112

1213
public abstract Point GetMousePosition();
1314

@@ -25,7 +26,11 @@ public abstract class AbstractRobot : IRobot
2526

2627
public abstract void MouseMove(int x, int y);
2728

28-
public abstract void MouseScrollVertical(int value);
29+
public abstract void MouseScroll(int value);
30+
31+
public abstract void MouseScroll(int value, TimeSpan duration);
32+
33+
public abstract void MouseScroll(int value, TimeSpan duration, int steps);
2934

3035
public void MouseMove(Point p)
3136
{
@@ -64,17 +69,14 @@ public void MouseUp(IClick click)
6469
click.ExecuteMouseUp(new MouseContext(GetMousePosition()));
6570
}
6671

67-
public void Delay(uint ms)
72+
public void Delay(int ms)
6873
{
69-
Thread.Sleep((int)ms);
74+
Thread.Sleep(ms);
7075
}
7176

7277
protected void ApplyAutoDelay()
7378
{
74-
if (AutoDelay > 0)
75-
{
76-
Thread.Sleep((int)AutoDelay);
77-
}
79+
Thread.Sleep(AutoDelay);
7880
}
79-
}
81+
}
8082
}

Desktop.Robot/Clicks/IClick.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Threading;
1+
using System.Threading;
32

43
namespace Desktop.Robot.Clicks
54
{

Desktop.Robot/Clicks/Linux/MiddleClick.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
namespace Desktop.Robot.Clicks.Linux
1+
namespace Desktop.Robot.Clicks.Linux
32
{
43
internal record MiddleClick(int delay) : IClick
54
{

Desktop.Robot/Clicks/MouseContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Drawing;
1+
using System.Drawing;
32

43
namespace Desktop.Robot.Clicks
54
{

Desktop.Robot/Desktop.Robot.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4-
<Version>1.4.0</Version>
4+
<Version>1.4.1</Version>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<Authors>Lucas Simas</Authors>
77
<Description>A library used to control your mouse and keyboard programmatically in .NET Core</Description>
88
<PackageProjectUrl>https://lucassklp.github.io/Desktop.Robot/</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/lucassklp/Desktop.Robot</RepositoryUrl>
1010
<PackageIcon>logo.mini.png</PackageIcon>
1111
<PackageLicenseFile>LICENSE</PackageLicenseFile>
12+
<PackageReadmeFile>README.md</PackageReadmeFile>
1213
</PropertyGroup>
1314

1415
<ItemGroup>
1516
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
1617
<None Include="..\Resources\logo.mini.png" Pack="true" PackagePath="\" />
18+
<None Include="..\README.md" Pack="true" PackagePath="\" />
1719
</ItemGroup>
1820

1921
<ItemGroup>

Desktop.Robot/Extensions/BezierMouseMovementExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public static void BezierMovement(this IRobot robot, Point initial, Point contro
4646
points.Add(GetPoint(t, initial, controlPoint, ending));
4747
}
4848

49-
var interval = duration.TotalMilliseconds / points.Count;
49+
var interval = Convert.ToInt32(duration.TotalMilliseconds / points.Count);
5050

5151
robot.MouseMove(initial);
5252

5353
//Avoiding AutoDelay
5454
var currentAutoDelay = robot.AutoDelay;
55-
robot.AutoDelay = (uint)interval;
55+
robot.AutoDelay = interval;
5656

5757
foreach (var point in points)
5858
{

Desktop.Robot/Extensions/ListenersExtension.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Drawing;
43
using System.Reactive.Concurrency;
54
using System.Reactive.Disposables;
65
using System.Reactive.Linq;
7-
using System.Runtime.CompilerServices;
8-
using System.Runtime.InteropServices;
96
using System.Threading;
107

118
namespace Desktop.Robot.Extensions

Desktop.Robot/Extensions/TypingExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public static IRobot Type(this IRobot robot, string text)
1414
return robot;
1515
}
1616

17-
public static IRobot Type(this IRobot robot, string text, uint delay)
17+
public static IRobot Type(this IRobot robot, string text, int delay)
1818
{
19-
Thread.Sleep((int)robot.AutoDelay);
19+
Thread.Sleep(robot.AutoDelay);
2020
var currentDelay = robot.AutoDelay;
2121
robot.AutoDelay = delay;
2222
foreach (var ch in text)

Desktop.Robot/IRobot.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Drawing;
1+
using System;
2+
using System.Drawing;
23
using Desktop.Robot.Clicks;
34

45
namespace Desktop.Robot
56
{
67
public interface IRobot
78
{
8-
uint AutoDelay { get; set; }
9+
int AutoDelay { get; set; }
910
Image CreateScreenCapture(Rectangle screenRect);
1011
Color GetPixelColor(int x, int y);
1112
void KeyPress(Key key);
@@ -17,10 +18,12 @@ public interface IRobot
1718
void Click(IClick click);
1819
void MouseDown(IClick click);
1920
void MouseUp(IClick click);
20-
void Delay(uint ms);
21+
void Delay(int ms);
2122
void MouseMove(int x, int y);
2223
void MouseMove(Point p);
23-
void MouseScrollVertical(int value);
24+
void MouseScroll(int value);
25+
void MouseScroll(int value, TimeSpan duration);
26+
void MouseScroll(int value, TimeSpan duration, int steps);
2427
Point GetMousePosition();
2528
}
2629
}

0 commit comments

Comments
 (0)