Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add popup handler mechanism. #40

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -152,6 +153,16 @@ public void setTooltipComponentSupplier(Supplier<JToolTip> tooltipComponentSuppl
canvas.setTooltipComponentSupplier(tooltipComponentSupplier);
}

/**
* Sets a callback that provides a reference to a frame when the user performs a
* "popup" action on the frame graph (typically a right-click with the mouse).
*
* @param consumer the consumer ({@code null} permitted).
*/
public void setPopupConsumer(Consumer<FrameBoxReference<T>> consumer) {
canvas.setPopupConsumer(consumer);
}
bric3 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Actually set the {@link FlameGraph} with typed data and configure how to use it.
* <p>
Expand Down Expand Up @@ -410,6 +421,8 @@ private static class FlameGraphCanvas<T> extends JPanel {
private boolean showMinimap = true;
private Supplier<JToolTip> tooltipComponentSupplier;

private Consumer<FrameBoxReference<T>> popupConsumer;

/**
* A flag controlling whether zoom transitions are animated. Defaults to true unless a
* system property is set to disable it (`-Dfireplace.zoom.animation.disabled=true`).
Expand Down Expand Up @@ -445,6 +458,10 @@ public void setAnimateZoomTransitions(boolean animateZoomTransitions) {
this.animateZoomTransitions = animateZoomTransitions;
}

public void setPopupConsumer(Consumer<FrameBoxReference<T>> consumer) {
this.popupConsumer = consumer;
}

/**
* Override this method to listen to LaF changes.
*/
Expand Down Expand Up @@ -640,6 +657,22 @@ public void mousePressed(MouseEvent e) {
if (isInsideMinimap(e.getPoint())) {
processMinimapMouseEvent(e);
}
if (e.isPopupTrigger() && popupConsumer != null) {
notifyPopupConsumer(e);
}
}

@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger() && popupConsumer != null) {
notifyPopupConsumer(e);
}
}

private void notifyPopupConsumer(MouseEvent e) {
FlameGraphCanvas<T> canvas = FlameGraphCanvas.this;
Optional<FrameBox<T>> f = flameGraphPainter.getFrameAt((Graphics2D) canvas.getGraphics(), canvas.getBounds(), e.getPoint());
popupConsumer.accept(new FrameBoxReference<>(f.orElse(null), e.getLocationOnScreen()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Dimension computeFlameGraphDimension(Graphics2D g2, int width, Insets ins
return new Dimension();
}

return new Dimension(width + insets.left + insets.right, depth * getFrameBoxHeight(g2));
return new Dimension(width + insets.left + insets.right, depth * getFrameBoxHeight(g2) + insets.top + insets.bottom);
}

private float getFrameBoxTextOffset(Graphics2D g2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Fireplace
*
* Copyright (c) 2021, Today - Brice Dutheil
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package io.github.bric3.fireplace.flamegraph;

import java.awt.*;

/**
* A general reference to a FrameBox and the mouse location where the reference has
* been triggered.
*/
public class FrameBoxReference<T> {

private final FrameBox<T> frameBox;

private final Point location;

public FrameBoxReference(FrameBox<T> frameBox, Point location) {
this.frameBox = frameBox;
this.location = location;
}

public FrameBox<T> getFrameBox() {
return this.frameBox;
}

public Point getLocation() {
return this.location;
}

@Override
public String toString() {
return "FrameBoxReference{" +
"frameBox=" + frameBox + ":" + frameBox.actualNode +
", location=" + location +
'}';
}
}