Skip to content

Commit 08f9838

Browse files
committed
Fixed memory leak in smooth scrolling due to multiple notifications.
1 parent 4ba4faa commit 08f9838

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

flatlaf-core/src/main/java/com/formdev/flatlaf/SmoothScrollingHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import java.awt.event.ComponentEvent;
1717
import java.awt.event.KeyEvent;
1818
import java.awt.event.MouseEvent;
19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.Collections;
20+
import java.util.IdentityHashMap;
21+
import java.util.Set;
2122
import javax.swing.JComponent;
2223
import javax.swing.JScrollBar;
2324
import javax.swing.JScrollPane;
@@ -72,7 +73,7 @@ public void eventDispatched( AWTEvent event ) {
7273
boolean isBlitScrollModeBlocked_ = isBlitScrollModeBlocked;
7374
if(!isBlitScrollModeBlocked_) {
7475
Component c = ((ComponentEvent)event).getComponent();
75-
for( JViewport viewport: viewportList ) {
76+
for( JViewport viewport: viewportSet ) {
7677
Container scrollPane = viewport.getParent();
7778
if(scrollPane == c || scrollPane.isAncestorOf( c )) {
7879
setInSmoothScrolling( viewport, true );
@@ -83,7 +84,7 @@ public void eventDispatched( AWTEvent event ) {
8384
}
8485
if( !isHoldingScrollModeBlocked && ( !isBlitScrollModeBlocked_ || event.getID() == MouseEvent.MOUSE_RELEASED )) {
8586
SwingUtilities.invokeLater( () -> {
86-
for( JViewport viewport: viewportList ) {
87+
for( JViewport viewport: viewportSet ) {
8788
setBlitScrollModeBlocked( viewport, false );
8889
setInSmoothScrolling( viewport, false );
8990
}
@@ -171,13 +172,13 @@ private static synchronized void setInSmoothScrolling( JViewport viewport, boole
171172
viewport.putClientProperty( clientPropertyName , isInSmoothScrolling? Boolean.TRUE: null);
172173
}
173174

174-
private static List<JViewport> viewportList = new ArrayList<>();
175+
private static Set<JViewport> viewportSet = Collections.newSetFromMap(new IdentityHashMap<JViewport, Boolean>());
175176

176177
public static synchronized void registerViewport( JViewport viewport ) {
177178
if( instance == null )
178179
return;
179180
if(viewport.getParent() instanceof JScrollPane) {
180-
viewportList.add( viewport );
181+
viewportSet.add( viewport );
181182
}
182183
}
183184

@@ -187,7 +188,7 @@ public static synchronized void unregisterViewport( JViewport viewport ) {
187188
if( instance.isBlitScrollModeBlocked ) {
188189
instance.setBlitScrollModeBlocked( viewport, false );
189190
}
190-
viewportList.remove( viewport );
191+
viewportSet.remove( viewport );
191192
}
192193

193194
}

0 commit comments

Comments
 (0)