Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added foreign language input composition #985

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.beans.NamedArg;
import javafx.beans.binding.Bindings;
Expand Down Expand Up @@ -46,6 +47,9 @@
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.IndexRange;
import javafx.scene.input.InputMethodEvent;
import javafx.scene.input.InputMethodRequests;
import javafx.scene.input.InputMethodTextRun;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
Expand Down Expand Up @@ -821,6 +825,81 @@ public GenericStyledArea(

placeHolderProp.addListener( (ob,ov,newNode) -> displayPlaceHolder( showPlaceholder.getValue(), newNode ) );
showPlaceholder.addListener( (ob,ov,show) -> displayPlaceHolder( show, getPlaceholder() ) );

if ( Platform.isFxApplicationThread() ) initInputMethodHandling();
else Platform.runLater( () -> initInputMethodHandling() );
}

private void initInputMethodHandling()
{
if( Platform.isSupported( ConditionalFeature.INPUT_METHOD ) )
{
setOnInputMethodTextChanged( event -> handleInputMethodEvent(event) );
// Both of these have to be set for input composition to work !
setInputMethodRequests( new InputMethodRequests()
{
@Override public Point2D getTextLocation( int offset ) {
Bounds charBounds = getCaretBounds().get();
return new Point2D( charBounds.getMaxX() - 5, charBounds.getMaxY() );
}

@Override public int getLocationOffset( int x, int y ) {
return 0;
}

@Override public void cancelLatestCommittedText() {}

@Override public String getSelectedText() {
return getSelectedText();
}
});
}
}

// Start/Length of the text under input method composition
private int imstart;
private int imlength;

protected void handleInputMethodEvent( InputMethodEvent event )
{
if ( isEditable() && !isDisabled() )
{
// remove previous input method text (if any) or selected text
if ( imlength != 0 ) {
selectRange( imstart, imstart + imlength );
}

// Insert committed text
if ( event.getCommitted().length() != 0 ) {
replaceText( getSelection(), event.getCommitted() );
}

// Replace composed text
imstart = getSelection().getStart();
StringBuilder composed = new StringBuilder();

for ( InputMethodTextRun run : event.getComposed() ) {
composed.append( run.getText() );
}

replaceText( getSelection(), composed.toString() );
imlength = composed.length();

if ( imlength != 0 )
{
int pos = imstart;
for ( InputMethodTextRun run : event.getComposed() ) {
int endPos = pos + run.getText().length();
pos = endPos;
}

// Set caret position in composed text
int caretPos = event.getCaretPosition();
if ( caretPos >= 0 && caretPos < imlength ) {
selectRange( imstart + caretPos, imstart + caretPos );
}
}
}
}

private Node placeholder;
Expand Down