Skip to content

Commit 05a48ae

Browse files
committed
Remove Observable from SwtRx.
1 parent 6d26b63 commit 05a48ae

File tree

1 file changed

+30
-21
lines changed
  • durian-swt/src/main/java/com/diffplug/common/swt

1 file changed

+30
-21
lines changed

Diff for: durian-swt/src/main/java/com/diffplug/common/swt/SwtRx.java

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,12 +20,13 @@
2020
import com.diffplug.common.collect.ImmutableList;
2121
import com.diffplug.common.primitives.Ints;
2222
import com.diffplug.common.rx.Chit;
23+
import com.diffplug.common.rx.Rx;
2324
import com.diffplug.common.rx.RxBox;
24-
import io.reactivex.Observable;
25-
import io.reactivex.subjects.PublishSubject;
2625
import java.util.Collection;
2726
import java.util.function.Function;
2827
import java.util.stream.Stream;
28+
import kotlinx.coroutines.flow.Flow;
29+
import kotlinx.coroutines.flow.MutableSharedFlow;
2930
import org.eclipse.swt.SWT;
3031
import org.eclipse.swt.graphics.Point;
3132
import org.eclipse.swt.widgets.Button;
@@ -38,34 +39,42 @@
3839

3940
/** Utilities that convert SWT events into Rx-friendly Observables. */
4041
public class SwtRx {
41-
/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
42-
public static @SwtThread Observable<Event> addListener(Widget widget, int... events) {
42+
/** Subscribes to the given widget and pipes the events to an {@link Flow}<{@link Event}>. */
43+
public static @SwtThread Flow<Event> addListener(Widget widget, int... events) {
4344
return addListener(widget, Ints.asList(events));
4445
}
4546

46-
/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
47-
public static @SwtThread Observable<Event> addListener(Widget widget, Collection<Integer> events) {
47+
/** Subscribes to the given widget and pipes the events to an {@link Flow}<{@link Event}>. */
48+
public static @SwtThread Flow<Event> addListener(Widget widget, Collection<Integer> events) {
4849
return addListener(widget, events.stream());
4950
}
5051

51-
/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
52-
public static @SwtThread Observable<Event> addListener(Widget widget, Stream<Integer> events) {
53-
PublishSubject<Event> subject = PublishSubject.create();
54-
events.forEach(event -> widget.addListener(event, subject::onNext));
55-
return subject;
52+
/** Subscribes to the given widget and pipes the events to an {@link Flow}<{@link Event}>. */
53+
public static @SwtThread Flow<Event> addListener(Widget widget, Stream<Integer> events) {
54+
MutableSharedFlow<Event> observable = Rx.INSTANCE.createEmitFlow();
55+
events.forEach(event -> widget.addListener(event, e -> {
56+
Rx.INSTANCE.emit(observable, e);
57+
}));
58+
return observable;
5659
}
5760

58-
/** Returns an {@link Observable}<{@link Point}> of the right-click mouse-up on the given control, in global coordinates. */
59-
public static @SwtThread Observable<Point> rightClickGlobal(Control ctl) {
60-
return rightClickLocal(ctl).map(ctl::toDisplay);
61+
/** Returns an {@link Flow}<{@link Point}> of the right-click mouse-up on the given control, in global coordinates. */
62+
public static @SwtThread Flow<Point> rightClickGlobal(Control ctl) {
63+
MutableSharedFlow<Point> observable = Rx.INSTANCE.createEmitFlow();
64+
ctl.addListener(MouseClick.RIGHT_CLICK_EVENT, e -> {
65+
if (e.button == MouseClick.RIGHT.code()) {
66+
Rx.INSTANCE.emit(observable, ctl.toDisplay(e.x, e.y));
67+
}
68+
});
69+
return observable;
6170
}
6271

63-
/** Returns an {@link Observable}<{@link Point}> of the right-click mouse-up on the given control, in local coordinates. */
64-
public static @SwtThread Observable<Point> rightClickLocal(Control ctl) {
65-
PublishSubject<Point> observable = PublishSubject.create();
66-
ctl.addListener(SWT.MouseUp, e -> {
67-
if (e.button == 3) {
68-
observable.onNext(new Point(e.x, e.y));
72+
/** Returns an {@link Flow}<{@link Point}> of the right-click mouse-up on the given control, in local coordinates. */
73+
public static @SwtThread Flow<Point> rightClickLocal(Control ctl) {
74+
MutableSharedFlow<Point> observable = Rx.INSTANCE.createEmitFlow();
75+
ctl.addListener(MouseClick.RIGHT_CLICK_EVENT, e -> {
76+
if (e.button == MouseClick.RIGHT.code()) {
77+
Rx.INSTANCE.emit(observable, new Point(e.x, e.y));
6978
}
7079
});
7180
return observable;

0 commit comments

Comments
 (0)