|
1 | 1 | /*
|
2 |
| - * Copyright 2020 DiffPlug |
| 2 | + * Copyright (C) 2020-2022 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import com.diffplug.common.collect.ImmutableList;
|
21 | 21 | import com.diffplug.common.primitives.Ints;
|
22 | 22 | import com.diffplug.common.rx.Chit;
|
| 23 | +import com.diffplug.common.rx.Rx; |
23 | 24 | import com.diffplug.common.rx.RxBox;
|
24 |
| -import io.reactivex.Observable; |
25 |
| -import io.reactivex.subjects.PublishSubject; |
26 | 25 | import java.util.Collection;
|
27 | 26 | import java.util.function.Function;
|
28 | 27 | import java.util.stream.Stream;
|
| 28 | +import kotlinx.coroutines.flow.Flow; |
| 29 | +import kotlinx.coroutines.flow.MutableSharedFlow; |
29 | 30 | import org.eclipse.swt.SWT;
|
30 | 31 | import org.eclipse.swt.graphics.Point;
|
31 | 32 | import org.eclipse.swt.widgets.Button;
|
|
38 | 39 |
|
39 | 40 | /** Utilities that convert SWT events into Rx-friendly Observables. */
|
40 | 41 | 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) { |
43 | 44 | return addListener(widget, Ints.asList(events));
|
44 | 45 | }
|
45 | 46 |
|
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) { |
48 | 49 | return addListener(widget, events.stream());
|
49 | 50 | }
|
50 | 51 |
|
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; |
56 | 59 | }
|
57 | 60 |
|
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; |
61 | 70 | }
|
62 | 71 |
|
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)); |
69 | 78 | }
|
70 | 79 | });
|
71 | 80 | return observable;
|
|
0 commit comments