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

Add feature: get character bounds on screen #455

Merged
merged 1 commit into from
Mar 23, 2017
Merged

Add feature: get character bounds on screen #455

merged 1 commit into from
Mar 23, 2017

Conversation

JordanMartinez
Copy link
Contributor

A few questions I have:

  • Should selectionShape be used to get the bounds? Will that switch-out approach cause any issues later on? I didn't want to create a new Path object each time the method is called so as to reduce memory usage.
  • I used the FORWARD bias in endPosition = offsetToPosition(to, Bias.FORWARD). Is that correct? Or should it be BACKWARD? I still don't understand how that part of the code works.

@JordanMartinez
Copy link
Contributor Author

Addresses #150.

@JordanMartinez
Copy link
Contributor Author

Manually tested this code (well, how would you test it via TestFX...?) with:

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.stage.Popup;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.InlineCssTextArea;

import java.util.Optional;

public class CharacterBoundsDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        InlineCssTextArea area = new InlineCssTextArea("0123456789\n123456789\n123456789");
        area.setWrapText(true);
        area.setStyle("-fx-font-size: 32pt;");
        VirtualizedScrollPane<InlineCssTextArea> vsPane = new VirtualizedScrollPane<>(area);

        Region r = new Region();
        r.setStyle("-fx-background-color: red;");
        Popup p = new Popup();
        p.getContent().add(r);

        TextField fromField = new TextField("0");
        TextField toField = new TextField("10");

        Button b = new Button("Show");
        b.setOnAction(ae -> {
            int from = Integer.parseInt(fromField.getText());
            int to = Integer.parseInt(toField.getText());
            Optional<Bounds> optB = area.getCharacterBoundsOnScreen(from, to);
            optB.ifPresent(bounds -> {
                r.setMinSize(bounds.getWidth(), bounds.getHeight());
                p.show(primaryStage, bounds.getMinX(), bounds.getMinY());
            });
        });
        BorderPane root = new BorderPane();
        root.setCenter(vsPane);
        root.setBottom(new HBox(fromField, toField, b));

        primaryStage.setScene(new Scene(root, 200, 400));
        primaryStage.show();
    }
}

@JordanMartinez
Copy link
Contributor Author

Should selectionShape be used to get the bounds? Will that switch-out approach cause any issues later on? I didn't want to create a new Path object each time the method is called so as to reduce memory usage.

I don't see this as a problem because it'll only run on the JavaFX Application Thread, so there shouldn't be any issue with concurrency-related things. Additionally, selectionShape is already bound to leftInsets and topInsets which does affect the accuracy of the returned Bounds object.

I used the FORWARD bias in endPosition = offsetToPosition(to, Bias.FORWARD). Is that correct? Or should it be BACKWARD? I still don't understand how that part of the code works.

After testing this out, there wasn't any difference between FORWARD and BACKWARD even across multi-line paragraphs or multiple paragraphs. I think that only matters for some other thing Position-related thing, though Tomas could better explain that.

@JordanMartinez JordanMartinez merged commit e28a9eb into FXMisc:master Mar 23, 2017
@JordanMartinez JordanMartinez deleted the characterBounds branch March 23, 2017 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant