Search in sources :

Example 6 with Either

use of org.reactfx.util.Either in project RichTextFX by FXMisc.

the class ParagraphBox method stationaryIndices.

public EventStream<Either<Tuple2<Point2D, Integer>, Object>> stationaryIndices(Duration delay) {
    EventStream<Either<Point2D, Void>> stationaryEvents = new MouseStationaryHelper(this).events(delay);
    EventStream<Tuple2<Point2D, Integer>> hits = stationaryEvents.filterMap(Either::asLeft).filterMap(p -> {
        OptionalInt charIdx = hit(p).getCharacterIndex();
        if (charIdx.isPresent()) {
            return Optional.of(t(p, charIdx.getAsInt()));
        } else {
            return Optional.empty();
        }
    });
    EventStream<?> stops = stationaryEvents.filter(Either::isRight).map(Either::getRight);
    return hits.or(stops);
}
Also used : MouseStationaryHelper(org.fxmisc.richtext.event.MouseStationaryHelper) Tuple2(org.reactfx.util.Tuple2) Either(org.reactfx.util.Either) OptionalInt(java.util.OptionalInt)

Example 7 with Either

use of org.reactfx.util.Either in project RichTextFX by FXMisc.

the class MouseStationaryHelper method events.

/**
 * Returns an {@link EventStream} that emits a {@link Point2D} whenever the mouse becomes stationary
 * over the helper's node and emits a {@code null} value whenever the mouse moves after being stationary.
 */
public EventStream<Either<Point2D, Void>> events(Duration delay) {
    EventStream<MouseEvent> mouseEvents = eventsOf(node, MouseEvent.ANY);
    EventStream<Point2D> stationaryPositions = mouseEvents.successionEnds(delay).filter(e -> e.getEventType() == MOUSE_MOVED).map(e -> new Point2D(e.getX(), e.getY()));
    EventStream<Void> stoppers = mouseEvents.supply((Void) null);
    return stationaryPositions.or(stoppers).distinct();
}
Also used : Either(org.reactfx.util.Either) Subscription(org.reactfx.Subscription) MouseEvent(javafx.scene.input.MouseEvent) Duration(java.time.Duration) Node(javafx.scene.Node) EventStreams(org.reactfx.EventStreams) Event(javafx.event.Event) Point2D(javafx.geometry.Point2D) EventStream(org.reactfx.EventStream) MouseEvent(javafx.scene.input.MouseEvent) Point2D(javafx.geometry.Point2D)

Aggregations

Either (org.reactfx.util.Either)7 IOException (java.io.IOException)3 ReadOnlyStyledDocument (org.fxmisc.richtext.model.ReadOnlyStyledDocument)3 StyledDocument (org.fxmisc.richtext.model.StyledDocument)3 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Node (javafx.scene.Node)2 IndexRange (javafx.scene.control.IndexRange)2 FileChooser (javafx.stage.FileChooser)2 Duration (java.time.Duration)1 List (java.util.List)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 Application (javafx.application.Application)1 BooleanBinding (javafx.beans.binding.BooleanBinding)1