diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 566119e3dc..28f7e08de8 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -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)); } }); } @@ -286,6 +276,10 @@ public void setHorizontalScrollPosition(int what) { } + public double getVerticalSmoothScrollPosition() { + return smoothVertical; + } + /** * Returns the object responsible for painting this text area. */ @@ -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; diff --git a/app/src/processing/app/syntax/TextAreaPainter.java b/app/src/processing/app/syntax/TextAreaPainter.java index 5824ef6b2c..e89e5dcbed 100644 --- a/app/src/processing/app/syntax/TextAreaPainter.java +++ b/app/src/processing/app/syntax/TextAreaPainter.java @@ -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; @@ -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.