Skip to content

Commit

Permalink
Moved the call to setPropagateToPeerEvent() from TextInputControlBeha…
Browse files Browse the repository at this point in the history
…vior to TextInputControl, so it happens even if no skins (ex: TextArea)
  • Loading branch information
salmonb committed Jul 19, 2024
1 parent e539032 commit 2f2ede1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public static void fireEvent(EventTarget eventTarget, Event event) {
// back to the browser even if it has been consumed by JavaFX. This is the purpose of the propagateToPeerEvent field.
private static Event propagateToPeerEvent;

// This setter can be called by the control (or behaviour) that consumed the event in JavaFX to request WebFX to
// not stop its propagation, but pass it to the peer.
// This setter is called by TextInputControl that consumed an event in JavaFX (to stop its propagation in JavaFX),
// but still requests WebFX to pass the event to the html peer to solve the case explained above.
public static void setPropagateToPeerEvent(Event propagateToPeerEvent) {
Event.propagateToPeerEvent = propagateToPeerEvent;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.sun.javafx.scene.control.behavior;

import javafx.event.Event;
import javafx.scene.control.TextInputControl;
import javafx.scene.input.KeyEvent;

import java.util.List;

Expand All @@ -23,22 +21,6 @@ public abstract class TextInputControlBehavior<T extends TextInputControl> exten
*/
public TextInputControlBehavior(T textInputControl, List<KeyBinding> bindings) {
super(textInputControl, bindings);
// Although the key events are entirely managed by the peer, we consume them in JavaFX to not propagate these
// events to further JavaFX controls.
textInputControl.addEventHandler(KeyEvent.ANY, e -> {
if (textInputControl.isFocused()) {
// Exception is made for accelerators such as Enter or ESC, as they should be passed beyond this control
switch (e.getCode()) {
case ENTER:
case ESCAPE:
return;
}
// Otherwise, we stop the propagation in JavaFX
e.consume();
// But we still ask WebFX to propagate them to the peer.
Event.setPropagateToPeerEvent(e); // See WebFX comments on Event class for more explanation.
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.webfx.kit.mapper.peers.javafxgraphics.markers.HasPromptTextProperty;
import dev.webfx.kit.mapper.peers.javafxgraphics.markers.HasTextProperty;
import javafx.beans.property.*;
import javafx.event.Event;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Font;

/**
Expand Down Expand Up @@ -98,4 +100,23 @@ public interface SelectableTextInputControlPeer {
void selectRange(int anchor, int caretPosition);

}

{
// Although the key events are entirely managed by the html peer, we consume them in JavaFX to not propagate
// these events to further JavaFX controls.
addEventHandler(KeyEvent.ANY, e -> {
if (isFocused()) {
// Exception is made for accelerators such as Enter or ESC, as they should be passed beyond this control
switch (e.getCode()) {
case ENTER:
case ESCAPE:
return;
}
// Otherwise, we stop the propagation in JavaFX
e.consume();
// But we still ask WebFX to propagate them to the peer.
Event.setPropagateToPeerEvent(e); // See WebFX comments on Event class for more explanation.
}
});
}
}

0 comments on commit 2f2ede1

Please sign in to comment.