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

autoheight based on number of lines? #930

Closed
3xau1o opened this issue Jun 3, 2020 · 5 comments
Closed

autoheight based on number of lines? #930

3xau1o opened this issue Jun 3, 2020 · 5 comments

Comments

@3xau1o
Copy link

3xau1o commented Jun 3, 2020

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

    public void autoresize() {
        double rows = getDocument().getParagraphs().size();
        Optional<Bounds> lineBound = getParagraphBoundsOnScreen(0);
        double rowSize_;

        if(rowSize==null){
            if (lineBound.isPresent() == true){
                rowSize_ = lineBound.get().getHeight();
                rowSize = rowSize_;
            }
            else rowSize_ = 25d;
        }
        else rowSize_ = rowSize;

        this.setPrefSize(getLayoutBounds().getWidth(), rowSize_ * rows + 5); //(rows*0.95d)
    }

    public void autoresizeAlternative() {
        Text text = new Text();
        text.setFont(new Font(18));
        text.setWrappingWidth(getWidth());
        text.setText(getText());
        double rows = getDocument().getParagraphs().size();
        double fix = (rows >= 20) ? rows - rows * 0.1d : 0d;
        setPrefSize(getLayoutBounds().getWidth(), text.getLayoutBounds().getHeight() + fix);
    }
@Jugen
Copy link
Collaborator

Jugen commented Jun 9, 2020

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 ) );

@Jugen
Copy link
Collaborator

Jugen commented Jun 10, 2020

Just a heads up that I've changed the previous code.
Also the area can be wrapped in a VirtualizedScrollPane if needed, but some tweaking still needs to be done to the submitted code to keep the vertical scrollbar hidden.

@3xau1o
Copy link
Author

3xau1o commented Jun 18, 2020

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);

@Jugen
Copy link
Collaborator

Jugen commented Jun 18, 2020

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.

@Jugen Jugen mentioned this issue Aug 13, 2020
@Jugen
Copy link
Collaborator

Jugen commented Oct 2, 2020

Closed via PR #944

@Jugen Jugen closed this as completed Oct 2, 2020
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

No branches or pull requests

2 participants