Skip to content

Commit

Permalink
Caching workaround for #723
Browse files Browse the repository at this point in the history
More caching added to improve performance
  • Loading branch information
matthewhorridge committed Jan 17, 2019
1 parent c87f152 commit 7960c48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void mouseMoved(MouseEvent e) {
handleMouseMoved();
}
};
mousePositionCache = MousePositionCache.createAndInstall(this);
mousePositionCache = MousePositionCache.createAndInstall(this, super::getMousePosition);
this.addMouseMotionListener(mouseMovementListener);
MouseListener mouseButtonListener = new MouseAdapter() {
@Override
Expand Down Expand Up @@ -157,7 +157,7 @@ private void handleMouseMoved() {
if (dirty != null) {
repaint(dirty);
}
Point pt = getCachedMousePosition();
Point pt = getMousePosition();
if (pt != null) {
lastMousePositionCellIndex = MList.this.locationToIndex(pt);
}
Expand Down Expand Up @@ -390,7 +390,7 @@ else if (obj instanceof MListItem) {

@Override
public String getToolTipText(MouseEvent event) {
Point mousePos = getCachedMousePosition();
Point mousePos = getMousePosition();
if (mousePos == null) {
return null;
}
Expand Down Expand Up @@ -494,7 +494,7 @@ private int paintButton(Graphics2D g2, Rectangle clipBound, int endOfButtonRun,
}

private Color getButtonColor(MListButton button) {
Point pt = getCachedMousePosition();
Point pt = getMousePosition();
if (pt == null) {
return button.getBackground();
}
Expand All @@ -509,9 +509,8 @@ private Color getButtonColor(MListButton button) {
return button.getBackground();
}

@Nullable
private Point getCachedMousePosition() {
@Override
public Point getMousePosition() throws HeadlessException {
return mousePositionCache.getMousePosition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -18,21 +19,27 @@ public class MousePositionCache {
@Nonnull
private final Component component;

@Nonnull
private final Supplier<Point> mousePositionSupplier;

private boolean stale = true;

private Point cachedPosition = null;

private MousePositionCache(@Nonnull Component component) {
private MousePositionCache(@Nonnull Component component,
@Nonnull Supplier<Point> mousePositionSupplier) {
this.component = checkNotNull(component);
this.mousePositionSupplier = checkNotNull(mousePositionSupplier);
}

public static MousePositionCache createAndInstall(@Nonnull Component component) {
MousePositionCache cache = new MousePositionCache(component);
cache.attacheListeners();
public static MousePositionCache createAndInstall(@Nonnull Component component,
@Nonnull Supplier<Point> mousePositionSupplier) {
MousePositionCache cache = new MousePositionCache(component, mousePositionSupplier);
cache.attachListeners();
return cache;
}

private void attacheListeners() {
private void attachListeners() {
component.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
Expand All @@ -48,7 +55,7 @@ private void markAsStale() {
@Nullable
public Point getMousePosition() {
if(stale) {
cachedPosition = component.getMousePosition();
cachedPosition = mousePositionSupplier.get();
stale = false;
}
return cachedPosition;
Expand Down

0 comments on commit 7960c48

Please sign in to comment.