Skip to content

Commit

Permalink
fix: Track user flamegraph interactions on scrollbar and key presses
Browse files Browse the repository at this point in the history
  • Loading branch information
bric3 committed Mar 26, 2024
1 parent b655415 commit 56fa5e1
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
Expand Down Expand Up @@ -346,8 +348,8 @@ public void layoutContainer(Container parent) {
int horizontalScrollBarPolicy = jScrollPane.getHorizontalScrollBarPolicy();
double lastScaleFactor = canvas.zoomModel.getLastScaleFactor();
int newPolicy = lastScaleFactor == 1.0 ?
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER :
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER :
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
if (horizontalScrollBarPolicy != newPolicy) {
jScrollPane.setHorizontalScrollBarPolicy(newPolicy);
}
Expand Down Expand Up @@ -1322,7 +1324,7 @@ public void mouseMoved(@NotNull MouseEvent e) {
this.addMouseListener(mouseAdapter);
this.addMouseMotionListener(mouseAdapter);

new UserPositionRecorderMouseAdapter(this).install(scrollPane);
new UserPositionRecorder(this).install(scrollPane);
}

void setFlamegraphRenderEngine(@NotNull FlamegraphRenderEngine<@NotNull T> flamegraphRenderEngine) {
Expand Down Expand Up @@ -1664,10 +1666,10 @@ public void install(@NotNull JScrollPane sp) {
}
}

private static class UserPositionRecorderMouseAdapter extends MouseAdapter {
private static class UserPositionRecorder extends MouseAdapter {
private final FlamegraphCanvas<?> canvas;

public <T> UserPositionRecorderMouseAdapter(FlamegraphCanvas<T> canvas) {
public <T> UserPositionRecorder(FlamegraphCanvas<T> canvas) {
this.canvas = canvas;
}

Expand All @@ -1682,8 +1684,25 @@ public void mouseWheelMoved(MouseWheelEvent e) {
}

public void install(JScrollPane scrollPane) {
scrollPane.addMouseListener(this); // regular mouse
scrollPane.addMouseListener(this); // regular mouse drag
scrollPane.addMouseWheelListener(this); // mousewheel and trackpad
scrollPane.getHorizontalScrollBar().addMouseListener(this); // horizontal scrollbar interactions

var registeredKeyStrokes = scrollPane.getRegisteredKeyStrokes();
scrollPane.addKeyListener(new KeyAdapter() { // key presses
@Override
public void keyReleased(KeyEvent e) {
for (KeyStroke keyStroke : registeredKeyStrokes) {
//noinspection MagicConstant
if (e.getKeyCode() == keyStroke.getKeyCode()
&& e.getModifiersEx() == keyStroke.getModifiers()
) {
canvas.zoomModel.recordLastPositionFromUserInteraction(canvas);
break;
}
}
}
});
}
}

Expand Down

0 comments on commit 56fa5e1

Please sign in to comment.