Skip to content

Commit 4f06969

Browse files
committed
Initial Smooth Scrolling Fix
1 parent 9760a63 commit 4f06969

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

app/src/processing/app/syntax/JEditTextArea.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -172,33 +172,23 @@ public JEditTextArea(TextAreaDefaults defaults, InputHandler inputHandler) {
172172
// focusedComponent = this;
173173

174174
addMouseWheelListener(e -> {
175-
if (scrollBarsInitialized) {
176-
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
177-
int scrollAmount = e.getUnitsToScroll();
178-
// System.out.println("rot/amt = " + e.getWheelRotation() + " " + amt);
179-
// int max = vertical.getMaximum();
180-
// System.out.println("UNIT SCROLL of " + amt + " at value " + vertical.getValue() + " and max " + max);
181-
// System.out.println(" get wheel rotation is " + e.getWheelRotation());
182-
// int ex = e.getModifiersEx();
183-
// String mods = InputEvent.getModifiersExText(ex);
184-
// if (ex != 0) {
185-
// System.out.println(" 3 2 1 0");
186-
// System.out.println(" 10987654321098765432109876543210");
187-
// System.out.println(" " + PApplet.binary(e.getModifiersEx()));
188-
//// if (mods.length() > 0) {
189-
// System.out.println(" mods extext = " + mods + " " + mods.length() + " " + PApplet.hex(mods.charAt(0)));
190-
// }
191-
// System.out.println(" " + e);
192-
193-
// inertia scrolling on OS X will fire several shift-wheel events
194-
// that are negative values.. this makes the scrolling area jump.
195-
boolean isHorizontal = Platform.isMacOS() && e.isShiftDown();
196-
if (isHorizontal) {
197-
horizontal.setValue(horizontal.getValue() + scrollAmount);
198-
}else{
199-
vertical.setValue(vertical.getValue() + scrollAmount);
200-
}
201-
}
175+
if (!scrollBarsInitialized) return;
176+
if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
177+
178+
var smoothScrollAmount = e.getPreciseWheelRotation();
179+
var isHorizontal = Platform.isMacOS() && e.isShiftDown();
180+
if (isHorizontal) {
181+
smoothHorizontal += smoothScrollAmount;
182+
smoothHorizontal = Math.max(0, smoothHorizontal);
183+
smoothHorizontal = Math.min(horizontal.getMaximum(), smoothHorizontal);
184+
painter.repaint();
185+
horizontal.setValue((int) Math.round(smoothHorizontal));
186+
}else{
187+
smoothVertical += smoothScrollAmount;
188+
smoothVertical = Math.max(0, smoothVertical);
189+
smoothVertical = Math.min(vertical.getMaximum(), smoothVertical);
190+
painter.repaint();
191+
vertical.setValue((int) Math.round(smoothVertical));
202192
}
203193
});
204194
}
@@ -286,6 +276,10 @@ public void setHorizontalScrollPosition(int what) {
286276
}
287277

288278

279+
public double getVerticalSmoothScrollPosition() {
280+
return smoothVertical;
281+
}
282+
289283
/**
290284
* Returns the object responsible for painting this text area.
291285
*/
@@ -2093,7 +2087,9 @@ public void processKeyEvent(KeyEvent event) {
20932087
protected int horizontalOffset;
20942088

20952089
protected JScrollBar vertical;
2090+
protected double smoothVertical;
20962091
protected JScrollBar horizontal;
2092+
protected double smoothHorizontal;
20972093
protected boolean scrollBarsInitialized;
20982094

20992095
protected InputHandler inputHandler;

app/src/processing/app/syntax/TextAreaPainter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.swing.text.*;
2626
import javax.swing.JComponent;
2727

28+
import processing.app.Messages;
2829
import processing.app.Preferences;
2930
import processing.app.syntax.im.CompositionTextPainter;
3031

@@ -230,6 +231,10 @@ public void paint(Graphics gfx) {
230231
int height = fontMetrics.getHeight();
231232
int firstLine = textArea.getFirstLine();
232233
int firstInvalid = firstLine + clipRect.y / height;
234+
235+
double verticalSmoothOffset = (textArea.getVerticalSmoothScrollPosition() - firstLine) * fontMetrics.getHeight();
236+
g2.translate(0, -verticalSmoothOffset);
237+
233238
// Because the clipRect height is usually an even multiple
234239
// of the font height, we subtract 1 from it, otherwise one
235240
// too many lines will always be painted.

0 commit comments

Comments
 (0)