-
Notifications
You must be signed in to change notification settings - Fork 16
Improve calculations of total requested height by caching cell heights #413
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -989,21 +989,22 @@ void keyTypedListener(KeyEvent e) { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| private void computeFullHeight() { | ||||||||||||||||
| RichListCell cell = new RichListCell(RichTextAreaSkin.this); | ||||||||||||||||
| double rtaWidth = getSkinnable().prefWidth(-1); | ||||||||||||||||
| Scene scene = new Scene(cell, rtaWidth, 1000); | ||||||||||||||||
| if (getSkinnable().getScene() != null) { | ||||||||||||||||
| scene.getStylesheets().setAll(getSkinnable().getScene().getStylesheets()); | ||||||||||||||||
| ObservableList<Paragraph> items = paragraphListView.getItems(); | ||||||||||||||||
| int psize = items.size(); | ||||||||||||||||
| int unknown = 0; | ||||||||||||||||
| double total = 0.; | ||||||||||||||||
| for (Paragraph p : items) { | ||||||||||||||||
| double ph = p.getPreferredHeight(); | ||||||||||||||||
| if (ph < 0) { | ||||||||||||||||
| unknown++; | ||||||||||||||||
| } else { | ||||||||||||||||
| total = total + ph; | ||||||||||||||||
|
||||||||||||||||
| total = total + ph; | |
| total += ph; |
Outdated
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Division by zero will occur when all paragraphs have unknown heights (i.e., psize == unknown). In this case, (psize - unknown) becomes 0, resulting in a division by zero error.
Add a check to handle this edge case:
if (unknown > 0) {
if (unknown == psize) {
// All heights are unknown, cannot estimate
total = 0.;
} else {
total = total * psize / (psize - unknown);
}
}| total = total * psize / (psize - unknown); | |
| if (unknown == psize) { | |
| // All heights are unknown, cannot estimate | |
| total = 0.0; | |
| } else { | |
| total = total * psize / (psize - unknown); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,8 @@ public class Paragraph { | |||||||||||||||||||
|
|
||||||||||||||||||||
| private ParagraphDecoration decoration; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private double preferredHeight = -1.; // cache the expected height | ||||||||||||||||||||
|
||||||||||||||||||||
| private double preferredHeight = -1.; // cache the expected height | |
| /** | |
| * Cached preferred height of the paragraph cell. | |
| * <p> | |
| * A value of -1 indicates that the height has not been computed or is invalid, | |
| * and needs to be recalculated. The cache should be invalidated and reset to -1 | |
| * whenever the paragraph's content or decoration changes in a way that affects its height. | |
| */ | |
| private double preferredHeight = -1.; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
this.getHeight()immediately afterupdateLayout()may not return the correct height. The cell's height is typically computed during the layout pass, which happens asynchronously. This could result in caching an incorrect (possibly 0 or outdated) height value.Consider using
prefHeight()instead, which computes the preferred height based on the current content:Alternatively, if you need the actual rendered height, consider caching it in a later phase of the layout cycle using
layoutBoundsProperty()listener or overridinglayoutChildren().