Skip to content
11 changes: 10 additions & 1 deletion rta/src/main/java/com/gluonhq/richtextarea/RichTextAreaSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ interface ActionBuilder extends Function<KeyEvent, ActionCmd>{}
private final Map<String, Image> imageCache = new ConcurrentHashMap<>();
private final SmartTimer objectsCacheEvictionTimer;

private final Consumer<TextBuffer.Event> textChangeListener = e -> refreshTextFlow();
private final Consumer<TextBuffer.Event> textChangeListener = e -> textBufferChanged(e);
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda expression can be simplified to a method reference for better readability.

Suggested change
private final Consumer<TextBuffer.Event> textChangeListener = e -> textBufferChanged(e);
private final Consumer<TextBuffer.Event> textChangeListener = this::textBufferChanged;

Copilot uses AI. Check for mistakes.
int lastValidCaretPosition = -1;
int mouseDragStart = -1;
int dragAndDropStart = -1;
Expand Down Expand Up @@ -834,6 +834,15 @@ private void updatePromptNodeLocation() {
promptNode.relocate(x, origin.getY());
}

private void textBufferChanged(TextBuffer.Event e) {
nonTextNodes.set(0);
viewModel.resetCharacterIterator();
if (nonTextNodesCount != nonTextNodes.get()) {
// when number of images changes, caret
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incomplete and unclear. It starts with "when number of images changes, caret" but doesn't finish the thought. Consider completing this comment to explain what happens to the caret when the number of images changes.

Copilot uses AI. Check for mistakes.
requestLayout();
nonTextNodesCount = nonTextNodes.get();
Comment on lines +838 to +843
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new textBufferChanged method is missing several important operations that were present in refreshTextFlow:

  1. paragraphListView.updateLayout() - which ensures decoration changes are applied
  2. getSkinnable().requestFocus() - which requests focus on the component
  3. The objectsCacheEvictionTimer pause/start logic for managing cache eviction

Without these, text buffer changes may not properly update the UI (decoration changes won't be applied), focus behavior may be incorrect, and the cache eviction timer won't be properly managed. Consider whether these operations are intentionally omitted or if they should be included in this method.

Suggested change
nonTextNodes.set(0);
viewModel.resetCharacterIterator();
if (nonTextNodesCount != nonTextNodes.get()) {
// when number of images changes, caret
requestLayout();
nonTextNodesCount = nonTextNodes.get();
objectsCacheEvictionTimer.pause();
try {
nonTextNodes.set(0);
viewModel.resetCharacterIterator();
// this ensures changes in decoration are applied:
paragraphListView.updateLayout();
computeFullHeight();
if (nonTextNodesCount != nonTextNodes.get()) {
// when number of images changes, caret
requestLayout();
nonTextNodesCount = nonTextNodes.get();
}
getSkinnable().requestFocus();
} finally {
objectsCacheEvictionTimer.start();

Copilot uses AI. Check for mistakes.
}
}
Comment on lines +837 to +845
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Event parameter 'e' is not used in this method, but there are different event types (InsertEvent, DeleteEvent, DecorateEvent) available in TextBuffer.Event. Consider using the event type to determine whether certain operations are needed. For example, DecorateEvent might specifically require paragraphListView.updateLayout() to ensure decoration changes are applied, while InsertEvent and DeleteEvent might not.

Copilot uses AI. Check for mistakes.
// TODO Need more optimal way of rendering text fragments.
// For now rebuilding the whole text flow
private void refreshTextFlow() {
Expand Down