Skip to content

Add Smooth Scrolling to the PDE #1001

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions app/src/processing/app/syntax/JEditTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,33 +172,23 @@ public JEditTextArea(TextAreaDefaults defaults, InputHandler inputHandler) {
// focusedComponent = this;

addMouseWheelListener(e -> {
if (scrollBarsInitialized) {
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int scrollAmount = e.getUnitsToScroll();
// System.out.println("rot/amt = " + e.getWheelRotation() + " " + amt);
// int max = vertical.getMaximum();
// System.out.println("UNIT SCROLL of " + amt + " at value " + vertical.getValue() + " and max " + max);
// System.out.println(" get wheel rotation is " + e.getWheelRotation());
// int ex = e.getModifiersEx();
// String mods = InputEvent.getModifiersExText(ex);
// if (ex != 0) {
// System.out.println(" 3 2 1 0");
// System.out.println(" 10987654321098765432109876543210");
// System.out.println(" " + PApplet.binary(e.getModifiersEx()));
//// if (mods.length() > 0) {
// System.out.println(" mods extext = " + mods + " " + mods.length() + " " + PApplet.hex(mods.charAt(0)));
// }
// System.out.println(" " + e);

// inertia scrolling on OS X will fire several shift-wheel events
// that are negative values.. this makes the scrolling area jump.
boolean isHorizontal = Platform.isMacOS() && e.isShiftDown();
if (isHorizontal) {
horizontal.setValue(horizontal.getValue() + scrollAmount);
}else{
vertical.setValue(vertical.getValue() + scrollAmount);
}
}
if (!scrollBarsInitialized) return;
if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;

var smoothScrollAmount = e.getPreciseWheelRotation();
var isHorizontal = Platform.isMacOS() && e.isShiftDown();
if (isHorizontal) {
smoothHorizontal += smoothScrollAmount;
smoothHorizontal = Math.max(0, smoothHorizontal);
smoothHorizontal = Math.min(horizontal.getMaximum(), smoothHorizontal);
painter.repaint();
horizontal.setValue((int) Math.round(smoothHorizontal));
}else{
smoothVertical += smoothScrollAmount;
smoothVertical = Math.max(0, smoothVertical);
smoothVertical = Math.min(vertical.getMaximum(), smoothVertical);
painter.repaint();
vertical.setValue((int) Math.round(smoothVertical));
}
});
}
Expand Down Expand Up @@ -286,6 +276,10 @@ public void setHorizontalScrollPosition(int what) {
}


public double getVerticalSmoothScrollPosition() {
return smoothVertical;
}

/**
* Returns the object responsible for painting this text area.
*/
Expand Down Expand Up @@ -2093,7 +2087,9 @@ public void processKeyEvent(KeyEvent event) {
protected int horizontalOffset;

protected JScrollBar vertical;
protected double smoothVertical;
protected JScrollBar horizontal;
protected double smoothHorizontal;
protected boolean scrollBarsInitialized;

protected InputHandler inputHandler;
Expand Down
5 changes: 5 additions & 0 deletions app/src/processing/app/syntax/TextAreaPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.swing.text.*;
import javax.swing.JComponent;

import processing.app.Messages;
import processing.app.Preferences;
import processing.app.syntax.im.CompositionTextPainter;

Expand Down Expand Up @@ -230,6 +231,10 @@ public void paint(Graphics gfx) {
int height = fontMetrics.getHeight();
int firstLine = textArea.getFirstLine();
int firstInvalid = firstLine + clipRect.y / height;

double verticalSmoothOffset = (textArea.getVerticalSmoothScrollPosition() - firstLine) * fontMetrics.getHeight();
g2.translate(0, -verticalSmoothOffset);

// Because the clipRect height is usually an even multiple
// of the font height, we subtract 1 from it, otherwise one
// too many lines will always be painted.
Expand Down