Skip to content

Commit

Permalink
Merge pull request #5759 from junichi11/php-avoid-being-scrolled-to-e…
Browse files Browse the repository at this point in the history
…of-in-preview-panel

Avoid being scrolled to the end of the editor in the preview panel for formatting options
  • Loading branch information
tmysik authored Apr 2, 2023
2 parents fa11160 + ac13165 commit 18db5e3
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
Expand Down Expand Up @@ -543,6 +544,10 @@ public void refreshPreview() {
// Ignore it
}

// keep the caret position
// to avoid being scrolled to the end of the editor
int caretPosition = pane.getCaretPosition();

Rectangle visibleRectangle = pane.getVisibleRect();
pane.setText(previewText);
pane.setIgnoreRepaint(true);
Expand All @@ -569,8 +574,10 @@ public void run() {
} else {
LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
}
pane.setCaretPosition(caretPosition);
pane.setIgnoreRepaint(false);
pane.scrollRectToVisible(visibleRectangle);
// invoke later because the preview pane is scrolled to the caret position when we change options after we scroll it anywhere
SwingUtilities.invokeLater(() -> pane.scrollRectToVisible(visibleRectangle));
pane.repaint(100);

}
Expand Down

0 comments on commit 18db5e3

Please sign in to comment.