Skip to content

Commit b8551f3

Browse files
Merge pull request #863 from Acardiac/swing-rel-mouse-motion
Optimize SwingMouseEventSource.fromRelativeMouseMotion
2 parents b0d975c + d764c1b commit b8551f3

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

rxjava-contrib/rxjava-swing/src/main/java/rx/swing/sources/MouseEventSource.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,36 +120,13 @@ public void call() {
120120
* @see rx.observables.SwingObservable#fromRelativeMouseMotion
121121
*/
122122
public static Observable<Point> fromRelativeMouseMotion(final Component component) {
123-
class OldAndRelative {
124-
public final Point old;
125-
public final Point relative;
126-
127-
private OldAndRelative(Point old, Point relative) {
128-
this.old = old;
129-
this.relative = relative;
130-
}
131-
}
132-
133-
class Relativize implements Func2<OldAndRelative, MouseEvent, OldAndRelative> {
123+
final Observable<MouseEvent> events = fromMouseMotionEventsOf(component);
124+
return Observable.zip(events, events.skip(1), new Func2<MouseEvent, MouseEvent, Point>() {
134125
@Override
135-
public OldAndRelative call(OldAndRelative last, MouseEvent event) {
136-
Point current = new Point(event.getX(), event.getY());
137-
Point relative = new Point(current.x - last.old.x, current.y - last.old.y);
138-
return new OldAndRelative(current, relative);
126+
public Point call(MouseEvent ev1, MouseEvent ev2) {
127+
return new Point(ev2.getX() - ev1.getX(), ev2.getY() - ev1.getY());
139128
}
140-
}
141-
142-
class OnlyRelative implements Func1<OldAndRelative, Point> {
143-
@Override
144-
public Point call(OldAndRelative oar) {
145-
return oar.relative;
146-
}
147-
}
148-
149-
return fromMouseMotionEventsOf(component)
150-
.scan(new OldAndRelative(new Point(0, 0), new Point(0, 0)), new Relativize())
151-
.map(new OnlyRelative())
152-
.skip(2); // skip the useless initial value and the invalid first computation
129+
});
153130
}
154131

155132
public static class UnitTest {

0 commit comments

Comments
 (0)