-
Notifications
You must be signed in to change notification settings - Fork 236
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
autoheight based on number of lines? #930
Comments
Hi @saulpalv, I think the following is a more robust solution. Could you try it out and let me know how it works for you: class AutoHeight<PS, SEG, S> implements Consumer<ListModification>
{
private ObservableList<Double> paragraphHeights = FXCollections.observableArrayList();
private DoubleProperty totalHeight = new SimpleDoubleProperty();
private final GenericStyledArea<PS, SEG, S> area;
public AutoHeight( GenericStyledArea<PS, SEG, S> area )
{
paragraphHeights.addListener( (ListChangeListener.Change<? extends Double> chg) ->
{
while ( chg.next() ) {
double delta = chg.getAddedSubList().stream().mapToDouble( d -> d ).sum();
delta -= chg.getRemoved().stream().mapToDouble( d -> d ).sum();
totalHeight.set( totalHeight.get() + delta );
}
});
area.prefHeightProperty().bind( totalHeight );
area.setMinHeight( 16.0 );
this.area = area;
}
@Override
public void accept( ListModification lm )
{
int paragraph = lm.getFrom();
final int addEnd = paragraph + lm.getAddedSize();
final int removEnd = paragraph + lm.getRemovedSize();
final boolean removExtra = addEnd < removEnd;
for ( ; paragraph < addEnd; paragraph++ ) {
boolean replace = removExtra || paragraph < removEnd;
addOrChangeParagraph( paragraph, replace );
}
if ( removExtra ) {
paragraphHeights.remove( paragraph, removEnd );
}
}
private void addOrChangeParagraph( int paragraph, boolean replace )
{
area.showParagraphInViewport( paragraph );
area.getParagraphBoundsOnScreen( paragraph ).map( b -> b.getHeight() )
.ifPresent( height ->
{
if ( paragraph >= paragraphHeights.size() ) paragraphHeights.add( height ); // Append
else if ( replace ) paragraphHeights.set( paragraph, height ); // Replace
else paragraphHeights.add( paragraph, height ); // Insert
});
}
} Use it like so: area.getParagraphs().addModificationObserver( new AutoHeight( area ) ); |
Just a heads up that I've changed the previous code. |
i tried the code, had to put on try catch in the addOrChangeParagraph(), it works well if code is added manually, but it doesn't work if add add text using replaceText(0, 0, text); |
The reason I think why you had to add the try catch and why the replaceText isn't working for you is probably because the RichtextFX control isn't part of a Scene or Stage yet. I'll have a look and see if I can improve that. In the meantime you can try delaying calling replaceText until after the control is added to a Scene or Stage. |
Closed via PR #944 |
any ideas on how to grow height RichTextFX height to match the number of lines?, i mean to autogrow the control hieght and avoid the vertical slider (with virtualizedScrollPane) to show and see all the text, like jupyter notebook code cells do.
size can be obtained with
Bounds bounds = codeArea.getLayoutBounds();
then to grow height
codeArea.setPrefSize(bounds.getWidth(), bounds.getHeight() + grow)
the question is to calculate the grow or the size based on the number of lines, or grow until vertical scrollbar disappears, but how do i know if the vertical slider is present?
UDPATE : This methods worked for me, not perfect but job is done, suggestions accepted
The text was updated successfully, but these errors were encountered: